Skip to content

Commit 26ce310

Browse files
committed
Address PR comments
1 parent 1fb2a09 commit 26ce310

File tree

4 files changed

+35
-36
lines changed

4 files changed

+35
-36
lines changed

libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/GrantNode.java renamed to libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/GrantEntry.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
import java.util.Enumeration;
1313
import java.util.LinkedList;
1414

15-
public class GrantNode {
15+
public class GrantEntry {
1616
public String codeBase;
17-
private final LinkedList<PermissionNode> permissionEntries = new LinkedList<>();
17+
private final LinkedList<PermissionEntry> permissionEntries = new LinkedList<>();
1818

19-
public void add(PermissionNode entry) {
19+
public void add(PermissionEntry entry) {
2020
permissionEntries.add(entry);
2121
}
2222

23-
public Enumeration<PermissionNode> permissionElements() {
23+
public Enumeration<PermissionEntry> permissionElements() {
2424
return Collections.enumeration(permissionEntries);
2525
}
2626

@@ -32,7 +32,7 @@ public void write(PrintWriter out) {
3232
out.print("\"");
3333
}
3434
out.println(" {");
35-
for (PermissionNode pe : permissionEntries) {
35+
for (PermissionEntry pe : permissionEntries) {
3636
out.print(" permission ");
3737
out.print(pe.permission);
3838
if (pe.name != null) {

libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PermissionNode.java renamed to libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PermissionEntry.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import java.io.PrintWriter;
1111
import java.util.Objects;
1212

13-
public class PermissionNode {
13+
public class PermissionEntry {
1414
public String permission;
1515
public String name;
1616
public String action;
@@ -24,7 +24,7 @@ public int hashCode() {
2424
public boolean equals(Object obj) {
2525
if (obj == this) return true;
2626

27-
return obj instanceof PermissionNode that
27+
return obj instanceof PermissionEntry that
2828
&& Objects.equals(this.permission, that.permission)
2929
&& Objects.equals(this.name, that.name)
3030
&& Objects.equals(this.action, that.action);

libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ public class PolicyFile extends java.security.Policy {
4444
"org.opensearch.SpecialPermission",
4545
"org.bouncycastle.crypto.CryptoServicesPermission",
4646
"org.opensearch.script.ClassPermission",
47-
"javax.security.auth.AuthPermission"
47+
"javax.security.auth.AuthPermission",
48+
"javax.security.auth.kerberos.ServicePermission"
4849
);
4950

5051
private static final int DEFAULT_CACHE_SIZE = 1;
@@ -76,8 +77,8 @@ private void init(URL policy, PolicyInfo newInfo) throws PolicyInitializationExc
7677
PolicyParser policyParser = new PolicyParser();
7778
policyParser.read(reader);
7879

79-
for (GrantNode grantNode : Collections.list(policyParser.grantElements())) {
80-
addGrantNode(grantNode, newInfo);
80+
for (GrantEntry grantEntry : Collections.list(policyParser.grantElements())) {
81+
addGrantEntry(grantEntry, newInfo);
8182
}
8283

8384
} catch (Exception e) {
@@ -95,7 +96,7 @@ public static InputStream getInputStream(URL url) throws IOException {
9596
}
9697
}
9798

98-
private CodeSource getCodeSource(GrantNode grantEntry, PolicyInfo newInfo) throws PolicyInitializationException {
99+
private CodeSource getCodeSource(GrantEntry grantEntry, PolicyInfo newInfo) throws PolicyInitializationException {
99100
try {
100101
Certificate[] certs = null;
101102
URL location = (grantEntry.codeBase != null) ? newURL(grantEntry.codeBase) : null;
@@ -105,16 +106,16 @@ private CodeSource getCodeSource(GrantNode grantEntry, PolicyInfo newInfo) throw
105106
}
106107
}
107108

108-
private void addGrantNode(GrantNode grantEntry, PolicyInfo newInfo) throws PolicyInitializationException {
109+
private void addGrantEntry(GrantEntry grantEntry, PolicyInfo newInfo) throws PolicyInitializationException {
109110
CodeSource codesource = getCodeSource(grantEntry, newInfo);
110111
if (codesource == null) {
111112
throw new PolicyInitializationException("Null CodeSource for: " + grantEntry.codeBase);
112113
}
113114

114115
PolicyEntry entry = new PolicyEntry(codesource);
115-
Enumeration<PermissionNode> enum_ = grantEntry.permissionElements();
116+
Enumeration<PermissionEntry> enum_ = grantEntry.permissionElements();
116117
while (enum_.hasMoreElements()) {
117-
PermissionNode pe = enum_.nextElement();
118+
PermissionEntry pe = enum_.nextElement();
118119
expandPermissionName(pe);
119120
try {
120121
Optional<Permission> perm = getInstance(pe.permission, pe.name, pe.action);
@@ -136,7 +137,7 @@ private void addGrantNode(GrantNode grantEntry, PolicyInfo newInfo) throws Polic
136137
newInfo.policyEntries.add(entry);
137138
}
138139

139-
private void expandPermissionName(PermissionNode pe) {
140+
private void expandPermissionName(PermissionEntry pe) {
140141
if (pe.name == null || !pe.name.contains("${{")) {
141142
return;
142143
}

libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyParser.java

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616
import java.io.Reader;
1717
import java.io.StreamTokenizer;
1818
import java.util.Enumeration;
19+
import java.util.Optional;
1920
import java.util.Vector;
2021

2122
public class PolicyParser {
2223

23-
private final Vector<GrantNode> grantEntries = new Vector<>();
24+
private final Vector<GrantEntry> grantEntries = new Vector<>();
2425
private TokenStream tokenStream;
2526

2627
public PolicyParser() {}
@@ -34,11 +35,7 @@ public void read(Reader policy) throws ParsingException, IOException {
3435

3536
while (!tokenStream.isEOF()) {
3637
if (peek("grant")) {
37-
GrantNode grantNode = parseGrantEntry();
38-
39-
if (grantNode != null) {
40-
addGrantNode(grantNode);
41-
}
38+
parseGrantEntry().ifPresent(this::addGrantEntry);
4239
}
4340
}
4441
}
@@ -81,23 +78,24 @@ private String poll(String expected) throws IOException, ParsingException {
8178
throw new ParsingException(token.line, expected, token.text);
8279
}
8380

84-
private GrantNode parseGrantEntry() throws ParsingException, IOException {
85-
GrantNode grantNode = new GrantNode();
81+
private Optional<GrantEntry> parseGrantEntry() throws ParsingException, IOException {
82+
GrantEntry grantEntry = new GrantEntry();
8683
poll("grant");
8784

8885
while (!peek("{")) {
8986
if (pollOnMatch("Codebase")) {
90-
if (grantNode.codeBase != null) {
87+
if (grantEntry.codeBase != null) {
9188
throw new ParsingException(tokenStream.line(), "Multiple Codebase expressions");
9289
}
9390

9491
String rawCodebase = poll(tokenStream.peek().text);
9592
try {
96-
grantNode.codeBase = PropertyExpander.expand(rawCodebase, true).replace(File.separatorChar, '/');
93+
grantEntry.codeBase = PropertyExpander.expand(rawCodebase, true).replace(File.separatorChar, '/');
9794
} catch (ExpandException e) {
9895
// skip this grant as expansion failed due to missing expansion property.
9996
skipCurrentGrantBlock();
100-
return null;
97+
98+
return Optional.empty();
10199
}
102100
pollOnMatch(",");
103101
} else {
@@ -109,8 +107,8 @@ private GrantNode parseGrantEntry() throws ParsingException, IOException {
109107

110108
while (!peek("}")) {
111109
if (peek("Permission")) {
112-
PermissionNode permissionEntry = parsePermissionEntry();
113-
grantNode.add(permissionEntry);
110+
PermissionEntry permissionEntry = parsePermissionEntry();
111+
grantEntry.add(permissionEntry);
114112
poll(";");
115113
} else {
116114
throw new ParsingException(tokenStream.line(), "Expected permission entry");
@@ -123,11 +121,11 @@ private GrantNode parseGrantEntry() throws ParsingException, IOException {
123121
poll(";");
124122
}
125123

126-
if (grantNode.codeBase != null) {
127-
grantNode.codeBase = grantNode.codeBase.replace(File.separatorChar, '/');
124+
if (grantEntry.codeBase != null) {
125+
grantEntry.codeBase = grantEntry.codeBase.replace(File.separatorChar, '/');
128126
}
129127

130-
return grantNode;
128+
return Optional.of(grantEntry);
131129
}
132130

133131
private void skipCurrentGrantBlock() throws IOException, ParsingException {
@@ -161,8 +159,8 @@ private void skipCurrentGrantBlock() throws IOException, ParsingException {
161159
}
162160
}
163161

164-
private PermissionNode parsePermissionEntry() throws ParsingException, IOException {
165-
PermissionNode permissionEntry = new PermissionNode();
162+
private PermissionEntry parsePermissionEntry() throws ParsingException, IOException {
163+
PermissionEntry permissionEntry = new PermissionEntry();
166164
poll("Permission");
167165
permissionEntry.permission = poll(tokenStream.peek().text);
168166

@@ -185,11 +183,11 @@ private boolean isQuotedToken(Token token) {
185183
return token.type == '"' || token.type == '\'';
186184
}
187185

188-
public void addGrantNode(GrantNode grantNode) {
189-
grantEntries.addElement(grantNode);
186+
public void addGrantEntry(GrantEntry grantEntry) {
187+
grantEntries.addElement(grantEntry);
190188
}
191189

192-
public Enumeration<GrantNode> grantElements() {
190+
public Enumeration<GrantEntry> grantElements() {
193191
return grantEntries.elements();
194192
}
195193

0 commit comments

Comments
 (0)