public ImmutableCredentials Copy() { ImmutableCredentials credentials = new ImmutableCredentials { AccessKey = this.AccessKey, SecretKey = this.SecretKey, Token = this.Token, }; return credentials; }
/// <summary> /// Returns a copy of the most recent instance profile credentials. /// </summary> public override ImmutableCredentials GetCredentials() { CheckIsIMDSEnabled(); ImmutableCredentials credentials = null; // Try to acquire read lock. The thread would be blocked if another thread has write lock. if (credentialsLock.TryEnterReadLock(credentialsLockTimeout)) { try { credentials = lastRetrievedCredentials?.Copy(); if (credentials != null) { return(credentials); } } finally { credentialsLock.ExitReadLock(); } } // If there's no credentials cached, hit IMDS directly. Try to acquire write lock. if (credentialsLock.TryEnterWriteLock(credentialsLockTimeout)) { try { // Check for last retrieved credentials again in case other thread might have already fetched it. credentials = lastRetrievedCredentials?.Copy(); if (credentials == null) { credentials = FetchCredentials(); lastRetrievedCredentials = credentials; } } finally { credentialsLock.ExitWriteLock(); } } if (credentials == null) { throw new AmazonServiceException(FailedToGetCredentialsMessage); } return(credentials); }