9
9
10
10
package org .elasticsearch .entitlement .runtime .policy .entitlements ;
11
11
12
+ import org .elasticsearch .core .Booleans ;
12
13
import org .elasticsearch .entitlement .runtime .policy .ExternalEntitlement ;
13
14
import org .elasticsearch .entitlement .runtime .policy .PathLookup ;
14
15
import org .elasticsearch .entitlement .runtime .policy .PolicyValidationException ;
17
18
import java .util .ArrayList ;
18
19
import java .util .HashMap ;
19
20
import java .util .List ;
21
+ import java .util .Locale ;
20
22
import java .util .Map ;
21
23
import java .util .Objects ;
22
24
import java .util .stream .Stream ;
@@ -85,12 +87,12 @@ static FileData ofRelativePath(Path relativePath, BaseDir baseDir, Mode mode) {
85
87
return new RelativePathFileData (relativePath , baseDir , mode , null );
86
88
}
87
89
88
- static FileData ofPathSetting (String setting , Mode mode ) {
89
- return new PathSettingFileData (setting , mode , null );
90
+ static FileData ofPathSetting (String setting , Mode mode , boolean ignoreUrl ) {
91
+ return new PathSettingFileData (setting , mode , ignoreUrl , null );
90
92
}
91
93
92
- static FileData ofRelativePathSetting (String setting , BaseDir baseDir , Mode mode ) {
93
- return new RelativePathSettingFileData (setting , baseDir , mode , null );
94
+ static FileData ofRelativePathSetting (String setting , BaseDir baseDir , Mode mode , boolean ignoreUrl ) {
95
+ return new RelativePathSettingFileData (setting , baseDir , mode , ignoreUrl , null );
94
96
}
95
97
96
98
/**
@@ -207,45 +209,51 @@ public FileData withPlatform(Platform platform) {
207
209
}
208
210
}
209
211
210
- private record PathSettingFileData (String setting , Mode mode , Platform platform ) implements FileData {
212
+ private record PathSettingFileData (String setting , Mode mode , boolean ignoreUrl , Platform platform ) implements FileData {
211
213
@ Override
212
214
public Stream <Path > resolvePaths (PathLookup pathLookup ) {
213
- return resolvePathSettings (pathLookup , setting );
215
+ return resolvePathSettings (pathLookup , setting , ignoreUrl );
214
216
}
215
217
216
218
@ Override
217
219
public FileData withPlatform (Platform platform ) {
218
220
if (platform == platform ()) {
219
221
return this ;
220
222
}
221
- return new PathSettingFileData (setting , mode , platform );
223
+ return new PathSettingFileData (setting , mode , ignoreUrl , platform );
222
224
}
223
225
}
224
226
225
- private record RelativePathSettingFileData (String setting , BaseDir baseDir , Mode mode , Platform platform )
227
+ private record RelativePathSettingFileData (String setting , BaseDir baseDir , Mode mode , boolean ignoreUrl , Platform platform )
226
228
implements
227
229
FileData ,
228
230
RelativeFileData {
229
231
@ Override
230
232
public Stream <Path > resolveRelativePaths (PathLookup pathLookup ) {
231
- return resolvePathSettings (pathLookup , setting );
233
+ return resolvePathSettings (pathLookup , setting , ignoreUrl );
232
234
}
233
235
234
236
@ Override
235
237
public FileData withPlatform (Platform platform ) {
236
238
if (platform == platform ()) {
237
239
return this ;
238
240
}
239
- return new RelativePathSettingFileData (setting , baseDir , mode , platform );
241
+ return new RelativePathSettingFileData (setting , baseDir , mode , ignoreUrl , platform );
240
242
}
241
243
}
242
244
243
- private static Stream <Path > resolvePathSettings (PathLookup pathLookup , String setting ) {
245
+ private static Stream <Path > resolvePathSettings (PathLookup pathLookup , String setting , boolean ignoreUrl ) {
246
+ Stream <String > result ;
244
247
if (setting .contains ("*" )) {
245
- return pathLookup .settingGlobResolver ().apply (setting ).map (Path ::of );
248
+ result = pathLookup .settingGlobResolver ().apply (setting );
249
+ } else {
250
+ String path = pathLookup .settingResolver ().apply (setting );
251
+ result = path == null ? Stream .of () : Stream .of (path );
252
+ }
253
+ if (ignoreUrl ) {
254
+ result = result .filter (s -> s .toLowerCase (Locale .ROOT ).startsWith ("https://" ) == false );
246
255
}
247
- String path = pathLookup .settingResolver ().apply (setting );
248
- return path == null ? Stream .of () : Stream .of (Path .of (path ));
256
+ return result .map (Path ::of );
249
257
}
250
258
251
259
private static Mode parseMode (String mode ) {
@@ -298,6 +306,7 @@ public static FilesEntitlement build(List<Object> paths) {
298
306
String relativePathSetting = file .remove ("relative_path_setting" );
299
307
String modeAsString = file .remove ("mode" );
300
308
String platformAsString = file .remove ("platform" );
309
+ String ignoreUrlAsString = file .remove ("ignore_url" );
301
310
302
311
if (file .isEmpty () == false ) {
303
312
throw new PolicyValidationException ("unknown key(s) [" + file + "] in a listed file for files entitlement" );
@@ -324,6 +333,14 @@ public static FilesEntitlement build(List<Object> paths) {
324
333
baseDir = parseBaseDir (relativeTo );
325
334
}
326
335
336
+ boolean ignoreUrl = false ;
337
+ if (ignoreUrlAsString != null ) {
338
+ if (relativePathAsString != null || pathAsString != null ) {
339
+ throw new PolicyValidationException ("'ignore_url' may only be used with `path_setting` or `relative_path_setting`" );
340
+ }
341
+ ignoreUrl = Booleans .parseBoolean (ignoreUrlAsString );
342
+ }
343
+
327
344
final FileData fileData ;
328
345
if (relativePathAsString != null ) {
329
346
if (baseDir == null ) {
@@ -342,12 +359,12 @@ public static FilesEntitlement build(List<Object> paths) {
342
359
}
343
360
fileData = FileData .ofPath (path , mode );
344
361
} else if (pathSetting != null ) {
345
- fileData = FileData .ofPathSetting (pathSetting , mode );
362
+ fileData = FileData .ofPathSetting (pathSetting , mode , ignoreUrl );
346
363
} else if (relativePathSetting != null ) {
347
364
if (baseDir == null ) {
348
365
throw new PolicyValidationException ("files entitlement with a 'relative_path_setting' must specify 'relative_to'" );
349
366
}
350
- fileData = FileData .ofRelativePathSetting (relativePathSetting , baseDir , mode );
367
+ fileData = FileData .ofRelativePathSetting (relativePathSetting , baseDir , mode , ignoreUrl );
351
368
} else {
352
369
throw new AssertionError ("File entry validation error" );
353
370
}
0 commit comments