1 /* 2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"). 5 * You may not use this file except in compliance with the License. 6 * A copy of the License is located at 7 * 8 * http://aws.amazon.com/apache2.0 9 * 10 * or in the "license" file accompanying this file. This file is distributed 11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 12 * express or implied. See the License for the specific language governing 13 * permissions and limitations under the License. 14 */ 15 16 package software.amazon.awssdk.services.s3.internal.plugins; 17 18 import software.amazon.awssdk.annotations.SdkInternalApi; 19 import software.amazon.awssdk.core.SdkPlugin; 20 import software.amazon.awssdk.core.SdkServiceClientConfiguration; 21 import software.amazon.awssdk.core.client.config.SdkClientConfiguration; 22 import software.amazon.awssdk.core.client.config.SdkClientOption; 23 import software.amazon.awssdk.http.auth.aws.signer.AwsV4FamilyHttpSigner; 24 import software.amazon.awssdk.services.s3.S3Configuration; 25 import software.amazon.awssdk.services.s3.S3ServiceClientConfiguration; 26 import software.amazon.awssdk.services.s3.auth.scheme.S3AuthSchemeProvider; 27 import software.amazon.awssdk.utils.Logger; 28 29 /** 30 * Internal plugin that uses the check if {@link S3Configuration#chunkedEncodingEnabled()} is configured and equals to 31 * {@code false}, if so, then it installs an instance of {@link S3DisableChunkEncodingAuthSchemeProvider} wrapping the configured 32 * {@link S3AuthSchemeProvider} that sets {@link AwsV4FamilyHttpSigner#CHUNK_ENCODING_ENABLED} to false. 33 * <p> 34 * This pre SRA logic was implemented before using an interceptor but now requires wrapping the S3AuthSchemeProvider for it to 35 * work. 36 */ 37 @SdkInternalApi 38 public final class S3DisableChunkEncodingIfConfiguredPlugin implements SdkPlugin { 39 40 private static final Logger LOG = Logger.loggerFor(S3DisableChunkEncodingIfConfiguredPlugin.class); 41 42 private final boolean isServiceConfigurationPresent; 43 private final boolean isChunkedEncodingEnabledConfigured; 44 private final boolean isChunkedEncodingEnabledDisabled; 45 private final boolean configuresDisableChunkEncoding; 46 S3DisableChunkEncodingIfConfiguredPlugin(SdkClientConfiguration config)47 public S3DisableChunkEncodingIfConfiguredPlugin(SdkClientConfiguration config) { 48 S3Configuration serviceConfiguration = 49 (S3Configuration) config.option(SdkClientOption.SERVICE_CONFIGURATION); 50 51 boolean isServiceConfigurationPresent = serviceConfiguration != null; 52 boolean shouldAddDisableChunkEncoding = false; 53 boolean isChunkedEncodingEnabledConfigured = false; 54 boolean isChunkedEncodingEnabledDisabled = false; 55 boolean configuresDisableChunkEncoding = false; 56 if (isServiceConfigurationPresent) { 57 isChunkedEncodingEnabledConfigured = serviceConfiguration.toBuilder().chunkedEncodingEnabled() != null; 58 isChunkedEncodingEnabledDisabled = !serviceConfiguration.chunkedEncodingEnabled(); 59 configuresDisableChunkEncoding = isChunkedEncodingEnabledConfigured && isChunkedEncodingEnabledDisabled; 60 if (configuresDisableChunkEncoding) { 61 shouldAddDisableChunkEncoding = true; 62 } 63 } 64 this.configuresDisableChunkEncoding = shouldAddDisableChunkEncoding; 65 this.isChunkedEncodingEnabledConfigured = isChunkedEncodingEnabledConfigured; 66 this.isChunkedEncodingEnabledDisabled = isChunkedEncodingEnabledDisabled; 67 this.isServiceConfigurationPresent = isServiceConfigurationPresent; 68 } 69 70 @Override configureClient(SdkServiceClientConfiguration.Builder config)71 public void configureClient(SdkServiceClientConfiguration.Builder config) { 72 if (configuresDisableChunkEncoding) { 73 LOG.debug(() -> String.format("chunkedEncodingEnabled was explicitly disabled in the configuration, adding " 74 + "`S3DisableChunkEncodingAuthSchemeProvider` auth provider wrapper.")); 75 S3ServiceClientConfiguration.Builder s3Config = (S3ServiceClientConfiguration.Builder) config; 76 77 S3AuthSchemeProvider disablingAuthSchemeProvider = 78 S3DisableChunkEncodingAuthSchemeProvider.create(s3Config.authSchemeProvider()); 79 s3Config.authSchemeProvider(disablingAuthSchemeProvider); 80 } 81 } 82 } 83