Skip to content

HADOOP-18708. AWS SDK V2 - Implement CSE #6164

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

Closed
Closed
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
12 changes: 12 additions & 0 deletions hadoop-project/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@
<surefire.fork.timeout>900</surefire.fork.timeout>
<aws-java-sdk.version>1.12.599</aws-java-sdk.version>
<aws-java-sdk-v2.version>2.24.6</aws-java-sdk-v2.version>
<amazon-s3-encryption-client-java.version>3.1.1</amazon-s3-encryption-client-java.version>
<aws.eventstream.version>1.0.1</aws.eventstream.version>
<hsqldb.version>2.7.1</hsqldb.version>
<frontend-maven-plugin.version>1.11.2</frontend-maven-plugin.version>
Expand Down Expand Up @@ -1149,6 +1150,17 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>software.amazon.encryption.s3</groupId>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this going to get into bundle.jar?

<artifactId>amazon-s3-encryption-client-java</artifactId>
<version>${amazon-s3-encryption-client-java.version}</version>
<exclusions>
<exclusion>
<groupId>io.netty</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.mina</groupId>
<artifactId>mina-core</artifactId>
Expand Down
15 changes: 15 additions & 0 deletions hadoop-tools/hadoop-aws/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,16 @@
<bannedImport>org.apache.hadoop.mapred.**</bannedImport>
</bannedImports>
</restrictImports>
<restrictImports>
<includeTestCode>false</includeTestCode>
<reason>Restrict encryption client imports to encryption client factory</reason>
<exclusions>
<exclusion>org.apache.hadoop.fs.s3a.EncryptionS3ClientFactory</exclusion>
</exclusions>
<bannedImports>
<bannedImport>software.amazon.encryption.s3.**</bannedImport>
</bannedImports>
</restrictImports>
</rules>
</configuration>
</execution>
Expand Down Expand Up @@ -508,6 +518,11 @@
<artifactId>bundle</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>software.amazon.encryption.s3</groupId>
<artifactId>amazon-s3-encryption-client-java</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import software.amazon.awssdk.identity.spi.AwsCredentialsIdentity;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3AsyncClient;
import software.amazon.awssdk.services.s3.S3AsyncClientBuilder;
import software.amazon.awssdk.services.s3.S3BaseClientBuilder;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.S3Configuration;
Expand Down Expand Up @@ -145,11 +146,17 @@ public S3AsyncClient createS3AsyncClient(
.thresholdInBytes(parameters.getMultiPartThreshold())
.build();

return configureClientBuilder(S3AsyncClient.builder(), parameters, conf, bucket)
.httpClientBuilder(httpClientBuilder)
.multipartConfiguration(multipartConfiguration)
.multipartEnabled(parameters.isMultipartCopy())
.build();
S3AsyncClientBuilder s3AsyncClientBuilder =
configureClientBuilder(S3AsyncClient.builder(), parameters, conf, bucket).httpClientBuilder(
httpClientBuilder);

if (!parameters.isClientSideEncryptionEnabled()) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to do this because currently if you try to do a ranged GET with multipart enabled, it fails. This is intentional, as the SDK team is working on adding "automatic multipart download" for a GET to the async client, and this capability is disabled till then.

When we build the S3EncryptionClient we configure and pass in a S3AsyncClient. Under the hood, S3EC uses the S3Async client for all it's encryption related operations (eg: GET). And so if we pass in a client with multipartEnabled, all GETs will start failing.

The impact of this that if you're using CSE, there's no multipart mode, so you copies become slower again.

A workaround can be to create a new async client with multipart disabled. So we have two async clients, one with MPU enable, one with disabled. and share underlying HTTP client b/w the two async clients, so it shouldn't have too much of an impact.

I'll make that change if required, once I have more clarity from SDK team on when ranged GETs become available in multipart mode.

s3AsyncClientBuilder
.multipartConfiguration(multipartConfiguration)
.multipartEnabled(parameters.isMultipartCopy());
}

return s3AsyncClientBuilder.build();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* 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.hadoop.fs.s3a;

import java.io.IOException;
import java.net.URI;

import software.amazon.awssdk.services.s3.S3AsyncClient;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.encryption.s3.S3AsyncEncryptionClient;
import software.amazon.encryption.s3.S3EncryptionClient;

import org.apache.hadoop.fs.s3a.impl.CSEMaterials;

import static org.apache.hadoop.fs.s3a.impl.InstantiationIOException.unavailable;

public class EncryptionS3ClientFactory extends DefaultS3ClientFactory {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

javadocs, which include mentioning that this needs the cse on the runtime


private static final String ENCRYPTION_CLIENT_CLASSNAME =
"software.amazon.encryption.s3.S3EncryptionClient";

/**
* Encryption client availability.
*/
private static final boolean ENCRYPTION_CLIENT_FOUND = checkForEncryptionClient();

/**
* S3Client to be wrapped by encryption client.
*/
private S3Client s3Client;

/**
* S3AsyncClient to be wrapped by encryption client.
*/
private S3AsyncClient s3AsyncClient;

private static boolean checkForEncryptionClient() {
try {
ClassLoader cl = EncryptionS3ClientFactory.class.getClassLoader();
cl.loadClass(ENCRYPTION_CLIENT_CLASSNAME);
LOG.debug("encryption client class {} found", ENCRYPTION_CLIENT_CLASSNAME);
return true;
} catch (Exception e) {
LOG.debug("encryption client class {} not found", ENCRYPTION_CLIENT_CLASSNAME, e);
return false;
}
}

/**
* Is the Encryption client available?
* @return true if it was found in the classloader
*/
private static synchronized boolean isEncryptionClientAvailable() {
return ENCRYPTION_CLIENT_FOUND;
}


@Override
public S3Client createS3Client(URI uri, S3ClientCreationParameters parameters) throws IOException {

if (!isEncryptionClientAvailable()) {
throw unavailable(uri, ENCRYPTION_CLIENT_CLASSNAME, null, "No encryption client available");
}

s3Client = super.createS3Client(uri, parameters);
s3AsyncClient = super.createS3AsyncClient(uri, parameters);

return createS3EncryptionClient(parameters.getClientSideEncryptionMaterials());
}

@Override
public S3AsyncClient createS3AsyncClient(URI uri, S3ClientCreationParameters parameters) throws IOException {

if (!isEncryptionClientAvailable()) {
throw unavailable(uri, ENCRYPTION_CLIENT_CLASSNAME, null, "No encryption client available");
}

return createS3AsyncEncryptionClient(parameters.getClientSideEncryptionMaterials());
}

private S3Client createS3EncryptionClient(final CSEMaterials cseMaterials) {
S3EncryptionClient.Builder s3EncryptionClientBuilder =
S3EncryptionClient.builder().wrappedAsyncClient(s3AsyncClient).wrappedClient(s3Client)
.enableLegacyUnauthenticatedModes(true);

if (cseMaterials.getCseKeyType().equals(CSEMaterials.CSEKeyType.KMS)) {
s3EncryptionClientBuilder.kmsKeyId(cseMaterials.getKmsKeyId());
}

return s3EncryptionClientBuilder.build();
}


private S3AsyncClient createS3AsyncEncryptionClient(final CSEMaterials cseMaterials) {

S3AsyncEncryptionClient.Builder s3EncryptionAsyncClientBuilder =
S3AsyncEncryptionClient.builder().wrappedClient(s3AsyncClient)
.enableLegacyUnauthenticatedModes(true);

if (cseMaterials.getCseKeyType().equals(CSEMaterials.CSEKeyType.KMS)) {
s3EncryptionAsyncClientBuilder.kmsKeyId(cseMaterials.getKmsKeyId());
}

return s3EncryptionAsyncClientBuilder.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import software.amazon.awssdk.services.s3.model.CompletedPart;
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
import software.amazon.awssdk.services.s3.model.PutObjectResponse;
import software.amazon.awssdk.services.s3.model.SdkPartType;
import software.amazon.awssdk.services.s3.model.UploadPartRequest;
import software.amazon.awssdk.services.s3.model.UploadPartResponse;

Expand Down Expand Up @@ -876,11 +877,17 @@ private void uploadBlockAsync(final S3ADataBlocks.DataBlock block,
? RequestBody.fromFile(uploadData.getFile())
: RequestBody.fromInputStream(uploadData.getUploadStream(), size);

request = writeOperationHelper.newUploadPartRequestBuilder(
UploadPartRequest.Builder requestBuilder = writeOperationHelper.newUploadPartRequestBuilder(
key,
uploadId,
currentPartNumber,
size).build();
size);

if (isLast) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add this as a param to pass down to WriteOperationHelper and let the factory do the work

requestBuilder.sdkPartType(SdkPartType.LAST);
}

request = requestBuilder.build();
} catch (SdkException aws) {
// catch and translate
IOException e = translateException("upload", key, aws);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
import org.apache.hadoop.fs.s3a.impl.AWSCannedACL;
import org.apache.hadoop.fs.s3a.impl.AWSHeaders;
import org.apache.hadoop.fs.s3a.impl.BulkDeleteRetryHandler;
import org.apache.hadoop.fs.s3a.impl.CSEMaterials;
import org.apache.hadoop.fs.s3a.impl.ChangeDetectionPolicy;
import org.apache.hadoop.fs.s3a.impl.ConfigurationHelper;
import org.apache.hadoop.fs.s3a.impl.ContextAccessors;
Expand Down Expand Up @@ -1034,6 +1035,21 @@ private void bindAWSClient(URI name, boolean dtEnabled) throws IOException {
S3_CLIENT_FACTORY_IMPL, DEFAULT_S3_CLIENT_FACTORY_IMPL,
S3ClientFactory.class);

S3ClientFactory clientFactory;
CSEMaterials cseMaterials = null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this stuff MUST pick up what is in EncryptionSecrets (which you can extend but MUST NOT use any aws sdk classes). this is what gets passed round with delegation tokens, so allows you to pass secrets with a job and without the cluster having the config


if (isCSEEnabled) {
String kmsKeyId = getS3EncryptionKey(bucket, conf, true);

cseMaterials = new CSEMaterials()
.withCSEKeyType(CSEMaterials.CSEKeyType.KMS)
.withKmsKeyId(kmsKeyId);

clientFactory = ReflectionUtils.newInstance(EncryptionS3ClientFactory.class, conf);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so if encryption is set, no custom client factory (done in places for unit tests) work. proposed: warn if cse is enabled and the value of (s3ClientFactoryClass is not the default

the other way would be to choose the default client factory based on the encryption flag, so tests will always override it (the documentation will have to cover it). I think I prefer that

} else {
clientFactory = ReflectionUtils.newInstance(s3ClientFactoryClass, conf);
}

S3ClientFactory.S3ClientCreationParameters parameters =
new S3ClientFactory.S3ClientCreationParameters()
.withCredentialSet(credentials)
Expand All @@ -1053,9 +1069,10 @@ private void bindAWSClient(URI name, boolean dtEnabled) throws IOException {
.withExpressCreateSession(
conf.getBoolean(S3EXPRESS_CREATE_SESSION, S3EXPRESS_CREATE_SESSION_DEFAULT))
.withChecksumValidationEnabled(
conf.getBoolean(CHECKSUM_VALIDATION, CHECKSUM_VALIDATION_DEFAULT));
conf.getBoolean(CHECKSUM_VALIDATION, CHECKSUM_VALIDATION_DEFAULT))
.withClientSideEncryptionEnabled(isCSEEnabled)
.withClientSideEncryptionMaterials(cseMaterials);

S3ClientFactory clientFactory = ReflectionUtils.newInstance(s3ClientFactoryClass, conf);
s3Client = clientFactory.createS3Client(getUri(), parameters);
createS3AsyncClient(clientFactory, parameters);
transferManager = clientFactory.createS3TransferManager(getS3AsyncClient());
Expand All @@ -1070,7 +1087,8 @@ private void bindAWSClient(URI name, boolean dtEnabled) throws IOException {
* @throws IOException on any IO problem
*/
private void createS3AsyncClient(S3ClientFactory clientFactory,
S3ClientFactory.S3ClientCreationParameters parameters) throws IOException {
S3ClientFactory.S3ClientCreationParameters parameters)
throws IOException {
s3AsyncClient = clientFactory.createS3AsyncClient(getUri(), parameters);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
import static org.apache.hadoop.fs.s3a.Constants.*;
import static org.apache.hadoop.fs.s3a.audit.AuditIntegration.maybeTranslateAuditException;
import static org.apache.hadoop.fs.s3a.impl.ErrorTranslation.isUnknownBucket;
import static org.apache.hadoop.fs.s3a.impl.ErrorTranslation.maybeExtractSdkException;
import static org.apache.hadoop.fs.s3a.impl.InstantiationIOException.instantiationException;
import static org.apache.hadoop.fs.s3a.impl.InstantiationIOException.isAbstract;
import static org.apache.hadoop.fs.s3a.impl.InstantiationIOException.isNotInstanceOf;
Expand Down Expand Up @@ -182,6 +183,8 @@ public static IOException translateException(@Nullable String operation,
path = "/";
}

exception = maybeExtractSdkException(exception);

if (!(exception instanceof AwsServiceException)) {
// exceptions raised client-side: connectivity, auth, network problems...
Exception innerCause = containsInterruptedException(exception);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

import org.apache.hadoop.classification.InterfaceAudience;
import org.apache.hadoop.classification.InterfaceStability;
import org.apache.hadoop.fs.s3a.impl.CSEMaterials;
import org.apache.hadoop.fs.s3a.statistics.StatisticsFromAwsSdk;

import static org.apache.hadoop.fs.s3a.Constants.DEFAULT_ENDPOINT;
Expand Down Expand Up @@ -186,6 +187,16 @@ final class S3ClientCreationParameters {
*/
private boolean fipsEnabled;

/**
* Is client side encryption enabled.
*/
private Boolean isCSEEnabled;

/**
* Client side encryption materials.
*/
private CSEMaterials cseMaterials;

/**
* List of execution interceptors to include in the chain
* of interceptors in the SDK.
Expand Down Expand Up @@ -504,5 +515,43 @@ public S3ClientCreationParameters withFipsEnabled(final boolean value) {
fipsEnabled = value;
return this;
}

/**
* Set the client side encryption flag.
*
* @param value new value
* @return the builder
*/
public S3ClientCreationParameters withClientSideEncryptionEnabled(final boolean value) {
this.isCSEEnabled = value;
return this;
}

/**
* Get the client side encryption flag.
* @return client side encryption flag
*/
public boolean isClientSideEncryptionEnabled() {
return this.isCSEEnabled;
}

/**
* Set the client side encryption materials.
*
* @param value new value
* @return the builder
*/
public S3ClientCreationParameters withClientSideEncryptionMaterials(final CSEMaterials value) {
this.cseMaterials = value;
return this;
}

/**
* Get the client side encryption materials.
* @return client side encryption materials
*/
public CSEMaterials getClientSideEncryptionMaterials() {
return this.cseMaterials;
}
}
}
Loading