-
Notifications
You must be signed in to change notification settings - Fork 484
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add chained credential provider (#1043)
- Loading branch information
1 parent
1b6a8ca
commit 093b0df
Showing
2 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
api/src/main/java/io/minio/credentials/ChainedProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |