Skip to content
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

zdu UID2-1776 Enforce Opt-Out Policy Check for New Participants added after 9/1/2023 #207

Merged
merged 13 commits into from
Sep 21, 2023

Conversation

zaiweidu
Copy link
Contributor

@zaiweidu zaiweidu commented Sep 11, 2023

Token/Generate & Identity/Map

  • Added logic to derive Site Creation timestamp from the oldest Client Key Creation timestamp.
  • Enforce opt-out policy for new client added after 9/1

Unit Tests (merged from @Ian-Nara's PR)

  • New tests to validate desire behavior:
  1. tokenGenerateNewClientNoPolicySpecified
  2. tokenGenerateNewClientWrongPolicySpecified
  3. tokenGenerateNewClientNoPolicySpecifiedOlderKeySuccessful
  4. tokenGenerateNewClientWrongPolicySpecifiedOlderKeySuccessful
  5. identityMapNewClientNoPolicySpecified
  6. identityMapNewClientWrongPolicySpecified
  7. identityMapNewClientNoPolicySpecifiedOlderKeySuccessful
  8. identityMapNewClientWrongPolicySpecifiedOlderKeySuccessful
  • Existing tests to validate desired behavior:
  1. tokenGenerateRespectOptOutOption
  2. identityMapRespectOptOutOption
  3. other existing identity map and token generate tests that do not specify policy for old participants

Copy link
Contributor

@cody-constine-ttd cody-constine-ttd left a comment

Choose a reason for hiding this comment

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

Can you write unit tests for the two endpoints that test this new functionality?

LOGGER.error("request body misses opt-out policy argument");
ResponseUtil.ClientError(rc, "request body misses opt-out policy argument");
return;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we make a function for the duplicate code?

Copy link
Contributor Author

@zaiweidu zaiweidu Sep 19, 2023

Choose a reason for hiding this comment

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

Refactored the code as suggested.

Copy link
Contributor

Choose a reason for hiding this comment

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

The two blocks still look largely similar. How can more stuff be pushed into the common function?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Refactored the similar code into a single method.

@@ -1636,6 +1651,11 @@ private TokenGeneratePolicy readTokenGeneratePolicy(JsonObject req) {
TokenGeneratePolicy.defaultPolicy();
}

private boolean isAfterCutoffDate(long timestamp) {
long cutoff = Instant.parse("2023-09-01T00:00:00.00Z").getEpochSecond();
Copy link
Contributor

Choose a reason for hiding this comment

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

String should be a static member

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd even suggest to make the whole Instant to be static final member. Or even the long. No reason to do these calculations every time.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Refactored as suggested.

@@ -777,6 +777,13 @@ private void handleTokenGenerateV2(RoutingContext rc) {
}
}

if (isAfterCutoffDate(clientKey.getCreated()) && (!req.containsKey(TOKEN_GENERATE_POLICY_PARAM)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this looking at the client API key used for auth?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Switched to the oldest client key.

if (isAfterCutoffDate(clientKey.getCreated()) && (!req.containsKey(TOKEN_GENERATE_POLICY_PARAM)
|| TokenGeneratePolicy.fromValue(req.getInteger(TOKEN_GENERATE_POLICY_PARAM)) != TokenGeneratePolicy.respectOptOut())) {
LOGGER.error("request body misses opt-out policy argument");
ResponseUtil.ClientError(rc, "request body misses opt-out policy argument");
Copy link
Contributor

Choose a reason for hiding this comment

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

The message can be misleading if one specifies policy=0 in the request.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated the error message.

@@ -1636,6 +1651,11 @@ private TokenGeneratePolicy readTokenGeneratePolicy(JsonObject req) {
TokenGeneratePolicy.defaultPolicy();
}

private boolean isAfterCutoffDate(long timestamp) {
long cutoff = Instant.parse("2023-09-01T00:00:00.00Z").getEpochSecond();
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd even suggest to make the whole Instant to be static final member. Or even the long. No reason to do these calculations every time.

@@ -1636,6 +1651,11 @@ private TokenGeneratePolicy readTokenGeneratePolicy(JsonObject req) {
TokenGeneratePolicy.defaultPolicy();
}

private boolean isAfterCutoffDate(long timestamp) {
Copy link
Contributor

Choose a reason for hiding this comment

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

In the context of this over-bloated class, isAfterCutoffDate is not descriptive enough. Cut-off for what?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Renamed the method to be more meaningful.

@zaiweidu zaiweidu force-pushed the zdu-UID2-1776-Add-policy-check-for-Publisher branch from 1256174 to 6cfbbe5 Compare September 18, 2023 22:00
@@ -741,6 +748,68 @@ public static void validateAdvertisingToken(String advertisingTokenString, Token
}
}

@ParameterizedTest
@ValueSource(strings = {"NoOutoutPolicySpecified", "WrongOutOutPolicySpecified"})
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you pass the policy values instead of strings? You should avoid switch (and if) statements in the unit tests.

Copy link
Contributor Author

@zaiweidu zaiweidu Sep 21, 2023

Choose a reason for hiding this comment

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

  1. The org.junit.jupiter.params.provider requires compile-time constant for annotation. Since Enum policy values aren't primitive type, I can't pass them as annotation.
  2. Removed Switch usage by splitting ParameterizedTest.

req.put("policy", 0);
break;
default:
req.put("policy", 1);
Copy link
Contributor

Choose a reason for hiding this comment

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

Judging by value source above this case would never get hit?

Copy link
Contributor Author

@zaiweidu zaiweidu Sep 21, 2023

Choose a reason for hiding this comment

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

Removed.

@@ -768,6 +771,13 @@ private void handleTokenGenerateV2(RoutingContext rc) {
}
}

boolean respsctOptOut = req.containsKey(TOKEN_GENERATE_POLICY_PARAM)
Copy link
Contributor

Choose a reason for hiding this comment

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

Typo in variable name?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oops...

LOGGER.error("request body misses opt-out policy argument");
ResponseUtil.ClientError(rc, "request body misses opt-out policy argument");
return;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

The two blocks still look largely similar. How can more stuff be pushed into the common function?

@zaiweidu zaiweidu merged commit a407bc5 into master Sep 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants