Skip to content

Commit

Permalink
Backport/154/8.6 (#159)
Browse files Browse the repository at this point in the history
* created working code snippets for manual bootstrapping (#154)

* created working code snippets for manual bootstrapping

* added missing dependencies

(cherry picked from commit 44ebe65)

* formatted readme
  • Loading branch information
jonathanlukas authored Dec 6, 2024
1 parent 56d0419 commit 1f36a72
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 11 deletions.
42 changes: 31 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ operate:
client-secret:
```
To adjust the (meaningful) default properties, you can also override them:
```yaml
operate:
client:
Expand Down Expand Up @@ -108,12 +110,17 @@ Build a Camunda Operate client with simple authentication:

```java
// properties you need to provide
String username = "demo";
String password = "demo";
URL operateUrl = URI.create("http://localhost:8081").toURL();
SimpleCredential credentials = new SimpleCredential("demo", "demo", operateUrl, Duration.ofMinutes(10));
// bootstrapping
SimpleCredential credentials =
new SimpleCredential(username, password, operateUrl, Duration.ofMinutes(10));
SimpleAuthentication authentication = new SimpleAuthentication(credentials);
ObjectMapper objectMapper = new ObjectMapper();
CamundaOperateClientConfiguration configuration = new CamundaOperateClientConfiguration(authentication, operateUrl, objectMapper, HttpClients.createDefault());
CamundaOperateClientConfiguration configuration =
new CamundaOperateClientConfiguration(
authentication, operateUrl, objectMapper, HttpClients.createDefault());
CamundaOperateClient client = new CamundaOperateClient(configuration);
```

Expand All @@ -124,30 +131,43 @@ Build a Camunda Operate client with identity authentication:
String clientId = "";
String clientSecret = "";
String audience = "operate-api";
String scope = ""; // can be omitted if not required
URL operateUrl = URI.create("http://localhost:8081").toURL();
URL authUrl = URI.create("http://localhost:18080/auth/realms/camunda-platform/protocol/openid-connect/token");
URL authUrl =
URI.create(
"http://localhost:18080/auth/realms/camunda-platform/protocol/openid-connect/token")
.toURL();
// bootstrapping
JwtCredential credentials = new JwtCredential(clientId, clientSecret, audience, authUrl);
JwtCredential credentials =
new JwtCredential(clientId, clientSecret, audience, authUrl, scope);
ObjectMapper objectMapper = new ObjectMapper();
JwtAuthentication authentication = new JwtAuthentication(credentials, objectMapper);
CamundaOperateClientConfiguration configuration = new CamundaOperateClientConfiguration(authentication, operateUrl, objectMapper, HttpClients.createDefault());
TokenResponseMapper tokenResponseMapper = new JacksonTokenResponseMapper(objectMapper);
JwtAuthentication authentication = new JwtAuthentication(credentials, tokenResponseMapper);
CamundaOperateClientConfiguration configuration =
new CamundaOperateClientConfiguration(
authentication, operateUrl, objectMapper, HttpClients.createDefault());
CamundaOperateClient client = new CamundaOperateClient(configuration);
```

Build a Camunda Operate client for Saas:

```java
// properties you need to provide
String region = "";
String clusterId = "";
String clientId = "";
String clientSecret = "";
// bootstrapping
URL operateUrl = URI.create("https://"+ region +".operate.camunda.io/" + clusterId).toURL();
URL authUrl = URI.create("https://login.cloud.camunda.io/oauth/token");
JwtCredential credentials = new JwtCredential(clientId, clientSecret, "operate.camunda.io", authUrl);
URL operateUrl = URI.create("https://" + region + ".operate.camunda.io/" + clusterId).toURL();
URL authUrl = URI.create("https://login.cloud.camunda.io/oauth/token").toURL();
JwtCredential credentials =
new JwtCredential(clientId, clientSecret, "operate.camunda.io", authUrl, null);
ObjectMapper objectMapper = new ObjectMapper();
JwtAuthentication authentication = new JwtAuthentication(credentials, objectMapper);
CamundaOperateClientConfiguration configuration = new CamundaOperateClientConfiguration(authentication, operateUrl, objectMapper, HttpClients.createDefault());
TokenResponseMapper tokenResponseMapper = new JacksonTokenResponseMapper(objectMapper);
JwtAuthentication authentication = new JwtAuthentication(credentials, tokenResponseMapper);
CamundaOperateClientConfiguration configuration =
new CamundaOperateClientConfiguration(
authentication, operateUrl, objectMapper, HttpClients.createDefault());
CamundaOperateClient client = new CamundaOperateClient(configuration);
```

Expand Down
1 change: 1 addition & 0 deletions examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<packaging>pom</packaging>
<modules>
<module>operate-example-load-process-instance</module>
<module>readme-snippets</module>
</modules>

</project>
39 changes: 39 additions & 0 deletions examples/readme-snippets/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.camunda.spring</groupId>
<artifactId>operate-examples-parent</artifactId>
<version>8.6.3-SNAPSHOT</version>
</parent>

<artifactId>readme-snippets</artifactId>

<dependencies>
<dependency>
<groupId>io.camunda.spring</groupId>
<artifactId>java-client-operate</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package io.camunda.operate.example;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.camunda.operate.CamundaOperateClient;
import io.camunda.operate.CamundaOperateClientConfiguration;
import io.camunda.operate.auth.JwtAuthentication;
import io.camunda.operate.auth.JwtCredential;
import io.camunda.operate.auth.SimpleAuthentication;
import io.camunda.operate.auth.SimpleCredential;
import io.camunda.operate.auth.TokenResponseMapper;
import io.camunda.operate.auth.TokenResponseMapper.JacksonTokenResponseMapper;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.time.Duration;
import org.apache.hc.client5.http.impl.classic.HttpClients;

public interface OperateClientBootstrapper {
CamundaOperateClient createOperateClient() throws MalformedURLException;

class SimpleAuthOperateClientBootstrapper implements OperateClientBootstrapper {
public CamundaOperateClient createOperateClient() throws MalformedURLException {
// properties you need to provide
String username = "demo";
String password = "demo";
URL operateUrl = URI.create("http://localhost:8081").toURL();
// bootstrapping
SimpleCredential credentials =
new SimpleCredential(username, password, operateUrl, Duration.ofMinutes(10));
SimpleAuthentication authentication = new SimpleAuthentication(credentials);
ObjectMapper objectMapper = new ObjectMapper();
CamundaOperateClientConfiguration configuration =
new CamundaOperateClientConfiguration(
authentication, operateUrl, objectMapper, HttpClients.createDefault());
CamundaOperateClient client = new CamundaOperateClient(configuration);
return client;
}
}

class IdentityAuthOperateClientBootstrapper implements OperateClientBootstrapper {
public CamundaOperateClient createOperateClient() throws MalformedURLException {
// properties you need to provide
String clientId = "";
String clientSecret = "";
String audience = "operate-api";
String scope = ""; // can be omitted if not required
URL operateUrl = URI.create("http://localhost:8081").toURL();
URL authUrl =
URI.create(
"http://localhost:18080/auth/realms/camunda-platform/protocol/openid-connect/token")
.toURL();
// bootstrapping
JwtCredential credentials =
new JwtCredential(clientId, clientSecret, audience, authUrl, scope);
ObjectMapper objectMapper = new ObjectMapper();
TokenResponseMapper tokenResponseMapper = new JacksonTokenResponseMapper(objectMapper);
JwtAuthentication authentication = new JwtAuthentication(credentials, tokenResponseMapper);
CamundaOperateClientConfiguration configuration =
new CamundaOperateClientConfiguration(
authentication, operateUrl, objectMapper, HttpClients.createDefault());
CamundaOperateClient client = new CamundaOperateClient(configuration);
return client;
}
}

class SaasClientBootstrapper implements OperateClientBootstrapper {
public CamundaOperateClient createOperateClient() throws MalformedURLException {
// properties you need to provide
String region = "";
String clusterId = "";
String clientId = "";
String clientSecret = "";
// bootstrapping
URL operateUrl = URI.create("https://" + region + ".operate.camunda.io/" + clusterId).toURL();
URL authUrl = URI.create("https://login.cloud.camunda.io/oauth/token").toURL();
JwtCredential credentials =
new JwtCredential(clientId, clientSecret, "operate.camunda.io", authUrl, null);
ObjectMapper objectMapper = new ObjectMapper();
TokenResponseMapper tokenResponseMapper = new JacksonTokenResponseMapper(objectMapper);
JwtAuthentication authentication = new JwtAuthentication(credentials, tokenResponseMapper);
CamundaOperateClientConfiguration configuration =
new CamundaOperateClientConfiguration(
authentication, operateUrl, objectMapper, HttpClients.createDefault());
CamundaOperateClient client = new CamundaOperateClient(configuration);
return client;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.camunda.operate.example;

import static org.assertj.core.api.Assertions.*;

import io.camunda.operate.CamundaOperateClient;
import io.camunda.operate.example.OperateClientBootstrapper.IdentityAuthOperateClientBootstrapper;
import io.camunda.operate.example.OperateClientBootstrapper.SaasClientBootstrapper;
import io.camunda.operate.example.OperateClientBootstrapper.SimpleAuthOperateClientBootstrapper;
import java.net.MalformedURLException;
import org.junit.jupiter.api.Test;

public class OperateClientBootstrapperTest {
@Test
void shouldBootstrapSimpleAuthClient() throws MalformedURLException {
OperateClientBootstrapper bootstrapper = new SimpleAuthOperateClientBootstrapper();
CamundaOperateClient client = bootstrapper.createOperateClient();
assertThat(client).isNotNull();
}

@Test
void shouldBootstrapIdentityAuthClient() throws MalformedURLException {
OperateClientBootstrapper bootstrapper = new IdentityAuthOperateClientBootstrapper();
CamundaOperateClient client = bootstrapper.createOperateClient();
assertThat(client).isNotNull();
}

@Test
void shouldBootstrapSaasClient() throws MalformedURLException {
OperateClientBootstrapper bootstrapper = new SaasClientBootstrapper();
CamundaOperateClient client = bootstrapper.createOperateClient();
assertThat(client).isNotNull();
}
}

0 comments on commit 1f36a72

Please sign in to comment.