5
5
*/
6
6
package org .elasticsearch .xpack .security ;
7
7
8
+ import org .elasticsearch .ElasticsearchSecurityException ;
8
9
import org .elasticsearch .Version ;
9
10
import org .elasticsearch .client .Client ;
10
11
import org .elasticsearch .cluster .ClusterName ;
33
34
import org .elasticsearch .test .VersionUtils ;
34
35
import org .elasticsearch .threadpool .ThreadPool ;
35
36
import org .elasticsearch .watcher .ResourceWatcherService ;
37
+ import org .elasticsearch .xpack .core .XPackField ;
36
38
import org .elasticsearch .xpack .core .XPackSettings ;
37
39
import org .elasticsearch .xpack .core .security .SecurityExtension ;
38
40
import org .elasticsearch .xpack .core .security .SecurityField ;
72
74
import static org .hamcrest .Matchers .empty ;
73
75
import static org .hamcrest .Matchers .equalTo ;
74
76
import static org .hamcrest .Matchers .hasItem ;
77
+ import static org .hamcrest .Matchers .instanceOf ;
78
+ import static org .hamcrest .Matchers .notNullValue ;
75
79
import static org .mockito .Mockito .mock ;
76
80
import static org .mockito .Mockito .when ;
77
81
@@ -94,18 +98,7 @@ public Map<String, Realm.Factory> getRealms(SecurityComponents components) {
94
98
}
95
99
}
96
100
97
- private Collection <Object > createComponents (Settings testSettings , SecurityExtension ... extensions ) throws Exception {
98
- if (security != null ) {
99
- throw new IllegalStateException ("Security object already exists (" + security + ")" );
100
- }
101
- Settings .Builder builder = Settings .builder ()
102
- .put ("xpack.security.enabled" , true )
103
- .put (testSettings )
104
- .put ("path.home" , createTempDir ());
105
- if (inFipsJvm ()) {
106
- builder .put (XPackSettings .DIAGNOSE_TRUST_EXCEPTIONS_SETTING .getKey (), false );
107
- }
108
- Settings settings = builder .build ();
101
+ private Collection <Object > createComponentsUtil (Settings settings , SecurityExtension ... extensions ) throws Exception {
109
102
Environment env = TestEnvironment .newEnvironment (settings );
110
103
licenseState = new TestUtils .UpdatableLicenseState (settings );
111
104
SSLService sslService = new SSLService (settings , env );
@@ -137,6 +130,36 @@ protected SSLService getSslService() {
137
130
xContentRegistry (), env , new IndexNameExpressionResolver ());
138
131
}
139
132
133
+ private Collection <Object > createComponentsWithSecurityNotExplicitlyEnabled (Settings testSettings , SecurityExtension ... extensions )
134
+ throws Exception {
135
+ if (security != null ) {
136
+ throw new IllegalStateException ("Security object already exists (" + security + ")" );
137
+ }
138
+ Settings .Builder builder = Settings .builder ()
139
+ .put (testSettings )
140
+ .put ("path.home" , createTempDir ());
141
+ if (inFipsJvm ()) {
142
+ builder .put (XPackSettings .DIAGNOSE_TRUST_EXCEPTIONS_SETTING .getKey (), false );
143
+ }
144
+ Settings settings = builder .build ();
145
+ return createComponentsUtil (settings , extensions );
146
+ }
147
+
148
+ private Collection <Object > createComponents (Settings testSettings , SecurityExtension ... extensions ) throws Exception {
149
+ if (security != null ) {
150
+ throw new IllegalStateException ("Security object already exists (" + security + ")" );
151
+ }
152
+ Settings .Builder builder = Settings .builder ()
153
+ .put ("xpack.security.enabled" , true )
154
+ .put (testSettings )
155
+ .put ("path.home" , createTempDir ());
156
+ if (inFipsJvm ()) {
157
+ builder .put (XPackSettings .DIAGNOSE_TRUST_EXCEPTIONS_SETTING .getKey (), false );
158
+ }
159
+ Settings settings = builder .build ();
160
+ return createComponentsUtil (settings , extensions );
161
+ }
162
+
140
163
private static <T > T findComponent (Class <T > type , Collection <Object > components ) {
141
164
for (Object obj : components ) {
142
165
if (type .isInstance (obj )) {
@@ -490,4 +513,16 @@ public void testValidateForFipsNoErrors() {
490
513
Security .validateForFips (settings );
491
514
// no exception thrown
492
515
}
516
+
517
+ private void logAndFail (Exception e ) {
518
+ logger .error ("unexpected exception" , e );
519
+ fail ("unexpected exception " + e .getMessage ());
520
+ }
521
+
522
+ private void VerifyBasicAuthenticationHeader (Exception e ) {
523
+ assertThat (e , instanceOf (ElasticsearchSecurityException .class ));
524
+ assertThat (((ElasticsearchSecurityException ) e ).getHeader ("WWW-Authenticate" ), notNullValue ());
525
+ assertThat (((ElasticsearchSecurityException ) e ).getHeader ("WWW-Authenticate" ),
526
+ hasItem ("Basic realm=\" " + XPackField .SECURITY + "\" charset=\" UTF-8\" " ));
527
+ }
493
528
}
0 commit comments