|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.xpack.security.privilege; |
| 9 | + |
| 10 | +import org.elasticsearch.client.Request; |
| 11 | +import org.elasticsearch.client.Response; |
| 12 | +import org.elasticsearch.common.settings.SecureString; |
| 13 | +import org.elasticsearch.common.settings.Settings; |
| 14 | +import org.elasticsearch.common.util.concurrent.ThreadContext; |
| 15 | +import org.elasticsearch.common.xcontent.XContentHelper; |
| 16 | +import org.elasticsearch.test.cluster.ElasticsearchCluster; |
| 17 | +import org.elasticsearch.test.cluster.FeatureFlag; |
| 18 | +import org.elasticsearch.test.rest.ESRestTestCase; |
| 19 | +import org.elasticsearch.xcontent.json.JsonXContent; |
| 20 | +import org.elasticsearch.xpack.security.SecurityOnTrialLicenseRestTestCase; |
| 21 | +import org.junit.ClassRule; |
| 22 | + |
| 23 | +import java.io.IOException; |
| 24 | +import java.util.Map; |
| 25 | + |
| 26 | +import static org.hamcrest.Matchers.equalTo; |
| 27 | + |
| 28 | +public class FailureStoreSecurityRestIT extends ESRestTestCase { |
| 29 | + |
| 30 | + @ClassRule |
| 31 | + public static ElasticsearchCluster cluster = ElasticsearchCluster.local() |
| 32 | + .apply(SecurityOnTrialLicenseRestTestCase.commonTrialSecurityClusterConfig) |
| 33 | + .feature(FeatureFlag.FAILURE_STORE_ENABLED) |
| 34 | + .build(); |
| 35 | + |
| 36 | + @Override |
| 37 | + protected String getTestRestCluster() { |
| 38 | + return cluster.getHttpAddresses(); |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + protected Settings restAdminSettings() { |
| 43 | + String token = basicAuthHeaderValue("admin_user", new SecureString("admin-password".toCharArray())); |
| 44 | + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); |
| 45 | + } |
| 46 | + |
| 47 | + public void testGetUserPrivileges() throws IOException { |
| 48 | + Request roleRequest = new Request("PUT", "/_security/role/role"); |
| 49 | + roleRequest.setJsonEntity(""" |
| 50 | + { |
| 51 | + "cluster": ["all"], |
| 52 | + "indices": [ |
| 53 | + { |
| 54 | + "names": ["*"], |
| 55 | + "privileges": ["read", "read_failure_store"] |
| 56 | + } |
| 57 | + ] |
| 58 | + } |
| 59 | + """); |
| 60 | + assertOK(adminClient().performRequest(roleRequest)); |
| 61 | + |
| 62 | + Request userRequest = new Request("PUT", "/_security/user/user"); |
| 63 | + userRequest.setJsonEntity(""" |
| 64 | + { |
| 65 | + "password": "x-pack-test-password", |
| 66 | + "roles": ["role"] |
| 67 | + } |
| 68 | + """); |
| 69 | + assertOK(adminClient().performRequest(userRequest)); |
| 70 | + |
| 71 | + Request request = new Request("GET", "/_security/user/_privileges"); |
| 72 | + request.setOptions( |
| 73 | + request.getOptions() |
| 74 | + .toBuilder() |
| 75 | + .addHeader("Authorization", basicAuthHeaderValue("user", new SecureString("x-pack-test-password".toCharArray()))) |
| 76 | + ); |
| 77 | + Response response = client().performRequest(request); |
| 78 | + assertOK(response); |
| 79 | + assertThat(responseAsMap(response), equalTo(mapFromJson(""" |
| 80 | + { |
| 81 | + "cluster": ["all"], |
| 82 | + "global": [], |
| 83 | + "indices": [{ |
| 84 | + "names": ["*"], |
| 85 | + "privileges": ["read"], |
| 86 | + "allow_restricted_indices": false |
| 87 | + }, |
| 88 | + { |
| 89 | + "names": ["*"], |
| 90 | + "privileges": ["read_failure_store"], |
| 91 | + "allow_restricted_indices": false |
| 92 | + }], |
| 93 | + "applications": [], |
| 94 | + "run_as": [] |
| 95 | + }"""))); |
| 96 | + } |
| 97 | + |
| 98 | + private static Map<String, Object> mapFromJson(String json) { |
| 99 | + return XContentHelper.convertToMap(JsonXContent.jsonXContent, json, false); |
| 100 | + } |
| 101 | +} |
0 commit comments