Skip to content

Commit 593ec00

Browse files
committed
Adds business metrics for credential providers
1 parent d8d52ae commit 593ec00

File tree

41 files changed

+351
-151
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+351
-151
lines changed

core/auth/src/main/java/software/amazon/awssdk/auth/credentials/ChildProfileCredentialsProviderFactory.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ public interface ChildProfileCredentialsProviderFactory {
3838
* provider. This credentials provider should be closed when it is no longer used.
3939
* @param profile The profile that should be used to load the configuration necessary to create the child credentials
4040
* provider.
41+
* @param source A string list of {@link software.amazon.awssdk.core.useragent.BusinessMetricFeatureId} denoting
42+
* previous credentials providers that are chained with this one.
4143
* @return The credentials provider with permissions derived from the source credentials provider and profile.
4244
*/
43-
AwsCredentialsProvider create(AwsCredentialsProvider sourceCredentialsProvider, Profile profile);
44-
}
45+
AwsCredentialsProvider create(AwsCredentialsProvider sourceCredentialsProvider, Profile profile, String source);
46+
}

core/auth/src/main/java/software/amazon/awssdk/auth/credentials/ContainerCredentialsProvider.java

+26-2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import software.amazon.awssdk.auth.credentials.internal.HttpCredentialsLoader.LoadedCredentials;
4040
import software.amazon.awssdk.core.SdkSystemSetting;
4141
import software.amazon.awssdk.core.exception.SdkClientException;
42+
import software.amazon.awssdk.core.useragent.BusinessMetricFeatureId;
4243
import software.amazon.awssdk.core.util.SdkUserAgent;
4344
import software.amazon.awssdk.regions.util.ResourcesEndpointProvider;
4445
import software.amazon.awssdk.regions.util.ResourcesEndpointRetryPolicy;
@@ -72,7 +73,7 @@
7273
public final class ContainerCredentialsProvider
7374
implements HttpCredentialsProvider,
7475
ToCopyableBuilder<ContainerCredentialsProvider.Builder, ContainerCredentialsProvider> {
75-
private static final String PROVIDER_NAME = "ContainerCredentialsProvider";
76+
private static final String PROVIDER_NAME = BusinessMetricFeatureId.CREDENTIALS_HTTP.value();
7677
private static final Predicate<InetAddress> IS_LOOPBACK_ADDRESS = InetAddress::isLoopbackAddress;
7778
private static final Predicate<InetAddress> ALLOWED_HOSTS_RULES = IS_LOOPBACK_ADDRESS;
7879
private static final String HTTPS = "https";
@@ -90,6 +91,7 @@ public final class ContainerCredentialsProvider
9091
private final Boolean asyncCredentialUpdateEnabled;
9192

9293
private final String asyncThreadName;
94+
private final String source;
9395

9496
/**
9597
* @see #builder()
@@ -98,7 +100,8 @@ private ContainerCredentialsProvider(BuilderImpl builder) {
98100
this.endpoint = builder.endpoint;
99101
this.asyncCredentialUpdateEnabled = builder.asyncCredentialUpdateEnabled;
100102
this.asyncThreadName = builder.asyncThreadName;
101-
this.httpCredentialsLoader = HttpCredentialsLoader.create(PROVIDER_NAME);
103+
this.source = builder.source;
104+
this.httpCredentialsLoader = HttpCredentialsLoader.create(providerName());
102105

103106
if (Boolean.TRUE.equals(builder.asyncCredentialUpdateEnabled)) {
104107
Validate.paramNotBlank(builder.asyncThreadName, "asyncThreadName");
@@ -160,6 +163,14 @@ private Instant prefetchTime(Instant expiration) {
160163
return ComparableUtils.minimum(oneHourFromNow, fifteenMinutesBeforeExpiration);
161164
}
162165

166+
private String providerName() {
167+
String providerName = PROVIDER_NAME;
168+
if (source != null && !source.isEmpty()) {
169+
providerName = String.format("%s,%s", source, providerName);
170+
}
171+
return providerName;
172+
}
173+
163174
@Override
164175
public AwsCredentials resolveCredentials() {
165176
return credentialsCache.get();
@@ -318,6 +329,7 @@ private static final class BuilderImpl implements Builder {
318329
private String endpoint;
319330
private Boolean asyncCredentialUpdateEnabled;
320331
private String asyncThreadName;
332+
private String source;
321333

322334
private BuilderImpl() {
323335
asyncThreadName("container-credentials-provider");
@@ -327,6 +339,7 @@ private BuilderImpl(ContainerCredentialsProvider credentialsProvider) {
327339
this.endpoint = credentialsProvider.endpoint;
328340
this.asyncCredentialUpdateEnabled = credentialsProvider.asyncCredentialUpdateEnabled;
329341
this.asyncThreadName = credentialsProvider.asyncThreadName;
342+
this.source = credentialsProvider.source;
330343
}
331344

332345
@Override
@@ -359,6 +372,17 @@ public void setAsyncThreadName(String asyncThreadName) {
359372
asyncThreadName(asyncThreadName);
360373
}
361374

375+
@Override
376+
public Builder source(String source) {
377+
this.source = source;
378+
return this;
379+
}
380+
381+
public void setSource(String source) {
382+
source(source);
383+
}
384+
385+
362386
@Override
363387
public ContainerCredentialsProvider build() {
364388
return new ContainerCredentialsProvider(this);

core/auth/src/main/java/software/amazon/awssdk/auth/credentials/EnvironmentVariableCredentialsProvider.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.Optional;
1919
import software.amazon.awssdk.annotations.SdkPublicApi;
2020
import software.amazon.awssdk.auth.credentials.internal.SystemSettingsCredentialsProvider;
21+
import software.amazon.awssdk.core.useragent.BusinessMetricFeatureId;
2122
import software.amazon.awssdk.utils.SystemSetting;
2223
import software.amazon.awssdk.utils.ToString;
2324

@@ -28,7 +29,7 @@
2829
@SdkPublicApi
2930
public final class EnvironmentVariableCredentialsProvider extends SystemSettingsCredentialsProvider {
3031

31-
private static final String PROVIDER_NAME = "EnvironmentVariableCredentialsProvider";
32+
private static final String PROVIDER_NAME = BusinessMetricFeatureId.CREDENTIALS_ENV_VARS.value();
3233

3334
private EnvironmentVariableCredentialsProvider() {
3435
}

core/auth/src/main/java/software/amazon/awssdk/auth/credentials/HttpCredentialsProvider.java

+9
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package software.amazon.awssdk.auth.credentials;
1717

1818
import software.amazon.awssdk.annotations.SdkPublicApi;
19+
import software.amazon.awssdk.core.useragent.BusinessMetricFeatureId;
1920
import software.amazon.awssdk.utils.SdkAutoCloseable;
2021

2122
/**
@@ -48,6 +49,14 @@ interface Builder<TypeToBuildT extends HttpCredentialsProvider, BuilderT extends
4849
*/
4950
BuilderT endpoint(String endpoint);
5051

52+
/**
53+
* An optional string list of {@link BusinessMetricFeatureId} denoting previous credentials providers
54+
* that are chained with this one.
55+
*/
56+
default BuilderT source(String source) {
57+
throw new UnsupportedOperationException();
58+
}
59+
5160
/**
5261
* Build the credentials provider based on the configuration on this builder.
5362
*/

core/auth/src/main/java/software/amazon/awssdk/auth/credentials/InstanceProfileCredentialsProvider.java

+25-2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import software.amazon.awssdk.core.SdkSystemSetting;
3939
import software.amazon.awssdk.core.exception.SdkClientException;
4040
import software.amazon.awssdk.core.exception.SdkServiceException;
41+
import software.amazon.awssdk.core.useragent.BusinessMetricFeatureId;
4142
import software.amazon.awssdk.profiles.ProfileFile;
4243
import software.amazon.awssdk.profiles.ProfileFileSupplier;
4344
import software.amazon.awssdk.profiles.ProfileFileSystemSetting;
@@ -68,7 +69,7 @@ public final class InstanceProfileCredentialsProvider
6869
implements HttpCredentialsProvider,
6970
ToCopyableBuilder<InstanceProfileCredentialsProvider.Builder, InstanceProfileCredentialsProvider> {
7071
private static final Logger log = Logger.loggerFor(InstanceProfileCredentialsProvider.class);
71-
private static final String PROVIDER_NAME = "InstanceProfileCredentialsProvider";
72+
private static final String PROVIDER_NAME = BusinessMetricFeatureId.CREDENTIALS_IMDS.value();
7273
private static final String EC2_METADATA_TOKEN_HEADER = "x-aws-ec2-metadata-token";
7374
private static final String SECURITY_CREDENTIALS_RESOURCE = "/latest/meta-data/iam/security-credentials/";
7475
private static final String TOKEN_RESOURCE = "/latest/api/token";
@@ -89,6 +90,7 @@ public final class InstanceProfileCredentialsProvider
8990
private final Supplier<ProfileFile> profileFile;
9091

9192
private final String profileName;
93+
private final String source;
9294

9395
/**
9496
* @see #builder()
@@ -102,8 +104,9 @@ private InstanceProfileCredentialsProvider(BuilderImpl builder) {
102104
.orElseGet(() -> ProfileFileSupplier.fixedProfileFile(ProfileFile.defaultProfileFile()));
103105
this.profileName = Optional.ofNullable(builder.profileName)
104106
.orElseGet(ProfileFileSystemSetting.AWS_PROFILE::getStringValueOrThrow);
107+
this.source = builder.source;
105108

106-
this.httpCredentialsLoader = HttpCredentialsLoader.create(PROVIDER_NAME);
109+
this.httpCredentialsLoader = HttpCredentialsLoader.create(providerName());
107110
this.configProvider =
108111
Ec2MetadataConfigProvider.builder()
109112
.profileFile(profileFile)
@@ -196,6 +199,14 @@ private Instant prefetchTime(Instant expiration) {
196199
return now.plus(maximum(timeUntilExpiration.dividedBy(2), Duration.ofMinutes(5)));
197200
}
198201

202+
private String providerName() {
203+
String providerName = PROVIDER_NAME;
204+
if (source != null && !source.isEmpty()) {
205+
providerName = String.format("%s,%s", source, providerName);
206+
}
207+
return providerName;
208+
}
209+
199210
@Override
200211
public void close() {
201212
credentialsCache.close();
@@ -346,6 +357,7 @@ static final class BuilderImpl implements Builder {
346357
private String asyncThreadName;
347358
private Supplier<ProfileFile> profileFile;
348359
private String profileName;
360+
private String source;
349361

350362
private BuilderImpl() {
351363
asyncThreadName("instance-profile-credentials-provider");
@@ -358,6 +370,7 @@ private BuilderImpl(InstanceProfileCredentialsProvider provider) {
358370
this.asyncThreadName = provider.asyncThreadName;
359371
this.profileFile = provider.profileFile;
360372
this.profileName = provider.profileName;
373+
this.source = provider.source;
361374
}
362375

363376
Builder clock(Clock clock) {
@@ -426,6 +439,16 @@ public void setProfileName(String profileName) {
426439
profileName(profileName);
427440
}
428441

442+
@Override
443+
public Builder source(String source) {
444+
this.source = source;
445+
return this;
446+
}
447+
448+
public void setSource(String source) {
449+
source(source);
450+
}
451+
429452
@Override
430453
public InstanceProfileCredentialsProvider build() {
431454
return new InstanceProfileCredentialsProvider(this);

core/auth/src/main/java/software/amazon/awssdk/auth/credentials/ProcessCredentialsProvider.java

+26-3
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@
2525
import java.util.Collections;
2626
import java.util.List;
2727
import software.amazon.awssdk.annotations.SdkPublicApi;
28+
import software.amazon.awssdk.core.useragent.BusinessMetricFeatureId;
2829
import software.amazon.awssdk.protocols.jsoncore.JsonNode;
2930
import software.amazon.awssdk.protocols.jsoncore.JsonNodeParser;
3031
import software.amazon.awssdk.utils.DateUtils;
3132
import software.amazon.awssdk.utils.IoUtils;
3233
import software.amazon.awssdk.utils.Platform;
3334
import software.amazon.awssdk.utils.SdkAutoCloseable;
35+
import software.amazon.awssdk.utils.StringUtils;
3436
import software.amazon.awssdk.utils.ToString;
3537
import software.amazon.awssdk.utils.Validate;
3638
import software.amazon.awssdk.utils.builder.CopyableBuilder;
@@ -64,7 +66,7 @@ public final class ProcessCredentialsProvider
6466
implements AwsCredentialsProvider,
6567
SdkAutoCloseable,
6668
ToCopyableBuilder<ProcessCredentialsProvider.Builder, ProcessCredentialsProvider> {
67-
private static final String PROVIDER_NAME = "ProcessCredentialsProvider";
69+
private static final String PROVIDER_NAME = BusinessMetricFeatureId.CREDENTIALS_PROCESS.value();
6870
private static final JsonNodeParser PARSER = JsonNodeParser.builder()
6971
.removeErrorLocations(true)
7072
.build();
@@ -73,6 +75,7 @@ public final class ProcessCredentialsProvider
7375
private final Duration credentialRefreshThreshold;
7476
private final long processOutputLimit;
7577
private final String staticAccountId;
78+
private final String source;
7679

7780
private final CachedSupplier<AwsCredentials> processCredentialCache;
7881

@@ -93,6 +96,7 @@ private ProcessCredentialsProvider(Builder builder) {
9396
this.commandAsListOfStringsFromBuilder = builder.commandAsListOfStrings;
9497
this.asyncCredentialUpdateEnabled = builder.asyncCredentialUpdateEnabled;
9598
this.staticAccountId = builder.staticAccountId;
99+
this.source = builder.source;
96100

97101
CachedSupplier.Builder<AwsCredentials> cacheBuilder = CachedSupplier.builder(this::refreshCredentials)
98102
.cachedValueName(toString());
@@ -192,13 +196,13 @@ private AwsCredentials credentials(JsonNode credentialsJson) {
192196
.sessionToken(sessionToken)
193197
.expirationTime(credentialExpirationTime(credentialsJson))
194198
.accountId(resolvedAccountId)
195-
.providerName(PROVIDER_NAME)
199+
.providerName(providerName())
196200
.build() :
197201
AwsBasicCredentials.builder()
198202
.accessKeyId(accessKeyId)
199203
.secretAccessKey(secretAccessKey)
200204
.accountId(resolvedAccountId)
201-
.providerName(PROVIDER_NAME)
205+
.providerName(providerName())
202206
.build();
203207
}
204208

@@ -250,6 +254,14 @@ private String executeCommand() throws IOException, InterruptedException {
250254
}
251255
}
252256

257+
private String providerName() {
258+
String providerName = PROVIDER_NAME;
259+
if (!StringUtils.isEmpty(this.source)) {
260+
providerName = String.format("%s,%s", this.source, providerName);
261+
}
262+
return providerName;
263+
}
264+
253265
@Override
254266
public void close() {
255267
processCredentialCache.close();
@@ -270,6 +282,7 @@ public static class Builder implements CopyableBuilder<Builder, ProcessCredentia
270282
private Duration credentialRefreshThreshold = Duration.ofSeconds(15);
271283
private long processOutputLimit = 64000;
272284
private String staticAccountId;
285+
private String source;
273286

274287
/**
275288
* @see #builder()
@@ -284,6 +297,7 @@ private Builder(ProcessCredentialsProvider provider) {
284297
this.credentialRefreshThreshold = provider.credentialRefreshThreshold;
285298
this.processOutputLimit = provider.processOutputLimit;
286299
this.staticAccountId = provider.staticAccountId;
300+
this.source = provider.source;
287301
}
288302

289303
/**
@@ -357,6 +371,15 @@ public Builder staticAccountId(String staticAccountId) {
357371
return this;
358372
}
359373

374+
/**
375+
* An optional string list of {@link BusinessMetricFeatureId} denoting previous credentials providers
376+
* that are chained with this one.
377+
*/
378+
public Builder source(String source) {
379+
this.source = source;
380+
return this;
381+
}
382+
360383
public ProcessCredentialsProvider build() {
361384
return new ProcessCredentialsProvider(this);
362385
}

core/auth/src/main/java/software/amazon/awssdk/auth/credentials/ProfileProviderCredentialsContext.java

+26-4
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@ public final class ProfileProviderCredentialsContext {
2929

3030
private final Profile profile;
3131
private final ProfileFile profileFile;
32+
private final String source;
3233

33-
private ProfileProviderCredentialsContext(Profile profile, ProfileFile profileFile) {
34-
this.profile = profile;
35-
this.profileFile = profileFile;
34+
private ProfileProviderCredentialsContext(Builder builder) {
35+
this.profile = builder.profile;
36+
this.profileFile = builder.profileFile;
37+
this.source = builder.source;
3638
}
3739

3840
public static Builder builder() {
@@ -55,6 +57,14 @@ public ProfileFile profileFile() {
5557
return profileFile;
5658
}
5759

60+
/**
61+
* An optional string list of {@link software.amazon.awssdk.core.useragent.BusinessMetricFeatureId} denoting previous
62+
* credentials providers that are chained with this one.
63+
*/
64+
public String source() {
65+
return source;
66+
}
67+
5868
@Override
5969
public boolean equals(Object o) {
6070
if (this == o) {
@@ -78,6 +88,7 @@ public int hashCode() {
7888
public static final class Builder {
7989
private Profile profile;
8090
private ProfileFile profileFile;
91+
private String source;
8192

8293
private Builder() {
8394
}
@@ -103,8 +114,19 @@ public Builder profileFile(ProfileFile profileFile) {
103114
return this;
104115
}
105116

117+
/**
118+
* Builder interface to set source.
119+
* @param source An optional string list of {@link software.amazon.awssdk.core.useragent.BusinessMetricFeatureId}
120+
* denoting previous credentials providers that are chained with this one.
121+
* @return Returns a reference to this object so that method calls can be chained together.
122+
*/
123+
public Builder source(String source) {
124+
this.source = source;
125+
return this;
126+
}
127+
106128
public ProfileProviderCredentialsContext build() {
107-
return new ProfileProviderCredentialsContext(profile, profileFile);
129+
return new ProfileProviderCredentialsContext(this);
108130
}
109131
}
110132
}

0 commit comments

Comments
 (0)