|
6 | 6 |
|
7 | 7 | package org.elasticsearch.xpack.core.security.authc.support.mapper;
|
8 | 8 |
|
| 9 | +import org.elasticsearch.cluster.ClusterChangedEvent; |
| 10 | +import org.elasticsearch.cluster.ClusterState; |
| 11 | +import org.elasticsearch.cluster.metadata.MetaData; |
9 | 12 | import org.elasticsearch.common.Strings;
|
10 | 13 | import org.elasticsearch.common.bytes.BytesArray;
|
11 | 14 | import org.elasticsearch.common.bytes.BytesReference;
|
|
17 | 20 | import org.elasticsearch.common.xcontent.NamedXContentRegistry;
|
18 | 21 | import org.elasticsearch.common.xcontent.XContentParser;
|
19 | 22 | import org.elasticsearch.common.xcontent.XContentType;
|
| 23 | +import org.elasticsearch.script.ScriptException; |
| 24 | +import org.elasticsearch.script.ScriptMetaData; |
20 | 25 | import org.elasticsearch.script.ScriptModule;
|
21 | 26 | import org.elasticsearch.script.ScriptService;
|
| 27 | +import org.elasticsearch.script.StoredScriptSource; |
22 | 28 | import org.elasticsearch.script.mustache.MustacheScriptEngine;
|
23 | 29 | import org.elasticsearch.test.ESTestCase;
|
24 | 30 | import org.elasticsearch.test.EqualsHashCodeTestUtils;
|
|
31 | 37 | import java.util.Collections;
|
32 | 38 |
|
33 | 39 | import static org.hamcrest.Matchers.contains;
|
| 40 | +import static org.hamcrest.Matchers.containsString; |
34 | 41 | import static org.hamcrest.Matchers.equalTo;
|
35 | 42 | import static org.hamcrest.Matchers.notNullValue;
|
| 43 | +import static org.mockito.Mockito.mock; |
| 44 | +import static org.mockito.Mockito.when; |
36 | 45 |
|
37 | 46 | public class TemplateRoleNameTests extends ESTestCase {
|
38 | 47 |
|
@@ -116,4 +125,112 @@ public void tryEquals(TemplateRoleName original) {
|
116 | 125 | };
|
117 | 126 | EqualsHashCodeTestUtils.checkEqualsAndHashCode(original, copy, mutate);
|
118 | 127 | }
|
| 128 | + |
| 129 | + public void testValidate() { |
| 130 | + final ScriptService scriptService = new ScriptService(Settings.EMPTY, |
| 131 | + Collections.singletonMap(MustacheScriptEngine.NAME, new MustacheScriptEngine()), ScriptModule.CORE_CONTEXTS); |
| 132 | + |
| 133 | + final TemplateRoleName plainString = new TemplateRoleName(new BytesArray("{ \"source\":\"heroes\" }"), Format.STRING); |
| 134 | + plainString.validate(scriptService); |
| 135 | + |
| 136 | + final TemplateRoleName user = new TemplateRoleName(new BytesArray("{ \"source\":\"_user_{{username}}\" }"), Format.STRING); |
| 137 | + user.validate(scriptService); |
| 138 | + |
| 139 | + final TemplateRoleName groups = new TemplateRoleName(new BytesArray("{ \"source\":\"{{#tojson}}groups{{/tojson}}\" }"), |
| 140 | + Format.JSON); |
| 141 | + groups.validate(scriptService); |
| 142 | + |
| 143 | + final TemplateRoleName notObject = new TemplateRoleName(new BytesArray("heroes"), Format.STRING); |
| 144 | + expectThrows(IllegalArgumentException.class, () -> notObject.validate(scriptService)); |
| 145 | + |
| 146 | + final TemplateRoleName invalidField = new TemplateRoleName(new BytesArray("{ \"foo\":\"heroes\" }"), Format.STRING); |
| 147 | + expectThrows(IllegalArgumentException.class, () -> invalidField.validate(scriptService)); |
| 148 | + } |
| 149 | + |
| 150 | + public void testValidateWillPassWithEmptyContext() { |
| 151 | + final ScriptService scriptService = new ScriptService(Settings.EMPTY, |
| 152 | + Collections.singletonMap(MustacheScriptEngine.NAME, new MustacheScriptEngine()), ScriptModule.CORE_CONTEXTS); |
| 153 | + |
| 154 | + final BytesReference template = new BytesArray("{ \"source\":\"" + |
| 155 | + "{{username}}/{{dn}}/{{realm}}/{{metadata}}" + |
| 156 | + "{{#realm}}" + |
| 157 | + " {{name}}/{{type}}" + |
| 158 | + "{{/realm}}" + |
| 159 | + "{{#toJson}}groups{{/toJson}}" + |
| 160 | + "{{^groups}}{{.}}{{/groups}}" + |
| 161 | + "{{#metadata}}" + |
| 162 | + " {{#first}}" + |
| 163 | + " <li><strong>{{name}}</strong></li>" + |
| 164 | + " {{/first}}" + |
| 165 | + " {{#link}}" + |
| 166 | + " <li><a href=\\\"{{url}}\\\">{{name}}</a></li>" + |
| 167 | + " {{/link}}" + |
| 168 | + " {{#toJson}}subgroups{{/toJson}}" + |
| 169 | + " {{something-else}}" + |
| 170 | + "{{/metadata}}\" }"); |
| 171 | + final TemplateRoleName templateRoleName = new TemplateRoleName(template, Format.STRING); |
| 172 | + templateRoleName.validate(scriptService); |
| 173 | + } |
| 174 | + |
| 175 | + public void testValidateWillFailForSyntaxError() { |
| 176 | + final ScriptService scriptService = new ScriptService(Settings.EMPTY, |
| 177 | + Collections.singletonMap(MustacheScriptEngine.NAME, new MustacheScriptEngine()), ScriptModule.CORE_CONTEXTS); |
| 178 | + |
| 179 | + final BytesReference template = new BytesArray("{ \"source\":\" {{#not-closed}} {{other-variable}} \" }"); |
| 180 | + |
| 181 | + final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, |
| 182 | + () -> new TemplateRoleName(template, Format.STRING).validate(scriptService)); |
| 183 | + assertTrue(e.getCause() instanceof ScriptException); |
| 184 | + } |
| 185 | + |
| 186 | + public void testValidationWillFailWhenInlineScriptIsNotEnabled() { |
| 187 | + final Settings settings = Settings.builder().put("script.allowed_types", ScriptService.ALLOW_NONE).build(); |
| 188 | + final ScriptService scriptService = new ScriptService(settings, |
| 189 | + Collections.singletonMap(MustacheScriptEngine.NAME, new MustacheScriptEngine()), ScriptModule.CORE_CONTEXTS); |
| 190 | + final BytesReference inlineScript = new BytesArray("{ \"source\":\"\" }"); |
| 191 | + final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, |
| 192 | + () -> new TemplateRoleName(inlineScript, Format.STRING).validate(scriptService)); |
| 193 | + assertThat(e.getMessage(), containsString("[inline]")); |
| 194 | + } |
| 195 | + |
| 196 | + public void testValidateWillFailWhenStoredScriptIsNotEnabled() { |
| 197 | + final Settings settings = Settings.builder().put("script.allowed_types", ScriptService.ALLOW_NONE).build(); |
| 198 | + final ScriptService scriptService = new ScriptService(settings, |
| 199 | + Collections.singletonMap(MustacheScriptEngine.NAME, new MustacheScriptEngine()), ScriptModule.CORE_CONTEXTS); |
| 200 | + final ClusterChangedEvent clusterChangedEvent = mock(ClusterChangedEvent.class); |
| 201 | + final ClusterState clusterState = mock(ClusterState.class); |
| 202 | + final MetaData metaData = mock(MetaData.class); |
| 203 | + final StoredScriptSource storedScriptSource = mock(StoredScriptSource.class); |
| 204 | + final ScriptMetaData scriptMetaData = new ScriptMetaData.Builder(null).storeScript("foo", storedScriptSource).build(); |
| 205 | + when(clusterChangedEvent.state()).thenReturn(clusterState); |
| 206 | + when(clusterState.metaData()).thenReturn(metaData); |
| 207 | + when(metaData.custom(ScriptMetaData.TYPE)).thenReturn(scriptMetaData); |
| 208 | + when(storedScriptSource.getLang()).thenReturn("mustache"); |
| 209 | + when(storedScriptSource.getSource()).thenReturn(""); |
| 210 | + when(storedScriptSource.getOptions()).thenReturn(Collections.emptyMap()); |
| 211 | + scriptService.applyClusterState(clusterChangedEvent); |
| 212 | + |
| 213 | + final BytesReference storedScript = new BytesArray("{ \"id\":\"foo\" }"); |
| 214 | + final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, |
| 215 | + () -> new TemplateRoleName(storedScript, Format.STRING).validate(scriptService)); |
| 216 | + assertThat(e.getMessage(), containsString("[stored]")); |
| 217 | + } |
| 218 | + |
| 219 | + public void testValidateWillFailWhenStoredScriptIsNotFound() { |
| 220 | + final ScriptService scriptService = new ScriptService(Settings.EMPTY, |
| 221 | + Collections.singletonMap(MustacheScriptEngine.NAME, new MustacheScriptEngine()), ScriptModule.CORE_CONTEXTS); |
| 222 | + final ClusterChangedEvent clusterChangedEvent = mock(ClusterChangedEvent.class); |
| 223 | + final ClusterState clusterState = mock(ClusterState.class); |
| 224 | + final MetaData metaData = mock(MetaData.class); |
| 225 | + final ScriptMetaData scriptMetaData = new ScriptMetaData.Builder(null).build(); |
| 226 | + when(clusterChangedEvent.state()).thenReturn(clusterState); |
| 227 | + when(clusterState.metaData()).thenReturn(metaData); |
| 228 | + when(metaData.custom(ScriptMetaData.TYPE)).thenReturn(scriptMetaData); |
| 229 | + scriptService.applyClusterState(clusterChangedEvent); |
| 230 | + |
| 231 | + final BytesReference storedScript = new BytesArray("{ \"id\":\"foo\" }"); |
| 232 | + final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, |
| 233 | + () -> new TemplateRoleName(storedScript, Format.STRING).validate(scriptService)); |
| 234 | + assertThat(e.getMessage(), containsString("unable to find script")); |
| 235 | + } |
119 | 236 | }
|
0 commit comments