-
Notifications
You must be signed in to change notification settings - Fork 9.1k
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
Changes from all commits
fe9ec5e
4f7a84a
d45b1e5
11c2192
62f84d6
eb56cbf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
---|---|---|
|
@@ -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; | ||
|
||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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) | ||
|
@@ -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()); | ||
|
@@ -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); | ||
} | ||
|
||
|
There was a problem hiding this comment.
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?