Skip to content

Commit f211d34

Browse files
authored
Intercept java.net.Socket::connect (#17954)
Signed-off-by: Andrew Ross <[email protected]>
1 parent 7372360 commit f211d34

File tree

5 files changed

+43
-7
lines changed

5 files changed

+43
-7
lines changed

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

+7-7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.opensearch.javaagent.bootstrap.AgentPolicy;
1212

1313
import java.lang.instrument.Instrumentation;
14+
import java.net.Socket;
1415
import java.nio.channels.FileChannel;
1516
import java.nio.channels.SocketChannel;
1617
import java.nio.file.Files;
@@ -71,8 +72,9 @@ public static void agentmain(String agentArguments, Instrumentation instrumentat
7172
initAgent(instrumentation);
7273
}
7374

74-
private static AgentBuilder createAgentBuilder(Instrumentation inst) throws Exception {
75-
final Junction<TypeDescription> systemType = ElementMatchers.isSubTypeOf(SocketChannel.class);
75+
private static AgentBuilder createAgentBuilder() throws Exception {
76+
final Junction<TypeDescription> socketType = ElementMatchers.isSubTypeOf(SocketChannel.class)
77+
.or(ElementMatchers.isSubTypeOf(Socket.class));
7678
final Junction<TypeDescription> pathType = ElementMatchers.isSubTypeOf(Files.class);
7779
final Junction<TypeDescription> fileChannelType = ElementMatchers.isSubTypeOf(FileChannel.class);
7880

@@ -98,11 +100,11 @@ private static AgentBuilder createAgentBuilder(Instrumentation inst) throws Exce
98100
);
99101

100102
final ByteBuddy byteBuddy = new ByteBuddy().with(Implementation.Context.Disabled.Factory.INSTANCE);
101-
final AgentBuilder agentBuilder = new AgentBuilder.Default(byteBuddy).with(AgentBuilder.InitializationStrategy.NoOp.INSTANCE)
103+
return new AgentBuilder.Default(byteBuddy).with(AgentBuilder.InitializationStrategy.NoOp.INSTANCE)
102104
.with(AgentBuilder.RedefinitionStrategy.REDEFINITION)
103105
.with(AgentBuilder.TypeStrategy.Default.REDEFINE)
104106
.ignore(ElementMatchers.nameContains("$MockitoMock$")) /* ingore all Mockito mocks */
105-
.type(systemType)
107+
.type(socketType)
106108
.transform(socketTransformer)
107109
.type(pathType.or(fileChannelType))
108110
.transform(fileTransformer)
@@ -118,12 +120,10 @@ private static AgentBuilder createAgentBuilder(Instrumentation inst) throws Exce
118120
Advice.to(RuntimeHaltInterceptor.class).on(ElementMatchers.named("halt"))
119121
)
120122
);
121-
122-
return agentBuilder;
123123
}
124124

125125
private static void initAgent(Instrumentation instrumentation) throws Exception {
126-
AgentBuilder agentBuilder = createAgentBuilder(instrumentation);
126+
AgentBuilder agentBuilder = createAgentBuilder();
127127
agentBuilder.installOn(instrumentation);
128128
}
129129
}

libs/agent-sm/agent/src/test/java/org/opensearch/javaagent/SocketChannelInterceptorTests.java

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.io.IOException;
1414
import java.net.InetAddress;
1515
import java.net.InetSocketAddress;
16+
import java.net.Socket;
1617
import java.net.UnixDomainSocketAddress;
1718
import java.nio.channels.SocketChannel;
1819

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

2930
assertThrows(SecurityException.class, () -> channel.connect(new InetSocketAddress("opensearch.org", 80)));
3031
}
32+
33+
assertThrows(SecurityException.class, () -> new Socket("localhost", 9200));
3134
}
3235

3336
@Test
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
grant {
10+
permission java.net.SocketPermission "*", "connect";
11+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
grant {
10+
permission java.net.SocketPermission "*", "connect";
11+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
grant {
10+
permission java.net.SocketPermission "*", "connect";
11+
};

0 commit comments

Comments
 (0)