Skip to content

Commit

Permalink
add chained credential provider (#1043)
Browse files Browse the repository at this point in the history
  • Loading branch information
balamurugana authored Aug 25, 2020
1 parent 1b6a8ca commit 093b0df
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
60 changes: 60 additions & 0 deletions api/src/main/java/io/minio/credentials/ChainedProvider.java
Original file line number Diff line number Diff line change
@@ -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<Provider> 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");
}
}
42 changes: 42 additions & 0 deletions examples/MinioClientWithChainedProvider.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 093b0df

Please sign in to comment.