Skip to content

Commit

Permalink
Encapsulate the logic for endpoints access checking into a method
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Liang <[email protected]>
  • Loading branch information
RyanL1997 committed Aug 22, 2023
1 parent be26148 commit e23d757
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@ public void accept(RestChannel channel) throws Exception {
try {
if (vendor == null) {
channel.sendResponse(
new BytesRestResponse(RestStatus.SERVICE_UNAVAILABLE, "on_behalf_of is either disabled or the configuration is invalid")
new BytesRestResponse(
RestStatus.SERVICE_UNAVAILABLE,
"on_behalf_of is either disabled or the configuration is invalid"
)
);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,7 @@ private AuthCredentials extractCredentials0(final RestRequest request) {
}

try {
Matcher matcher = PATTERN_PATH_PREFIX.matcher(request.path());
final String suffix = matcher.matches() ? matcher.group(2) : null;
if (request.method() == RestRequest.Method.POST && ON_BEHALF_OF_SUFFIX.equals(suffix)
|| request.method() == RestRequest.Method.PUT && ACCOUNT_SUFFIX.equals(suffix)) {
final OpenSearchException exception = ExceptionUtils.invalidUsageOfOBOTokenException();
log.error(exception.toString());
if (!isAllowedRequest(request)) {
return null;
}

Expand Down Expand Up @@ -234,6 +229,18 @@ private AuthCredentials extractCredentials0(final RestRequest request) {
}
}

public Boolean isAllowedRequest(final RestRequest request) {
Matcher matcher = PATTERN_PATH_PREFIX.matcher(request.path());
final String suffix = matcher.matches() ? matcher.group(2) : null;
if (request.method() == RestRequest.Method.POST && ON_BEHALF_OF_SUFFIX.equals(suffix)
|| request.method() == RestRequest.Method.PUT && ACCOUNT_SUFFIX.equals(suffix)) {
final OpenSearchException exception = ExceptionUtils.invalidUsageOfOBOTokenException();
log.error(exception.toString());
return false;
}
return true;
}

@Override
public boolean reRequestAuthentication(final RestChannel channel, AuthCredentials creds) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,7 @@ public void testRoles() throws Exception {
final AuthCredentials credentials = extractCredentialsFromJwtHeader(
signingKeyB64Encoded,
claimsEncryptionKey,
Jwts.builder()
.setIssuer(clusterNameString)
.setSubject("Leonard McCoy")
.claim("dr", "role1,role2")
.setAudience("svc1"),
Jwts.builder().setIssuer(clusterNameString).setSubject("Leonard McCoy").claim("dr", "role1,role2").setAudience("svc1"),
true
);

Expand All @@ -257,11 +253,7 @@ public void testNullClaim() throws Exception {
final AuthCredentials credentials = extractCredentialsFromJwtHeader(
signingKeyB64Encoded,
claimsEncryptionKey,
Jwts.builder()
.setIssuer(clusterNameString)
.setSubject("Leonard McCoy")
.claim("dr", null)
.setAudience("svc1"),
Jwts.builder().setIssuer(clusterNameString).setSubject("Leonard McCoy").claim("dr", null).setAudience("svc1"),
false
);

Expand All @@ -276,11 +268,7 @@ public void testNonStringClaim() throws Exception {
final AuthCredentials credentials = extractCredentialsFromJwtHeader(
signingKeyB64Encoded,
claimsEncryptionKey,
Jwts.builder()
.setIssuer(clusterNameString)
.setSubject("Leonard McCoy")
.claim("dr", 123L)
.setAudience("svc1"),
Jwts.builder().setIssuer(clusterNameString).setSubject("Leonard McCoy").claim("dr", 123L).setAudience("svc1"),
true
);

Expand Down Expand Up @@ -312,11 +300,7 @@ public void testWrongSubjectKey() throws Exception {
final AuthCredentials credentials = extractCredentialsFromJwtHeader(
signingKeyB64Encoded,
claimsEncryptionKey,
Jwts.builder()
.setIssuer(clusterNameString)
.claim("roles", "role1,role2")
.claim("asub", "Dr. Who")
.setAudience("svc1"),
Jwts.builder().setIssuer(clusterNameString).claim("roles", "role1,role2").claim("asub", "Dr. Who").setAudience("svc1"),
false
);

Expand Down

0 comments on commit e23d757

Please sign in to comment.