Skip to content

Commit 0511ac8

Browse files
authored
b/433111438 Add backoff/retry logic for updating group settings (#730)
Updating group settings can fail intermittelty for newly created groups. This didn't use to happen much in the past, but is now happening more frequently. Add backoff/retry logic to compensate.
1 parent 6691dd1 commit 0511ac8

1 file changed

Lines changed: 50 additions & 4 deletions

File tree

sources/src/main/java/com/google/solutions/jitaccess/apis/clients/CloudIdentityGroupsClient.java

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public class CloudIdentityGroupsClient {
5454
private static final int SEARCH_PAGE_SIZE = 1000;
5555
public static final String LABEL_DISCUSSION_FORUM = "cloudidentity.googleapis.com/groups.discussion_forum";
5656
public static final String LABEL_SECURITY = "cloudidentity.googleapis.com/groups.security";
57+
private static final int MAX_GROUP_SETTINGS_PATCH_ATTEMPTS = 5;
5758

5859
private final @NotNull Options options;
5960
private final @NotNull GoogleCredentials credentials;
@@ -147,6 +148,54 @@ private static void translateAndThrowApiException(
147148
}
148149
}
149150

151+
152+
/**
153+
* Update group settings to restrictive defaults.
154+
*/
155+
private void restrictGroupSettings(@NotNull GroupId emailAddress) throws IOException {
156+
var settingsClient = createSettingsClient();
157+
158+
//
159+
// The group settings API is prone to fail for newly created groups.
160+
//
161+
for (int attempt = 0; attempt < MAX_GROUP_SETTINGS_PATCH_ATTEMPTS; attempt++) {
162+
try {
163+
settingsClient
164+
.groups()
165+
.update(emailAddress.email, this.RESTRICTED_SETTINGS)
166+
.execute();
167+
168+
//
169+
// Successful update -> quit loop.
170+
//
171+
return;
172+
}
173+
catch (GoogleJsonResponseException e) {
174+
if (
175+
e.getStatusCode() == 404 ||
176+
e.getStatusCode() == 400 &&
177+
e.getDetails() != null &&
178+
e.getDetails().getErrors() != null &&
179+
e.getDetails().getErrors()
180+
.stream()
181+
.anyMatch(err -> e.getMessage() != null && err.getMessage().contains("INVALID_GAIA_GROUP"))) {
182+
183+
//
184+
// This is most likely an intermittent error.
185+
//
186+
try {
187+
Thread.sleep(200);
188+
}
189+
catch (InterruptedException ignored) {
190+
}
191+
}
192+
else {
193+
throw (GoogleJsonResponseException) e.fillInStackTrace();
194+
}
195+
}
196+
}
197+
}
198+
150199
//---------------------------------------------------------------------
151200
// Manage groups.
152201
//---------------------------------------------------------------------
@@ -323,10 +372,7 @@ public enum GroupType {
323372
//
324373
// Lock down group settings.
325374
//
326-
createSettingsClient()
327-
.groups()
328-
.update(emailAddress.email, this.RESTRICTED_SETTINGS)
329-
.execute();
375+
restrictGroupSettings(emailAddress);
330376

331377
return groupKey;
332378
}

0 commit comments

Comments
 (0)