|
@@ -11,14 +11,19 @@ import org.elasticsearch.common.ValidationException;
|
|
import org.elasticsearch.test.ESTestCase;
|
|
import org.elasticsearch.test.ESTestCase;
|
|
import org.elasticsearch.xpack.core.security.action.rolemapping.PutRoleMappingRequest;
|
|
import org.elasticsearch.xpack.core.security.action.rolemapping.PutRoleMappingRequest;
|
|
import org.elasticsearch.xpack.core.security.action.rolemapping.PutRoleMappingRequestBuilder;
|
|
import org.elasticsearch.xpack.core.security.action.rolemapping.PutRoleMappingRequestBuilder;
|
|
|
|
+import org.elasticsearch.xpack.core.security.authc.support.mapper.ExpressionRoleMapping;
|
|
import org.elasticsearch.xpack.core.security.authc.support.mapper.expressiondsl.RoleMapperExpression;
|
|
import org.elasticsearch.xpack.core.security.authc.support.mapper.expressiondsl.RoleMapperExpression;
|
|
import org.junit.Before;
|
|
import org.junit.Before;
|
|
import org.mockito.Mockito;
|
|
import org.mockito.Mockito;
|
|
|
|
|
|
import java.util.Collections;
|
|
import java.util.Collections;
|
|
|
|
+import java.util.Map;
|
|
|
|
|
|
|
|
+import static org.elasticsearch.xpack.core.security.authc.support.mapper.ExpressionRoleMapping.READ_ONLY_ROLE_MAPPING_METADATA_FLAG;
|
|
import static org.hamcrest.Matchers.containsString;
|
|
import static org.hamcrest.Matchers.containsString;
|
|
|
|
+import static org.hamcrest.Matchers.is;
|
|
import static org.hamcrest.Matchers.notNullValue;
|
|
import static org.hamcrest.Matchers.notNullValue;
|
|
|
|
+import static org.hamcrest.Matchers.nullValue;
|
|
|
|
|
|
public class PutRoleMappingRequestTests extends ESTestCase {
|
|
public class PutRoleMappingRequestTests extends ESTestCase {
|
|
|
|
|
|
@@ -54,6 +59,62 @@ public class PutRoleMappingRequestTests extends ESTestCase {
|
|
assertValidationFailure(request, "metadata key");
|
|
assertValidationFailure(request, "metadata key");
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ public void testValidateReadyOnlyMetadataKey() {
|
|
|
|
+ assertValidationFailure(
|
|
|
|
+ builder.name("test")
|
|
|
|
+ .roles("superuser")
|
|
|
|
+ .expression(Mockito.mock(RoleMapperExpression.class))
|
|
|
|
+ .metadata(Map.of("_secret", false, ExpressionRoleMapping.READ_ONLY_ROLE_MAPPING_METADATA_FLAG, true))
|
|
|
|
+ .request(),
|
|
|
|
+ "metadata contains ["
|
|
|
|
+ + READ_ONLY_ROLE_MAPPING_METADATA_FLAG
|
|
|
|
+ + "] flag. You cannot create or update role-mappings with a read-only flag"
|
|
|
|
+ );
|
|
|
|
+
|
|
|
|
+ assertValidationFailure(
|
|
|
|
+ builder.name("test")
|
|
|
|
+ .roles("superuser")
|
|
|
|
+ .expression(Mockito.mock(RoleMapperExpression.class))
|
|
|
|
+ .metadata(Map.of(ExpressionRoleMapping.READ_ONLY_ROLE_MAPPING_METADATA_FLAG, true))
|
|
|
|
+ .request(),
|
|
|
|
+ "metadata contains ["
|
|
|
|
+ + READ_ONLY_ROLE_MAPPING_METADATA_FLAG
|
|
|
|
+ + "] flag. You cannot create or update role-mappings with a read-only flag"
|
|
|
|
+ );
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void testValidateMetadataKeySkipped() {
|
|
|
|
+ assertThat(
|
|
|
|
+ builder.name("test")
|
|
|
|
+ .roles("superuser")
|
|
|
|
+ .expression(Mockito.mock(RoleMapperExpression.class))
|
|
|
|
+ .metadata(Map.of("_secret", false, ExpressionRoleMapping.READ_ONLY_ROLE_MAPPING_METADATA_FLAG, true))
|
|
|
|
+ .request()
|
|
|
|
+ .validate(false),
|
|
|
|
+ is(nullValue())
|
|
|
|
+ );
|
|
|
|
+
|
|
|
|
+ assertThat(
|
|
|
|
+ builder.name("test")
|
|
|
|
+ .roles("superuser")
|
|
|
|
+ .expression(Mockito.mock(RoleMapperExpression.class))
|
|
|
|
+ .metadata(Map.of(ExpressionRoleMapping.READ_ONLY_ROLE_MAPPING_METADATA_FLAG, true))
|
|
|
|
+ .request()
|
|
|
|
+ .validate(false),
|
|
|
|
+ is(nullValue())
|
|
|
|
+ );
|
|
|
|
+
|
|
|
|
+ assertThat(
|
|
|
|
+ builder.name("test")
|
|
|
|
+ .roles("superuser")
|
|
|
|
+ .expression(Mockito.mock(RoleMapperExpression.class))
|
|
|
|
+ .metadata(Map.of("_secret", false))
|
|
|
|
+ .request()
|
|
|
|
+ .validate(false),
|
|
|
|
+ is(nullValue())
|
|
|
|
+ );
|
|
|
|
+ }
|
|
|
|
+
|
|
private void assertValidationFailure(PutRoleMappingRequest request, String expectedMessage) {
|
|
private void assertValidationFailure(PutRoleMappingRequest request, String expectedMessage) {
|
|
final ValidationException ve = request.validate();
|
|
final ValidationException ve = request.validate();
|
|
assertThat(ve, notNullValue());
|
|
assertThat(ve, notNullValue());
|