Skip to content

Commit e23d757

Browse files
committed
Encapsulate the logic for endpoints access checking into a method
Signed-off-by: Ryan Liang <jiallian@amazon.com>
1 parent be26148 commit e23d757

3 files changed

Lines changed: 21 additions & 27 deletions

File tree

src/main/java/org/opensearch/security/action/onbehalf/CreateOnBehalfOfTokenAction.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,10 @@ public void accept(RestChannel channel) throws Exception {
110110
try {
111111
if (vendor == null) {
112112
channel.sendResponse(
113-
new BytesRestResponse(RestStatus.SERVICE_UNAVAILABLE, "on_behalf_of is either disabled or the configuration is invalid")
113+
new BytesRestResponse(
114+
RestStatus.SERVICE_UNAVAILABLE,
115+
"on_behalf_of is either disabled or the configuration is invalid"
116+
)
114117
);
115118
return;
116119
}

src/main/java/org/opensearch/security/http/OnBehalfOfAuthenticator.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,7 @@ private AuthCredentials extractCredentials0(final RestRequest request) {
181181
}
182182

183183
try {
184-
Matcher matcher = PATTERN_PATH_PREFIX.matcher(request.path());
185-
final String suffix = matcher.matches() ? matcher.group(2) : null;
186-
if (request.method() == RestRequest.Method.POST && ON_BEHALF_OF_SUFFIX.equals(suffix)
187-
|| request.method() == RestRequest.Method.PUT && ACCOUNT_SUFFIX.equals(suffix)) {
188-
final OpenSearchException exception = ExceptionUtils.invalidUsageOfOBOTokenException();
189-
log.error(exception.toString());
184+
if (!isAllowedRequest(request)) {
190185
return null;
191186
}
192187

@@ -234,6 +229,18 @@ private AuthCredentials extractCredentials0(final RestRequest request) {
234229
}
235230
}
236231

232+
public Boolean isAllowedRequest(final RestRequest request) {
233+
Matcher matcher = PATTERN_PATH_PREFIX.matcher(request.path());
234+
final String suffix = matcher.matches() ? matcher.group(2) : null;
235+
if (request.method() == RestRequest.Method.POST && ON_BEHALF_OF_SUFFIX.equals(suffix)
236+
|| request.method() == RestRequest.Method.PUT && ACCOUNT_SUFFIX.equals(suffix)) {
237+
final OpenSearchException exception = ExceptionUtils.invalidUsageOfOBOTokenException();
238+
log.error(exception.toString());
239+
return false;
240+
}
241+
return true;
242+
}
243+
237244
@Override
238245
public boolean reRequestAuthentication(final RestChannel channel, AuthCredentials creds) {
239246
return false;

src/test/java/org/opensearch/security/http/OnBehalfOfAuthenticatorTest.java

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -237,11 +237,7 @@ public void testRoles() throws Exception {
237237
final AuthCredentials credentials = extractCredentialsFromJwtHeader(
238238
signingKeyB64Encoded,
239239
claimsEncryptionKey,
240-
Jwts.builder()
241-
.setIssuer(clusterNameString)
242-
.setSubject("Leonard McCoy")
243-
.claim("dr", "role1,role2")
244-
.setAudience("svc1"),
240+
Jwts.builder().setIssuer(clusterNameString).setSubject("Leonard McCoy").claim("dr", "role1,role2").setAudience("svc1"),
245241
true
246242
);
247243

@@ -257,11 +253,7 @@ public void testNullClaim() throws Exception {
257253
final AuthCredentials credentials = extractCredentialsFromJwtHeader(
258254
signingKeyB64Encoded,
259255
claimsEncryptionKey,
260-
Jwts.builder()
261-
.setIssuer(clusterNameString)
262-
.setSubject("Leonard McCoy")
263-
.claim("dr", null)
264-
.setAudience("svc1"),
256+
Jwts.builder().setIssuer(clusterNameString).setSubject("Leonard McCoy").claim("dr", null).setAudience("svc1"),
265257
false
266258
);
267259

@@ -276,11 +268,7 @@ public void testNonStringClaim() throws Exception {
276268
final AuthCredentials credentials = extractCredentialsFromJwtHeader(
277269
signingKeyB64Encoded,
278270
claimsEncryptionKey,
279-
Jwts.builder()
280-
.setIssuer(clusterNameString)
281-
.setSubject("Leonard McCoy")
282-
.claim("dr", 123L)
283-
.setAudience("svc1"),
271+
Jwts.builder().setIssuer(clusterNameString).setSubject("Leonard McCoy").claim("dr", 123L).setAudience("svc1"),
284272
true
285273
);
286274

@@ -312,11 +300,7 @@ public void testWrongSubjectKey() throws Exception {
312300
final AuthCredentials credentials = extractCredentialsFromJwtHeader(
313301
signingKeyB64Encoded,
314302
claimsEncryptionKey,
315-
Jwts.builder()
316-
.setIssuer(clusterNameString)
317-
.claim("roles", "role1,role2")
318-
.claim("asub", "Dr. Who")
319-
.setAudience("svc1"),
303+
Jwts.builder().setIssuer(clusterNameString).claim("roles", "role1,role2").claim("asub", "Dr. Who").setAudience("svc1"),
320304
false
321305
);
322306

0 commit comments

Comments
 (0)