Skip to content

Commit eb80aaf

Browse files
authored
fix: update references to deprecated hashed_account_id (GoogleCloudPlatform#8974)
* Update references to hashed_account_id to account_id in related accounts. * Update references to hashed_account_id to account_id in related accounts. * Add tests. * Remove unused code.
1 parent 1c70654 commit eb80aaf

File tree

2 files changed

+10
-23
lines changed

2 files changed

+10
-23
lines changed

recaptcha_enterprise/snippets/src/main/java/recaptcha/account_defender/SearchRelatedAccountGroupMemberships.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
// [START recaptcha_enterprise_search_related_account_group_membership]
2020

2121
import com.google.cloud.recaptchaenterprise.v1.RecaptchaEnterpriseServiceClient;
22-
import com.google.protobuf.ByteString;
2322
import com.google.recaptchaenterprise.v1.RelatedAccountGroupMembership;
2423
import com.google.recaptchaenterprise.v1.SearchRelatedAccountGroupMembershipsRequest;
2524
import java.io.IOException;
@@ -32,29 +31,29 @@ public static void main(String[] args) throws IOException, NoSuchAlgorithmExcept
3231
// projectId: Google Cloud Project Id.
3332
String projectId = "project-id";
3433

35-
// HMAC SHA-256 hashed unique id of the customer.
36-
ByteString hashedAccountId = ByteString.copyFrom(new byte[] {});
34+
// Unique id of the customer.
35+
String accountId = "default" + UUID.randomUUID().toString().split("-")[0];
3736

38-
searchRelatedAccountGroupMemberships(projectId, hashedAccountId);
37+
searchRelatedAccountGroupMemberships(projectId, accountId);
3938
}
4039

41-
// List group memberships for the hashed account id.
40+
// List group memberships for the account id.
4241
public static void searchRelatedAccountGroupMemberships(
43-
String projectId, ByteString hashedAccountId) throws IOException {
42+
String projectId, String accountId) throws IOException {
4443
try (RecaptchaEnterpriseServiceClient client = RecaptchaEnterpriseServiceClient.create()) {
4544

4645
SearchRelatedAccountGroupMembershipsRequest request =
4746
SearchRelatedAccountGroupMembershipsRequest.newBuilder()
4847
.setProject(projectId)
49-
.setHashedAccountId(hashedAccountId)
48+
.setAccountId(accountId)
5049
.build();
5150

5251
for (RelatedAccountGroupMembership groupMembership :
5352
client.searchRelatedAccountGroupMemberships(request).iterateAll()) {
5453
System.out.println(groupMembership.getName());
5554
}
5655
System.out.printf(
57-
"Finished searching related account group memberships for %s!", hashedAccountId);
56+
"Finished searching related account group memberships for %s!", accountId);
5857
}
5958
}
6059
}

recaptcha_enterprise/snippets/src/test/java/app/SnippetsIT.java

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import java.util.concurrent.ExecutionException;
3939
import java.util.concurrent.TimeUnit;
4040
import java.util.concurrent.TimeoutException;
41-
import javax.crypto.Mac;
4241
import javax.crypto.spec.SecretKeySpec;
4342
import org.json.JSONException;
4443
import org.json.JSONObject;
@@ -184,17 +183,6 @@ public void testCreateAnnotateAccountDefender()
184183
String testURL = "http://localhost:" + randomServerPort + "/";
185184
String accountId = "default-" + UUID.randomUUID().toString().split("-")[0];
186185

187-
// Secret not shared with Google.
188-
String HMAC_KEY = "123456789";
189-
// Get instance of Mac object implementing HmacSHA256, and initialize it with the above
190-
// secret key.
191-
Mac mac = Mac.getInstance("HmacSHA256");
192-
SecretKeySpec secretKeySpec = new SecretKeySpec(HMAC_KEY.getBytes(StandardCharsets.UTF_8),
193-
"HmacSHA256");
194-
mac.init(secretKeySpec);
195-
byte[] hashBytes = mac.doFinal(accountId.getBytes(StandardCharsets.UTF_8));
196-
asdfByteString hashedAccountId = ByteString.copyFrom(hashBytes);
197-
198186
// Create the assessment.
199187
JSONObject createAssessmentResult =
200188
createAssessment(testURL, accountId, AssessmentType.ACCOUNT_DEFENDER);
@@ -221,13 +209,13 @@ public void testCreateAnnotateAccountDefender()
221209
ListRelatedAccountGroupMemberships.listRelatedAccountGroupMemberships(PROJECT_ID, "legitimate");
222210
assertThat(stdOut.toString()).contains("Finished listing related account group memberships.");
223211

224-
// Search related group memberships for a hashed account id.
212+
// Search related group memberships for a account id.
225213
SearchRelatedAccountGroupMemberships.searchRelatedAccountGroupMemberships(
226-
PROJECT_ID, hashedAccountId);
214+
PROJECT_ID, accountId);
227215
assertThat(stdOut.toString())
228216
.contains(
229217
String.format(
230-
"Finished searching related account group memberships for %s", hashedAccountId));
218+
"Finished searching related account group memberships for %s", accountId));
231219
}
232220

233221
@Test

0 commit comments

Comments
 (0)