Skip to content

Commit

Permalink
feat: Checks that the passed URI belongs to this file system.
Browse files Browse the repository at this point in the history
Signed-off-by: Yang Yu <[email protected]>
  • Loading branch information
yuyang733 committed Dec 11, 2023
1 parent 5dd9781 commit 657d7eb
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 23 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<hadoop.version>3.3.3</hadoop.version>
<hadoop.version>2.8.5</hadoop.version>
<cos_api.version>5.6.137.2</cos_api.version>
<google.guava.version>24.1.1-jre</google.guava.version>
<commons_lang3.version>3.1</commons_lang3.version>
Expand Down
33 changes: 12 additions & 21 deletions src/main/java/org/apache/hadoop/fs/CosNFileSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -1527,28 +1527,19 @@ private Path makeAbsolute(Path path) {
return new Path(workingDir, path);
}

// key and path relate
public static String pathToKey(Path path) {
if (path.toUri().getScheme() != null && path.toUri().getPath().isEmpty()) {
// allow uris without trailing slash after bucket to refer to root,
// like cosn://mybucket
return "";
}
if (!path.isAbsolute()) {
throw new IllegalArgumentException("Path must be absolute: " + path);
}
String ret = path.toUri().getPath();
if (ret.endsWith("/") && (ret.indexOf("/") != ret.length() - 1)) {
ret = ret.substring(0, ret.length() - 1);
}
return ret;
/**
* 将一个 hadoop 的 path 路径转换为 COS 的 key 路径。
* 由于每个方法都会调用这个方法,因此这里调用了 checkPath 检查传入路径是否归属于当前的文件系统 URI。
*
* @param path hadoop 的 path 路径,原则上应是一个绝对路径。
* @return 传入的 hadoop 的 path 对应的 COS 的 key 路径。
*/
public String pathToKey(Path path) {
super.checkPath(path);
return CosNUtils.pathToKey(path);
}

public static Path keyToPath(String key) {
if (!key.startsWith(PATH_DELIMITER)) {
return new Path("/" + key);
} else {
return new Path(key);
}
public Path keyToPath(String key) {
return CosNUtils.keyToPath(key, PATH_DELIMITER);
}
}
23 changes: 23 additions & 0 deletions src/main/java/org/apache/hadoop/fs/CosNUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -309,4 +309,27 @@ public static Configuration propagateBucketOptions(Configuration source, String
return dest;
}

public static String pathToKey(Path path) {
if (path.toUri().getScheme() != null && path.toUri().getPath().isEmpty()) {
// allow uris without trailing slash after bucket to refer to root,
// like cosn://mybucket
return "";
}
if (!path.isAbsolute()) {
throw new IllegalArgumentException("Path must be absolute: " + path);
}
String ret = path.toUri().getPath();
if (ret.endsWith("/") && (ret.indexOf("/") != ret.length() - 1)) {
ret = ret.substring(0, ret.length() - 1);
}
return ret;
}

public static Path keyToPath(String key, String pathDelimiter) {
if (!key.startsWith(pathDelimiter)) {
return new Path("/" + key);
} else {
return new Path(key);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void doCheckPermission(Path f, RangerAccessType rangerAccessType, String
}

Path absolutePath = makeAbsolute(f, workingDirectory);
String allowKey = CosNFileSystem.pathToKey(absolutePath);
String allowKey = CosNUtils.pathToKey(absolutePath);
if (allowKey.startsWith("/")) {
allowKey = allowKey.substring(1);
}
Expand Down

0 comments on commit 657d7eb

Please sign in to comment.