|
@@ -50,7 +50,6 @@ import org.elasticsearch.xpack.core.security.authc.Authentication.RealmRef;
|
|
|
import org.elasticsearch.xpack.core.security.authc.TokenMetaData;
|
|
|
import org.elasticsearch.xpack.core.security.user.User;
|
|
|
import org.elasticsearch.xpack.core.watcher.watch.ClockMock;
|
|
|
-import org.elasticsearch.xpack.security.SecurityLifecycleService;
|
|
|
import org.elasticsearch.xpack.security.support.SecurityIndexManager;
|
|
|
import org.junit.AfterClass;
|
|
|
import org.junit.Before;
|
|
@@ -86,7 +85,6 @@ public class TokenServiceTests extends ESTestCase {
|
|
|
.put(XPackSettings.TOKEN_SERVICE_ENABLED_SETTING.getKey(), true).build();
|
|
|
|
|
|
private Client client;
|
|
|
- private SecurityLifecycleService lifecycleService;
|
|
|
private SecurityIndexManager securityIndex;
|
|
|
private ClusterService clusterService;
|
|
|
private Settings tokenServiceEnabledSettings = Settings.builder()
|
|
@@ -132,9 +130,7 @@ public class TokenServiceTests extends ESTestCase {
|
|
|
}).when(client).execute(eq(IndexAction.INSTANCE), any(IndexRequest.class), any(ActionListener.class));
|
|
|
|
|
|
// setup lifecycle service
|
|
|
- lifecycleService = mock(SecurityLifecycleService.class);
|
|
|
securityIndex = mock(SecurityIndexManager.class);
|
|
|
- when(lifecycleService.securityIndex()).thenReturn(securityIndex);
|
|
|
doAnswer(invocationOnMock -> {
|
|
|
Runnable runnable = (Runnable) invocationOnMock.getArguments()[1];
|
|
|
runnable.run();
|
|
@@ -157,7 +153,7 @@ public class TokenServiceTests extends ESTestCase {
|
|
|
}
|
|
|
|
|
|
public void testAttachAndGetToken() throws Exception {
|
|
|
- TokenService tokenService = new TokenService(tokenServiceEnabledSettings, systemUTC(), client, lifecycleService, clusterService);
|
|
|
+ TokenService tokenService = new TokenService(tokenServiceEnabledSettings, systemUTC(), client, securityIndex, clusterService);
|
|
|
Authentication authentication = new Authentication(new User("joe", "admin"), new RealmRef("native_realm", "native", "node1"), null);
|
|
|
PlainActionFuture<Tuple<UserToken, String>> tokenFuture = new PlainActionFuture<>();
|
|
|
tokenService.createUserToken(authentication, authentication, tokenFuture, Collections.emptyMap());
|
|
@@ -177,7 +173,7 @@ public class TokenServiceTests extends ESTestCase {
|
|
|
|
|
|
try (ThreadContext.StoredContext ignore = requestContext.newStoredContext(true)) {
|
|
|
// verify a second separate token service with its own salt can also verify
|
|
|
- TokenService anotherService = new TokenService(tokenServiceEnabledSettings, systemUTC(), client, lifecycleService
|
|
|
+ TokenService anotherService = new TokenService(tokenServiceEnabledSettings, systemUTC(), client, securityIndex
|
|
|
, clusterService);
|
|
|
anotherService.refreshMetaData(tokenService.getTokenMetaData());
|
|
|
PlainActionFuture<UserToken> future = new PlainActionFuture<>();
|
|
@@ -188,7 +184,7 @@ public class TokenServiceTests extends ESTestCase {
|
|
|
}
|
|
|
|
|
|
public void testRotateKey() throws Exception {
|
|
|
- TokenService tokenService = new TokenService(tokenServiceEnabledSettings, systemUTC(), client, lifecycleService, clusterService);
|
|
|
+ TokenService tokenService = new TokenService(tokenServiceEnabledSettings, systemUTC(), client, securityIndex, clusterService);
|
|
|
Authentication authentication = new Authentication(new User("joe", "admin"), new RealmRef("native_realm", "native", "node1"), null);
|
|
|
PlainActionFuture<Tuple<UserToken, String>> tokenFuture = new PlainActionFuture<>();
|
|
|
tokenService.createUserToken(authentication, authentication, tokenFuture, Collections.emptyMap());
|
|
@@ -240,12 +236,12 @@ public class TokenServiceTests extends ESTestCase {
|
|
|
}
|
|
|
|
|
|
public void testKeyExchange() throws Exception {
|
|
|
- TokenService tokenService = new TokenService(tokenServiceEnabledSettings, systemUTC(), client, lifecycleService, clusterService);
|
|
|
+ TokenService tokenService = new TokenService(tokenServiceEnabledSettings, systemUTC(), client, securityIndex, clusterService);
|
|
|
int numRotations = 0;randomIntBetween(1, 5);
|
|
|
for (int i = 0; i < numRotations; i++) {
|
|
|
rotateKeys(tokenService);
|
|
|
}
|
|
|
- TokenService otherTokenService = new TokenService(tokenServiceEnabledSettings, systemUTC(), client, lifecycleService,
|
|
|
+ TokenService otherTokenService = new TokenService(tokenServiceEnabledSettings, systemUTC(), client, securityIndex,
|
|
|
clusterService);
|
|
|
otherTokenService.refreshMetaData(tokenService.getTokenMetaData());
|
|
|
Authentication authentication = new Authentication(new User("joe", "admin"), new RealmRef("native_realm", "native", "node1"), null);
|
|
@@ -277,7 +273,7 @@ public class TokenServiceTests extends ESTestCase {
|
|
|
}
|
|
|
|
|
|
public void testPruneKeys() throws Exception {
|
|
|
- TokenService tokenService = new TokenService(tokenServiceEnabledSettings, systemUTC(), client, lifecycleService, clusterService);
|
|
|
+ TokenService tokenService = new TokenService(tokenServiceEnabledSettings, systemUTC(), client, securityIndex, clusterService);
|
|
|
Authentication authentication = new Authentication(new User("joe", "admin"), new RealmRef("native_realm", "native", "node1"), null);
|
|
|
PlainActionFuture<Tuple<UserToken, String>> tokenFuture = new PlainActionFuture<>();
|
|
|
tokenService.createUserToken(authentication, authentication, tokenFuture, Collections.emptyMap());
|
|
@@ -338,7 +334,7 @@ public class TokenServiceTests extends ESTestCase {
|
|
|
}
|
|
|
|
|
|
public void testPassphraseWorks() throws Exception {
|
|
|
- TokenService tokenService = new TokenService(tokenServiceEnabledSettings, systemUTC(), client, lifecycleService, clusterService);
|
|
|
+ TokenService tokenService = new TokenService(tokenServiceEnabledSettings, systemUTC(), client, securityIndex, clusterService);
|
|
|
Authentication authentication = new Authentication(new User("joe", "admin"), new RealmRef("native_realm", "native", "node1"), null);
|
|
|
PlainActionFuture<Tuple<UserToken, String>> tokenFuture = new PlainActionFuture<>();
|
|
|
tokenService.createUserToken(authentication, authentication, tokenFuture, Collections.emptyMap());
|
|
@@ -358,7 +354,7 @@ public class TokenServiceTests extends ESTestCase {
|
|
|
|
|
|
try (ThreadContext.StoredContext ignore = requestContext.newStoredContext(true)) {
|
|
|
// verify a second separate token service with its own passphrase cannot verify
|
|
|
- TokenService anotherService = new TokenService(Settings.EMPTY, systemUTC(), client, lifecycleService,
|
|
|
+ TokenService anotherService = new TokenService(Settings.EMPTY, systemUTC(), client, securityIndex,
|
|
|
clusterService);
|
|
|
PlainActionFuture<UserToken> future = new PlainActionFuture<>();
|
|
|
anotherService.getAndValidateToken(requestContext, future);
|
|
@@ -367,7 +363,7 @@ public class TokenServiceTests extends ESTestCase {
|
|
|
}
|
|
|
|
|
|
public void testGetTokenWhenKeyCacheHasExpired() throws Exception {
|
|
|
- TokenService tokenService = new TokenService(tokenServiceEnabledSettings, systemUTC(), client, lifecycleService, clusterService);
|
|
|
+ TokenService tokenService = new TokenService(tokenServiceEnabledSettings, systemUTC(), client, securityIndex, clusterService);
|
|
|
Authentication authentication = new Authentication(new User("joe", "admin"), new RealmRef("native_realm", "native", "node1"), null);
|
|
|
|
|
|
PlainActionFuture<Tuple<UserToken, String>> tokenFuture = new PlainActionFuture<>();
|
|
@@ -382,7 +378,7 @@ public class TokenServiceTests extends ESTestCase {
|
|
|
public void testInvalidatedToken() throws Exception {
|
|
|
when(securityIndex.indexExists()).thenReturn(true);
|
|
|
TokenService tokenService =
|
|
|
- new TokenService(tokenServiceEnabledSettings, systemUTC(), client, lifecycleService, clusterService);
|
|
|
+ new TokenService(tokenServiceEnabledSettings, systemUTC(), client, securityIndex, clusterService);
|
|
|
Authentication authentication = new Authentication(new User("joe", "admin"), new RealmRef("native_realm", "native", "node1"), null);
|
|
|
PlainActionFuture<Tuple<UserToken, String>> tokenFuture = new PlainActionFuture<>();
|
|
|
tokenService.createUserToken(authentication, authentication, tokenFuture, Collections.emptyMap());
|
|
@@ -436,7 +432,7 @@ public class TokenServiceTests extends ESTestCase {
|
|
|
|
|
|
public void testTokenExpiry() throws Exception {
|
|
|
ClockMock clock = ClockMock.frozen();
|
|
|
- TokenService tokenService = new TokenService(tokenServiceEnabledSettings, clock, client, lifecycleService, clusterService);
|
|
|
+ TokenService tokenService = new TokenService(tokenServiceEnabledSettings, clock, client, securityIndex, clusterService);
|
|
|
Authentication authentication = new Authentication(new User("joe", "admin"), new RealmRef("native_realm", "native", "node1"), null);
|
|
|
PlainActionFuture<Tuple<UserToken, String>> tokenFuture = new PlainActionFuture<>();
|
|
|
tokenService.createUserToken(authentication, authentication, tokenFuture, Collections.emptyMap());
|
|
@@ -488,7 +484,7 @@ public class TokenServiceTests extends ESTestCase {
|
|
|
TokenService tokenService = new TokenService(Settings.builder()
|
|
|
.put(XPackSettings.TOKEN_SERVICE_ENABLED_SETTING.getKey(), false)
|
|
|
.build(),
|
|
|
- Clock.systemUTC(), client, lifecycleService, clusterService);
|
|
|
+ Clock.systemUTC(), client, securityIndex, clusterService);
|
|
|
IllegalStateException e = expectThrows(IllegalStateException.class, () -> tokenService.createUserToken(null, null, null, null));
|
|
|
assertEquals("tokens are not enabled", e.getMessage());
|
|
|
|
|
@@ -530,7 +526,7 @@ public class TokenServiceTests extends ESTestCase {
|
|
|
final int numBytes = randomIntBetween(1, TokenService.MINIMUM_BYTES + 32);
|
|
|
final byte[] randomBytes = new byte[numBytes];
|
|
|
random().nextBytes(randomBytes);
|
|
|
- TokenService tokenService = new TokenService(Settings.EMPTY, systemUTC(), client, lifecycleService, clusterService);
|
|
|
+ TokenService tokenService = new TokenService(Settings.EMPTY, systemUTC(), client, securityIndex, clusterService);
|
|
|
|
|
|
ThreadContext requestContext = new ThreadContext(Settings.EMPTY);
|
|
|
requestContext.putHeader("Authorization", "Bearer " + Base64.getEncoder().encodeToString(randomBytes));
|
|
@@ -544,7 +540,7 @@ public class TokenServiceTests extends ESTestCase {
|
|
|
|
|
|
public void testIndexNotAvailable() throws Exception {
|
|
|
TokenService tokenService =
|
|
|
- new TokenService(tokenServiceEnabledSettings, systemUTC(), client, lifecycleService, clusterService);
|
|
|
+ new TokenService(tokenServiceEnabledSettings, systemUTC(), client, securityIndex, clusterService);
|
|
|
Authentication authentication = new Authentication(new User("joe", "admin"), new RealmRef("native_realm", "native", "node1"), null);
|
|
|
PlainActionFuture<Tuple<UserToken, String>> tokenFuture = new PlainActionFuture<>();
|
|
|
tokenService.createUserToken(authentication, authentication, tokenFuture, Collections.emptyMap());
|
|
@@ -577,7 +573,7 @@ public class TokenServiceTests extends ESTestCase {
|
|
|
|
|
|
public void testGetAuthenticationWorksWithExpiredToken() throws Exception {
|
|
|
TokenService tokenService =
|
|
|
- new TokenService(tokenServiceEnabledSettings, Clock.systemUTC(), client, lifecycleService, clusterService);
|
|
|
+ new TokenService(tokenServiceEnabledSettings, Clock.systemUTC(), client, securityIndex, clusterService);
|
|
|
Authentication authentication = new Authentication(new User("joe", "admin"), new RealmRef("native_realm", "native", "node1"), null);
|
|
|
UserToken expired = new UserToken(authentication, Instant.now().minus(3L, ChronoUnit.DAYS));
|
|
|
mockGetTokenFromId(expired);
|