Skip to content

Commit

Permalink
Merge pull request #59 from lsst-sqre/tickets/DM-30904-1
Browse files Browse the repository at this point in the history
[DM-30906] Add retry loop for Gafaelfawr request
  • Loading branch information
rra authored Jun 30, 2021
2 parents ccbb096 + ae1118a commit 08cd44f
Showing 1 changed file with 30 additions and 12 deletions.
42 changes: 30 additions & 12 deletions src/main/java/org/opencadc/tap/impl/AuthenticatorImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,23 @@ public Subject augment(Subject subject)
if (principal instanceof BearerTokenPrincipal) {
BearerTokenPrincipal tp = (BearerTokenPrincipal) principal;

HttpRequest request = HttpRequest.newBuilder(
URI.create(gafaelfawr_url))
.header("Accept", "application/json")
.header("Authorization", "bearer " + tp.getName())
.build();

try {
HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
String body = response.body();

Gson gson = new Gson();
JsonObject authData = gson.fromJson(body, JsonObject.class);
JsonObject authData = null;
boolean success = false;
for (int i = 1; i < 5; i++) {
try {
authData = getTokenInfo(tp.getName());
success = true;
} catch (IOException e) {
log.warn(e);
log.warn("IOException while getting info Gafaelfawr, retrying");
}
}

if (!success) {
// Try one more time to throw an accurate exception.
authData = getTokenInfo(tp.getName());
}

String username = authData.getAsJsonPrimitive("username").getAsString();
int uid = authData.getAsJsonPrimitive("uid").getAsInt();
Expand All @@ -87,7 +92,7 @@ public Subject augment(Subject subject)
log.warn("InterruptedException thrown while getting info from Gafaelfawr");
log.warn(e);
} catch (IOException e) {
log.warn("IOException while getting info from Gafaelfawr");
log.warn("IOException while getting info from Gafaelfawr, failing");
log.warn(e);
}
}
Expand All @@ -106,4 +111,17 @@ public Subject augment(Subject subject)
public Subject validate(Subject subject) throws AccessControlException {
return subject;
}

private JsonObject getTokenInfo(String token) throws IOException, InterruptedException {
HttpRequest request = HttpRequest.newBuilder(URI.create(gafaelfawr_url))
.header("Accept", "application/json")
.header("Authorization", "bearer " + token)
.build();

HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
String body = response.body();

Gson gson = new Gson();
return gson.fromJson(body, JsonObject.class);
}
}

0 comments on commit 08cd44f

Please sign in to comment.