1 /** 2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 * SPDX-License-Identifier: Apache-2.0. 4 */ 5 package software.amazon.awssdk.crt.auth.credentials; 6 7 import java.util.concurrent.CompletableFuture; 8 import software.amazon.awssdk.crt.CrtResource; 9 import software.amazon.awssdk.crt.Log; 10 11 /** 12 * A base class that represents a source of AWS credentials 13 */ 14 public class CredentialsProvider extends CrtResource { 15 16 private final CompletableFuture<Void> shutdownComplete = new CompletableFuture<>(); 17 18 /** 19 * Default constructor 20 */ CredentialsProvider()21 protected CredentialsProvider() {} 22 23 /** 24 * Request credentials from the provider 25 * @return A Future for Credentials that will be completed when they are acquired. 26 */ getCredentials()27 public CompletableFuture<Credentials> getCredentials() { 28 CompletableFuture<Credentials> future = new CompletableFuture<>(); 29 try { 30 credentialsProviderGetCredentials(this, future, getNativeHandle()); 31 } catch (Exception e) { 32 future.completeExceptionally(e); 33 } 34 35 return future; 36 } 37 38 /** 39 * Completion callback for credentials fetching. Alternatively the future could have been completed 40 * at the JNI layer. 41 * @param future the future that the credentials should be applied to 42 * @param credentials the fetched credentials, if successful 43 */ onGetCredentialsComplete(CompletableFuture<Credentials> future, Credentials credentials)44 private void onGetCredentialsComplete(CompletableFuture<Credentials> future, Credentials credentials) { 45 if (credentials != null) { 46 future.complete(credentials); 47 } else { 48 future.completeExceptionally(new RuntimeException("Failed to get a valid set of credentials")); 49 } 50 } 51 52 /** 53 * Begins the release process of the provider's native handle 54 */ 55 @Override releaseNativeHandle()56 protected void releaseNativeHandle() { 57 if (!isNull()) { 58 credentialsProviderDestroy(this, getNativeHandle()); 59 } 60 } 61 62 /** 63 * Determines whether a resource releases its dependencies at the same time the native handle is released or if it waits. 64 * Resources that wait are responsible for calling releaseReferences() manually. 65 */ 66 @Override canReleaseReferencesImmediately()67 protected boolean canReleaseReferencesImmediately() { return false; } 68 69 /** 70 * Called from Native when the asynchronous shutdown process needed for credentials providers has completed. 71 */ onShutdownComplete()72 private void onShutdownComplete() { 73 Log.log(Log.LogLevel.Trace, Log.LogSubject.AuthCredentialsProvider, "CrtCredentialsProvider.onShutdownComplete"); 74 75 releaseReferences(); 76 77 this.shutdownComplete.complete(null); 78 } 79 80 /** 81 * Learn when this object has finished shutting down. 82 * @return future that completes when all of this object's native resources have shut down or released 83 * properly. 84 */ getShutdownCompleteFuture()85 public CompletableFuture<Void> getShutdownCompleteFuture() { return shutdownComplete; } 86 87 /******************************************************************************* 88 * native methods 89 ******************************************************************************/ credentialsProviderDestroy(CredentialsProvider thisObj, long nativeHandle)90 private static native void credentialsProviderDestroy(CredentialsProvider thisObj, long nativeHandle); credentialsProviderGetCredentials(CredentialsProvider thisObj, CompletableFuture<Credentials> future, long nativeHandle)91 private static native void credentialsProviderGetCredentials(CredentialsProvider thisObj, CompletableFuture<Credentials> future, long nativeHandle); 92 } 93