Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,17 @@ public Response renew(String token) {
Response.Status errorStatus = Response.Status.BAD_REQUEST;

if (tokenStateService == null) {
error = "Token renewal support is not configured";
// If the token state service is disabled, then return the expiration from the specified token
try {
JWTToken jwt = new JWTToken(token);
log.renewalDisabled(getTopologyName(), TokenUtils.getTokenDisplayText(token), TokenUtils.getTokenId(jwt));
expiration = Long.parseLong(jwt.getExpires());
} catch (ParseException e) {
log.invalidToken(getTopologyName(), TokenUtils.getTokenDisplayText(token), e);
error = safeGetMessage(e);
} catch (Exception e) {
error = safeGetMessage(e);
}
} else {
String renewer = SubjectUtils.getCurrentEffectivePrincipalName();
if (allowedRenewers.contains(renewer)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,7 @@ void invalidToken(String topologyName,
@Message( level = MessageLevel.DEBUG, text = "Knox Token service ({0}) stored state for token {1} ({2})")
void storedToken(String topologyName, String tokenDisplayText, String tokenId);

@Message( level = MessageLevel.WARN,
text = "Renewal is disabled for the Knox Token service ({0}). Responding with the expiration from the token {1} ({2})")
void renewalDisabled(String topologyName, String tokenDisplayText, String tokenId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -704,20 +704,54 @@ public void testTokenRenewal_ServerManagedStateDisabledAtGatewayWithServiceOverr
@Test
public void testTokenRenewal_ServerManagedStateEnabledAtGatewayWithServiceOverride() throws Exception {
final String caller = "yarn";
Response renewalResponse = doTestTokenRenewal(false, true, caller, null, createTestSubject(caller)).getValue();
validateRenewalResponse(renewalResponse, 400, false, "Token renewal support is not configured");
Map.Entry<TestTokenStateService, Response> result =
doTestTokenRenewal(false, true, caller, null, createTestSubject(caller));

// Make sure the expiration was not recorded by the TokenStateService, since it is disabled for this test
TestTokenStateService tss = result.getKey();
assertEquals("TokenStateService should be disabled for this test.", 0, tss.expirationData.size());

Response renewalResponse = result.getValue();
validateSuccessfulRenewalResponse(renewalResponse);
String responseContent = (String) renewalResponse.getEntity();
assertNotNull(responseContent);
Map<String, String> json = parseJSONResponse(responseContent);
assertTrue(Boolean.parseBoolean(json.get("renewed")));
assertNotNull(json.get("expires")); // Should get back the original expiration from the token itself
}

@Test
public void testTokenRenewal_ServerManagedStateNotConfiguredAtAll() throws Exception {
Response renewalResponse = doTestTokenRenewal(null, null, null, null, null).getValue();
validateRenewalResponse(renewalResponse, 400, false, "Token renewal support is not configured");
Map.Entry<TestTokenStateService, Response> result = doTestTokenRenewal(null, null, null, null, null);

// Make sure the expiration was not recorded by the TokenStateService, since it is disabled for this test
TestTokenStateService tss = result.getKey();
assertEquals("TokenStateService should be disabled for this test.", 0, tss.expirationData.size());

Response renewalResponse = result.getValue();
validateSuccessfulRenewalResponse(renewalResponse);
String responseContent = (String) renewalResponse.getEntity();
assertNotNull(responseContent);
Map<String, String> json = parseJSONResponse(responseContent);
assertTrue(Boolean.parseBoolean(json.get("renewed")));
assertNotNull(json.get("expires")); // Should get back the original expiration from the token itself
}

@Test
public void testTokenRenewal_Disabled() throws Exception {
Response renewalResponse = doTestTokenRenewal(false, null, null);
validateRenewalResponse(renewalResponse, 400, false, "Token renewal support is not configured");
Map.Entry<TestTokenStateService, Response> result = doTestTokenRenewal(false, null, null, null);

// Make sure the expiration was not recorded by the TokenStateService, since it is disabled for this test
TestTokenStateService tss = result.getKey();
assertEquals("TokenStateService should be disabled for this test.", 0, tss.expirationData.size());

Response renewalResponse = result.getValue();
validateSuccessfulRenewalResponse(renewalResponse);
String responseContent = (String) renewalResponse.getEntity();
assertNotNull(responseContent);
Map<String, String> json = parseJSONResponse(responseContent);
assertTrue(Boolean.parseBoolean(json.get("renewed")));
assertNotNull(json.get("expires")); // Should get back the original expiration from the token itself
}

@Test
Expand Down