forked from opensearch-project/security
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Enhancement] Setup auth token utils for obo (opensearch-project#3419)
Setup auth token utils for obo (opensearch-project#3419) --------- Signed-off-by: Ryan Liang <[email protected]>
- Loading branch information
Showing
5 changed files
with
129 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/main/java/org/opensearch/security/util/AuthTokenUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
package org.opensearch.security.util; | ||
|
||
import org.opensearch.common.settings.Settings; | ||
import org.opensearch.rest.RestRequest; | ||
|
||
import static org.opensearch.rest.RestRequest.Method.POST; | ||
import static org.opensearch.rest.RestRequest.Method.PUT; | ||
|
||
public class AuthTokenUtils { | ||
private static final String ON_BEHALF_OF_SUFFIX = "api/generateonbehalfoftoken"; | ||
private static final String ACCOUNT_SUFFIX = "api/account"; | ||
|
||
public static Boolean isAccessToRestrictedEndpoints(final RestRequest request, final String suffix) { | ||
if (suffix == null) { | ||
return false; | ||
} | ||
switch (suffix) { | ||
case ON_BEHALF_OF_SUFFIX: | ||
return request.method() == POST; | ||
case ACCOUNT_SUFFIX: | ||
return request.method() == PUT; | ||
default: | ||
return false; | ||
} | ||
} | ||
|
||
public static Boolean isKeyNull(Settings settings, String key) { | ||
return settings.get(key) == null; | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
src/test/java/org/opensearch/security/authtoken/jwt/AuthTokenUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
package org.opensearch.security.authtoken.jwt; | ||
|
||
import org.opensearch.common.settings.Settings; | ||
import org.opensearch.core.xcontent.NamedXContentRegistry; | ||
import org.opensearch.rest.RestRequest; | ||
import org.opensearch.security.util.AuthTokenUtils; | ||
import org.opensearch.test.rest.FakeRestRequest; | ||
import org.junit.Test; | ||
|
||
import java.util.Collections; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
public class AuthTokenUtilsTest { | ||
|
||
@Test | ||
public void testIsAccessToRestrictedEndpointsForOnBehalfOfToken() { | ||
NamedXContentRegistry namedXContentRegistry = new NamedXContentRegistry(Collections.emptyList()); | ||
|
||
FakeRestRequest request = new FakeRestRequest.Builder(namedXContentRegistry).withPath("/api/generateonbehalfoftoken") | ||
.withMethod(RestRequest.Method.POST) | ||
.build(); | ||
|
||
assertTrue(AuthTokenUtils.isAccessToRestrictedEndpoints(request, "api/generateonbehalfoftoken")); | ||
} | ||
|
||
@Test | ||
public void testIsAccessToRestrictedEndpointsForAccount() { | ||
NamedXContentRegistry namedXContentRegistry = new NamedXContentRegistry(Collections.emptyList()); | ||
|
||
FakeRestRequest request = new FakeRestRequest.Builder(namedXContentRegistry).withPath("/api/account") | ||
.withMethod(RestRequest.Method.PUT) | ||
.build(); | ||
|
||
assertTrue(AuthTokenUtils.isAccessToRestrictedEndpoints(request, "api/account")); | ||
} | ||
|
||
@Test | ||
public void testIsAccessToRestrictedEndpointsFalseCase() { | ||
NamedXContentRegistry namedXContentRegistry = new NamedXContentRegistry(Collections.emptyList()); | ||
|
||
FakeRestRequest request = new FakeRestRequest.Builder(namedXContentRegistry).withPath("/api/someotherendpoint") | ||
.withMethod(RestRequest.Method.GET) | ||
.build(); | ||
|
||
assertFalse(AuthTokenUtils.isAccessToRestrictedEndpoints(request, "api/someotherendpoint")); | ||
} | ||
|
||
@Test | ||
public void testIsKeyNullWithNullValue() { | ||
Settings settings = Settings.builder().put("someKey", (String) null).build(); | ||
assertTrue(AuthTokenUtils.isKeyNull(settings, "someKey")); | ||
} | ||
|
||
@Test | ||
public void testIsKeyNullWithNonNullValue() { | ||
Settings settings = Settings.builder().put("someKey", "value").build(); | ||
assertFalse(AuthTokenUtils.isKeyNull(settings, "someKey")); | ||
} | ||
|
||
@Test | ||
public void testIsKeyNullWithAbsentKey() { | ||
Settings settings = Settings.builder().build(); | ||
assertTrue(AuthTokenUtils.isKeyNull(settings, "absentKey")); | ||
} | ||
} |