-
Notifications
You must be signed in to change notification settings - Fork 302
Add support for S3 compatible storage #3188
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
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 |
|---|---|---|
|
|
@@ -16,10 +16,13 @@ | |
|
|
||
| package io.athenz.server.aws.common.store.impl; | ||
|
|
||
| import com.yahoo.athenz.auth.util.Crypto; | ||
| import io.athenz.server.aws.common.utils.Utils; | ||
| import software.amazon.awssdk.core.ResponseInputStream; | ||
| import software.amazon.awssdk.http.apache.ApacheHttpClient; | ||
| import software.amazon.awssdk.regions.Region; | ||
| import software.amazon.awssdk.services.s3.S3Client; | ||
| import software.amazon.awssdk.services.s3.S3ClientBuilder; | ||
| import software.amazon.awssdk.services.s3.model.*; | ||
| import com.fasterxml.jackson.databind.DeserializationFeature; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
|
@@ -31,6 +34,10 @@ | |
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import javax.net.ssl.TrustManagerFactory; | ||
| import java.net.URI; | ||
| import java.security.KeyStore; | ||
| import java.security.cert.X509Certificate; | ||
| import java.util.*; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.concurrent.ExecutorService; | ||
|
|
@@ -53,6 +60,8 @@ public class S3ChangeLogStore implements ChangeLogStore { | |
|
|
||
| private static final String NUMBER_OF_THREADS = "athenz.zts.bucket.threads"; | ||
| private static final String DEFAULT_TIMEOUT_SECONDS = "athenz.zts.bucket.threads.timeout"; | ||
| private static final String ZTS_PROP_AWS_S3_ENDPOINT = "athenz.zts.aws_s3_endpoint"; | ||
| private static final String ZTS_PROP_AWS_S3_CA_CERT = "athenz.zts.aws_s3_ca_cert"; | ||
| private final int nThreads = Integer.parseInt(System.getProperty(NUMBER_OF_THREADS, "10")); | ||
| private final int defaultTimeoutSeconds = Integer.parseInt(System.getProperty(DEFAULT_TIMEOUT_SECONDS, "1800")); | ||
| protected Map<String, SignedDomain> tempSignedDomainMap = new ConcurrentHashMap<>(); | ||
|
|
@@ -497,7 +506,38 @@ S3Client getS3Client() { | |
| throw new RuntimeException("S3ChangeLogStore: Couldn't detect AWS region"); | ||
| } | ||
|
|
||
| return S3Client.builder().region(Region.of(awsRegion)).build(); | ||
| S3ClientBuilder s3ClientBuilder = S3Client.builder().region(Region.of(awsRegion)); | ||
|
|
||
| // check if we have a custom endpoint | ||
| String s3Endpoint = System.getProperty(ZTS_PROP_AWS_S3_ENDPOINT); | ||
| if (!StringUtil.isEmpty(s3Endpoint)) { | ||
| s3ClientBuilder.endpointOverride(URI.create(s3Endpoint)); | ||
| } | ||
|
|
||
| // check if we have a custom ca cert | ||
| String s3CaCert = System.getProperty(ZTS_PROP_AWS_S3_CA_CERT); | ||
| if (!StringUtil.isEmpty(s3CaCert)) { | ||
| try { | ||
| ApacheHttpClient.Builder httpClientBuilder = ApacheHttpClient.builder(); | ||
| X509Certificate[] certs = Crypto.loadX509Certificates(s3CaCert); | ||
| KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); | ||
| keyStore.load(null, null); // Initialize empty keystore | ||
| int i = 0; | ||
| for (X509Certificate cert : certs) { | ||
| keyStore.setCertificateEntry("custom-ca-" + i++, cert); | ||
| } | ||
| TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); | ||
| tmf.init(keyStore); | ||
|
|
||
| httpClientBuilder.tlsTrustManagersProvider(tmf::getTrustManagers); | ||
| s3ClientBuilder.httpClient(httpClientBuilder.build()); | ||
| } catch (Exception ex) { | ||
| LOGGER.error("S3ChangeLogStore: unable to load custom ca cert: {}", s3CaCert, ex); | ||
| throw new RuntimeException("S3ChangeLogStore: unable to load custom ca cert"); | ||
| } | ||
| } | ||
|
Comment on lines
+519
to
+538
Contributor
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 block of code for handling custom CA certificates is nearly identical to the one in |
||
|
|
||
| return s3ClientBuilder.build(); | ||
| } | ||
|
|
||
| public ExecutorService getExecutorService() { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,18 +16,26 @@ | |
|
|
||
| package io.athenz.syncer.aws.common.impl; | ||
|
|
||
| import com.yahoo.athenz.auth.util.Crypto; | ||
| import io.athenz.syncer.common.zms.Config; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; | ||
| import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; | ||
| import software.amazon.awssdk.http.SdkHttpClient; | ||
| import software.amazon.awssdk.services.s3.S3Client; | ||
| import software.amazon.awssdk.services.s3.S3ClientBuilder; | ||
| import software.amazon.awssdk.http.apache.ApacheHttpClient; | ||
| import software.amazon.awssdk.core.checksums.RequestChecksumCalculation; | ||
| import software.amazon.awssdk.core.checksums.ResponseChecksumValidation; | ||
| import software.amazon.awssdk.regions.Region; | ||
| import software.amazon.awssdk.regions.providers.DefaultAwsRegionProviderChain; | ||
| import software.amazon.awssdk.services.s3.model.HeadBucketRequest; | ||
|
|
||
| import javax.net.ssl.TrustManagerFactory; | ||
| import java.net.URI; | ||
| import java.security.KeyStore; | ||
| import java.security.cert.X509Certificate; | ||
| import java.time.Duration; | ||
|
|
||
| public class S3ClientFactory { | ||
|
|
@@ -71,31 +79,56 @@ public static S3Client getS3Client() throws Exception { | |
| } | ||
| } | ||
|
|
||
| SdkHttpClient apacheHttpClient = ApacheHttpClient.builder() | ||
| ApacheHttpClient.Builder httpClientBuilder = ApacheHttpClient.builder() | ||
| .connectionTimeout(Duration.ofMillis(connectionTimeout)) | ||
| .socketTimeout(Duration.ofMillis(requestTimeout)) | ||
| .build(); | ||
| .socketTimeout(Duration.ofMillis(requestTimeout)); | ||
|
|
||
| final String caCertPath = Config.getInstance().getConfigParam(Config.SYNC_CFG_PARAM_AWS_S3_CA_CERT); | ||
| if (!Config.isEmpty(caCertPath)) { | ||
| X509Certificate[] certs = Crypto.loadX509Certificates(caCertPath); | ||
| KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); | ||
| keyStore.load(null, null); // Initialize empty keystore | ||
| int i = 0; | ||
| for (X509Certificate cert : certs) { | ||
| keyStore.setCertificateEntry("custom-ca-" + i++, cert); | ||
| } | ||
| TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); | ||
| tmf.init(keyStore); | ||
|
|
||
| httpClientBuilder.tlsTrustManagersProvider(tmf::getTrustManagers); | ||
| } | ||
|
Comment on lines
+87
to
+99
Contributor
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. |
||
|
|
||
| SdkHttpClient apacheHttpClient = httpClientBuilder.build(); | ||
|
|
||
| S3ClientBuilder s3ClientBuilder = S3Client.builder() | ||
| .httpClient(apacheHttpClient) | ||
| .region(getRegion()); | ||
|
|
||
| // Enable checksum calculation and validation if configured | ||
| final String checksumValidation = Config.getInstance().getConfigParam(Config.SYNC_CFG_PARAM_AWS_S3_CHECKSUM_VALIDATION); | ||
| if (!Config.isEmpty(checksumValidation) && Boolean.parseBoolean(checksumValidation)) { | ||
| s3ClientBuilder | ||
| .requestChecksumCalculation(RequestChecksumCalculation.WHEN_REQUIRED) | ||
| .responseChecksumValidation(ResponseChecksumValidation.WHEN_REQUIRED); | ||
| LOGGER.debug("S3 checksum calculation and validation enabled"); | ||
| } | ||
|
|
||
| final String awsS3Endpoint = Config.getInstance().getConfigParam(Config.SYNC_CFG_PARAM_AWS_S3_ENDPOINT); | ||
| if (!Config.isEmpty(awsS3Endpoint)) { | ||
| s3ClientBuilder.endpointOverride(URI.create(awsS3Endpoint)); | ||
| } | ||
|
|
||
| S3Client s3client; | ||
| final String awsKeyId = Config.getInstance().getConfigParam(Config.SYNC_CFG_PARAM_AWS_KEY_ID); | ||
| final String awsAccKey = Config.getInstance().getConfigParam(Config.SYNC_CFG_PARAM_AWS_ACCESS_KEY); | ||
| if (!Config.isEmpty(awsKeyId) && !Config.isEmpty(awsAccKey)) { | ||
| AwsBasicCredentials awsCreds = AwsBasicCredentials.builder() | ||
| .accessKeyId(awsKeyId).secretAccessKey(awsAccKey).build(); | ||
| StaticCredentialsProvider credentialsProvider = StaticCredentialsProvider.create(awsCreds); | ||
|
|
||
| s3client = S3Client.builder() | ||
| .credentialsProvider(credentialsProvider) | ||
| .httpClient(apacheHttpClient) | ||
| .region(getRegion()) | ||
| .build(); | ||
| } else { | ||
| s3client = S3Client.builder() | ||
| .httpClient(apacheHttpClient) | ||
| .region(getRegion()) | ||
| .build(); | ||
| s3ClientBuilder.credentialsProvider(credentialsProvider); | ||
| } | ||
|
|
||
| S3Client s3client = s3ClientBuilder.build(); | ||
|
|
||
| verifyBucketExist(s3client, bucket); | ||
|
|
||
| LOGGER.debug("success: using bucket: {}", bucket); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.