|
|
@@ -13,7 +13,6 @@ import org.apache.logging.log4j.Logger;
|
|
|
import org.apache.lucene.search.TotalHits;
|
|
|
import org.elasticsearch.ElasticsearchException;
|
|
|
import org.elasticsearch.TransportVersion;
|
|
|
-import org.elasticsearch.TransportVersions;
|
|
|
import org.elasticsearch.Version;
|
|
|
import org.elasticsearch.action.ActionListener;
|
|
|
import org.elasticsearch.action.DocWriteRequest;
|
|
|
@@ -3008,48 +3007,6 @@ public class ApiKeyServiceTests extends ESTestCase {
|
|
|
assertThat(e.getMessage(), containsString("authentication realm must be [_es_api_key]"));
|
|
|
}
|
|
|
|
|
|
- public void testMaybeRemoveRemoteIndicesPrivilegesWithUnsupportedVersion() {
|
|
|
- final String apiKeyId = randomAlphaOfLengthBetween(5, 8);
|
|
|
- final Set<RoleDescriptor> userRoleDescriptors = Set.copyOf(
|
|
|
- randomList(
|
|
|
- 2,
|
|
|
- 5,
|
|
|
- () -> RoleDescriptorTestHelper.builder()
|
|
|
- .allowReservedMetadata(randomBoolean())
|
|
|
- .allowRemoteIndices(randomBoolean())
|
|
|
- .allowRestriction(randomBoolean())
|
|
|
- .allowRemoteClusters(false)
|
|
|
- .build()
|
|
|
- )
|
|
|
- );
|
|
|
-
|
|
|
- // Selecting random unsupported version.
|
|
|
- final TransportVersion minTransportVersion = TransportVersionUtils.randomVersionBetween(
|
|
|
- random(),
|
|
|
- TransportVersions.MINIMUM_COMPATIBLE,
|
|
|
- TransportVersionUtils.getPreviousVersion(TRANSPORT_VERSION_ADVANCED_REMOTE_CLUSTER_SECURITY)
|
|
|
- );
|
|
|
-
|
|
|
- final Set<RoleDescriptor> result = ApiKeyService.maybeRemoveRemotePrivileges(userRoleDescriptors, minTransportVersion, apiKeyId);
|
|
|
- assertThat(result.stream().anyMatch(RoleDescriptor::hasRemoteIndicesPrivileges), equalTo(false));
|
|
|
- assertThat(result.size(), equalTo(userRoleDescriptors.size()));
|
|
|
-
|
|
|
- // Roles for which warning headers are added.
|
|
|
- final List<String> userRoleNamesWithRemoteIndicesPrivileges = userRoleDescriptors.stream()
|
|
|
- .filter(RoleDescriptor::hasRemoteIndicesPrivileges)
|
|
|
- .map(RoleDescriptor::getName)
|
|
|
- .sorted()
|
|
|
- .toList();
|
|
|
-
|
|
|
- if (false == userRoleNamesWithRemoteIndicesPrivileges.isEmpty()) {
|
|
|
- assertWarnings(
|
|
|
- "Removed API key's remote indices privileges from role(s) "
|
|
|
- + userRoleNamesWithRemoteIndicesPrivileges
|
|
|
- + ". Remote indices are not supported by all nodes in the cluster. "
|
|
|
- );
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
public void testMaybeRemoveRemoteClusterPrivilegesWithUnsupportedVersion() {
|
|
|
final String apiKeyId = randomAlphaOfLengthBetween(5, 8);
|
|
|
final Set<RoleDescriptor> userRoleDescriptors = Set.copyOf(
|
|
|
@@ -3124,52 +3081,6 @@ public class ApiKeyServiceTests extends ESTestCase {
|
|
|
assertThat(e.getMessage(), equalTo("limit must be positive number"));
|
|
|
}
|
|
|
|
|
|
- public void testCreateCrossClusterApiKeyMinVersionConstraint() {
|
|
|
- final Authentication authentication = randomValueOtherThanMany(
|
|
|
- Authentication::isApiKey,
|
|
|
- () -> AuthenticationTestHelper.builder().build()
|
|
|
- );
|
|
|
- final AbstractCreateApiKeyRequest request = mock(AbstractCreateApiKeyRequest.class);
|
|
|
- when(request.getType()).thenReturn(ApiKey.Type.CROSS_CLUSTER);
|
|
|
-
|
|
|
- final ClusterService clusterService = mock(ClusterService.class);
|
|
|
- when(clusterService.getClusterSettings()).thenReturn(
|
|
|
- new ClusterSettings(Settings.EMPTY, Set.of(ApiKeyService.DELETE_RETENTION_PERIOD, ApiKeyService.DELETE_INTERVAL))
|
|
|
- );
|
|
|
- final ClusterState clusterState = mock(ClusterState.class);
|
|
|
- when(clusterService.state()).thenReturn(clusterState);
|
|
|
- final TransportVersion minTransportVersion = TransportVersionUtils.randomVersionBetween(
|
|
|
- random(),
|
|
|
- TransportVersions.MINIMUM_COMPATIBLE,
|
|
|
- TransportVersionUtils.getPreviousVersion(TRANSPORT_VERSION_ADVANCED_REMOTE_CLUSTER_SECURITY)
|
|
|
- );
|
|
|
- when(clusterState.getMinTransportVersion()).thenReturn(minTransportVersion);
|
|
|
-
|
|
|
- final ApiKeyService service = new ApiKeyService(
|
|
|
- Settings.EMPTY,
|
|
|
- clock,
|
|
|
- client,
|
|
|
- securityIndex,
|
|
|
- clusterService,
|
|
|
- cacheInvalidatorRegistry,
|
|
|
- threadPool,
|
|
|
- MeterRegistry.NOOP
|
|
|
- );
|
|
|
-
|
|
|
- final PlainActionFuture<CreateApiKeyResponse> future = new PlainActionFuture<>();
|
|
|
- service.createApiKey(authentication, request, Set.of(), future);
|
|
|
- final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, future::actionGet);
|
|
|
-
|
|
|
- assertThat(
|
|
|
- e.getMessage(),
|
|
|
- containsString(
|
|
|
- "all nodes must have version ["
|
|
|
- + TRANSPORT_VERSION_ADVANCED_REMOTE_CLUSTER_SECURITY.toReleaseVersion()
|
|
|
- + "] or higher to support creating cross cluster API keys"
|
|
|
- )
|
|
|
- );
|
|
|
- }
|
|
|
-
|
|
|
public void testAuthenticationFailureWithApiKeyTypeMismatch() throws Exception {
|
|
|
final Settings settings = Settings.builder().put(XPackSettings.API_KEY_SERVICE_ENABLED_SETTING.getKey(), true).build();
|
|
|
final ApiKeyService service = spy(createApiKeyService(settings));
|
|
|
@@ -3268,73 +3179,6 @@ public class ApiKeyServiceTests extends ESTestCase {
|
|
|
assertThat(auth3.getMetadata(), hasEntry(API_KEY_TYPE_KEY, apiKeyDoc3.type.value()));
|
|
|
}
|
|
|
|
|
|
- public void testCreateOrUpdateApiKeyWithWorkflowsRestrictionForUnsupportedVersion() {
|
|
|
- final Authentication authentication = AuthenticationTestHelper.builder().build();
|
|
|
- final ClusterService clusterService = mock(ClusterService.class);
|
|
|
- when(clusterService.getClusterSettings()).thenReturn(
|
|
|
- new ClusterSettings(Settings.EMPTY, Set.of(ApiKeyService.DELETE_RETENTION_PERIOD, ApiKeyService.DELETE_INTERVAL))
|
|
|
- );
|
|
|
- final ClusterState clusterState = mock(ClusterState.class);
|
|
|
- when(clusterService.state()).thenReturn(clusterState);
|
|
|
- final TransportVersion minTransportVersion = TransportVersionUtils.randomVersionBetween(
|
|
|
- random(),
|
|
|
- TransportVersions.MINIMUM_COMPATIBLE,
|
|
|
- TransportVersionUtils.getPreviousVersion(WORKFLOWS_RESTRICTION_VERSION)
|
|
|
- );
|
|
|
- when(clusterState.getMinTransportVersion()).thenReturn(minTransportVersion);
|
|
|
-
|
|
|
- final ApiKeyService service = new ApiKeyService(
|
|
|
- Settings.EMPTY,
|
|
|
- clock,
|
|
|
- client,
|
|
|
- securityIndex,
|
|
|
- clusterService,
|
|
|
- cacheInvalidatorRegistry,
|
|
|
- threadPool,
|
|
|
- MeterRegistry.NOOP
|
|
|
- );
|
|
|
-
|
|
|
- final List<RoleDescriptor> roleDescriptorsWithWorkflowsRestriction = randomList(
|
|
|
- 1,
|
|
|
- 3,
|
|
|
- () -> randomRoleDescriptorWithWorkflowsRestriction()
|
|
|
- );
|
|
|
-
|
|
|
- final AbstractCreateApiKeyRequest createRequest = mock(AbstractCreateApiKeyRequest.class);
|
|
|
- when(createRequest.getType()).thenReturn(ApiKey.Type.REST);
|
|
|
- when(createRequest.getRoleDescriptors()).thenReturn(roleDescriptorsWithWorkflowsRestriction);
|
|
|
-
|
|
|
- final PlainActionFuture<CreateApiKeyResponse> createFuture = new PlainActionFuture<>();
|
|
|
- service.createApiKey(authentication, createRequest, Set.of(), createFuture);
|
|
|
- final IllegalArgumentException e1 = expectThrows(IllegalArgumentException.class, createFuture::actionGet);
|
|
|
- assertThat(
|
|
|
- e1.getMessage(),
|
|
|
- containsString(
|
|
|
- "all nodes must have version ["
|
|
|
- + WORKFLOWS_RESTRICTION_VERSION.toReleaseVersion()
|
|
|
- + "] or higher to support restrictions for API keys"
|
|
|
- )
|
|
|
- );
|
|
|
-
|
|
|
- final BulkUpdateApiKeyRequest updateRequest = new BulkUpdateApiKeyRequest(
|
|
|
- randomList(1, 3, () -> randomAlphaOfLengthBetween(3, 5)),
|
|
|
- roleDescriptorsWithWorkflowsRestriction,
|
|
|
- Map.of(),
|
|
|
- ApiKeyTests.randomFutureExpirationTime()
|
|
|
- );
|
|
|
- final PlainActionFuture<BulkUpdateApiKeyResponse> updateFuture = new PlainActionFuture<>();
|
|
|
- service.updateApiKeys(authentication, updateRequest, Set.of(), updateFuture);
|
|
|
- final IllegalArgumentException e2 = expectThrows(IllegalArgumentException.class, createFuture::actionGet);
|
|
|
- assertThat(
|
|
|
- e2.getMessage(),
|
|
|
- containsString(
|
|
|
- "all nodes must have version ["
|
|
|
- + WORKFLOWS_RESTRICTION_VERSION.toReleaseVersion()
|
|
|
- + "] or higher to support restrictions for API keys"
|
|
|
- )
|
|
|
- );
|
|
|
- }
|
|
|
-
|
|
|
public void testValidateOwnerUserRoleDescriptorsWithWorkflowsRestriction() {
|
|
|
final Authentication authentication = AuthenticationTestHelper.builder().build();
|
|
|
final ClusterService clusterService = mock(ClusterService.class);
|