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

davsclaus pushed a commit to branch CAMEL-27667-aws-session-credentials
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 9551e6c989aeb271ea8fb9c9fb0e7ff8564c00c7
Author: Claus Ibsen <[email protected]>
AuthorDate: Fri Nov 27 16:26:39 2026 +0100

    CAMEL-27667: camel-aws - useSessionCredentials without keys falls back to 
the default credentials chain
    
    AwsClientBuilderUtil.resolveCredentialsProvider returned null when
    useSessionCredentials=true but the accessKey, secretKey or sessionToken
    was missing, so the client used the AWS SDK default credentials chain
    (or the keys without the session token). It now fails with an
    IllegalArgumentException that names the missing options. The same is
    done when only one of accessKey and secretKey is set. No credentials
    option at all still uses the SDK default chain.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    Signed-off-by: Claus Ibsen <[email protected]>
---
 .../component/aws/common/AwsClientBuilderUtil.java |  56 +++--
 .../AwsClientBuilderUtilCredentialsTest.java       | 251 +++++++++++++++++++++
 .../component/aws2/ecs/ECS2ClientFactoryTest.java  |  37 +++
 .../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc    |  14 ++
 4 files changed, 344 insertions(+), 14 deletions(-)

diff --git 
a/components/camel-aws/camel-aws-common/src/main/java/org/apache/camel/component/aws/common/AwsClientBuilderUtil.java
 
b/components/camel-aws/camel-aws-common/src/main/java/org/apache/camel/component/aws/common/AwsClientBuilderUtil.java
index 210987a30ba9..9585fc9af647 100644
--- 
a/components/camel-aws/camel-aws-common/src/main/java/org/apache/camel/component/aws/common/AwsClientBuilderUtil.java
+++ 
b/components/camel-aws/camel-aws-common/src/main/java/org/apache/camel/component/aws/common/AwsClientBuilderUtil.java
@@ -17,6 +17,8 @@
 package org.apache.camel.component.aws.common;
 
 import java.net.URI;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.function.Consumer;
 import java.util.function.Supplier;
 
@@ -269,11 +271,18 @@ public final class AwsClientBuilderUtil {
      * <li>Static credentials (access key + secret key)</li>
      * </ol>
      * </p>
+     * <p>
+     * When none of these is configured, the SDK default chain is used. A 
configuration that asks for session or static
+     * credentials but misses one of the keys or the session token fails, 
instead of falling back to the SDK default
+     * chain.
+     * </p>
      *
-     * @param  config The AWS configuration
-     * @return        The resolved credentials provider, or null to use SDK 
default chain
+     * @param  config                   The AWS configuration
+     * @return                          The resolved credentials provider, or 
null to use SDK default chain
+     * @throws IllegalArgumentException when useSessionCredentials is true and 
the accessKey, secretKey or sessionToken
+     *                                  is missing, or when only one of 
accessKey and secretKey is set
      */
-    private static AwsCredentialsProvider 
resolveCredentialsProvider(AwsCommonConfiguration config) {
+    static AwsCredentialsProvider 
resolveCredentialsProvider(AwsCommonConfiguration config) {
 
         // Priority 1: Default credentials provider (IAM roles, env vars, etc.)
         if (config.isUseDefaultCredentialsProvider()) {
@@ -293,29 +302,48 @@ public final class AwsClientBuilderUtil {
             return ProfileCredentialsProvider.create();
         }
 
+        boolean hasAccessKey = ObjectHelper.isNotEmpty(config.getAccessKey());
+        boolean hasSecretKey = ObjectHelper.isNotEmpty(config.getSecretKey());
+
         // Priority 3: Session credentials (temporary credentials with session 
token)
         if (config.isUseSessionCredentials()) {
-            if (ObjectHelper.isNotEmpty(config.getAccessKey())
-                    && ObjectHelper.isNotEmpty(config.getSecretKey())
-                    && ObjectHelper.isNotEmpty(config.getSessionToken())) {
-                LOG.trace("Using session credentials");
-                AwsSessionCredentials sessionCredentials = 
AwsSessionCredentials.create(
-                        config.getAccessKey(),
-                        config.getSecretKey(),
-                        config.getSessionToken());
-                return StaticCredentialsProvider.create(sessionCredentials);
+            List<String> missing = new ArrayList<>(3);
+            if (!hasAccessKey) {
+                missing.add("accessKey");
+            }
+            if (!hasSecretKey) {
+                missing.add("secretKey");
             }
+            if (ObjectHelper.isEmpty(config.getSessionToken())) {
+                missing.add("sessionToken");
+            }
+            if (!missing.isEmpty()) {
+                throw new IllegalArgumentException(
+                        "useSessionCredentials is set to true, but " + 
String.join(", ", missing)
+                                                   + (missing.size() == 1 ? " 
is" : " are")
+                                                   + " not set: accessKey, 
secretKey and sessionToken must be specified");
+            }
+            LOG.trace("Using session credentials");
+            AwsSessionCredentials sessionCredentials = 
AwsSessionCredentials.create(
+                    config.getAccessKey(),
+                    config.getSecretKey(),
+                    config.getSessionToken());
+            return StaticCredentialsProvider.create(sessionCredentials);
         }
 
         // Priority 4: Static credentials (access key + secret key)
-        if (ObjectHelper.isNotEmpty(config.getAccessKey())
-                && ObjectHelper.isNotEmpty(config.getSecretKey())) {
+        if (hasAccessKey && hasSecretKey) {
             LOG.trace("Using static credentials");
             AwsBasicCredentials basicCredentials = AwsBasicCredentials.create(
                     config.getAccessKey(),
                     config.getSecretKey());
             return StaticCredentialsProvider.create(basicCredentials);
         }
+        if (hasAccessKey || hasSecretKey) {
+            throw new IllegalArgumentException(
+                    (hasAccessKey ? "accessKey is set, but secretKey is not 
set" : "secretKey is set, but accessKey is not set")
+                                               + ": both accessKey and 
secretKey must be specified");
+        }
 
         // No explicit credentials - let SDK use its default chain
         LOG.trace("No explicit credentials configured, using SDK default 
chain");
diff --git 
a/components/camel-aws/camel-aws-common/src/test/java/org/apache/camel/component/aws/common/AwsClientBuilderUtilCredentialsTest.java
 
b/components/camel-aws/camel-aws-common/src/test/java/org/apache/camel/component/aws/common/AwsClientBuilderUtilCredentialsTest.java
new file mode 100644
index 000000000000..03585d60c6fd
--- /dev/null
+++ 
b/components/camel-aws/camel-aws-common/src/test/java/org/apache/camel/component/aws/common/AwsClientBuilderUtilCredentialsTest.java
@@ -0,0 +1,251 @@
+/*
+ * 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.aws.common;
+
+import org.junit.jupiter.api.Test;
+import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
+import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
+import software.amazon.awssdk.auth.credentials.AwsSessionCredentials;
+import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
+import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
+import software.amazon.awssdk.core.Protocol;
+import software.amazon.awssdk.services.sts.StsAsyncClient;
+import software.amazon.awssdk.services.sts.StsClient;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+class AwsClientBuilderUtilCredentialsTest {
+
+    @Test
+    void sessionCredentialsAreUsed() {
+        TestConfiguration config = new TestConfiguration();
+        config.useSessionCredentials = true;
+        config.accessKey = "accessKey";
+        config.secretKey = "secretKey";
+        config.sessionToken = "sessionToken";
+
+        AwsCredentialsProvider provider = 
AwsClientBuilderUtil.resolveCredentialsProvider(config);
+
+        
assertThat(provider.resolveCredentials()).isInstanceOfSatisfying(AwsSessionCredentials.class,
 credentials -> {
+            assertThat(credentials.accessKeyId()).isEqualTo("accessKey");
+            assertThat(credentials.secretAccessKey()).isEqualTo("secretKey");
+            assertThat(credentials.sessionToken()).isEqualTo("sessionToken");
+        });
+    }
+
+    @Test
+    void sessionCredentialsWithoutKeysFail() {
+        TestConfiguration config = new TestConfiguration();
+        config.useSessionCredentials = true;
+
+        assertThatThrownBy(() -> 
AwsClientBuilderUtil.resolveCredentialsProvider(config))
+                .isInstanceOf(IllegalArgumentException.class)
+                .hasMessageStartingWith(
+                        "useSessionCredentials is set to true, but accessKey, 
secretKey, sessionToken are not set");
+    }
+
+    @Test
+    void sessionCredentialsWithoutSessionTokenFail() {
+        TestConfiguration config = new TestConfiguration();
+        config.useSessionCredentials = true;
+        config.accessKey = "accessKey";
+        config.secretKey = "secretKey";
+
+        // before, the keys were used as static credentials without the 
session token
+        assertThatThrownBy(() -> 
AwsClientBuilderUtil.resolveCredentialsProvider(config))
+                .isInstanceOf(IllegalArgumentException.class)
+                .hasMessageStartingWith("useSessionCredentials is set to true, 
but sessionToken is not set");
+    }
+
+    @Test
+    void sessionCredentialsWithEmptySecretKeyFail() {
+        TestConfiguration config = new TestConfiguration();
+        config.useSessionCredentials = true;
+        config.accessKey = "accessKey";
+        config.secretKey = "";
+        config.sessionToken = "sessionToken";
+
+        assertThatThrownBy(() -> 
AwsClientBuilderUtil.resolveCredentialsProvider(config))
+                .isInstanceOf(IllegalArgumentException.class)
+                .hasMessageStartingWith("useSessionCredentials is set to true, 
but secretKey is not set");
+    }
+
+    @Test
+    void buildClientFailsWithSessionCredentialsWithoutKeys() {
+        TestConfiguration config = new TestConfiguration();
+        config.useSessionCredentials = true;
+        config.region = "us-east-1";
+
+        assertThatThrownBy(() -> AwsClientBuilderUtil.buildClient(config, 
StsClient::builder))
+                .isInstanceOf(IllegalArgumentException.class)
+                .hasMessageContaining("useSessionCredentials");
+        assertThatThrownBy(() -> AwsClientBuilderUtil.buildAsyncClient(config, 
StsAsyncClient::builder))
+                .isInstanceOf(IllegalArgumentException.class)
+                .hasMessageContaining("useSessionCredentials");
+    }
+
+    @Test
+    void staticCredentialsAreUsed() {
+        TestConfiguration config = new TestConfiguration();
+        config.accessKey = "accessKey";
+        config.secretKey = "secretKey";
+
+        AwsCredentialsProvider provider = 
AwsClientBuilderUtil.resolveCredentialsProvider(config);
+
+        
assertThat(provider.resolveCredentials()).isInstanceOfSatisfying(AwsBasicCredentials.class,
 credentials -> {
+            assertThat(credentials.accessKeyId()).isEqualTo("accessKey");
+            assertThat(credentials.secretAccessKey()).isEqualTo("secretKey");
+        });
+    }
+
+    @Test
+    void accessKeyWithoutSecretKeyFails() {
+        TestConfiguration config = new TestConfiguration();
+        config.accessKey = "accessKey";
+
+        assertThatThrownBy(() -> 
AwsClientBuilderUtil.resolveCredentialsProvider(config))
+                .isInstanceOf(IllegalArgumentException.class)
+                .hasMessageStartingWith("accessKey is set, but secretKey is 
not set");
+    }
+
+    @Test
+    void secretKeyWithoutAccessKeyFails() {
+        TestConfiguration config = new TestConfiguration();
+        config.accessKey = "";
+        config.secretKey = "secretKey";
+
+        assertThatThrownBy(() -> 
AwsClientBuilderUtil.resolveCredentialsProvider(config))
+                .isInstanceOf(IllegalArgumentException.class)
+                .hasMessageStartingWith("secretKey is set, but accessKey is 
not set");
+    }
+
+    @Test
+    void noCredentialsUseTheSdkDefaultChain() {
+        TestConfiguration config = new TestConfiguration();
+
+        
assertThat(AwsClientBuilderUtil.resolveCredentialsProvider(config)).isNull();
+
+        config.accessKey = "";
+        config.secretKey = "";
+        
assertThat(AwsClientBuilderUtil.resolveCredentialsProvider(config)).isNull();
+    }
+
+    @Test
+    void defaultAndProfileCredentialsProviderTakePrecedence() {
+        TestConfiguration config = new TestConfiguration();
+        config.useSessionCredentials = true;
+        config.useDefaultCredentialsProvider = true;
+
+        AwsCredentialsProvider provider = 
AwsClientBuilderUtil.resolveCredentialsProvider(config);
+        try {
+            
assertThat(provider).isInstanceOf(DefaultCredentialsProvider.class);
+        } finally {
+            ((DefaultCredentialsProvider) provider).close();
+        }
+
+        config.useDefaultCredentialsProvider = false;
+        config.useProfileCredentialsProvider = true;
+        provider = AwsClientBuilderUtil.resolveCredentialsProvider(config);
+        try {
+            
assertThat(provider).isInstanceOf(ProfileCredentialsProvider.class);
+        } finally {
+            ((ProfileCredentialsProvider) provider).close();
+        }
+    }
+
+    private static final class TestConfiguration implements 
AwsCommonConfiguration {
+
+        private String accessKey;
+        private String secretKey;
+        private String sessionToken;
+        private boolean useDefaultCredentialsProvider;
+        private boolean useProfileCredentialsProvider;
+        private boolean useSessionCredentials;
+        private String region;
+
+        @Override
+        public String getAccessKey() {
+            return accessKey;
+        }
+
+        @Override
+        public String getSecretKey() {
+            return secretKey;
+        }
+
+        @Override
+        public String getSessionToken() {
+            return sessionToken;
+        }
+
+        @Override
+        public boolean isUseDefaultCredentialsProvider() {
+            return useDefaultCredentialsProvider;
+        }
+
+        @Override
+        public boolean isUseProfileCredentialsProvider() {
+            return useProfileCredentialsProvider;
+        }
+
+        @Override
+        public boolean isUseSessionCredentials() {
+            return useSessionCredentials;
+        }
+
+        @Override
+        public String getProfileCredentialsName() {
+            return null;
+        }
+
+        @Override
+        public Protocol getProxyProtocol() {
+            return null;
+        }
+
+        @Override
+        public String getProxyHost() {
+            return null;
+        }
+
+        @Override
+        public Integer getProxyPort() {
+            return null;
+        }
+
+        @Override
+        public String getRegion() {
+            return region;
+        }
+
+        @Override
+        public boolean isOverrideEndpoint() {
+            return false;
+        }
+
+        @Override
+        public String getUriEndpointOverride() {
+            return null;
+        }
+
+        @Override
+        public boolean isTrustAllCertificates() {
+            return false;
+        }
+    }
+}
diff --git 
a/components/camel-aws/camel-aws2-ecs/src/test/java/org/apache/camel/component/aws2/ecs/ECS2ClientFactoryTest.java
 
b/components/camel-aws/camel-aws2-ecs/src/test/java/org/apache/camel/component/aws2/ecs/ECS2ClientFactoryTest.java
index f748a68b6458..55b31f53e5df 100644
--- 
a/components/camel-aws/camel-aws2-ecs/src/test/java/org/apache/camel/component/aws2/ecs/ECS2ClientFactoryTest.java
+++ 
b/components/camel-aws/camel-aws2-ecs/src/test/java/org/apache/camel/component/aws2/ecs/ECS2ClientFactoryTest.java
@@ -21,6 +21,8 @@ import org.junit.jupiter.api.Test;
 import software.amazon.awssdk.services.ecs.EcsClient;
 
 import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
 public class ECS2ClientFactoryTest {
 
@@ -56,4 +58,39 @@ public class ECS2ClientFactoryTest {
         assertNotNull(ecsClient);
         ecsClient.close();
     }
+
+    @Test
+    public void getEcsClientWithSessionCredentials() {
+        ECS2Configuration configuration = new ECS2Configuration();
+        configuration.setUseSessionCredentials(true);
+        configuration.setAccessKey("testAccessKey");
+        configuration.setSecretKey("testSecretKey");
+        configuration.setSessionToken("testSessionToken");
+        configuration.setRegion("eu-west-1");
+        EcsClient ecsClient = ECS2ClientFactory.getEcsClient(configuration);
+        assertNotNull(ecsClient);
+        ecsClient.close();
+    }
+
+    @Test
+    public void getEcsClientWithSessionCredentialsWithoutSessionToken() {
+        ECS2Configuration configuration = new ECS2Configuration();
+        configuration.setUseSessionCredentials(true);
+        configuration.setAccessKey("testAccessKey");
+        configuration.setSecretKey("testSecretKey");
+        configuration.setRegion("eu-west-1");
+        IllegalArgumentException e
+                = assertThrows(IllegalArgumentException.class, () -> 
ECS2ClientFactory.getEcsClient(configuration));
+        assertTrue(e.getMessage().contains("sessionToken"), e.getMessage());
+    }
+
+    @Test
+    public void getEcsClientWithSessionCredentialsWithoutKeys() {
+        ECS2Configuration configuration = new ECS2Configuration();
+        configuration.setUseSessionCredentials(true);
+        configuration.setRegion("eu-west-1");
+        IllegalArgumentException e
+                = assertThrows(IllegalArgumentException.class, () -> 
ECS2ClientFactory.getEcsClient(configuration));
+        assertTrue(e.getMessage().contains("accessKey, secretKey, 
sessionToken"), e.getMessage());
+    }
 }
diff --git 
a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc 
b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
index 25708c2cd51b..058deb57312d 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
@@ -12297,6 +12297,20 @@ With `allowUrlBody=true`, a body that does not start 
with `version: 1` and is no
 with a space or a relative name, now fails with an `InvalidPayloadException`, 
as a URL with an unknown scheme already
 did. Before, it failed with an `IllegalArgumentException`. A URL that cannot 
be fetched keeps its own exception.
 
+=== camel-aws - incomplete session or static credentials fail when the client 
is created
+
+The AWS components (all that use `camel-aws-common` to create their client, 
such as `aws2-s3`, `aws2-sqs`,
+`aws2-ecs` and `aws-security-hub`) now fail with an `IllegalArgumentException` 
that names the missing option when:
+
+* `useSessionCredentials=true` is set, but the `accessKey`, `secretKey` or 
`sessionToken` is not set (or empty).
+  Before, the client used the default credentials chain of the AWS SDK 
(environment variables, system properties,
+  `~/.aws` profiles, instance or container role), or the access key and secret 
key without the session token.
+* only one of `accessKey` and `secretKey` is set (the other one is not set or 
empty). Before, the client used the
+  default credentials chain of the AWS SDK.
+
+When no credentials option is set at all, the client uses the default 
credentials chain of the AWS SDK, as before.
+`useDefaultCredentialsProvider` and `useProfileCredentialsProvider` are not 
changed.
+
 === Kamelets - removed Kamelets
 
 The Kamelet catalog (camel-kamelets 4.24) removes Kamelets that could not work 
on Camel 4.23 or 4.24:

Reply via email to