Skip to content
Draft
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 @@ -2,5 +2,5 @@
"type": "feature",
"category": "AWS SDK for Java v2",
"contributor": "",
"description": "Add CredentialDaemonCredentialsProvider, which resolves credentials from a local credential daemon over a Unix domain socket for profiles that set daemon_credential in the shared config file. Compatibility note: the provider requires Java 16 or later on Linux or macOS. On Java 8 to 15 it fails with UNSUPPORTED_PLATFORM, and a profile that runs `aws configure export-credentials` as its credential_process can be used instead."
"description": "Add CredentialDaemonCredentialsProvider, which resolves credentials from a local credential daemon over a Unix domain socket for profiles that set credential_daemon in the shared config file. Compatibility note: the provider requires Java 16 or later on Linux or macOS. On Java 8 to 15 it fails with UNSUPPORTED_PLATFORM, and a profile that runs `aws configure export-credentials` as its credential_process can be used instead."
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

package software.amazon.awssdk.auth.credentials;

import java.nio.file.Path;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
Expand All @@ -38,12 +39,14 @@

/**
* Loads short-lived credentials from a local credential daemon over a Unix domain socket. This is used to support the
* {@code daemon_credential} setting in the shared config file.
* {@code credential_daemon} setting in the shared config file.
*
* <p>
* Each fetch opens a new connection to the daemon and sends one JSON-RPC 2.0 {@code credential/get} request. The socket is
* looked up again on every fetch, in this order:
* <ol>
* <li>The {@link Builder#socketPath(Path)} setting, when it is an absolute path.</li>
* <li>The {@code aws.credentialDaemonSocket} system property, when it holds an absolute path.</li>
* <li>The {@code AWS_CREDENTIAL_DAEMON_SOCKET} environment variable, when it holds an absolute path.</li>
* <li>{@code $HOME/.aws/credential-daemon.sock}, when anything is there: a socket, a symbolic link, or a link whose
* target is missing. {@code HOME} is read from the environment, and this location is skipped when {@code HOME} is
Expand All @@ -64,12 +67,12 @@
*
* <p>
* On such a platform, an application can still use the daemon's credentials through the AWS CLI. Give it a separate profile
* that does not set {@code daemon_credential} and runs the CLI as a credential process, for example:
* that does not set {@code credential_daemon} and runs the CLI as a credential process, for example:
* <pre>
* [profile daemon-via-cli]
* credential_process = aws configure export-credentials --profile my-daemon-profile --format process
* </pre>
* where {@code my-daemon-profile} is the profile that sets {@code daemon_credential}.
* where {@code my-daemon-profile} is the profile that sets {@code credential_daemon}.
*
* <p>
* Failures are raised as {@link CredentialDaemonException}, whose {@link CredentialDaemonException#errorCode()} identifies
Expand All @@ -95,6 +98,7 @@ public final class CredentialDaemonCredentialsProvider
private final String credentialName;
private final String accountId;
private final String roleName;
private final Path socketPath;
private final Duration connectTimeout;
private final Duration callTimeout;
private final Boolean asyncCredentialUpdateEnabled;
Expand All @@ -116,13 +120,14 @@ private CredentialDaemonCredentialsProvider(BuilderImpl builder) {
this.accountId = builder.accountId;
this.roleName = builder.roleName;
Validate.isTrue((accountId == null) == (roleName == null), "accountId and roleName must be set together.");
this.socketPath = builder.socketPath;
this.connectTimeout = Validate.isPositive(builder.connectTimeout, "connectTimeout");
this.callTimeout = Validate.isPositive(builder.callTimeout, "callTimeout");
this.asyncCredentialUpdateEnabled = builder.asyncCredentialUpdateEnabled;
this.sourceChain = builder.sourceChain;
String providerName = StringUtils.isEmpty(sourceChain) ? PROVIDER_NAME : sourceChain + "," + PROVIDER_NAME;
this.request = new CredentialDaemonRequest(credentialName, accountId, roleName);
this.client = CredentialDaemonClient.create(connectTimeout, callTimeout, providerName);
this.client = CredentialDaemonClient.create(socketPath, connectTimeout, callTimeout, providerName);
this.clock = builder.clock;

CachedSupplier.Builder<AwsCredentials> cacheBuilder = CachedSupplier.builder(this::refresh)
Expand Down Expand Up @@ -192,6 +197,17 @@ public interface Builder extends CopyableBuilder<Builder, CredentialDaemonCreden
*/
Builder roleName(String roleName);

/**
* Sets the path of the daemon's socket. When this is an absolute path, it is dialed instead of the
* {@code aws.credentialDaemonSocket} system property, the {@code AWS_CREDENTIAL_DAEMON_SOCKET} environment variable
* and {@code $HOME/.aws/credential-daemon.sock}, and no other location is tried if that dial fails. A relative path
* counts as unset.
*
* <p>
* Default: not set.
*/
Builder socketPath(Path socketPath);

/**
* Sets how long to wait for the daemon to accept the connection.
*
Expand Down Expand Up @@ -241,6 +257,7 @@ static final class BuilderImpl implements Builder {
private String credentialName;
private String accountId;
private String roleName;
private Path socketPath;
private Duration connectTimeout = DEFAULT_CONNECT_TIMEOUT;
private Duration callTimeout = DEFAULT_CALL_TIMEOUT;
private Boolean asyncCredentialUpdateEnabled = false;
Expand All @@ -254,6 +271,7 @@ private BuilderImpl(CredentialDaemonCredentialsProvider provider) {
this.credentialName = provider.credentialName;
this.accountId = provider.accountId;
this.roleName = provider.roleName;
this.socketPath = provider.socketPath;
this.connectTimeout = provider.connectTimeout;
this.callTimeout = provider.callTimeout;
this.asyncCredentialUpdateEnabled = provider.asyncCredentialUpdateEnabled;
Expand Down Expand Up @@ -284,6 +302,12 @@ public Builder roleName(String roleName) {
return this;
}

@Override
public Builder socketPath(Path socketPath) {
this.socketPath = socketPath;
return this;
}

@Override
public Builder connectTimeout(Duration connectTimeout) {
this.connectTimeout = connectTimeout;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ private Optional<AwsCredentialsProvider> credentialsProvider(Set<String> childre
* Internal method that returns both the credentials provider and its feature ID.
*/
private Optional<CredentialsWithFeatureId> credentialsProviderWithFeatureID(Set<String> children) {
if (properties.containsKey(ProfileProperty.DAEMON_CREDENTIAL)) {
if (properties.containsKey(ProfileProperty.CREDENTIAL_DAEMON)) {
Optional<CredentialsWithFeatureId> daemonProvider = daemonProfileCredentialsProvider();
if (daemonProvider.isPresent()) {
return daemonProvider;
Expand Down Expand Up @@ -176,8 +176,8 @@ private Optional<CredentialsWithFeatureId> credentialsProviderWithFeatureID(Set<
}

/**
* Load the credential daemon provider configured with {@code daemon_credential} in this profile. This is empty only for an
* SSO profile that sets neither sso_account_id nor sso_role_name, which is then resolved as if daemon_credential were
* Load the credential daemon provider configured with {@code credential_daemon} in this profile. This is empty only for an
* SSO profile that sets neither sso_account_id nor sso_role_name, which is then resolved as if credential_daemon were
* absent.
*/
private Optional<CredentialsWithFeatureId> daemonProfileCredentialsProvider() {
Expand All @@ -188,27 +188,27 @@ private Optional<CredentialsWithFeatureId> daemonProfileCredentialsProvider() {
return Optional.empty();
}

String credentialName = properties.get(ProfileProperty.DAEMON_CREDENTIAL);
String credentialName = properties.get(ProfileProperty.CREDENTIAL_DAEMON);
if (!CredentialDaemonUtils.isValidCredentialName(credentialName)) {
throw CredentialDaemonUtils.error(
CredentialDaemonException.INVALID_CONFIG,
"Profile '" + name + "' sets an invalid " + ProfileProperty.DAEMON_CREDENTIAL + ". A credential name is 1 to 64 "
"Profile '" + name + "' sets an invalid " + ProfileProperty.CREDENTIAL_DAEMON + ". A credential name is 1 to 64 "
+ "characters from A-Z, a-z, 0-9, '.', '_' and '-', and does not start with '.'.",
null);
}

if (properties.containsKey(ProfileProperty.WEB_IDENTITY_TOKEN_FILE)) {
throw CredentialDaemonUtils.error(
CredentialDaemonException.INVALID_CONFIG,
"Profile '" + name + "' has two credential sources, " + ProfileProperty.DAEMON_CREDENTIAL + " and "
"Profile '" + name + "' has two credential sources, " + ProfileProperty.CREDENTIAL_DAEMON + " and "
+ ProfileProperty.WEB_IDENTITY_TOKEN_FILE + ". Remove one of them.",
null);
}

if (properties.containsKey(ProfileProperty.ROLE_ARN)) {
throw CredentialDaemonUtils.error(
CredentialDaemonException.INVALID_CONFIG,
"Profile '" + name + "' sets both " + ProfileProperty.DAEMON_CREDENTIAL + " and " + ProfileProperty.ROLE_ARN
"Profile '" + name + "' sets both " + ProfileProperty.CREDENTIAL_DAEMON + " and " + ProfileProperty.ROLE_ARN
+ ". Move " + ProfileProperty.ROLE_ARN + " into its own profile that sets " + ProfileProperty.SOURCE_PROFILE
+ " = " + name + ".",
null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import software.amazon.awssdk.protocols.jsoncore.JsonNode;
import software.amazon.awssdk.protocols.jsoncore.JsonNodeParser;
import software.amazon.awssdk.utils.IoUtils;
import software.amazon.awssdk.utils.SystemSetting;
import software.amazon.awssdk.utils.ThreadFactoryBuilder;
import software.amazon.awssdk.utils.internal.SystemSettingUtils;

Expand All @@ -69,13 +70,16 @@
@SdkInternalApi
public final class CredentialDaemonClient {
static final int MAX_LINE = 64_000;
static final String SOCKET_SYSTEM_PROPERTY = "aws.credentialDaemonSocket";
static final String SOCKET_ENVIRONMENT_VARIABLE = "AWS_CREDENTIAL_DAEMON_SOCKET";
static final String LINK_LOCATION = "$HOME/.aws/credential-daemon.sock";

private static final String SESSION_CREDENTIALS = "session_credentials";
private static final JsonNodeParser PARSER = JsonNodeParser.builder().removeErrorLocations(true).build();

private final CredentialDaemonPlatform platform;
private final Path socketPath;
private final Function<String, Optional<String>> systemProperties;
private final Function<String, Optional<String>> environment;
private final Duration connectTimeout;
private final Duration callTimeout;
Expand All @@ -89,7 +93,21 @@ public final class CredentialDaemonClient {
Duration callTimeout,
ChannelOpener channelOpener,
String providerName) {
this(platform, null, k -> Optional.empty(), environment, connectTimeout, callTimeout, channelOpener, providerName);
}

@SdkTestInternalApi
CredentialDaemonClient(CredentialDaemonPlatform platform,
Path socketPath,
Function<String, Optional<String>> systemProperties,
Function<String, Optional<String>> environment,
Duration connectTimeout,
Duration callTimeout,
ChannelOpener channelOpener,
String providerName) {
this.platform = platform;
this.socketPath = socketPath;
this.systemProperties = systemProperties;
this.environment = environment;
this.connectTimeout = connectTimeout;
this.callTimeout = callTimeout;
Expand All @@ -98,11 +116,14 @@ public final class CredentialDaemonClient {
}

/**
* Creates a client for this platform. The provider name is put on the credentials the daemon returns.
* Creates a client for this platform. The socket path, if not null, is the first location looked up. The provider name is
* put on the credentials the daemon returns.
*/
public static CredentialDaemonClient create(Duration connectTimeout, Duration callTimeout, String providerName) {
public static CredentialDaemonClient create(Path socketPath, Duration connectTimeout, Duration callTimeout,
String providerName) {
CredentialDaemonPlatform platform = CredentialDaemonPlatform.current();
return new CredentialDaemonClient(platform, SystemSettingUtils::resolveEnvironmentVariable,
return new CredentialDaemonClient(platform, socketPath, CredentialDaemonClient::systemProperty,
SystemSettingUtils::resolveEnvironmentVariable,
connectTimeout, callTimeout, platform::openChannel, providerName);
}

Expand All @@ -121,13 +142,21 @@ public CredentialDaemonResponse fetch(CredentialDaemonRequest request) {
}

/**
* Returns the one socket path the client dials: {@code AWS_CREDENTIAL_DAEMON_SOCKET} when it holds an absolute path,
* and otherwise {@code $HOME/.aws/credential-daemon.sock} when anything is there.
* Returns the one socket path the client dials, from the first location that applies: the socket path given in code, the
* {@code aws.credentialDaemonSocket} system property and {@code AWS_CREDENTIAL_DAEMON_SOCKET}, each when it holds an
* absolute path, and otherwise {@code $HOME/.aws/credential-daemon.sock} when anything is there.
*
* @throws CredentialDaemonException with {@link CredentialDaemonException#NOT_RUNNING} when no location applies, or when
* the link at {@code $HOME/.aws/credential-daemon.sock} points to a target that is missing.
*/
Path locate() {
if (socketPath != null && socketPath.isAbsolute()) {
return socketPath;
}
Path fromSystemProperty = absolutePath(systemProperties.apply(SOCKET_SYSTEM_PROPERTY).orElse(null));
if (fromSystemProperty != null) {
return fromSystemProperty;
}
Path fromEnvironment = absolutePath(env(SOCKET_ENVIRONMENT_VARIABLE));
if (fromEnvironment != null) {
return fromEnvironment;
Expand Down Expand Up @@ -421,6 +450,30 @@ private String env(String name) {
return environment.apply(name).orElse(null);
}

/**
* Reads a system property on its own, without its environment variable, so that an empty or relative value moves on to
* the environment variable instead of hiding it.
*/
private static Optional<String> systemProperty(String name) {
SystemSetting property = new SystemSetting() {
@Override
public String property() {
return name;
}

@Override
public String environmentVariable() {
return null;
}

@Override
public String defaultValue() {
return null;
}
};
return property.getNonDefaultStringValue();
}

private static Path absolutePath(String value) {
if (value == null || value.isEmpty()) {
return null;
Expand Down Expand Up @@ -454,8 +507,9 @@ private static CredentialDaemonException connectFailure(Path path, Exception e)

private static CredentialDaemonException noLocation(String reason) {
return CredentialDaemonUtils.error(CredentialDaemonException.NOT_RUNNING,
"No credential daemon socket was found: " + SOCKET_ENVIRONMENT_VARIABLE
+ " is not set to an absolute path, and " + reason + ". Install the credential "
"No credential daemon socket was found: neither the "
+ SOCKET_SYSTEM_PROPERTY + " system property nor " + SOCKET_ENVIRONMENT_VARIABLE
+ " is set to an absolute path, and " + reason + ". Install the credential "
+ "daemon or link its socket to " + LINK_LOCATION + ", or set "
+ SOCKET_ENVIRONMENT_VARIABLE + ". See " + CredentialDaemonUtils.CREDENTIAL_DAEMON_DOC
+ ".",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
@SdkInternalApi
public final class CredentialDaemonUtils {

static final String CREDENTIAL_DAEMON_DOC = "https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-credential-daemon.html";
static final String CREDENTIAL_DAEMON_DOC = "https://docs.aws.amazon.com/cli/latest/userguide/cli-authentication-credsd.html";

static final String START_HINT = "Start the credential daemon and sign in; see " + CREDENTIAL_DAEMON_DOC + ".";

Expand Down Expand Up @@ -99,7 +99,7 @@ public static boolean daemonHoldsSsoSession(Profile profile) {
return false;
}
Map<String, String> properties = profile.properties();
return properties.containsKey(ProfileProperty.DAEMON_CREDENTIAL)
return properties.containsKey(ProfileProperty.CREDENTIAL_DAEMON)
&& properties.containsKey(ProfileProperty.SSO_ACCOUNT_ID)
&& properties.containsKey(ProfileProperty.SSO_ROLE_NAME);
}
Expand All @@ -110,7 +110,7 @@ public static boolean daemonHoldsSsoSession(Profile profile) {
*/
public static String bearerTokenUnavailableMessage(String profileName) {
return "Profile " + profileName + " does not have a usable sso_session: its SSO session is held in the credential "
+ "daemon (daemon_credential). Services that use bearer tokens need an SSO session stored in files; configure a "
+ "daemon (credential_daemon). Services that use bearer tokens need an SSO session stored in files; configure a "
+ "profile with sso_session and sign in with a file-based SSO login.";
}

Expand Down
Loading