Skip to content

Commit 49d2dde

Browse files
committed
Add a policy parser for java agent
Signed-off-by: Gulshan <kumargu@amazon.com>
1 parent 15d27a1 commit 49d2dde

File tree

14 files changed

+998
-0
lines changed

14 files changed

+998
-0
lines changed

gradle/missing-javadoc.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ configure([
106106
project(":libs:opensearch-secure-sm"),
107107
project(":libs:opensearch-ssl-config"),
108108
project(":libs:opensearch-x-content"),
109+
project(":libs:agent-sm:agent-policy"),
109110
project(":modules:aggs-matrix-stats"),
110111
project(":modules:analysis-common"),
111112
project(":modules:geo"),
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*
8+
* Modifications Copyright OpenSearch Contributors. See
9+
* GitHub history for details.
10+
*/
11+
12+
apply plugin: 'opensearch.build'
13+
apply plugin: 'opensearch.publish'
14+
15+
ext {
16+
failOnJavadocWarning = false
17+
}
18+
19+
base {
20+
archivesName = 'opensearch-agent-policy'
21+
}
22+
23+
disableTasks('forbiddenApisMain')
24+
25+
dependencies {
26+
testImplementation(project(":test:framework"))
27+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
package org.opensearch.secure_sm.policy;
9+
10+
import java.io.PrintWriter;
11+
import java.util.Collections;
12+
import java.util.Enumeration;
13+
import java.util.LinkedList;
14+
15+
public class GrantNode {
16+
public String codeBase;
17+
private final LinkedList<PermissionNode> permissionEntries = new LinkedList<>();
18+
19+
public void add(PermissionNode entry) {
20+
permissionEntries.add(entry);
21+
}
22+
23+
public Enumeration<PermissionNode> permissionElements() {
24+
return Collections.enumeration(permissionEntries);
25+
}
26+
27+
public void write(PrintWriter out) {
28+
out.print("grant");
29+
if (codeBase != null) {
30+
out.print(" Codebase \"");
31+
out.print(codeBase);
32+
out.print("\"");
33+
}
34+
out.println(" {");
35+
for (PermissionNode pe : permissionEntries) {
36+
out.print(" permission ");
37+
out.print(pe.permission);
38+
if (pe.name != null) {
39+
out.print(" \"");
40+
out.print(pe.name);
41+
out.print("\"");
42+
}
43+
if (pe.action != null) {
44+
out.print(", \"");
45+
out.print(pe.action);
46+
out.print("\"");
47+
}
48+
out.println(";");
49+
}
50+
out.println("};");
51+
}
52+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
package org.opensearch.secure_sm.policy;
9+
10+
import java.io.PrintWriter;
11+
import java.util.Objects;
12+
13+
public class PermissionNode {
14+
public String permission;
15+
public String name;
16+
public String action;
17+
18+
@Override
19+
public int hashCode() {
20+
return Objects.hash(permission, name, action);
21+
}
22+
23+
@Override
24+
public boolean equals(Object obj) {
25+
if (obj == this) return true;
26+
27+
return obj instanceof PermissionNode that
28+
&& Objects.equals(this.permission, that.permission)
29+
&& Objects.equals(this.name, that.name)
30+
&& Objects.equals(this.action, that.action);
31+
}
32+
33+
public void write(PrintWriter out) {
34+
out.print("permission ");
35+
out.print(permission);
36+
if (name != null) {
37+
out.print(" \"");
38+
out.print(name.replaceAll("\\\\", "\\\\\\\\").replaceAll("\"", "\\\\\\\""));
39+
out.print('"');
40+
}
41+
if (action != null) {
42+
out.print(", \"");
43+
out.print(action);
44+
out.print('"');
45+
}
46+
out.println(";");
47+
}
48+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.secure_sm.policy;
10+
11+
import java.security.CodeSource;
12+
import java.security.Permission;
13+
import java.util.ArrayList;
14+
import java.util.List;
15+
16+
public class PolicyEntry {
17+
18+
private final CodeSource codesource;
19+
final List<Permission> permissions;
20+
21+
PolicyEntry(CodeSource cs) {
22+
this.codesource = cs;
23+
this.permissions = new ArrayList<Permission>();
24+
}
25+
26+
/**
27+
* add a Permission object to this entry.
28+
* No need to sync add op because perms are added to entry only
29+
* while entry is being initialized
30+
*/
31+
void add(Permission p) {
32+
permissions.add(p);
33+
}
34+
35+
CodeSource getCodeSource() {
36+
return codesource;
37+
}
38+
39+
@Override
40+
public String toString() {
41+
StringBuilder sb = new StringBuilder();
42+
sb.append("{");
43+
sb.append(getCodeSource());
44+
sb.append("\n");
45+
for (int j = 0; j < permissions.size(); j++) {
46+
Permission p = permissions.get(j);
47+
sb.append(" ");
48+
sb.append(" ");
49+
sb.append(p);
50+
sb.append("\n");
51+
}
52+
sb.append("}");
53+
sb.append("\n");
54+
return sb.toString();
55+
}
56+
}

0 commit comments

Comments
 (0)