forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
183 additions
and
29 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
...ntitlement/src/main/java/org/elasticsearch/entitlement/runtime/policy/FileAccessTree.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
package org.elasticsearch.entitlement.runtime.policy; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
class FileAccessTree { | ||
static final FileAccessTree EMPTY = new FileAccessTree(List.of()); | ||
|
||
private final String[] readPaths; | ||
private final String[] writePaths; | ||
|
||
FileAccessTree(List<FileEntitlement> fileEntitlements) { | ||
List<String> readPaths = new ArrayList<>(); | ||
List<String> writePaths = new ArrayList<>(); | ||
for (FileEntitlement fileEntitlement : fileEntitlements) { | ||
var mode = fileEntitlement.mode(); | ||
if (mode == FileEntitlement.Mode.READ_WRITE) { | ||
writePaths.add(fileEntitlement.path()); | ||
} | ||
readPaths.add(fileEntitlement.path()); | ||
} | ||
|
||
readPaths.sort(String::compareTo); | ||
writePaths.sort(String::compareTo); | ||
|
||
this.readPaths = readPaths.toArray(new String[0]); | ||
this.writePaths = writePaths.toArray(new String[0]); | ||
} | ||
|
||
boolean canRead(Path path) { | ||
return checkPath(normalize(path), readPaths); | ||
} | ||
|
||
boolean canRead(File file) { | ||
return checkPath(normalize(file.toPath()), readPaths); | ||
} | ||
|
||
boolean canWrite(Path path) { | ||
return checkPath(normalize(path), writePaths); | ||
} | ||
|
||
boolean canWrite(File file) { | ||
return checkPath(normalize(file.toPath()), writePaths); | ||
} | ||
|
||
private static String normalize(Path path) { | ||
return path.toAbsolutePath().normalize().toString(); | ||
} | ||
|
||
private static boolean checkPath(String path, String[] paths) { | ||
if (paths.length == 0) { | ||
return false; | ||
} | ||
int ndx = Arrays.binarySearch(paths, path); | ||
if (ndx < -1) { | ||
String maybeParent = paths[-ndx - 2]; | ||
return path.startsWith(maybeParent); | ||
} | ||
return ndx >= 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
...ement/src/test/java/org/elasticsearch/entitlement/runtime/policy/FileAccessTreeTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
package org.elasticsearch.entitlement.runtime.policy; | ||
|
||
import org.elasticsearch.test.ESTestCase; | ||
|
||
import java.util.List; | ||
|
||
import static org.hamcrest.Matchers.is; | ||
|
||
public class FileAccessTreeTests extends ESTestCase { | ||
|
||
public void testEmpty() { | ||
var tree = new FileAccessTree(List.of()); | ||
assertThat(tree.canRead("/path"), is(false)); | ||
assertThat(tree.canWrite("/path"), is(false)); | ||
} | ||
|
||
public void testRead() { | ||
var tree = new FileAccessTree(List.of(new FileEntitlement("/foo", "read"))); | ||
assertThat(tree.canRead("/foo"), is(true)); | ||
assertThat(tree.canRead("/foo/subdir"), is(true)); | ||
assertThat(tree.canWrite("/foo"), is(false)); | ||
|
||
assertThat(tree.canRead("/before"), is(false)); | ||
assertThat(tree.canRead("/later"), is(false)); | ||
} | ||
|
||
public void testWrite() { | ||
var tree = new FileAccessTree(List.of(new FileEntitlement("/foo", "read_write"))); | ||
assertThat(tree.canWrite("/foo"), is(true)); | ||
assertThat(tree.canWrite("/foo/subdir"), is(true)); | ||
assertThat(tree.canRead("/foo"), is(true)); | ||
|
||
assertThat(tree.canWrite("/before"), is(false)); | ||
assertThat(tree.canWrite("/later"), is(false)); | ||
} | ||
|
||
public void testTwoPaths() { | ||
var tree = new FileAccessTree(List.of(new FileEntitlement("/foo", "read"), new FileEntitlement("/bar", "read"))); | ||
assertThat(tree.canRead("/a"), is(false)); | ||
assertThat(tree.canRead("/bar"), is(true)); | ||
assertThat(tree.canRead("/bar/subdir"), is(true)); | ||
assertThat(tree.canRead("/c"), is(false)); | ||
assertThat(tree.canRead("/foo"), is(true)); | ||
assertThat(tree.canRead("/foo/subdir"), is(true)); | ||
assertThat(tree.canRead("/z"), is(false)); | ||
} | ||
|
||
public void testReadWriteUnderRead() { | ||
var tree = new FileAccessTree(List.of(new FileEntitlement("/foo", "read"), new FileEntitlement("/foo/bar", "read_write"))); | ||
assertThat(tree.canRead("/foo"), is(true)); | ||
assertThat(tree.canWrite("/foo"), is(false)); | ||
assertThat(tree.canRead("/foo/bar"), is(true)); | ||
assertThat(tree.canWrite("/foo/bar"), is(true)); | ||
} | ||
|
||
public void testNormalizePath() { | ||
var tree = new FileAccessTree(List.of(new FileEntitlement("/foo/../bar", "read"))); | ||
assertThat(tree.canRead("/foo/../bar"), is(true)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters