Skip to content

Add create() for new AwsCredentials Identity types #3884

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void isAnonymous_AwsCredentialsIdentity_true() {

@Test
public void isAnonymous_AwsCredentialsIdentity_false() {
assertThat(CredentialUtils.isAnonymous((AwsCredentialsIdentity) AwsBasicCredentials.create("akid", "skid"))).isFalse();
assertThat(CredentialUtils.isAnonymous(AwsCredentialsIdentity.create("akid", "skid"))).isFalse();
}

@Test
Expand All @@ -59,22 +59,8 @@ public void toCredentials_AwsSessionCredentials_doesNotCreateNewObject() {

@Test
public void toCredentials_AwsSessionCredentialsIdentity_returnsAwsSessionCredentials() {
AwsCredentials awsCredentials = CredentialUtils.toCredentials(new AwsSessionCredentialsIdentity() {
@Override
public String accessKeyId() {
return "akid";
}

@Override
public String secretAccessKey() {
return "skid";
}

@Override
public String sessionToken() {
return "session";
}
});
AwsCredentials awsCredentials = CredentialUtils.toCredentials(AwsSessionCredentialsIdentity.create(
"akid", "skid", "session"));

assertThat(awsCredentials).isInstanceOf(AwsSessionCredentials.class);
AwsSessionCredentials awsSessionCredentials = (AwsSessionCredentials) awsCredentials;
Expand All @@ -92,17 +78,7 @@ public void toCredentials_AwsCredentials_returnsAsIs() {

@Test
public void toCredentials_AwsCredentialsIdentity_returnsAwsCredentials() {
AwsCredentials awsCredentials = CredentialUtils.toCredentials(new AwsCredentialsIdentity() {
@Override
public String accessKeyId() {
return "akid";
}

@Override
public String secretAccessKey() {
return "skid";
}
});
AwsCredentials awsCredentials = CredentialUtils.toCredentials(AwsCredentialsIdentity.create("akid", "skid"));

assertThat(awsCredentials.accessKeyId()).isEqualTo("akid");
assertThat(awsCredentials.secretAccessKey()).isEqualTo("skid");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@

package software.amazon.awssdk.identity.spi;

import java.util.Objects;
import software.amazon.awssdk.annotations.SdkPublicApi;
import software.amazon.awssdk.annotations.ThreadSafe;
import software.amazon.awssdk.utils.ToString;
import software.amazon.awssdk.utils.Validate;

/**
* Provides access to the AWS credentials used for accessing services: AWS access key ID and secret access key. These
Expand All @@ -25,6 +28,8 @@
* <p>For more details on AWS access keys, see:
* <a href="https://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html#access-keys-and-secret-access-keys">
* https://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html#access-keys-and-secret-access-keys</a></p>
*
* @see AwsSessionCredentialsIdentity
*/
@SdkPublicApi
@ThreadSafe
Expand All @@ -39,4 +44,55 @@ public interface AwsCredentialsIdentity extends Identity {
* Retrieve the AWS secret access key, used to authenticate the user interacting with services.
*/
String secretAccessKey();

/**
* Constructs a new credentials object, with the specified AWS access key and AWS secret key.
*
* @param accessKeyId The AWS access key, used to identify the user interacting with services.
* @param secretAccessKey The AWS secret access key, used to authenticate the user interacting with services.
* */
static AwsCredentialsIdentity create(String accessKeyId, String secretAccessKey) {
Validate.paramNotNull(accessKeyId, "accessKeyId");
Validate.paramNotNull(secretAccessKey, "secretAccessKey");

return new AwsCredentialsIdentity() {
@Override
public String accessKeyId() {
return accessKeyId;
}

@Override
public String secretAccessKey() {
return secretAccessKey;
}

@Override
public String toString() {
return ToString.builder("AwsCredentialsIdentity")
.add("accessKeyId", accessKeyId)
.build();
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
AwsCredentialsIdentity that = (AwsCredentialsIdentity) o;
return Objects.equals(accessKeyId, that.accessKeyId()) &&
Objects.equals(secretAccessKey, that.secretAccessKey());
}

@Override
public int hashCode() {
int hashCode = 1;
hashCode = 31 * hashCode + Objects.hashCode(accessKeyId());
hashCode = 31 * hashCode + Objects.hashCode(secretAccessKey());
return hashCode;
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@

package software.amazon.awssdk.identity.spi;

import java.util.Objects;
import software.amazon.awssdk.annotations.SdkPublicApi;
import software.amazon.awssdk.annotations.ThreadSafe;
import software.amazon.awssdk.utils.ToString;
import software.amazon.awssdk.utils.Validate;

/**
* A special type of {@link AwsCredentialsIdentity} that provides a session token to be used in service authentication. Session
Expand All @@ -32,4 +35,66 @@ public interface AwsSessionCredentialsIdentity extends AwsCredentialsIdentity {
* user has received temporary permission to access some resource.
*/
String sessionToken();

/**
* Constructs a new session credentials object, with the specified AWS access key, AWS secret key and AWS session token.
*
* @param accessKeyId The AWS access key, used to identify the user interacting with services.
* @param secretAccessKey The AWS secret access key, used to authenticate the user interacting with services.
* @param sessionToken The AWS session token, retrieved from an AWS token service, used for authenticating that this user has
* received temporary permission to access some resource.
*/
static AwsSessionCredentialsIdentity create(String accessKeyId, String secretAccessKey, String sessionToken) {
Validate.paramNotNull(accessKeyId, "accessKeyId");
Validate.paramNotNull(secretAccessKey, "secretAccessKey");
Validate.paramNotNull(sessionToken, "sessionToken");

return new AwsSessionCredentialsIdentity() {
@Override
public String accessKeyId() {
return accessKeyId;
}

@Override
public String secretAccessKey() {
return secretAccessKey;
}

@Override
public String sessionToken() {
return sessionToken;
}

@Override
public String toString() {
return ToString.builder("AwsSessionCredentialsIdentity")
.add("accessKeyId", accessKeyId())
.build();
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

AwsSessionCredentialsIdentity that = (AwsSessionCredentialsIdentity) o;
return Objects.equals(accessKeyId, that.accessKeyId()) &&
Objects.equals(secretAccessKey, that.secretAccessKey()) &&
Objects.equals(sessionToken, that.sessionToken());
}

@Override
public int hashCode() {
int hashCode = 1;
hashCode = 31 * hashCode + Objects.hashCode(accessKeyId());
hashCode = 31 * hashCode + Objects.hashCode(secretAccessKey());
hashCode = 31 * hashCode + Objects.hashCode(sessionToken());
return hashCode;
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,7 @@ void crtCredentials_withoutSession_shouldConvert() {
@Test
void crtCredentials_provideAwsCredentials_shouldInvokeResolveAndClose() {
IdentityProvider<? extends AwsCredentialsIdentity> awsCredentialsProvider = Mockito.mock(HttpCredentialsProvider.class);
AwsCredentialsIdentity credentials = new AwsCredentialsIdentity() {
@Override
public String accessKeyId() {
return "foo";
}

@Override
public String secretAccessKey() {
return "bar";
}
};
AwsCredentialsIdentity credentials = AwsCredentialsIdentity.create("foo", "bar");
when(awsCredentialsProvider.resolveIdentity()).thenAnswer(invocation -> CompletableFuture.completedFuture(credentials));

CrtCredentialsProviderAdapter adapter = new CrtCredentialsProviderAdapter(awsCredentialsProvider);
Expand Down