Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix and extend aws secret manager vault integration tests for real in… #6609

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions integration-tests-jvm/aws-secrets-manager/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-aws-secrets-manager</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-direct</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy</artifactId>
Expand Down Expand Up @@ -90,6 +94,19 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-direct-deployment</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</profile>
</profiles>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
import java.net.URI;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.event.Observes;
import jakarta.inject.Inject;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.GET;
Expand All @@ -35,12 +37,15 @@
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.component.aws.secretsmanager.SecretsManagerConstants;
import org.apache.camel.component.aws.secretsmanager.SecretsManagerOperations;
import org.apache.camel.impl.event.CamelContextReloadedEvent;
import org.apache.camel.spi.PeriodTaskResolver;
import org.apache.camel.support.PluginHelper;
import org.apache.camel.util.CollectionHelper;
import org.jboss.logging.Logger;
import software.amazon.awssdk.services.secretsmanager.model.CreateSecretResponse;
import software.amazon.awssdk.services.secretsmanager.model.DeleteSecretRequest;
import software.amazon.awssdk.services.secretsmanager.model.DeleteSecretResponse;
import software.amazon.awssdk.services.secretsmanager.model.DescribeSecretResponse;
import software.amazon.awssdk.services.secretsmanager.model.ListSecretsResponse;
Expand All @@ -64,25 +69,45 @@ public class AwsSecretsManagerResource {
@Inject
ProducerTemplate producerTemplate;

static final AtomicBoolean contextReloaded = new AtomicBoolean(false);

void onReload(@Observes CamelContextReloadedEvent event) {
contextReloaded.set(true);
}

@Path("/operation/{operation}")
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response post(@PathParam("operation") String operation, @QueryParam("body") String body, Map<String, Object> headers)
throws Exception {

Exchange ex = producerTemplate.send("aws-secrets-manager://test?operation=" + operation, e -> {
e.getIn().setHeaders(headers);
e.getIn().setBody(body);
});
final Object resultBody;
if (operation.equals("forceDeleteSecret")) {
operation = SecretsManagerOperations.deleteSecret.toString();
DeleteSecretRequest.Builder builder = DeleteSecretRequest.builder();
builder.secretId((String) headers.get(SecretsManagerConstants.SECRET_ID));
builder.forceDeleteWithoutRecovery(true);
resultBody = builder.build();
} else {
resultBody = body;
}

String region = headers.containsKey("region") ? String.format("region=%s&", headers.get("region")) : "";

Exchange ex = producerTemplate.send(String.format("aws-secrets-manager://test?%soperation=%s", region, operation),
e -> {
e.getIn().setHeaders(headers);
e.getIn().setBody(resultBody);
});

Object result;
switch (SecretsManagerOperations.valueOf(operation)) {
case createSecret:
result = ex.getIn().getBody(CreateSecretResponse.class).arn();
break;
case listSecrets:
//returns map with ar as a key and a flag, whether is deleted or not
//returns map with arn as a key and a flag, whether is deleted or not
result = ex.getIn().getBody(ListSecretsResponse.class).secretList().stream()
.collect(Collectors.toMap(SecretListEntry::arn, e -> e.deletedDate() == null ? false : true));
break;
Expand Down Expand Up @@ -124,4 +149,22 @@ public boolean awsSecretRefreshPeriodicTaskExists() {
Objects.requireNonNull(periodTaskResolver, "Expected a PeriodTaskResolver to be configured");
return periodTaskResolver.newInstance("aws-secret-refresh", Runnable.class).isPresent();
}

@Path("/context/reload")
@GET
@Produces(MediaType.TEXT_PLAIN)
public boolean contextReloadStatus() {
return contextReloaded.get();
}

@Path("/propertyFunction/{secretName}")
@GET
@Produces(MediaType.TEXT_PLAIN)
public Response propertyFunction(@PathParam("secretName") String secretName)
throws Exception {
Exchange ex = producerTemplate.request(String.format("direct:{{aws:%s}}", secretName), null);

return Response.ok(new URI("https://camel.apache.org/")).entity(ex.getIn().getBody()).build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.quarkus.component.aws.secrets.manager.it;

import jakarta.enterprise.context.ApplicationScoped;
import org.apache.camel.builder.RouteBuilder;

@ApplicationScoped
public class AwsSecretsManagerRouteBuilder extends RouteBuilder {

public static final String MSG_FIRST = "We have received the top secret message.";
public static final String MSG_SECOND = "We have received the updated top secret message.";

@Override
public void configure() throws Exception {
from("direct:loadFirst")
.setBody(simple(MSG_FIRST));
from("direct:loadSecond")
.setBody(simple(MSG_SECOND));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
## ---------------------------------------------------------------------------
## Licensed to the Apache Software Foundation (ASF) under one or more
## contributor license agreements. See the NOTICE file distributed with
## this work for additional information regarding copyright ownership.
## The ASF licenses this file to You under the Apache License, Version 2.0
## (the "License"); you may not use this file except in compliance with
## the License. You may obtain a copy of the License at
##
## http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
## ---------------------------------------------------------------------------
#
# Camel :: AWS Secrets Manager options
#
camel.component.aws-secrets-manager.access-key=${AWS_ACCESS_KEY}
camel.component.aws-secrets-manager.secret-key=${AWS_SECRET_KEY}
camel.component.aws-secrets-manager.region=${AWS_REGION:us-east-1}
camel.component.aws-secrets-manager.useDefaultCredentialsProvider=${AWS_USE_DEFAULT_CREDENTIALS_PROVIDER}

# Handy for debugging purposes
#quarkus.log.category."software.amazon.awssdk".level=TRACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.quarkus.component.aws.secrets.manager.it;

import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

import io.quarkus.logging.Log;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import org.apache.camel.component.aws.secretsmanager.SecretsManagerConstants;
import org.apache.camel.component.aws.secretsmanager.SecretsManagerOperations;
import org.testcontainers.shaded.org.awaitility.Awaitility;

import static org.hamcrest.CoreMatchers.is;

class AwsSecretsManagerAbstractTest {

protected String createSecret(String secretName, String secretValue) {
String createdArn = Awaitility.await()
.pollInterval(5, TimeUnit.SECONDS)
.atMost(1, TimeUnit.MINUTES)
.until(() -> {
try {
return RestAssured.given()
.contentType(ContentType.JSON)
.body(Collections.singletonMap(SecretsManagerConstants.SECRET_NAME, secretName))
.queryParam("body", secretValue)
.post("/aws-secrets-manager/operation/" + SecretsManagerOperations.createSecret)
.then()
.statusCode(201)
.extract().asString();
} catch (Exception e) {
return null;
}
}, Objects::nonNull);

return createdArn;
}

protected void updateSecret(String secretArn, String newValue) {
RestAssured.given()
.contentType(ContentType.JSON)
.body(Collections.singletonMap(SecretsManagerConstants.SECRET_ID, secretArn))
.queryParam("body", newValue)
.post("/aws-secrets-manager/operation/" + SecretsManagerOperations.updateSecret)
.then()
.statusCode(201)
.body(is("true"));
}

protected Map<String, Boolean> listSecrets() {
return RestAssured.given()
.contentType(ContentType.JSON)
.body(Collections.emptyMap())
.post("/aws-secrets-manager/operation/" + SecretsManagerOperations.listSecrets)
.then()
.statusCode(201)
.extract().as(Map.class);
}

protected void deleteSecretImmediately(String arn) {
if (arn != null) {
Log.info("Deleting secret: " + arn);
RestAssured.given()
.contentType(ContentType.JSON)
.body(Collections.singletonMap(SecretsManagerConstants.SECRET_ID, arn))
.post("/aws-secrets-manager/operation/forceDeleteSecret")
.then()
.statusCode(201)
.body(is("true"));
}
}
}
Loading