Repository navigation
Tag DynamoDB and S3 client spans with the owning AWS account #12602
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
Open
pedramsafaei
wants to merge
5
commits into
DataDog:master
Choose a base branch
from
pedramsafaei:pedramsafaei/aws-account-identity-tags
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+775
−3
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9e8bb14
Tag DynamoDB and S3 client spans with the owning AWS account
pedramsafaei f4f8f85
Document AWS account resource-owner semantics
pedramsafaei 02fa7c2
Merge branch 'master' into pedramsafaei/aws-account-identity-tags
pedramsafaei cbc13d0
Declare aws_account once on aws.client
pedramsafaei 939324b
Test the tag-registry rejects a tag declared on sibling span types
pedramsafaei File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
...a/aws-java-common/src/main/java/datadog/trace/instrumentation/aws/AwsAccountIdentity.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package datadog.trace.instrumentation.aws; | ||
|
|
||
| /** | ||
| * Account identity of resources addressed by AWS SDK requests. | ||
| * | ||
| * <p>The {@code aws_account} span tag always identifies the owner of the addressed resource, not | ||
| * the caller in general. The account that owns the signing credentials may be used only when the | ||
| * service contract guarantees that it also owns the addressed resource. | ||
| * | ||
| * <p>Two sources are trustworthy and used by the SDK instrumentations: | ||
| * | ||
| * <ul> | ||
| * <li>An ARN carried by the request itself (for example a DynamoDB {@code TableName} given as an | ||
| * ARN), parsed once with {@link AwsArn}. | ||
| * <li>The account that owns the credentials signing the request, when the SDK exposes it ({@code | ||
| * AwsCredentialsIdentity.accountId()} in AWS SDK for Java v2 2.26+), but only for a bare | ||
| * DynamoDB table name. DynamoDB guarantees that a bare name is resolved in the requestor's | ||
| * own account, so the credential owner and table owner are the same. This source must not be | ||
| * reused for services that can address resources owned by another account. | ||
| * </ul> | ||
| */ | ||
| public final class AwsAccountIdentity { | ||
|
|
||
| private AwsAccountIdentity() {} | ||
|
|
||
| /** {@code true} for exactly twelve ASCII digits. */ | ||
| public static boolean isAccountId(final String value) { | ||
| if (value == null || value.length() != 12) { | ||
| return false; | ||
| } | ||
| for (int i = 0; i < 12; i++) { | ||
| char c = value.charAt(i); | ||
| if (c < '0' || c > '9') { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
| } |
107 changes: 107 additions & 0 deletions
107
...tion/aws-java/aws-java-common/src/main/java/datadog/trace/instrumentation/aws/AwsArn.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| package datadog.trace.instrumentation.aws; | ||
|
|
||
| /** | ||
| * A parsed Amazon Resource Name: {@code arn:<partition>:<service>:<region>:<account>:<resource>}. | ||
| * | ||
| * <p>Parsed once so callers that need several fields do not re-scan the string. The AWS SDK ships | ||
| * its own parser ({@code software.amazon.awssdk.arns.Arn}) but it lives in a module that neither | ||
| * the SDK v2 2.2.0 floor these instrumentations compile against nor SDK v1 provide, so a minimal | ||
| * one is kept here. | ||
| */ | ||
| public final class AwsArn { | ||
|
|
||
| private final String raw; | ||
| private final String partition; | ||
| private final String service; | ||
| private final String region; | ||
| private final String account; | ||
| private final String resource; | ||
|
|
||
| private AwsArn( | ||
| final String raw, | ||
| final String partition, | ||
| final String service, | ||
| final String region, | ||
| final String account, | ||
| final String resource) { | ||
| this.raw = raw; | ||
| this.partition = partition; | ||
| this.service = service; | ||
| this.region = region; | ||
| this.account = account; | ||
| this.resource = resource; | ||
| } | ||
|
|
||
| /** | ||
| * Parses an ARN. | ||
| * | ||
| * @return the parsed ARN, or {@code null} when the value is not of the form {@code | ||
| * arn:partition:service:region:account:resource} (partition and service non-empty). | ||
| */ | ||
| public static AwsArn parse(final String value) { | ||
| if (value == null || !value.startsWith("arn:")) { | ||
| return null; | ||
| } | ||
| int c1 = value.indexOf(':', 4); | ||
| if (c1 < 0) { | ||
| return null; | ||
| } | ||
| int c2 = value.indexOf(':', c1 + 1); | ||
| if (c2 < 0) { | ||
| return null; | ||
| } | ||
| int c3 = value.indexOf(':', c2 + 1); | ||
| if (c3 < 0) { | ||
| return null; | ||
| } | ||
| int c4 = value.indexOf(':', c3 + 1); | ||
| if (c4 < 0) { | ||
| return null; | ||
| } | ||
| if (c1 == 4 || c2 == c1 + 1 || c4 == value.length() - 1) { | ||
| // empty partition, empty service or empty resource | ||
| return null; | ||
| } | ||
| return new AwsArn( | ||
| value, | ||
| value.substring(4, c1), | ||
| value.substring(c1 + 1, c2), | ||
| emptyToNull(value.substring(c2 + 1, c3)), | ||
| emptyToNull(value.substring(c3 + 1, c4)), | ||
| value.substring(c4 + 1)); | ||
| } | ||
|
|
||
| /** The original string. */ | ||
| public String raw() { | ||
| return raw; | ||
| } | ||
|
|
||
| /** {@code aws}, {@code aws-cn}, {@code aws-us-gov}, ... */ | ||
| public String partition() { | ||
| return partition; | ||
| } | ||
|
|
||
| /** {@code dynamodb}, {@code sns}, {@code s3}, ... */ | ||
| public String service() { | ||
| return service; | ||
| } | ||
|
|
||
| /** The Region, or {@code null} for global services (S3, IAM). */ | ||
| public String region() { | ||
| return region; | ||
| } | ||
|
|
||
| /** The 12-digit account, or {@code null} when absent (S3 ARNs) or not exactly twelve digits. */ | ||
| public String account() { | ||
| return AwsAccountIdentity.isAccountId(account) ? account : null; | ||
| } | ||
|
|
||
| /** Everything after the fifth colon. Never empty. */ | ||
| public String resource() { | ||
| return resource; | ||
| } | ||
|
|
||
| private static String emptyToNull(final String value) { | ||
| return value.isEmpty() ? null : value; | ||
| } | ||
| } |
25 changes: 25 additions & 0 deletions
25
...s-java-common/src/test/java/datadog/trace/instrumentation/aws/AwsAccountIdentityTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package datadog.trace.instrumentation.aws; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.NullAndEmptySource; | ||
| import org.junit.jupiter.params.provider.ValueSource; | ||
|
|
||
| class AwsAccountIdentityTest { | ||
|
|
||
| @ParameterizedTest(name = "{0} is an account id") | ||
| @ValueSource(strings = {"123456789012", "000000000000", "999999999999"}) | ||
| void acceptsTwelveDigits(String value) { | ||
| assertTrue(AwsAccountIdentity.isAccountId(value)); | ||
| } | ||
|
|
||
| @ParameterizedTest(name = "{0} is not an account id") | ||
| @NullAndEmptySource | ||
| @ValueSource( | ||
| strings = {"12345", "1234567890123", "12345678901a", " 23456789012", "arn:aws:s3:::b"}) | ||
| void rejectsNonAccountIds(String value) { | ||
| assertFalse(AwsAccountIdentity.isAccountId(value)); | ||
| } | ||
| } |
75 changes: 75 additions & 0 deletions
75
.../aws-java/aws-java-common/src/test/java/datadog/trace/instrumentation/aws/AwsArnTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| package datadog.trace.instrumentation.aws; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||
|
|
||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.CsvSource; | ||
| import org.junit.jupiter.params.provider.NullAndEmptySource; | ||
| import org.junit.jupiter.params.provider.ValueSource; | ||
|
|
||
| class AwsArnTest { | ||
|
|
||
| @ParameterizedTest(name = "parses {0}") | ||
| @CsvSource( | ||
| nullValues = "NULL", | ||
| value = { | ||
| "arn:aws:dynamodb:us-east-1:123456789012:table/orders, aws, dynamodb, us-east-1, 123456789012, table/orders", | ||
| "arn:aws:dynamodb:us-east-1:123456789012:table/orders/index/by-user, aws, dynamodb, us-east-1, 123456789012, table/orders/index/by-user", | ||
| "arn:aws-cn:dynamodb:cn-north-1:123456789012:table/orders, aws-cn, dynamodb, cn-north-1, 123456789012, table/orders", | ||
| "arn:aws-us-gov:sns:us-gov-west-1:123456789012:alerts, aws-us-gov, sns, us-gov-west-1, 123456789012, alerts", | ||
| "arn:aws:s3:::my-bucket, aws, s3, NULL, NULL, my-bucket", | ||
| "arn:aws:iam::123456789012:role/app, aws, iam, NULL, 123456789012, role/app", | ||
| "arn:aws:states:us-east-1:123456789012:stateMachine:orders:extra:colons, aws, states, us-east-1, 123456789012, stateMachine:orders:extra:colons", | ||
| }) | ||
| void parsesFields( | ||
| String value, | ||
| String partition, | ||
| String service, | ||
| String region, | ||
| String account, | ||
| String resource) { | ||
| AwsArn arn = AwsArn.parse(value); | ||
| assertNotNull(arn); | ||
| assertEquals(value, arn.raw()); | ||
| assertEquals(partition, arn.partition()); | ||
| assertEquals(service, arn.service()); | ||
| assertEquals(region, arn.region()); | ||
| assertEquals(account, arn.account()); | ||
| assertEquals(resource, arn.resource()); | ||
| } | ||
|
|
||
| @ParameterizedTest(name = "rejects {0}") | ||
| @NullAndEmptySource | ||
| @ValueSource( | ||
| strings = { | ||
| "orders", | ||
| "arn:", | ||
| "arn:aws", | ||
| "arn:aws:dynamodb", | ||
| "arn:aws:dynamodb:us-east-1", | ||
| "arn:aws:dynamodb:us-east-1:123456789012", | ||
| "arn:aws:dynamodb:us-east-1:123456789012:", | ||
| "arn::dynamodb:us-east-1:123456789012:table/orders", | ||
| "arn:aws::us-east-1:123456789012:table/orders", | ||
| "table/orders", | ||
| "https://sqs.us-east-1.amazonaws.com/123456789012/q", | ||
| }) | ||
| void rejectsNonArn(String value) { | ||
| assertNull(AwsArn.parse(value)); | ||
| } | ||
|
|
||
| @ParameterizedTest(name = "account of {0} is null") | ||
| @ValueSource( | ||
| strings = { | ||
| "arn:aws:dynamodb:us-east-1:12345:table/orders", | ||
| "arn:aws:dynamodb:us-east-1:12345678901a:table/orders", | ||
| "arn:aws:dynamodb:us-east-1::table/orders", | ||
| }) | ||
| void accountMustBeTwelveDigits(String value) { | ||
| AwsArn arn = AwsArn.parse(value); | ||
| assertNotNull(arn); | ||
| assertNull(arn.account()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like we could use AWS SDK provided ARN parser for a good chunk of the work here, any reason to reimplement it ourselves here ?
https://docs.aws.amazon.com/java/api/latest/software/amazon/awssdk/arns/Arn.html
Also, if we want to keep our own implem, I think we should parse it once in an object. Here we end up checking several times if an ARN is valid for instance when we retrieve multiple values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The arns module is not a dependency of aws-core at the 2.2.0 floor this instrumentation compiles against (checked the 2.2.0 POM), and SDK v1 has no equivalent, so depending on it would break loading on older v2 and be unusable from the v1 module. Agreed on parsing once though pushed a new change that replaces the per-field helpers with an AwsArn value type parsed in a single pass, and both decorators call parse once.