Skip to content

Commit

Permalink
feat: support secrets in request body
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleksiivanov authored and chillleader committed Dec 8, 2022
1 parent 3cbc093 commit 2b95abc
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class HttpJsonRequest {
@Secret
private String connectionTimeoutInSeconds;

private Object body;
@Secret private Object body;

public boolean hasAuthentication() {
return authentication != null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ interface Headers {
String ID = "CLUSTER_ID_KEY";
String USER_AGENT = "USER_AGENT_KEY";
}

interface Body {
String CUSTOMER_ID = "CUSTOMER_ID_KEY";
String CUSTOMER_NAME = "NAME_KEY";
String CUSTOMER_EMAIL = "EMAIL_KEY";
String TEXT = "TEXT_KEY";
String TEXT_PART_1 = "TEXT_PART_1_KEY";
String TEXT_PART_2 = "TEXT_PART_2_KEY";
String TEXT_PART_3 = "TEXT_PART_3_KEY";
}
}

protected interface ActualValue {
Expand All @@ -82,13 +92,31 @@ interface Headers {
String CLUSTER_ID = "testClusterId";
String USER_AGENT = "http-connector-demo";
}

interface Body {
String CUSTOMER_ID_SECRET = "secret id key";
String CUSTOMER_ID_REAL = "startId plus " + CUSTOMER_ID_SECRET;
String CUSTOMER_NAME_SECRET = "secret name";
String CUSTOMER_NAME_REAL = CUSTOMER_NAME_SECRET + " plus some text";
String CUSTOMER_EMAIL_SECRET = "start email plus secret email part plus end email";
String CUSTOMER_EMAIL_REAL = "start email plus " + CUSTOMER_EMAIL_SECRET + " plus end email";
String TEXT_PART_1 = "start secret text plus ";
String TEXT_PART_2 = "mid of text plus ";
String TEXT_PART_3 = "end of text";
String TEXT = TEXT_PART_1 + TEXT_PART_2 + TEXT_PART_3;
}
}

protected interface JsonKeys {
String CLUSTER_ID = "X-Camunda-Cluster-ID";
String USER_AGENT = "User-Agent";
String QUERY = "q";
String PRIORITY = "priority";
String CUSTOMER = "customer";
String ID = "id";
String NAME = "name";
String EMAIL = "email";
String TEXT = "text";
}

protected OutboundConnectorContextBuilder getContextBuilderWithSecrets() {
Expand All @@ -109,7 +137,14 @@ protected OutboundConnectorContextBuilder getContextBuilderWithSecrets() {
.secret(SecretsConstant.QueryParameters.QUEUE, ActualValue.QueryParameters.QUEUE)
.secret(SecretsConstant.QueryParameters.PRIORITY, ActualValue.QueryParameters.PRIORITY)
.secret(SecretsConstant.Headers.ID, ActualValue.Headers.CLUSTER_ID)
.secret(SecretsConstant.Headers.USER_AGENT, ActualValue.Headers.USER_AGENT);
.secret(SecretsConstant.Headers.USER_AGENT, ActualValue.Headers.USER_AGENT)
.secret(SecretsConstant.Body.CUSTOMER_NAME, ActualValue.Body.CUSTOMER_NAME_SECRET)
.secret(SecretsConstant.Body.CUSTOMER_EMAIL, ActualValue.Body.CUSTOMER_EMAIL_SECRET)
.secret(SecretsConstant.Body.CUSTOMER_ID, ActualValue.Body.CUSTOMER_ID_SECRET)
.secret(SecretsConstant.Body.TEXT, ActualValue.Body.TEXT)
.secret(SecretsConstant.Body.TEXT_PART_1, ActualValue.Body.TEXT_PART_1)
.secret(SecretsConstant.Body.TEXT_PART_2, ActualValue.Body.TEXT_PART_2)
.secret(SecretsConstant.Body.TEXT_PART_3, ActualValue.Body.TEXT_PART_3);
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import io.camunda.connector.http.model.HttpJsonRequest;
import java.io.IOException;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

Expand Down Expand Up @@ -123,4 +124,47 @@ void replaceSecrets_shouldReplaceHeadersSecrets(String input) {
assertThat(headers.get(JsonKeys.USER_AGENT).getAsString())
.isEqualTo(ActualValue.Headers.USER_AGENT);
}

@ParameterizedTest(name = "Should replace body secrets")
@MethodSource("successReplaceSecretsCases")
void replaceSecrets_shouldReplaceBodySecrets(String input) {
// Given request with secrets
HttpJsonRequest httpJsonRequest = gson.fromJson(input, HttpJsonRequest.class);
context = getContextBuilderWithSecrets().variables(httpJsonRequest).build();
// When
context.replaceSecrets(httpJsonRequest);
// Then should replace secrets
JsonObject body = gson.toJsonTree(httpJsonRequest.getBody()).getAsJsonObject();
JsonObject customer = body.get(JsonKeys.CUSTOMER).getAsJsonObject();

assertThat(customer.get(JsonKeys.ID).getAsString())
.isEqualTo(ActualValue.Body.CUSTOMER_ID_REAL);
assertThat(customer.get(JsonKeys.NAME).getAsString())
.isEqualTo(ActualValue.Body.CUSTOMER_NAME_REAL);
assertThat(customer.get(JsonKeys.EMAIL).getAsString())
.isEqualTo(ActualValue.Body.CUSTOMER_EMAIL_REAL);

assertThat(body.get(JsonKeys.TEXT).getAsString()).isEqualTo(ActualValue.Body.TEXT);
}

@Test
void replaceSecrets_shouldReplaceBodyWhenBodyIsString() {
// Given request with secrets
HttpJsonRequest request = new HttpJsonRequest();
request.setBody(
"{{secrets."
+ SecretsConstant.Body.TEXT_PART_1
+ "}}"
+ "{{secrets."
+ SecretsConstant.Body.TEXT_PART_2
+ "}}"
+ "{{secrets."
+ SecretsConstant.Body.TEXT_PART_3
+ "}}");
context = getContextBuilderWithSecrets().variables(request).build();
// When
context.replaceSecrets(request);
// Then should replace secrets
assertThat(request.getBody().toString()).isEqualTo(ActualValue.Body.TEXT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
"connectionTimeoutInSeconds":"secrets.CONNECT_TIMEOUT_KEY",
"body": {
"customer": {
"id": "secrets.CUSTOMER_ID_KEY",
"name": "secrets.NAME_KEY",
"email": "secrets.EMAIL_KEY"
}
"id": "startId plus {{secrets.CUSTOMER_ID_KEY}}",
"name": "{{secrets.NAME_KEY}} plus some text",
"email": "start email plus {{secrets.EMAIL_KEY}} plus end email"
},
"text": "secrets.TEXT_KEY"
}
},
{
Expand All @@ -40,10 +41,11 @@
"connectionTimeoutInSeconds":"secrets.CONNECT_TIMEOUT_KEY",
"body": {
"customer": {
"id": "secrets.CUSTOMER_ID_KEY",
"name": "secrets.NAME_KEY",
"email": "secrets.EMAIL_KEY"
}
"id": "startId plus {{secrets.CUSTOMER_ID_KEY}}",
"name": "{{secrets.NAME_KEY}} plus some text",
"email": "start email plus {{secrets.EMAIL_KEY}} plus end email"
},
"text": "{{secrets.TEXT_KEY}}"
}
},
{
Expand All @@ -65,10 +67,11 @@
"connectionTimeoutInSeconds":"secrets.CONNECT_TIMEOUT_KEY",
"body": {
"customer": {
"id": "secrets.CUSTOMER_ID_KEY",
"name": "secrets.NAME_KEY",
"email": "secrets.EMAIL_KEY"
}
"id": "startId plus {{secrets.CUSTOMER_ID_KEY}}",
"name": "{{secrets.NAME_KEY}} plus some text",
"email": "start email plus {{secrets.EMAIL_KEY}} plus end email"
},
"text": "start secret text plus mid of text plus {{secrets.TEXT_PART_3_KEY}}"
}
},
{
Expand All @@ -94,10 +97,11 @@
"connectionTimeoutInSeconds":"secrets.CONNECT_TIMEOUT_KEY",
"body": {
"customer": {
"id": "secrets.CUSTOMER_ID_KEY",
"name": "secrets.NAME_KEY",
"email": "secrets.EMAIL_KEY"
}
"id": "startId plus {{secrets.CUSTOMER_ID_KEY}}",
"name": "{{secrets.NAME_KEY}} plus some text",
"email": "start email plus {{secrets.EMAIL_KEY}} plus end email"
},
"text": "{{secrets.TEXT_PART_1_KEY}}{{secrets.TEXT_PART_2_KEY}}{{secrets.TEXT_PART_3_KEY}}"
}
}
]

0 comments on commit 2b95abc

Please sign in to comment.