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.
Clean up issuing, encrypting, and decrypting logic, add tests for the…
… feature Signed-off-by: Derek Ho <[email protected]>
- Loading branch information
Showing
8 changed files
with
440 additions
and
16 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
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
121 changes: 121 additions & 0 deletions
121
src/test/java/org/opensearch/security/action/apitokens/ApiTokenRepositoryTest.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,121 @@ | ||
/* | ||
* 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.action.apitokens; | ||
|
||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import org.opensearch.index.IndexNotFoundException; | ||
import org.opensearch.security.authtoken.jwt.ExpiringBearerAuthToken; | ||
import org.opensearch.security.identity.SecurityTokenManager; | ||
|
||
import org.mockito.Mock; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.mockito.Mockito.any; | ||
import static org.mockito.Mockito.argThat; | ||
import static org.mockito.Mockito.doThrow; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class ApiTokenRepositoryTest { | ||
@Mock | ||
private SecurityTokenManager securityTokenManager; | ||
|
||
@Mock | ||
private ApiTokenIndexHandler apiTokenIndexHandler; | ||
|
||
private ApiTokenRepository repository; | ||
|
||
@Before | ||
public void setUp() { | ||
apiTokenIndexHandler = mock(ApiTokenIndexHandler.class); | ||
securityTokenManager = mock(SecurityTokenManager.class); | ||
repository = new ApiTokenRepository(apiTokenIndexHandler, securityTokenManager); | ||
} | ||
|
||
@Test | ||
public void testDeleteApiToken() throws ApiTokenException { | ||
String tokenName = "test-token"; | ||
|
||
repository.deleteApiToken(tokenName); | ||
|
||
verify(apiTokenIndexHandler).deleteToken(tokenName); | ||
} | ||
|
||
@Test | ||
public void testGetApiTokens() throws IndexNotFoundException { | ||
Map<String, ApiToken> expectedTokens = new HashMap<>(); | ||
expectedTokens.put("token1", new ApiToken("token1", Arrays.asList("perm1"), Arrays.asList(), Long.MAX_VALUE)); | ||
when(apiTokenIndexHandler.getTokenMetadatas()).thenReturn(expectedTokens); | ||
|
||
Map<String, ApiToken> result = repository.getApiTokens(); | ||
|
||
assertThat(result, equalTo(expectedTokens)); | ||
verify(apiTokenIndexHandler).getTokenMetadatas(); | ||
} | ||
|
||
@Test | ||
public void testCreateApiToken() { | ||
String tokenName = "test-token"; | ||
List<String> clusterPermissions = Arrays.asList("cluster:admin"); | ||
List<ApiToken.IndexPermission> indexPermissions = Arrays.asList( | ||
new ApiToken.IndexPermission(Arrays.asList("test-*"), Arrays.asList("read")) | ||
); | ||
Long expiration = 3600L; | ||
|
||
String completeToken = "complete-token"; | ||
String encryptedToken = "encrypted-token"; | ||
ExpiringBearerAuthToken bearerToken = mock(ExpiringBearerAuthToken.class); | ||
when(bearerToken.getCompleteToken()).thenReturn(completeToken); | ||
when(securityTokenManager.issueApiToken(any())).thenReturn(bearerToken); | ||
when(securityTokenManager.encryptToken(completeToken)).thenReturn(encryptedToken); | ||
|
||
String result = repository.createApiToken(tokenName, clusterPermissions, indexPermissions, expiration); | ||
|
||
verify(apiTokenIndexHandler).createApiTokenIndexIfAbsent(); | ||
verify(securityTokenManager).issueApiToken(any(ApiToken.class)); | ||
verify(securityTokenManager).encryptToken(completeToken); | ||
verify(apiTokenIndexHandler).indexTokenMetadata( | ||
argThat( | ||
token -> token.getName().equals(tokenName) | ||
&& token.getJti().equals(encryptedToken) | ||
&& token.getClusterPermissions().equals(clusterPermissions) | ||
&& token.getIndexPermissions().equals(indexPermissions) | ||
) | ||
); | ||
assertThat(result, equalTo(completeToken)); | ||
} | ||
|
||
@Test(expected = IndexNotFoundException.class) | ||
public void testGetApiTokensThrowsIndexNotFoundException() throws IndexNotFoundException { | ||
when(apiTokenIndexHandler.getTokenMetadatas()).thenThrow(new IndexNotFoundException("test-index")); | ||
|
||
repository.getApiTokens(); | ||
|
||
} | ||
|
||
@Test(expected = ApiTokenException.class) | ||
public void testDeleteApiTokenThrowsApiTokenException() throws ApiTokenException { | ||
String tokenName = "test-token"; | ||
doThrow(new ApiTokenException("Token not found")).when(apiTokenIndexHandler).deleteToken(tokenName); | ||
|
||
repository.deleteApiToken(tokenName); | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
src/test/java/org/opensearch/security/action/apitokens/ApiTokenTest.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,83 @@ | ||
/* | ||
* 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.action.apitokens; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import org.opensearch.client.Client; | ||
import org.opensearch.client.IndicesAdminClient; | ||
import org.opensearch.cluster.metadata.Metadata; | ||
import org.opensearch.cluster.service.ClusterService; | ||
import org.opensearch.common.settings.Settings; | ||
import org.opensearch.common.util.concurrent.ThreadContext; | ||
|
||
import org.mockito.Mock; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class ApiTokenTest { | ||
|
||
@Mock | ||
private Client client; | ||
|
||
@Mock | ||
private IndicesAdminClient indicesAdminClient; | ||
|
||
@Mock | ||
private ClusterService clusterService; | ||
|
||
@Mock | ||
private Metadata metadata; | ||
|
||
private ApiTokenIndexHandler indexHandler; | ||
|
||
@Before | ||
public void setup() { | ||
|
||
client = mock(Client.class, RETURNS_DEEP_STUBS); | ||
indicesAdminClient = mock(IndicesAdminClient.class); | ||
clusterService = mock(ClusterService.class, RETURNS_DEEP_STUBS); | ||
metadata = mock(Metadata.class); | ||
|
||
when(client.admin().indices()).thenReturn(indicesAdminClient); | ||
|
||
when(clusterService.state().metadata()).thenReturn(metadata); | ||
|
||
ThreadContext threadContext = new ThreadContext(Settings.EMPTY); | ||
when(client.threadPool().getThreadContext()).thenReturn(threadContext); | ||
|
||
indexHandler = new ApiTokenIndexHandler(client, clusterService); | ||
} | ||
|
||
@Test | ||
public void testIndexPermissionToStringFromString() { | ||
String indexPermissionString = "{\"index_pattern\":[\"index1\",\"index2\"],\"allowed_actions\":[\"action1\",\"action2\"]}"; | ||
ApiToken.IndexPermission indexPermission = new ApiToken.IndexPermission( | ||
Arrays.asList("index1", "index2"), | ||
Arrays.asList("action1", "action2") | ||
); | ||
assertThat(indexPermission.toString(), equalTo(indexPermissionString)); | ||
|
||
ApiToken.IndexPermission indexPermissionFromString = ApiToken.IndexPermission.fromString(indexPermissionString); | ||
assertThat(indexPermissionFromString.getIndexPatterns(), equalTo(List.of("index1", "index2"))); | ||
assertThat(indexPermissionFromString.getAllowedActions(), equalTo(List.of("action1", "action2"))); | ||
} | ||
|
||
} |
Oops, something went wrong.