Skip to content

Refactors credential identity with separate implementing classes #4024

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
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 @@ -15,11 +15,9 @@

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;
import software.amazon.awssdk.identity.spi.internal.DefaultAwsCredentialsIdentity;

/**
* Provides access to the AWS credentials used for accessing services: AWS access key ID and secret access key. These
Expand All @@ -45,54 +43,33 @@ public interface AwsCredentialsIdentity extends Identity {
*/
String secretAccessKey();

static Builder builder() {
return DefaultAwsCredentialsIdentity.builder();
}

/**
* 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;
}
return builder().accessKeyId(accessKeyId)
.secretAccessKey(secretAccessKey)
.build();
}

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extends CopyableBuilder?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't think it was necessarily needed, but we can add it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I played around with different versions of CopyableBuilder, but it doesn't work for the subtypes. If AwsCredentialsIdentity implements ToCopyableBuilder the returned builder will be of type AwsCredentialsIdentity.Builder for AwsSessionCredentialsIdentity too and you'll lose the specific information in the subtype.

/**
* The AWS access key, used to identify the user interacting with services.
*/
Builder accessKeyId(String accessKeyId);

@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());
}
/**
* The AWS secret access key, used to authenticate the user interacting with services.
*/
Builder secretAccessKey(String secretAccessKey);

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

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;
import software.amazon.awssdk.identity.spi.internal.DefaultAwsSessionCredentialsIdentity;

/**
* A special type of {@link AwsCredentialsIdentity} that provides a session token to be used in service authentication. Session
Expand All @@ -36,6 +34,10 @@ public interface AwsSessionCredentialsIdentity extends AwsCredentialsIdentity {
*/
String sessionToken();

static AwsSessionCredentialsIdentity.Builder builder() {
return DefaultAwsSessionCredentialsIdentity.builder();
}

/**
* Constructs a new session credentials object, with the specified AWS access key, AWS secret key and AWS session token.
*
Expand All @@ -45,56 +47,26 @@ public interface AwsSessionCredentialsIdentity extends AwsCredentialsIdentity {
* 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;
}
return builder().accessKeyId(accessKeyId)
.secretAccessKey(secretAccessKey)
.sessionToken(sessionToken)
.build();
}

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

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

AwsSessionCredentialsIdentity that = (AwsSessionCredentialsIdentity) o;
return Objects.equals(accessKeyId, that.accessKeyId()) &&
Objects.equals(secretAccessKey, that.secretAccessKey()) &&
Objects.equals(sessionToken, that.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.
*/
Builder sessionToken(String 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;
}
};
@Override
AwsSessionCredentialsIdentity build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file 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 software.amazon.awssdk.identity.spi.internal;

import java.util.Objects;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.identity.spi.AwsCredentialsIdentity;
import software.amazon.awssdk.utils.ToString;
import software.amazon.awssdk.utils.Validate;

@SdkInternalApi
public final class DefaultAwsCredentialsIdentity implements AwsCredentialsIdentity {

private final String accessKeyId;
private final String secretAccessKey;

private DefaultAwsCredentialsIdentity(Builder builder) {
this.accessKeyId = builder.accessKeyId;
this.secretAccessKey = builder.secretAccessKey;

Validate.paramNotNull(accessKeyId, "accessKeyId");
Validate.paramNotNull(secretAccessKey, "secretAccessKey");
}

public static AwsCredentialsIdentity.Builder builder() {
return new Builder();
}

@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;
}

private static final class Builder implements AwsCredentialsIdentity.Builder {
private String accessKeyId;
private String secretAccessKey;

private Builder() {
}

@Override
public Builder accessKeyId(String accessKeyId) {
this.accessKeyId = accessKeyId;
return this;
}

@Override
public Builder secretAccessKey(String secretAccessKey) {
this.secretAccessKey = secretAccessKey;
return this;
}

@Override
public AwsCredentialsIdentity build() {
return new DefaultAwsCredentialsIdentity(this);
}
}
}
Loading