Skip to content

Add a policy parser for Java Agent #17753

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- [Security Manager Replacement] Create initial Java Agent to intercept Socket::connect calls ([#17724](https://github.com/opensearch-project/OpenSearch/pull/17724))
- Add ingestion management APIs for pause, resume and get ingestion state ([#17631](https://github.com/opensearch-project/OpenSearch/pull/17631))
- [Security Manager Replacement] Enhance Java Agent to intercept System::exit ([#17746](https://github.com/opensearch-project/OpenSearch/pull/17746))
- [Security Manager Replacement] Add a policy parser for Java agent security policies ([#17753](https://github.com/opensearch-project/OpenSearch/pull/17753))
- [Security Manager Replacement] Implement File Interceptor and add integration tests ([#17760](https://github.com/opensearch-project/OpenSearch/pull/17760))
- [Security Manager Replacement] Enhance Java Agent to intercept Runtime::halt ([#17757](https://github.com/opensearch-project/OpenSearch/pull/17757))
- Support AutoExpand for SearchReplica ([#17741](https://github.com/opensearch-project/OpenSearch/pull/17741))
Expand Down
1 change: 1 addition & 0 deletions gradle/missing-javadoc.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ configure([
project(":libs:opensearch-secure-sm"),
project(":libs:opensearch-ssl-config"),
project(":libs:opensearch-x-content"),
project(":libs:agent-sm:agent-policy"),
project(":modules:aggs-matrix-stats"),
project(":modules:analysis-common"),
project(":modules:geo"),
Expand Down
27 changes: 27 additions & 0 deletions libs/agent-sm/agent-policy/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

apply plugin: 'opensearch.build'
apply plugin: 'opensearch.publish'

ext {
failOnJavadocWarning = false
}

base {
archivesName = 'opensearch-agent-policy'
}

disableTasks('forbiddenApisMain')

dependencies {
testImplementation(project(":test:framework"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kumargu sorry I am late - we cannot depend on :test:framework here since it will introduce cycle: :test:framework -> :agent -> :agent-policy :(

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
package org.opensearch.secure_sm.policy;

import java.util.List;

public record GrantEntry(String codeBase, List<PermissionEntry> permissionEntries) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
package org.opensearch.secure_sm.policy;

public record PermissionEntry(String permission, String name, String action) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,320 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.secure_sm.policy;

import java.io.File;
import java.io.FileInputStream;
import java.io.FilePermission;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.NetPermission;
import java.net.SocketPermission;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.security.AllPermission;
import java.security.CodeSource;
import java.security.Permission;
import java.security.PermissionCollection;
import java.security.Permissions;
import java.security.ProtectionDomain;
import java.security.SecurityPermission;
import java.security.cert.Certificate;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Optional;
import java.util.PropertyPermission;
import java.util.Set;

@SuppressWarnings("removal")
public class PolicyFile extends java.security.Policy {
public static final Set<String> PERM_CLASSES_TO_SKIP = Set.of(

Check warning on line 42 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L42 was not covered by tests
"org.opensearch.secure_sm.ThreadContextPermission",
"org.opensearch.secure_sm.ThreadPermission",
"org.opensearch.SpecialPermission",
"org.bouncycastle.crypto.CryptoServicesPermission",
"org.opensearch.script.ClassPermission",
"javax.security.auth.AuthPermission",
"javax.security.auth.kerberos.ServicePermission"
);

private final PolicyInfo policyInfo;
private final URL url;

public PolicyFile(URL url) {
this.url = url;

Check warning on line 56 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L55 - L56 were not covered by tests
try {
policyInfo = init(url);
} catch (PolicyInitializationException e) {
throw new RuntimeException("Failed to initialize policy file", e);
}
}

Check warning on line 62 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L58 - L62 were not covered by tests

private PolicyInfo init(URL policy) throws PolicyInitializationException {
PolicyInfo info = new PolicyInfo();
try (InputStreamReader reader = new InputStreamReader(getInputStream(policy), StandardCharsets.UTF_8)) {
List<GrantEntry> grantEntries = PolicyParser.read(reader);

Check warning on line 67 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L65 - L67 were not covered by tests
for (GrantEntry grantEntry : grantEntries) {
addGrantEntry(grantEntry, info);
}
} catch (Exception e) {
throw new PolicyInitializationException("Failed to load policy from: " + policy, e);
}
return info;

Check warning on line 74 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L69 - L74 were not covered by tests
}

public static InputStream getInputStream(URL url) throws IOException {
if ("file".equals(url.getProtocol())) {
String path = url.getFile().replace('/', File.separatorChar);
path = URLDecoder.decode(path, StandardCharsets.UTF_8);
return new FileInputStream(path);

Check warning on line 81 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L79 - L81 were not covered by tests
} else {
return url.openStream();

Check warning on line 83 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L83 was not covered by tests
}
}

private CodeSource getCodeSource(GrantEntry grantEntry) throws PolicyInitializationException {
try {
Certificate[] certs = null;

Check warning on line 89 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L89 was not covered by tests
URL location = (grantEntry.codeBase() != null) ? newURL(grantEntry.codeBase()) : null;
return canonicalizeCodebase(new CodeSource(location, certs));
} catch (Exception e) {
throw new PolicyInitializationException("Failed to get CodeSource", e);

Check warning on line 93 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L91 - L93 were not covered by tests
}
}

private void addGrantEntry(GrantEntry grantEntry, PolicyInfo newInfo) throws PolicyInitializationException {
CodeSource codesource = getCodeSource(grantEntry);

Check warning on line 98 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L98 was not covered by tests
if (codesource == null) {
throw new PolicyInitializationException("Null CodeSource for: " + grantEntry.codeBase());

Check warning on line 100 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L100 was not covered by tests
}

List<Permission> permissions = new ArrayList<>();
List<PermissionEntry> permissionList = grantEntry.permissionEntries();

Check warning on line 104 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L103 - L104 were not covered by tests
for (PermissionEntry pe : permissionList) {
final PermissionEntry expandedEntry = expandPermissionName(pe);

Check warning on line 106 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L106 was not covered by tests
try {
Optional<Permission> perm = getInstance(expandedEntry.permission(), expandedEntry.name(), expandedEntry.action());

Check warning on line 108 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L108 was not covered by tests
if (perm.isPresent()) {
permissions.add(perm.get());

Check warning on line 110 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L110 was not covered by tests
}
} catch (ClassNotFoundException e) {

Check warning on line 112 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L112 was not covered by tests
// these were mostly custom permission classes added for security
// manager. Since security manager is deprecated, we can skip these
// permissions classes.
if (PERM_CLASSES_TO_SKIP.contains(pe.permission())) {
continue; // skip this permission

Check warning on line 117 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L117 was not covered by tests
}
throw new PolicyInitializationException("Permission class not found: " + pe.permission(), e);
}
}
newInfo.policyEntries.add(new PolicyEntry(codesource, permissions));
}

Check warning on line 123 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L119 - L123 were not covered by tests

private static PermissionEntry expandPermissionName(PermissionEntry pe) {
if (pe.name() == null || !pe.name().contains("${{")) {
return pe;

Check warning on line 127 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L127 was not covered by tests
}

int startIndex = 0;

Check warning on line 130 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L130 was not covered by tests
int b, e;
StringBuilder sb = new StringBuilder();

Check warning on line 132 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L132 was not covered by tests

while ((b = pe.name().indexOf("${{", startIndex)) != -1 && (e = pe.name().indexOf("}}", b)) != -1) {
sb.append(pe.name(), startIndex, b);
String value = pe.name().substring(b + 3, e);
sb.append("${{").append(value).append("}}");
startIndex = e + 2;
}

Check warning on line 139 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L135 - L139 were not covered by tests

sb.append(pe.name().substring(startIndex));
return new PermissionEntry(pe.permission(), sb.toString(), pe.action());

Check warning on line 142 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L141 - L142 were not covered by tests
}

private static final Optional<Permission> getInstance(String type, String name, String actions) throws ClassNotFoundException {
Class<?> pc = Class.forName(type, false, null);
Permission answer = getKnownPermission(pc, name, actions);

Check warning on line 147 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L146 - L147 were not covered by tests

return Optional.ofNullable(answer);

Check warning on line 149 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L149 was not covered by tests
}

private static Permission getKnownPermission(Class<?> claz, String name, String actions) {
if (claz.equals(FilePermission.class)) {
return new FilePermission(name, actions);

Check warning on line 154 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L154 was not covered by tests
} else if (claz.equals(SocketPermission.class)) {
return new SocketPermission(name, actions);

Check warning on line 156 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L156 was not covered by tests
} else if (claz.equals(RuntimePermission.class)) {
return new RuntimePermission(name, actions);

Check warning on line 158 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L158 was not covered by tests
} else if (claz.equals(PropertyPermission.class)) {
return new PropertyPermission(name, actions);

Check warning on line 160 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L160 was not covered by tests
} else if (claz.equals(NetPermission.class)) {
return new NetPermission(name, actions);

Check warning on line 162 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L162 was not covered by tests
} else if (claz.equals(AllPermission.class)) {
return new AllPermission();

Check warning on line 164 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L164 was not covered by tests
} else if (claz.equals(SecurityPermission.class)) {
return new SecurityPermission(name, actions);

Check warning on line 166 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L166 was not covered by tests
} else {
return null;

Check warning on line 168 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L168 was not covered by tests
}
}

@Override
public void refresh() {
try {
init(url);
} catch (PolicyInitializationException e) {
throw new RuntimeException("Failed to refresh policy", e);
}
}

Check warning on line 179 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L175 - L179 were not covered by tests

@Override
public boolean implies(ProtectionDomain pd, Permission p) {
PermissionCollection pc = getPermissions(pd);

Check warning on line 183 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L183 was not covered by tests
return pc != null && pc.implies(p);
}

@Override
public PermissionCollection getPermissions(ProtectionDomain domain) {
Permissions perms = new Permissions();

Check warning on line 189 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L189 was not covered by tests
if (domain == null) return perms;

try {
getPermissionsForProtectionDomain(perms, domain);
} catch (PolicyInitializationException e) {
throw new RuntimeException("Failed to get permissions for domain", e);
}

Check warning on line 196 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L193 - L196 were not covered by tests

PermissionCollection pc = domain.getPermissions();

Check warning on line 198 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L198 was not covered by tests
if (pc != null) {
synchronized (pc) {
Enumeration<Permission> e = pc.elements();

Check warning on line 201 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L200 - L201 were not covered by tests
while (e.hasMoreElements()) {
perms.add(e.nextElement());

Check warning on line 203 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L203 was not covered by tests
}
}

Check warning on line 205 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L205 was not covered by tests
}

return perms;

Check warning on line 208 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L208 was not covered by tests
}

@Override
public PermissionCollection getPermissions(CodeSource codesource) {
if (codesource == null) return new Permissions();

Permissions perms = new Permissions();

Check warning on line 215 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L215 was not covered by tests
CodeSource canonicalCodeSource;

try {
canonicalCodeSource = canonicalizeCodebase(codesource);
} catch (PolicyInitializationException e) {
throw new RuntimeException("Failed to canonicalize CodeSource", e);
}

Check warning on line 222 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L219 - L222 were not covered by tests

for (PolicyEntry entry : policyInfo.policyEntries) {
if (entry.codeSource().implies(canonicalCodeSource)) {
for (Permission permission : entry.permissions) {
perms.add(permission);
}

Check warning on line 228 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L227 - L228 were not covered by tests
}
}

Check warning on line 230 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L230 was not covered by tests

return perms;

Check warning on line 232 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L232 was not covered by tests
}

private void getPermissionsForProtectionDomain(Permissions perms, ProtectionDomain pd) throws PolicyInitializationException {
final CodeSource cs = pd.getCodeSource();

Check warning on line 236 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L236 was not covered by tests
if (cs == null) return;

CodeSource canonicalCodeSource = canonicalizeCodebase(cs);

Check warning on line 239 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L239 was not covered by tests

for (PolicyEntry entry : policyInfo.policyEntries) {
if (entry.codeSource().implies(canonicalCodeSource)) {
for (Permission permission : entry.permissions) {
perms.add(permission);
}

Check warning on line 245 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L244 - L245 were not covered by tests
}
}
}

Check warning on line 248 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L247 - L248 were not covered by tests

private CodeSource canonicalizeCodebase(CodeSource cs) throws PolicyInitializationException {
URL location = cs.getLocation();

Check warning on line 251 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L251 was not covered by tests
if (location == null) return cs;

try {
URL canonicalUrl = canonicalizeUrl(location);
return new CodeSource(canonicalUrl, cs.getCertificates());
} catch (IOException e) {
throw new PolicyInitializationException("Failed to canonicalize CodeSource", e);

Check warning on line 258 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L255 - L258 were not covered by tests
}
}

@SuppressWarnings("deprecation")
private URL canonicalizeUrl(URL url) throws IOException {
String protocol = url.getProtocol();

Check warning on line 264 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L264 was not covered by tests

if ("jar".equals(protocol)) {
String spec = url.getFile();
int separator = spec.indexOf("!/");

Check warning on line 268 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L267 - L268 were not covered by tests
if (separator != -1) {
try {
url = new URL(spec.substring(0, separator));
} catch (MalformedURLException e) {
throw new IOException("Malformed nested jar URL", e);
}

Check warning on line 274 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L271 - L274 were not covered by tests
}
}

if ("file".equals(url.getProtocol())) {
String path = url.getPath();
path = canonicalizePath(path);
return new File(path).toURI().toURL();

Check warning on line 281 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L279 - L281 were not covered by tests
}

return url;

Check warning on line 284 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L284 was not covered by tests
}

private String canonicalizePath(String path) throws IOException {
if (path.endsWith("*")) {
path = path.substring(0, path.length() - 1);
return new File(path).getCanonicalPath() + "*";

Check warning on line 290 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L289 - L290 were not covered by tests
} else {
return new File(path).getCanonicalPath();

Check warning on line 292 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L292 was not covered by tests
}
}

private record PolicyEntry(CodeSource codeSource, List<Permission> permissions) {

Check warning on line 296 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L296 was not covered by tests
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("{").append(codeSource).append("\n");

Check warning on line 300 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L299 - L300 were not covered by tests
for (Permission p : permissions) {
sb.append(" ").append(p).append("\n");
}
sb.append("}\n");
return sb.toString();

Check warning on line 305 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L302 - L305 were not covered by tests
}
}

private static class PolicyInfo {
final List<PolicyEntry> policyEntries;

PolicyInfo() {
policyEntries = new ArrayList<>();
}

Check warning on line 314 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L312 - L314 were not covered by tests
}

private static URL newURL(String spec) throws MalformedURLException, URISyntaxException {
return new URI(spec).toURL();

Check warning on line 318 in libs/agent-sm/agent-policy/src/main/java/org/opensearch/secure_sm/policy/PolicyFile.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L318 was not covered by tests
}
}
Loading
Loading