Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.opensearch.javaagent.bootstrap.AgentPolicy;

import java.lang.instrument.Instrumentation;
import java.net.Socket;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;
import java.nio.file.Files;
Expand Down Expand Up @@ -71,8 +72,9 @@
initAgent(instrumentation);
}

private static AgentBuilder createAgentBuilder(Instrumentation inst) throws Exception {
final Junction<TypeDescription> systemType = ElementMatchers.isSubTypeOf(SocketChannel.class);
private static AgentBuilder createAgentBuilder() throws Exception {
final Junction<TypeDescription> socketType = ElementMatchers.isSubTypeOf(SocketChannel.class)
.or(ElementMatchers.isSubTypeOf(Socket.class));

Check warning on line 77 in libs/agent-sm/agent/src/main/java/org/opensearch/javaagent/Agent.java

View check run for this annotation

Codecov / codecov/patch

libs/agent-sm/agent/src/main/java/org/opensearch/javaagent/Agent.java#L76-L77

Added lines #L76 - L77 were not covered by tests
final Junction<TypeDescription> pathType = ElementMatchers.isSubTypeOf(Files.class);
final Junction<TypeDescription> fileChannelType = ElementMatchers.isSubTypeOf(FileChannel.class);

Expand All @@ -98,11 +100,11 @@
);

final ByteBuddy byteBuddy = new ByteBuddy().with(Implementation.Context.Disabled.Factory.INSTANCE);
final AgentBuilder agentBuilder = new AgentBuilder.Default(byteBuddy).with(AgentBuilder.InitializationStrategy.NoOp.INSTANCE)
return new AgentBuilder.Default(byteBuddy).with(AgentBuilder.InitializationStrategy.NoOp.INSTANCE)

Check warning on line 103 in libs/agent-sm/agent/src/main/java/org/opensearch/javaagent/Agent.java

View check run for this annotation

Codecov / codecov/patch

libs/agent-sm/agent/src/main/java/org/opensearch/javaagent/Agent.java#L103

Added line #L103 was not covered by tests
.with(AgentBuilder.RedefinitionStrategy.REDEFINITION)
.with(AgentBuilder.TypeStrategy.Default.REDEFINE)
.ignore(ElementMatchers.nameContains("$MockitoMock$")) /* ingore all Mockito mocks */
.type(systemType)
.type(socketType)

Check warning on line 107 in libs/agent-sm/agent/src/main/java/org/opensearch/javaagent/Agent.java

View check run for this annotation

Codecov / codecov/patch

libs/agent-sm/agent/src/main/java/org/opensearch/javaagent/Agent.java#L107

Added line #L107 was not covered by tests
.transform(socketTransformer)
.type(pathType.or(fileChannelType))
.transform(fileTransformer)
Expand All @@ -118,12 +120,10 @@
Advice.to(RuntimeHaltInterceptor.class).on(ElementMatchers.named("halt"))
)
);

return agentBuilder;
}

private static void initAgent(Instrumentation instrumentation) throws Exception {
AgentBuilder agentBuilder = createAgentBuilder(instrumentation);
AgentBuilder agentBuilder = createAgentBuilder();

Check warning on line 126 in libs/agent-sm/agent/src/main/java/org/opensearch/javaagent/Agent.java

View check run for this annotation

Codecov / codecov/patch

libs/agent-sm/agent/src/main/java/org/opensearch/javaagent/Agent.java#L126

Added line #L126 was not covered by tests
agentBuilder.installOn(instrumentation);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnixDomainSocketAddress;
import java.nio.channels.SocketChannel;

Expand All @@ -28,6 +29,8 @@ public void testConnections() throws IOException {

assertThrows(SecurityException.class, () -> channel.connect(new InetSocketAddress("opensearch.org", 80)));
}

assertThrows(SecurityException.class, () -> new Socket("localhost", 9200));
}

@Test
Expand Down
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.
*/

grant {
permission java.net.SocketPermission "*", "connect";
};
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.
*/

grant {
permission java.net.SocketPermission "*", "connect";
};
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.
*/

grant {
permission java.net.SocketPermission "*", "connect";
};
Loading