From 093b0df6fe12273ace64a78a546a23784f28e7c1 Mon Sep 17 00:00:00 2001 From: Bala FA Date: Tue, 25 Aug 2020 19:35:04 +0000 Subject: [PATCH] add chained credential provider (#1043) --- .../io/minio/credentials/ChainedProvider.java | 60 +++++++++++++++++++ examples/MinioClientWithChainedProvider.java | 42 +++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 api/src/main/java/io/minio/credentials/ChainedProvider.java create mode 100644 examples/MinioClientWithChainedProvider.java diff --git a/api/src/main/java/io/minio/credentials/ChainedProvider.java b/api/src/main/java/io/minio/credentials/ChainedProvider.java new file mode 100644 index 000000000..12e835a72 --- /dev/null +++ b/api/src/main/java/io/minio/credentials/ChainedProvider.java @@ -0,0 +1,60 @@ +/* + * MinIO Java SDK for Amazon S3 Compatible Cloud Storage, (C) 2020 MinIO, Inc. + * + * Licensed 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 + * + * https://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 io.minio.credentials; + +import java.util.Arrays; +import java.util.List; +import javax.annotation.Nonnull; + +/** Chained credential provider work with list of credential providers. */ +public class ChainedProvider implements Provider { + private final List providers; + private Provider currentProvider; + private Credentials credentials; + + public ChainedProvider(@Nonnull Provider... providers) { + this.providers = Arrays.asList(providers); + } + + @Override + public synchronized Credentials fetch() { + if (credentials != null && !credentials.isExpired()) { + return credentials; + } + + if (currentProvider != null) { + try { + credentials = currentProvider.fetch(); + return credentials; + } catch (IllegalStateException e) { + // Ignore and fallback to iteration. + } + } + + for (Provider provider : providers) { + try { + credentials = provider.fetch(); + currentProvider = provider; + return credentials; + } catch (IllegalStateException e) { + // Ignore and continue to next iteration. + } + } + + throw new IllegalStateException("All providers fail to fetch credentials"); + } +} diff --git a/examples/MinioClientWithChainedProvider.java b/examples/MinioClientWithChainedProvider.java new file mode 100644 index 000000000..84af8c095 --- /dev/null +++ b/examples/MinioClientWithChainedProvider.java @@ -0,0 +1,42 @@ +/* + * MinIO Java SDK for Amazon S3 Compatible Cloud Storage, (C) 2020 MinIO, Inc. + * + * Licensed 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 + * + * https://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. + */ + +import io.minio.MinioClient; +import io.minio.StatObjectArgs; +import io.minio.StatObjectResponse; +import io.minio.credentials.AwsEnvironmentProvider; +import io.minio.credentials.ChainedProvider; +import io.minio.credentials.MinioEnvironmentProvider; +import io.minio.credentials.Provider; + +public class MinioClientWithChainedProvider { + public static void main(String[] args) throws Exception { + Provider provider = + new ChainedProvider(new AwsEnvironmentProvider(), new MinioEnvironmentProvider()); + + MinioClient minioClient = + MinioClient.builder() + .endpoint("https://MINIO-HOST:MINIO-PORT") + .credentialsProvider(provider) + .build(); + + // Get information of an object. + StatObjectResponse stat = + minioClient.statObject( + StatObjectArgs.builder().bucket("my-bucketname").object("my-objectname").build()); + System.out.println(stat); + } +}