-
-
Notifications
You must be signed in to change notification settings - Fork 475
feat(core): [Data Collection 3] Add policy resolver #5799
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
Draft
adinauer
wants to merge
5
commits into
feat/data-collection-options
Choose a base branch
from
feat/data-collection-resolver
base: feat/data-collection-options
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.
+362
−0
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1ad9a19
feat(core): Add Data Collection resolver
adinauer 1ef94b8
merge: Update Data Collection stack
adinauer 9b6ddb7
Merge branch 'feat/data-collection-options' into feat/data-collection…
adinauer 6a66949
ref(core): Rename URL query parameter resolver
adinauer 9bce9f3
Merge branch 'feat/data-collection-options' into feat/data-collection…
adinauer 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
105 changes: 105 additions & 0 deletions
105
sentry/src/main/java/io/sentry/DataCollectionResolver.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,105 @@ | ||
| package io.sentry; | ||
|
|
||
| import java.util.Set; | ||
| import org.jetbrains.annotations.ApiStatus; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.jetbrains.annotations.Nullable; | ||
|
|
||
| /** Resolves effective Data Collection policies for SDK integrations. */ | ||
| @ApiStatus.Internal | ||
| public final class DataCollectionResolver { | ||
|
|
||
| private static final @NotNull KeyValueCollectionBehavior OFF = KeyValueCollectionBehavior.off(); | ||
| private static final @NotNull KeyValueCollectionBehavior EMPTY_DENY_LIST = | ||
| KeyValueCollectionBehavior.denyList(); | ||
|
|
||
| private final @NotNull SentryOptions options; | ||
|
|
||
| DataCollectionResolver(final @NotNull SentryOptions options) { | ||
| this.options = options; | ||
| } | ||
|
|
||
| public boolean isDataCollectionConfigured() { | ||
| return options.getDataCollection().isExplicitlyConfigured(); | ||
| } | ||
|
|
||
| public boolean isUserInfo() { | ||
| return explicitOrSendDefaultPii(options.getDataCollection().getUserInfo(), true); | ||
| } | ||
|
|
||
| public boolean isDatabaseQueryData() { | ||
| return explicitOrSendDefaultPii(options.getDataCollection().getDatabaseQueryData(), true); | ||
| } | ||
|
|
||
| public boolean isGraphqlDocument() { | ||
| return explicitOrSendDefaultPii(options.getDataCollection().getGraphql().getDocument(), true); | ||
| } | ||
|
|
||
| public boolean isGraphqlVariables() { | ||
| return explicitOrSendDefaultPii(options.getDataCollection().getGraphql().getVariables(), true); | ||
| } | ||
|
|
||
| public @NotNull KeyValueCollectionBehavior getCookies() { | ||
| final @NotNull DataCollection dataCollection = options.getDataCollection(); | ||
| final @Nullable KeyValueCollectionBehavior cookies = dataCollection.getCookies(); | ||
|
|
||
| if (cookies != null) { | ||
| return cookies; | ||
| } | ||
| if (isDataCollectionConfigured()) { | ||
| return EMPTY_DENY_LIST; | ||
| } | ||
| return options.isSendDefaultPii() ? EMPTY_DENY_LIST : OFF; | ||
| } | ||
|
|
||
| public @NotNull KeyValueCollectionBehavior getUrlQueryParams() { | ||
| return explicitOrEmptyDenyList(options.getDataCollection().getUrlQueryParams()); | ||
| } | ||
|
|
||
| public @NotNull KeyValueCollectionBehavior getHttpRequestHeaders() { | ||
| return explicitOrEmptyDenyList(options.getDataCollection().getHttpHeaders().getRequest()); | ||
| } | ||
|
|
||
| public @NotNull KeyValueCollectionBehavior getHttpResponseHeaders() { | ||
| return explicitOrEmptyDenyList(options.getDataCollection().getHttpHeaders().getResponse()); | ||
| } | ||
|
|
||
| public boolean isIncomingRequestBody() { | ||
| return isHttpBodyEnabled(HttpBodyType.INCOMING_REQUEST, options.isSendDefaultPii()); | ||
| } | ||
|
|
||
| public boolean isOutgoingRequestBody() { | ||
| return isHttpBodyEnabled(HttpBodyType.OUTGOING_REQUEST, true); | ||
| } | ||
|
|
||
| public boolean isIncomingResponseBody() { | ||
| return isHttpBodyEnabled(HttpBodyType.INCOMING_RESPONSE, true); | ||
| } | ||
|
|
||
| public boolean isOutgoingResponseBody() { | ||
| return isHttpBodyEnabled(HttpBodyType.OUTGOING_RESPONSE, options.isSendDefaultPii()); | ||
| } | ||
|
|
||
| private boolean explicitOrSendDefaultPii( | ||
| final @Nullable Boolean explicit, final boolean defaultValue) { | ||
| if (explicit != null) { | ||
| return explicit; | ||
| } | ||
| return isDataCollectionConfigured() ? defaultValue : options.isSendDefaultPii(); | ||
| } | ||
|
|
||
| private @NotNull KeyValueCollectionBehavior explicitOrEmptyDenyList( | ||
| final @Nullable KeyValueCollectionBehavior explicit) { | ||
| return explicit != null ? explicit : EMPTY_DENY_LIST; | ||
| } | ||
|
|
||
| private boolean isHttpBodyEnabled( | ||
| final @NotNull HttpBodyType bodyType, final boolean legacyFallback) { | ||
| final @NotNull DataCollection dataCollection = options.getDataCollection(); | ||
| final @Nullable Set<HttpBodyType> httpBodies = dataCollection.getHttpBodies(); | ||
| if (httpBodies != null) { | ||
| return httpBodies.contains(bodyType); | ||
| } | ||
| return isDataCollectionConfigured() || legacyFallback; | ||
| } | ||
| } |
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
231 changes: 231 additions & 0 deletions
231
sentry/src/test/java/io/sentry/DataCollectionResolverTest.kt
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,231 @@ | ||
| package io.sentry | ||
|
|
||
| import com.google.common.truth.Truth.assertThat | ||
| import kotlin.test.Test | ||
|
|
||
| class DataCollectionResolverTest { | ||
| @Test | ||
| fun `one resolver is reused per options instance`() { | ||
| val options = SentryOptions() | ||
|
|
||
| assertThat(options.dataCollectionResolver).isSameInstanceAs(options.dataCollectionResolver) | ||
| } | ||
|
|
||
| @Test | ||
| fun `each options instance owns its resolver`() { | ||
| val first = SentryOptions() | ||
| val second = SentryOptions() | ||
|
|
||
| assertThat(first.dataCollectionResolver).isNotSameInstanceAs(second.dataCollectionResolver) | ||
| } | ||
|
|
||
| @Test | ||
| fun `data collection configured reflects namespace explicitness`() { | ||
| val options = SentryOptions() | ||
|
|
||
| assertThat(options.dataCollectionResolver.isDataCollectionConfigured).isFalse() | ||
|
|
||
| options.dataCollection.urlQueryParams = KeyValueCollectionBehavior.denyList() | ||
|
|
||
| assertThat(options.dataCollectionResolver.isDataCollectionConfigured).isTrue() | ||
| } | ||
|
|
||
| @Test | ||
| fun `user info falls back to sendDefaultPii when unset`() { | ||
| val options = SentryOptions() | ||
|
|
||
| assertThat(options.dataCollectionResolver.isUserInfo).isFalse() | ||
|
|
||
| options.isSendDefaultPii = true | ||
|
|
||
| assertThat(options.dataCollectionResolver.isUserInfo).isTrue() | ||
| } | ||
|
|
||
| @Test | ||
| fun `user info override takes precedence over sendDefaultPii`() { | ||
| val options = SentryOptions().apply { isSendDefaultPii = true } | ||
|
|
||
| options.dataCollection.setUserInfo(false) | ||
|
|
||
| assertThat(options.dataCollectionResolver.isUserInfo).isFalse() | ||
|
|
||
| options.isSendDefaultPii = false | ||
| options.dataCollection.setUserInfo(true) | ||
|
|
||
| assertThat(options.dataCollectionResolver.isUserInfo).isTrue() | ||
| } | ||
|
|
||
| @Test | ||
| fun `omitted booleans use data collection defaults once namespace is explicit`() { | ||
| val options = SentryOptions().apply { isSendDefaultPii = false } | ||
|
|
||
| options.dataCollection.cookies = KeyValueCollectionBehavior.off() | ||
|
|
||
| assertThat(options.dataCollectionResolver.isUserInfo).isTrue() | ||
| assertThat(options.dataCollectionResolver.isDatabaseQueryData).isTrue() | ||
| assertThat(options.dataCollectionResolver.isGraphqlDocument).isTrue() | ||
| assertThat(options.dataCollectionResolver.isGraphqlVariables).isTrue() | ||
| } | ||
|
|
||
| @Test | ||
| fun `database query data falls back to sendDefaultPii and override takes precedence`() { | ||
| val options = SentryOptions().apply { isSendDefaultPii = true } | ||
|
|
||
| assertThat(options.dataCollectionResolver.isDatabaseQueryData).isTrue() | ||
|
|
||
| options.dataCollection.setDatabaseQueryData(false) | ||
|
|
||
| assertThat(options.dataCollectionResolver.isDatabaseQueryData).isFalse() | ||
| } | ||
|
|
||
| @Test | ||
| fun `GraphQL document falls back to sendDefaultPii and override takes precedence`() { | ||
| val options = SentryOptions().apply { isSendDefaultPii = true } | ||
|
|
||
| assertThat(options.dataCollectionResolver.isGraphqlDocument).isTrue() | ||
|
|
||
| options.dataCollection.graphql.setDocument(false) | ||
|
|
||
| assertThat(options.dataCollectionResolver.isGraphqlDocument).isFalse() | ||
| } | ||
|
|
||
| @Test | ||
| fun `cookies are off when unset and sendDefaultPii is false`() { | ||
| val options = SentryOptions() | ||
|
|
||
| assertThat(options.dataCollectionResolver.cookies).isEqualTo(KeyValueCollectionBehavior.off()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `cookies use default deny list when unset and sendDefaultPii is true`() { | ||
| val options = SentryOptions().apply { isSendDefaultPii = true } | ||
|
|
||
| assertThat(options.dataCollectionResolver.cookies) | ||
| .isEqualTo(KeyValueCollectionBehavior.denyList()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `cookies use default deny list when namespace is explicit`() { | ||
| val options = SentryOptions().apply { isSendDefaultPii = false } | ||
|
|
||
| options.dataCollection.setUserInfo(false) | ||
|
|
||
| assertThat(options.dataCollectionResolver.cookies) | ||
| .isEqualTo(KeyValueCollectionBehavior.denyList()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `cookies override takes precedence over sendDefaultPii`() { | ||
| val options = SentryOptions().apply { isSendDefaultPii = false } | ||
| val behavior = KeyValueCollectionBehavior.allowList("language", "theme") | ||
|
|
||
| options.dataCollection.cookies = behavior | ||
|
|
||
| assertThat(options.dataCollectionResolver.cookies).isEqualTo(behavior) | ||
| } | ||
|
|
||
| @Test | ||
| fun `URL query params use default deny list when unset`() { | ||
| val options = SentryOptions() | ||
|
|
||
| assertThat(options.dataCollectionResolver.urlQueryParams) | ||
| .isEqualTo(KeyValueCollectionBehavior.denyList()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `URL query params override takes precedence`() { | ||
| val options = SentryOptions() | ||
| val behavior = KeyValueCollectionBehavior.allowList("language", "theme") | ||
|
|
||
| options.dataCollection.urlQueryParams = behavior | ||
|
|
||
| assertThat(options.dataCollectionResolver.urlQueryParams).isEqualTo(behavior) | ||
| } | ||
|
|
||
| @Test | ||
| fun `HTTP request headers use default deny list when unset`() { | ||
| val options = SentryOptions() | ||
|
|
||
| assertThat(options.dataCollectionResolver.httpRequestHeaders) | ||
| .isEqualTo(KeyValueCollectionBehavior.denyList()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `HTTP request headers override takes precedence`() { | ||
| val options = SentryOptions() | ||
| val behavior = KeyValueCollectionBehavior.allowList("content-type") | ||
|
|
||
| options.dataCollection.httpHeaders.request = behavior | ||
|
|
||
| assertThat(options.dataCollectionResolver.httpRequestHeaders).isEqualTo(behavior) | ||
| } | ||
|
|
||
| @Test | ||
| fun `HTTP response headers use default deny list when unset`() { | ||
| val options = SentryOptions() | ||
|
|
||
| assertThat(options.dataCollectionResolver.httpResponseHeaders) | ||
| .isEqualTo(KeyValueCollectionBehavior.denyList()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `HTTP response headers override takes precedence`() { | ||
| val options = SentryOptions() | ||
| val behavior = KeyValueCollectionBehavior.off() | ||
|
|
||
| options.dataCollection.httpHeaders.response = behavior | ||
|
|
||
| assertThat(options.dataCollectionResolver.httpResponseHeaders).isEqualTo(behavior) | ||
| } | ||
|
|
||
| @Test | ||
| fun `HTTP bodies preserve direction-specific legacy fallbacks when data collection is absent`() { | ||
| val options = SentryOptions() | ||
|
|
||
| assertThat(options.dataCollectionResolver.isIncomingRequestBody).isFalse() | ||
| assertThat(options.dataCollectionResolver.isOutgoingRequestBody).isTrue() | ||
| assertThat(options.dataCollectionResolver.isIncomingResponseBody).isTrue() | ||
| assertThat(options.dataCollectionResolver.isOutgoingResponseBody).isFalse() | ||
|
|
||
| options.isSendDefaultPii = true | ||
|
|
||
| assertThat(options.dataCollectionResolver.isIncomingRequestBody).isTrue() | ||
| assertThat(options.dataCollectionResolver.isOutgoingRequestBody).isTrue() | ||
| assertThat(options.dataCollectionResolver.isIncomingResponseBody).isTrue() | ||
| assertThat(options.dataCollectionResolver.isOutgoingResponseBody).isTrue() | ||
| } | ||
|
|
||
| @Test | ||
| fun `explicit empty data collection enables every HTTP body direction`() { | ||
| val options = SentryOptions().apply { dataCollection = DataCollection() } | ||
|
|
||
| assertThat(options.dataCollectionResolver.isIncomingRequestBody).isTrue() | ||
| assertThat(options.dataCollectionResolver.isOutgoingRequestBody).isTrue() | ||
| assertThat(options.dataCollectionResolver.isIncomingResponseBody).isTrue() | ||
| assertThat(options.dataCollectionResolver.isOutgoingResponseBody).isTrue() | ||
| } | ||
|
|
||
| @Test | ||
| fun `explicit HTTP body set controls every direction`() { | ||
| val options = SentryOptions().apply { isSendDefaultPii = true } | ||
|
|
||
| options.dataCollection.httpBodies = | ||
| setOf(HttpBodyType.INCOMING_REQUEST, HttpBodyType.OUTGOING_RESPONSE) | ||
|
|
||
| assertThat(options.dataCollectionResolver.isIncomingRequestBody).isTrue() | ||
| assertThat(options.dataCollectionResolver.isOutgoingRequestBody).isFalse() | ||
| assertThat(options.dataCollectionResolver.isIncomingResponseBody).isFalse() | ||
| assertThat(options.dataCollectionResolver.isOutgoingResponseBody).isTrue() | ||
| } | ||
|
|
||
| @Test | ||
| fun `GraphQL variables fall back to sendDefaultPii and override takes precedence`() { | ||
| val options = SentryOptions().apply { isSendDefaultPii = true } | ||
|
|
||
| assertThat(options.dataCollectionResolver.isGraphqlVariables).isTrue() | ||
|
|
||
| options.dataCollection.graphql.setVariables(false) | ||
|
|
||
| assertThat(options.dataCollectionResolver.isGraphqlVariables).isFalse() | ||
| } | ||
| } | ||
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.
(l) Ties into the comment about the
overriddenvariable naming. MaybeGraphQL variables fall back to sendDefaultPii and DataCollection takes precedence?