7
7
8
8
package org .elasticsearch .xpack .core .security .action .settings ;
9
9
10
+ import org .elasticsearch .action .ActionRequestValidationException ;
11
+ import org .elasticsearch .cluster .metadata .IndexMetadata ;
12
+ import org .elasticsearch .cluster .routing .allocation .DataTier ;
10
13
import org .elasticsearch .test .ESTestCase ;
11
14
12
15
import java .util .Collections ;
13
16
import java .util .HashMap ;
14
17
import java .util .List ;
15
18
import java .util .Map ;
19
+ import java .util .function .Supplier ;
16
20
import java .util .regex .Pattern ;
17
21
18
- import static org .elasticsearch .xpack .core .security .action .settings .UpdateSecuritySettingsAction .ALLOWED_SETTING_KEYS ;
22
+ import static org .elasticsearch .xpack .core .security .action .settings .UpdateSecuritySettingsAction .ALLOWED_SETTING_VALIDATORS ;
19
23
import static org .elasticsearch .xpack .core .security .action .settings .UpdateSecuritySettingsAction .MAIN_INDEX_NAME ;
20
24
import static org .elasticsearch .xpack .core .security .action .settings .UpdateSecuritySettingsAction .PROFILES_INDEX_NAME ;
21
25
import static org .elasticsearch .xpack .core .security .action .settings .UpdateSecuritySettingsAction .TOKENS_INDEX_NAME ;
27
31
28
32
public class UpdateSecuritySettingsActionTests extends ESTestCase {
29
33
34
+ static final Map <String , Supplier <String >> ALLOWED_SETTING_GENERATORS = Map .of (
35
+ IndexMetadata .SETTING_NUMBER_OF_REPLICAS ,
36
+ () -> randomAlphaOfLength (5 ), // no additional validation
37
+ IndexMetadata .SETTING_AUTO_EXPAND_REPLICAS ,
38
+ () -> randomAlphaOfLength (5 ), // no additional validation
39
+ DataTier .TIER_PREFERENCE ,
40
+ () -> randomFrom (DataTier .DATA_CONTENT , DataTier .DATA_HOT , DataTier .DATA_WARM , DataTier .DATA_COLD )
41
+ );
42
+
30
43
public void testValidateSettingsEmpty () {
31
44
var req = new UpdateSecuritySettingsAction .Request (
32
45
TEST_REQUEST_TIMEOUT ,
@@ -43,9 +56,10 @@ public void testValidateSettingsEmpty() {
43
56
44
57
public void testAllowedSettingsOk () {
45
58
Map <String , Object > allAllowedSettingsMap = new HashMap <>();
46
- for (String allowedSetting : ALLOWED_SETTING_KEYS ) {
47
- Map <String , Object > allowedSettingMap = Map .of (allowedSetting , randomAlphaOfLength (5 ));
48
- allAllowedSettingsMap .put (allowedSetting , randomAlphaOfLength (5 ));
59
+ for (String allowedSetting : ALLOWED_SETTING_VALIDATORS .keySet ()) {
60
+ String settingValue = ALLOWED_SETTING_GENERATORS .get (allowedSetting ).get ();
61
+ Map <String , Object > allowedSettingMap = Map .of (allowedSetting , settingValue );
62
+ allAllowedSettingsMap .put (allowedSetting , settingValue );
49
63
var req = new UpdateSecuritySettingsAction .Request (
50
64
TEST_REQUEST_TIMEOUT ,
51
65
TEST_REQUEST_TIMEOUT ,
@@ -86,11 +100,12 @@ public void testAllowedSettingsOk() {
86
100
87
101
public void testDisallowedSettingsFailsValidation () {
88
102
String disallowedSetting = "index."
89
- + randomValueOtherThanMany ((value ) -> ALLOWED_SETTING_KEYS . contains ("index." + value ), () -> randomAlphaOfLength (5 ));
103
+ + randomValueOtherThanMany ((value ) -> ALLOWED_SETTING_VALIDATORS . containsKey ("index." + value ), () -> randomAlphaOfLength (5 ));
90
104
Map <String , Object > disallowedSettingMap = Map .of (disallowedSetting , randomAlphaOfLength (5 ));
105
+ String validSetting = randomFrom (ALLOWED_SETTING_VALIDATORS .keySet ());
91
106
Map <String , Object > validOrEmptySettingMap = randomFrom (
92
107
Collections .emptyMap (),
93
- Map .of (randomFrom ( ALLOWED_SETTING_KEYS ), randomAlphaOfLength ( 5 ))
108
+ Map .of (validSetting , ALLOWED_SETTING_GENERATORS . get ( validSetting ). get ( ))
94
109
);
95
110
{
96
111
var req = new UpdateSecuritySettingsAction .Request (
@@ -106,11 +121,11 @@ public void testDisallowedSettingsFailsValidation() {
106
121
assertThat (
107
122
errorMsg ,
108
123
matchesRegex (
109
- "illegal settings for index \\ ["
124
+ "illegal setting for index \\ ["
110
125
+ Pattern .quote (TOKENS_INDEX_NAME )
111
126
+ "\\ ]: \\ ["
112
127
+ disallowedSetting
113
- + "\\ ], these settings may not be configured. Only the following settings may be configured for that index.*"
128
+ + "\\ ], this setting may not be configured. Only the following settings may be configured for that index.*"
114
129
)
115
130
);
116
131
}
@@ -130,13 +145,13 @@ public void testDisallowedSettingsFailsValidation() {
130
145
assertThat (
131
146
errorMsg ,
132
147
matchesRegex (
133
- "illegal settings for index \\ [("
148
+ "illegal setting for index \\ [("
134
149
+ Pattern .quote (MAIN_INDEX_NAME )
135
150
+ "|"
136
151
+ Pattern .quote (PROFILES_INDEX_NAME )
137
152
+ ")\\ ]: \\ ["
138
153
+ disallowedSetting
139
- + "\\ ], these settings may not be configured. Only the following settings may be configured for that index.*"
154
+ + "\\ ], this setting may not be configured. Only the following settings may be configured for that index.*"
140
155
)
141
156
);
142
157
}
@@ -156,19 +171,72 @@ public void testDisallowedSettingsFailsValidation() {
156
171
assertThat (
157
172
errorMsg ,
158
173
matchesRegex (
159
- "illegal settings for index \\ [("
174
+ "illegal setting for index \\ [("
160
175
+ Pattern .quote (MAIN_INDEX_NAME )
161
176
+ "|"
162
177
+ Pattern .quote (TOKENS_INDEX_NAME )
163
178
+ "|"
164
179
+ Pattern .quote (PROFILES_INDEX_NAME )
165
180
+ ")\\ ]: \\ ["
166
181
+ disallowedSetting
167
- + "\\ ], these settings may not be configured. Only the following settings may be configured for that index.*"
182
+ + "\\ ], this setting may not be configured. Only the following settings may be configured for that index.*"
168
183
)
169
184
);
170
185
}
171
186
}
172
187
}
173
188
189
+ public void testSettingValuesAreValidated () {
190
+ Map <String , Object > forbiddenSettingsMap = Map .of (DataTier .TIER_PREFERENCE , DataTier .DATA_FROZEN );
191
+ String badTier = randomAlphaOfLength (5 );
192
+ Map <String , Object > badSettingsMap = Map .of (DataTier .TIER_PREFERENCE , badTier );
193
+ Map <String , Object > allowedSettingMap = Map .of (
194
+ DataTier .TIER_PREFERENCE ,
195
+ randomFrom (DataTier .DATA_HOT , DataTier .DATA_WARM , DataTier .DATA_CONTENT , DataTier .DATA_COLD )
196
+ );
197
+ {
198
+ var req = new UpdateSecuritySettingsAction .Request (
199
+ TEST_REQUEST_TIMEOUT ,
200
+ TEST_REQUEST_TIMEOUT ,
201
+ allowedSettingMap ,
202
+ Collections .emptyMap (),
203
+ Collections .emptyMap ()
204
+ );
205
+ assertThat (req .validate (), nullValue ());
206
+ }
207
+
208
+ {
209
+ var req = new UpdateSecuritySettingsAction .Request (
210
+ TEST_REQUEST_TIMEOUT ,
211
+ TEST_REQUEST_TIMEOUT ,
212
+ forbiddenSettingsMap ,
213
+ Collections .emptyMap (),
214
+ Collections .emptyMap ()
215
+ );
216
+ ActionRequestValidationException exception = req .validate ();
217
+ assertThat (exception , notNullValue ());
218
+ assertThat (exception .validationErrors (), hasSize (1 ));
219
+ assertThat (
220
+ exception .validationErrors ().get (0 ),
221
+ containsString ("disallowed data tiers [" + DataTier .DATA_FROZEN + "] found, allowed tiers are " )
222
+ );
223
+ }
224
+
225
+ {
226
+ var req = new UpdateSecuritySettingsAction .Request (
227
+ TEST_REQUEST_TIMEOUT ,
228
+ TEST_REQUEST_TIMEOUT ,
229
+ badSettingsMap ,
230
+ Collections .emptyMap (),
231
+ Collections .emptyMap ()
232
+ );
233
+ var exception = req .validate ();
234
+ assertThat (exception , notNullValue ());
235
+ assertThat (exception .validationErrors (), hasSize (1 ));
236
+ assertThat (
237
+ exception .validationErrors ().get (0 ),
238
+ containsString ("disallowed data tiers [" + badTier + "] found, allowed tiers are " )
239
+ );
240
+ }
241
+ }
174
242
}
0 commit comments