You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ac...@apache.org on 2020/09/30 07:08:20 UTC

[camel] 02/05: Camel-AWS2-STS: Added localstack test for getSessionToken operation

This is an automated email from the ASF dual-hosted git repository.

acosentino pushed a commit to branch sts-localstack
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 6ed49a6fc04fefae931d007b4d602e4a73003a88
Author: Andrea Cosentino <an...@gmail.com>
AuthorDate: Wed Sep 30 08:57:39 2020 +0200

    Camel-AWS2-STS: Added localstack test for getSessionToken operation
---
 .../aws2/sts/localstack/Aws2StsBaseTest.java       | 76 ++++++++++++++++++++++
 .../StsGetSessionTokenLocalstackTest.java          | 69 ++++++++++++++++++++
 2 files changed, 145 insertions(+)

diff --git a/components/camel-aws2-sts/src/test/java/org/apache/camel/component/aws2/sts/localstack/Aws2StsBaseTest.java b/components/camel-aws2-sts/src/test/java/org/apache/camel/component/aws2/sts/localstack/Aws2StsBaseTest.java
new file mode 100644
index 0000000..9d85c25
--- /dev/null
+++ b/components/camel-aws2-sts/src/test/java/org/apache/camel/component/aws2/sts/localstack/Aws2StsBaseTest.java
@@ -0,0 +1,76 @@
+/*
+ * 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.component.aws2.sts.localstack;
+
+import java.net.URI;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.component.aws2.sts.STS2Component;
+import org.apache.camel.test.testcontainers.junit5.ContainerAwareTestSupport;
+import org.apache.camel.test.testcontainers.junit5.Wait;
+import org.junit.jupiter.api.TestInstance;
+import org.testcontainers.containers.GenericContainer;
+import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
+import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
+import software.amazon.awssdk.regions.Region;
+import software.amazon.awssdk.services.sts.StsClient;
+
+@TestInstance(TestInstance.Lifecycle.PER_CLASS)
+public class Aws2StsBaseTest extends ContainerAwareTestSupport {
+
+    public static final String CONTAINER_IMAGE = "localstack/localstack:0.11.5";
+    public static final String CONTAINER_NAME = "sts";
+
+    @Override
+    protected GenericContainer<?> createContainer() {
+        return localstackContainer();
+    }
+
+    public static GenericContainer localstackContainer() {
+        return new GenericContainer(CONTAINER_IMAGE)
+                .withNetworkAliases(CONTAINER_NAME)
+                .withEnv("SERVICES", "sts")
+                .withExposedPorts(4566)
+                .waitingFor(Wait.forListeningPort())
+                .waitingFor(Wait.forLogMessageContaining("Ready.", 1));
+    }
+
+    public String getEventbridgeUrl() {
+        return String.format(
+                "%s:%d",
+                getContainerHost(CONTAINER_NAME),
+                getContainerPort(CONTAINER_NAME, 4566));
+    }
+
+    public StsClient getStsClient() {
+    	StsClient stsClient = StsClient
+                .builder()
+                .endpointOverride(URI.create("http://" + getEventbridgeUrl()))
+                .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("xxx", "yyy")))
+                .region(Region.EU_WEST_1)
+                .build();
+        return stsClient;
+    }
+
+    @Override
+    protected CamelContext createCamelContext() throws Exception {
+        CamelContext context = super.createCamelContext();
+        STS2Component stsComponent = context.getComponent("aws2-sts", STS2Component.class);
+        stsComponent.getConfiguration().setStsClient(getStsClient());
+        return context;
+    }
+}
diff --git a/components/camel-aws2-sts/src/test/java/org/apache/camel/component/aws2/sts/localstack/StsGetSessionTokenLocalstackTest.java b/components/camel-aws2-sts/src/test/java/org/apache/camel/component/aws2/sts/localstack/StsGetSessionTokenLocalstackTest.java
new file mode 100644
index 0000000..7190615
--- /dev/null
+++ b/components/camel-aws2-sts/src/test/java/org/apache/camel/component/aws2/sts/localstack/StsGetSessionTokenLocalstackTest.java
@@ -0,0 +1,69 @@
+/*
+ * 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.component.aws2.sts.localstack;
+
+import org.apache.camel.EndpointInject;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.aws2.sts.STS2Constants;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.jupiter.api.Test;
+
+import software.amazon.awssdk.services.sts.model.GetSessionTokenResponse;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+public class StsGetSessionTokenLocalstackTest extends Aws2StsBaseTest {
+
+    @EndpointInject
+    private ProducerTemplate template;
+
+    @EndpointInject("mock:result")
+    private MockEndpoint result;
+
+    @Test
+    public void sendIn() throws Exception {
+        result.expectedMessageCount(1);
+
+        template.send("direct:createKey", new Processor() {
+
+            @Override
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setHeader(STS2Constants.OPERATION, "getSessionToken");
+            }
+        });
+
+        assertMockEndpointsSatisfied();
+        assertEquals(1, result.getExchanges().size());
+        assertNotNull(result.getExchanges().get(0).getIn().getBody(GetSessionTokenResponse.class).credentials().accessKeyId());
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                String awsEndpoint
+                        = "aws2-sts://default?operation=getSessionToken";
+                from("direct:getSessonToken").to(awsEndpoint).to("mock:result");
+            }
+        };
+    }
+}