11/*
2- * Copyright OpenSearch Contributors
3- * SPDX-License-Identifier: Apache-2.0
4- *
5- * The OpenSearch Contributors require contributions made to
6- * this file be licensed under the Apache-2.0 license or a
7- * compatible open source license.
8- *
9- */
2+ * Copyright OpenSearch Contributors
3+ * SPDX-License-Identifier: Apache-2.0
4+ *
5+ * The OpenSearch Contributors require contributions made to
6+ * this file be licensed under the Apache-2.0 license or a
7+ * compatible open source license.
8+ *
9+ */
1010package org .opensearch .security ;
1111
1212import java .io .IOException ;
1919import com .carrotsearch .randomizedtesting .annotations .ThreadLeakScope ;
2020import com .fasterxml .jackson .databind .JsonNode ;
2121import org .apache .commons .io .FileUtils ;
22+ import org .apache .http .HttpStatus ;
2223import org .awaitility .Awaitility ;
2324import org .junit .AfterClass ;
24- import org .junit .ClassRule ;
2525import org .junit .Test ;
2626import org .junit .runner .RunWith ;
2727
28- import org .opensearch .test . framework . TestSecurityConfig . User ;
29- import org .opensearch .test .framework .cluster . ClusterManager ;
28+ import org .opensearch .security . state . SecurityMetadata ;
29+ import org .opensearch .test .framework .TestSecurityConfig ;
3030import org .opensearch .test .framework .cluster .LocalCluster ;
3131import org .opensearch .test .framework .cluster .TestRestClient ;
32- import org .opensearch .test .framework .cluster .TestRestClient .HttpResponse ;
3332
3433import static org .hamcrest .MatcherAssert .assertThat ;
3534import static org .hamcrest .Matchers .aMapWithSize ;
3635import static org .hamcrest .Matchers .allOf ;
3736import static org .hamcrest .Matchers .equalTo ;
3837import static org .hamcrest .Matchers .hasKey ;
3938import static org .hamcrest .Matchers .not ;
39+ import static org .junit .Assert .assertNotNull ;
40+ import static org .junit .Assert .assertTrue ;
4041
4142@ RunWith (com .carrotsearch .randomizedtesting .RandomizedRunner .class )
4243@ ThreadLeakScope (ThreadLeakScope .Scope .NONE )
43- public class DefaultConfigurationTests {
44-
45- private final static Path configurationFolder = ConfigurationFiles .createConfigurationDirectory ();
46- private static final User ADMIN_USER = new User ("admin" );
47- private static final User NEW_USER = new User ("new-user" );
48- private static final User LIMITED_USER = new User ("limited-user" );
49-
50- @ ClassRule
51- public static LocalCluster cluster = new LocalCluster .Builder ().clusterManager (ClusterManager .SINGLENODE )
52- .nodeSettings (
53- Map .of (
54- "plugins.security.allow_default_init_securityindex" ,
55- true ,
56- "plugins.security.restapi.roles_enabled" ,
57- List .of ("user_admin__all_access" )
58- )
59- )
60- .defaultConfigurationInitDirectory (configurationFolder .toString ())
61- .loadConfigurationIntoIndex (false )
62- .build ();
44+ public abstract class AbstractDefaultConfigurationTests {
45+ public final static Path configurationFolder = ConfigurationFiles .createConfigurationDirectory ();
46+ private static final TestSecurityConfig .User ADMIN_USER = new TestSecurityConfig .User ("admin" );
47+ private static final TestSecurityConfig .User NEW_USER = new TestSecurityConfig .User ("new-user" );
48+ private static final TestSecurityConfig .User LIMITED_USER = new TestSecurityConfig .User ("limited-user" );
49+
50+ private final LocalCluster cluster ;
51+
52+ protected AbstractDefaultConfigurationTests (LocalCluster cluster ) {
53+ this .cluster = cluster ;
54+ }
6355
6456 @ AfterClass
6557 public static void cleanConfigurationDirectory () throws IOException {
@@ -73,18 +65,43 @@ public void shouldLoadDefaultConfiguration() {
7365 }
7466 try (TestRestClient client = cluster .getRestClient (ADMIN_USER )) {
7567 client .confirmCorrectCredentials (ADMIN_USER .getName ());
76- HttpResponse response = client .get ("_plugins/_security/api/internalusers" );
77- response .assertStatusCode (200 );
68+ TestRestClient . HttpResponse response = client .get ("_plugins/_security/api/internalusers" );
69+ response .assertStatusCode (HttpStatus . SC_OK );
7870 Map <String , Object > users = response .getBodyAs (Map .class );
7971 assertThat (
72+ response .getBody (),
8073 users ,
8174 allOf (aMapWithSize (3 ), hasKey (ADMIN_USER .getName ()), hasKey (NEW_USER .getName ()), hasKey (LIMITED_USER .getName ()))
8275 );
8376 }
8477 }
8578
79+ void assertClusterState (final TestRestClient client ) {
80+ if (cluster .node ().settings ().getAsBoolean ("plugins.security.allow_default_init_securityindex.use_cluster_state" , false )) {
81+ final TestRestClient .HttpResponse response = client .get ("_cluster/state" );
82+ response .assertStatusCode (HttpStatus .SC_OK );
83+ final var clusterState = response .getBodyAs (Map .class );
84+ assertTrue (response .getBody (), clusterState .containsKey (SecurityMetadata .TYPE ));
85+ @ SuppressWarnings ("unchecked" )
86+ final var securityClusterState = (Map <String , Object >) clusterState .get (SecurityMetadata .TYPE );
87+ @ SuppressWarnings ("unchecked" )
88+ final var securityConfiguration = (Map <String , Object >) ((Map <?, ?>) clusterState .get (SecurityMetadata .TYPE )).get (
89+ "configuration"
90+ );
91+ assertTrue (response .getBody (), securityClusterState .containsKey ("created" ));
92+ assertNotNull (response .getBody (), securityClusterState .get ("created" ));
93+
94+ for (final var k : securityConfiguration .keySet ()) {
95+ @ SuppressWarnings ("unchecked" )
96+ final var sc = (Map <String , Object >) securityConfiguration .get (k );
97+ assertTrue (response .getBody (), sc .containsKey ("hash" ));
98+ assertTrue (response .getBody (), sc .containsKey ("last_modified" ));
99+ }
100+ }
101+ }
102+
86103 @ Test
87- public void securityRolesUgrade () throws Exception {
104+ public void securityRolesUpgrade () throws Exception {
88105 try (var client = cluster .getRestClient (ADMIN_USER )) {
89106 // Setup: Make sure the config is ready before starting modifications
90107 Awaitility .await ().alias ("Load default configuration" ).until (() -> client .getAuthInfo ().getStatusCode (), equalTo (200 ));
@@ -159,4 +176,5 @@ private Set<String> extractFieldNames(final JsonNode json) {
159176 json .fieldNames ().forEachRemaining (set ::add );
160177 return set ;
161178 }
179+
162180}
0 commit comments