Skip to content

Problem when using Amazon S3 encryption client

0

Hello, I would like to use client-side encryption using the Amazon Encryption client SDK for Java v2. I have imported in my pom.xml: <dependency> <groupId>software.amazon.encryption.s3</groupId> <artifactId>amazon-s3-encryption-client-java</artifactId> <version>3.2.1</version> </dependency> So far I have tried establishing a client in two ways:

  1. Use directly the client, providing credentials, region, etc.: S3Client s3Client = S3EncryptionClient.builder() .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(new String(<my access key>), new String(<my secret key>)))) .region(<my region>).aesKey(<my secret key>) .enableDelayedAuthenticationMode(true) .enableLegacyWrappingAlgorithms(true).build();
  2. Use wrapped original S3 client: S3Client s3Client = S3Client .builder() .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(new String(<my access key>), new String(<my secret key>)))). region(<my region>).build(); S3EncryptionClient.builder().wrappedClient(s3Client).aesKey(<my secret key>) .enableDelayedAuthenticationMode(true) .enableLegacyWrappingAlgorithms(true).build();

In both cases, trying to build a client results either in:

  1. Unable to load region information (which I think fixed)
  2. Unable to load an HTTP implementation from any provider in the chain. Once I supplied information about particular provider - using .httpClient(<apache http client>) or .httpClient(<url-connection-client) results in the same error, or in: "Multiple HTTP implementation found on the classpath". I have tried in my classpath to have both clients or one by one. The same errors persist.

Can someone provide any hints or help how to troubleshoot or resolve the issue. The AWS SDK version I use is: 2.21.20. The amazon-s3-encryption-client-java version is 3.2.1. I have tried with 3.2.0, 3.2.2 and 3.3.0, all seem to not work on this case. My specific case is to to AES-GCM/CBC encryption

Thanks!

asked 2 years ago608 views

4 Answers
0

Thank you for providing details about your issue with the Amazon S3 Encryption Client for Java. It seems you're encountering some configuration challenges. Let's address these step by step:

  1. Region Information: It's good that you've resolved the "Unable to load region information" error. Make sure you're specifying the correct region for your S3 bucket.

  2. HTTP Implementation: The error "Unable to load an HTTP implementation from any provider in the chain" suggests there might be a conflict or missing dependency for the HTTP client. To resolve this:

    • Ensure you have the necessary HTTP client dependencies in your pom.xml. For example, you might need to add:

      <dependency>
          <groupId>software.amazon.awssdk</groupId>
          <artifactId>apache-client</artifactId>
          <version>2.21.20</version>
      </dependency>
      
    • If you're still seeing "Multiple HTTP implementation found on the classpath", you may need to exclude conflicting dependencies.

  3. AES Key Configuration: For AES-GCM/CBC encryption, you should use the AesGcmKeyring instead of directly providing the AES key to the client builder. Here's an example of how to set this up:

    KeyGenerator keyGen = KeyGenerator.getInstance("AES");
    keyGen.init(256);
    SecretKey secretKey = keyGen.generateKey();
    
    Keyring wrappingKey = AesGcmKeyring.builder()
            .wrappingKey(secretKey)
            .build();
    
    CryptographicMaterialsManager cmm = DefaultCryptoMaterialsManager.builder()
            .keyring(wrappingKey)
            .build();
    
    S3Client s3Client = S3EncryptionClient.builder()
            .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(accessKey, secretKey)))
            .region(region)
            .cryptoMaterialsManager(cmm)
            .build();
    
  4. Delayed Authentication and Legacy Algorithms: Be cautious when enabling delayed authentication mode and legacy wrapping algorithms, as these can potentially compromise security. Only use these if absolutely necessary for compatibility with older encrypted objects.

  5. Wrapping an existing S3 client: If you prefer to wrap an existing S3 client, make sure you're building the S3EncryptionClient correctly:

    S3EncryptionClient encryptionClient = S3EncryptionClient.builder()
            .wrappedClient(s3Client)
            .cryptoMaterialsManager(cmm)
            .build();
    

Remember to handle exceptions properly and ensure you're using compatible versions of the AWS SDK and the S3 Encryption Client. If you continue to face issues, consider enabling debug logging for more detailed error information.

Lastly, always ensure you're following best practices for key management and encryption in your production environment.
Sources
Facing issue while doing Putobject method using S3 Encryption Client | AWS re:Post
Amazon S3 Encryption Client for Java examples - Amazon S3 Encryption Client
Supported encryption algorithms - Amazon S3 Encryption Client

answered 2 years ago

  • Unfortunately, I have tried this, as I stated. Using the Keyring and the CMM probably are the proper way to use the client, but I does not get to the point to use them because it fails before this point. I tried with providing the Keyring and CMM properties as well now, but the result is the same: "software.amazon.awssdk.core.exception.SdkClientException: Unable to load an HTTP implementation from any provider in the chain. You must declare a dependency on an appropriate HTTP implementation or pass in an SdkHttpClient explicitly to the client builder." As I said earlier - I have tried also by providing http client (url connection client), but the result remains the same.

0

Would it be possible to share the code sample? Please remove any confidential information before sharing the code.

AWS
SUPPORT ENGINEER

answered 2 years ago

0

Here is the basic code sample, which is almost identical to the one proposed above:

KeyGenerator keyGen = null;
        try {
            keyGen = KeyGenerator.getInstance("AES");
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
        keyGen.init(256);
        SecretKey secretKey = keyGen.generateKey();

        Keyring wrappingKey = AesKeyring.builder()
                .wrappingKey(secretKey)
                .build();

        CryptographicMaterialsManager cmm = DefaultCryptoMaterialsManager.builder()
                .keyring(wrappingKey)
                .build();

        S3Client s3Client = S3EncryptionClient.builder()
                .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(new String(accessKey), new String(secret))))
                .region(Region.of(region))
                .cryptoMaterialsManager(cmm)
                .build();

Whenever I try at later point to execute putObject, get, etc, I get the mentioned error above:

s3Client.getObject(GetObjectRequest.builder()
                .bucket("some bucket").key("my requested object").build());

Later version which I tried was:

S3Client s3Client = S3Client.builder()
                .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(new String(accessKey), new String(secret))))
                .region(Region.of(region))
                .httpClient(UrlConnectionHttpClient.create())
                .build();

        S3EncryptionClient s3EncClient = S3EncryptionClient.builder()
                .wrappedClient(s3Client)
                .region(Region.of(region))
                .enableLegacyWrappingAlgorithms(true)
                .enableLegacyUnauthenticatedModes(true)
                .cryptoMaterialsManager(cmm)
                .build();

but the results were the same. As I said, in my classpath, there is the url-connection-client.jar present. I am not sure how the encryption-client and the original AWS SDK versions correspond correctly - for the encryption client I tried with 3.2.0 and 3.2.1 (with 3.3.0 there were NoClassDefFound exceptions), the AWS SDK is 2.21.20 for Java V2. Thanks!

answered 2 years ago

0

Thank you. Additionally, please can you share the details of your dependency tree?

The command is $ mvn dependency:tree (Maven) or $ gradle dependencies (Gradle).

For the "Multiple HTTP implementation found on the classpath" error, this exception is not thrown anymore with the latest version of the Java SDK. The SDK used to throw this error if it had multiple http clients in the classpath (e.g., Apache and UrlConnection for sync clients) but we changed the verification to choose one. This change was made in Java SDK 2.22.0 [1], so you would have to upgrade amazon-s3-encryption-client-java from 3.2.1 to 3.3.x

I completely understand that you have also tried version 3.3.0 and observed the issue.

[1] changelog -- https://github.com/aws/aws-sdk-java-v2/blob/master/changelogs/2.22.x-CHANGELOG.md#aws-sdk-for-java-v2-9

AWS
SUPPORT ENGINEER

answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.