From e1f3662724952e6ea440aa765ae740ae17e8da13 Mon Sep 17 00:00:00 2001 From: Jonathan Lukas Date: Fri, 6 Dec 2024 06:38:36 +0100 Subject: [PATCH] created working code snippets for manual bootstrapping --- README.md | 38 +++++--- examples/pom.xml | 1 + examples/readme-snippets/pom.xml | 31 +++++++ .../example/OperateClientBootstrapper.java | 88 +++++++++++++++++++ .../OperateClientBootstrapperTest.java | 33 +++++++ extension/java-client-operate/pom.xml | 4 - .../pom.xml | 7 -- 7 files changed, 181 insertions(+), 21 deletions(-) create mode 100644 examples/readme-snippets/pom.xml create mode 100644 examples/readme-snippets/src/main/java/io/camunda/operate/example/OperateClientBootstrapper.java create mode 100644 examples/readme-snippets/src/test/java/io/camunda/operate/example/OperateClientBootstrapperTest.java diff --git a/README.md b/README.md index 454737f..e6d65ca 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,8 @@ operate: client-secret: ``` +To adjust the (meaningful) default properties, you can also override them: + ```yaml operate: client: @@ -110,12 +112,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); ``` @@ -128,29 +135,40 @@ 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, 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, null); +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); ``` diff --git a/examples/pom.xml b/examples/pom.xml index 7a715b6..62209c8 100644 --- a/examples/pom.xml +++ b/examples/pom.xml @@ -13,6 +13,7 @@ pom operate-example-load-process-instance + readme-snippets \ No newline at end of file diff --git a/examples/readme-snippets/pom.xml b/examples/readme-snippets/pom.xml new file mode 100644 index 0000000..ba2eecf --- /dev/null +++ b/examples/readme-snippets/pom.xml @@ -0,0 +1,31 @@ + + + 4.0.0 + + io.camunda.spring + operate-examples-parent + 8.7.0-SNAPSHOT + + + readme-snippets + + + + io.camunda.spring + java-client-operate + + + org.junit.jupiter + junit-jupiter + test + + + org.assertj + assertj-core + test + + + + \ No newline at end of file diff --git a/examples/readme-snippets/src/main/java/io/camunda/operate/example/OperateClientBootstrapper.java b/examples/readme-snippets/src/main/java/io/camunda/operate/example/OperateClientBootstrapper.java new file mode 100644 index 0000000..8fe1893 --- /dev/null +++ b/examples/readme-snippets/src/main/java/io/camunda/operate/example/OperateClientBootstrapper.java @@ -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; + } + } +} diff --git a/examples/readme-snippets/src/test/java/io/camunda/operate/example/OperateClientBootstrapperTest.java b/examples/readme-snippets/src/test/java/io/camunda/operate/example/OperateClientBootstrapperTest.java new file mode 100644 index 0000000..5268f62 --- /dev/null +++ b/examples/readme-snippets/src/test/java/io/camunda/operate/example/OperateClientBootstrapperTest.java @@ -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(); + } +} diff --git a/extension/java-client-operate/pom.xml b/extension/java-client-operate/pom.xml index 08db6b2..e41626e 100644 --- a/extension/java-client-operate/pom.xml +++ b/extension/java-client-operate/pom.xml @@ -13,7 +13,6 @@ org.slf4j slf4j-api - provided io.camunda @@ -26,7 +25,6 @@ org.apache.httpcomponents.core5 httpcore5 - provided com.fasterxml.jackson.core @@ -35,12 +33,10 @@ com.fasterxml.jackson.core jackson-core - provided com.fasterxml.jackson.core jackson-annotations - provided org.junit.jupiter diff --git a/extension/spring-boot-starter-camunda-operate/pom.xml b/extension/spring-boot-starter-camunda-operate/pom.xml index c994334..5c02511 100644 --- a/extension/spring-boot-starter-camunda-operate/pom.xml +++ b/extension/spring-boot-starter-camunda-operate/pom.xml @@ -24,37 +24,30 @@ org.springframework.boot spring-boot-autoconfigure - provided org.apache.httpcomponents.client5 httpclient5 - provided org.springframework spring-core - provided org.springframework spring-beans - provided org.springframework spring-context - provided org.springframework.boot spring-boot - provided com.fasterxml.jackson.core jackson-databind - provided io.camunda.spring