Skip to content

Commit 4631ebb

Browse files
authored
Merge pull request #35 from intergral/snyk-fix-61cb39884fcab0939f98c8aa57bf8c85
[Snyk] Fix for 1 vulnerabilities
2 parents 1e61e89 + f61b023 commit 4631ebb

File tree

9 files changed

+86
-23
lines changed

9 files changed

+86
-23
lines changed

.github/workflows/on_push.yml

+7
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ jobs:
2323
- name: Run tests
2424
run: make coverage
2525

26+
- name: Test Reports
27+
uses: actions/upload-artifact@v3
28+
if: always()
29+
with:
30+
name: Test Reports
31+
path: ${{ github.workspace }}/**/target/site
32+
2633
# Optional: Uploads the full dependency graph to GitHub to improve the quality of Dependabot alerts this repository can receive
2734
- name: Update dependency graph
2835
uses: advanced-security/maven-dependency-submission-action@571e99aab1055c2e71a1e2309b9691de18d6b7d6

agent/src/main/java/com/intergral/deep/agent/DeepAgent.java

+4
Original file line numberDiff line numberDiff line change
@@ -198,4 +198,8 @@ public synchronized void setEnabled(final boolean enabled) {
198198
this.tracepointConfig.configUpdate(0, null, Collections.emptyList());
199199
}
200200
}
201+
202+
public void shutdown() {
203+
this.grpcService.shutdown();
204+
}
201205
}

agent/src/main/java/com/intergral/deep/agent/grpc/GrpcService.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,21 @@ public void start() {
6464
try {
6565
setupChannel();
6666
} catch (Exception e) {
67-
LOGGER.debug("Error setting up GRPC channel", e);
67+
LOGGER.error("Error setting up GRPC channel", e);
68+
}
69+
}
70+
71+
/**
72+
* Shutdown the grpc channel.
73+
*/
74+
public void shutdown() {
75+
if (this.channel == null) {
76+
return;
77+
}
78+
try {
79+
this.channel.shutdown().awaitTermination(10, TimeUnit.SECONDS);
80+
} catch (InterruptedException e) {
81+
LOGGER.debug("Could not shutdown cleanly.", e);
6882
}
6983
}
7084

agent/src/main/java/com/intergral/deep/agent/poll/DriftAwareThread.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,11 @@ public void run() {
144144
}
145145

146146
private void trace(final String msg) {
147-
LOGGER.trace(msg);
147+
LOGGER.trace(this.getName() + " - " + msg);
148148
}
149149

150150

151-
private void error(final String msg, final Throwable throwable) {
151+
void error(final String msg, final Throwable throwable) {
152152
LOGGER.error(this.getName() + " - " + msg, throwable);
153153
}
154154

agent/src/test/java/com/intergral/deep/agent/DeepAgentTest.java

+26-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
import com.intergral.deep.agent.settings.Settings;
3131
import com.intergral.deep.agent.tracepoint.handler.Callback;
3232
import com.intergral.deep.agent.tracepoint.inst.TracepointInstrumentationService;
33+
import io.grpc.Server;
34+
import io.grpc.ServerBuilder;
35+
import java.io.IOException;
36+
import java.net.ServerSocket;
3337
import org.junit.jupiter.api.BeforeEach;
3438
import org.junit.jupiter.api.Test;
3539
import org.mockito.MockedStatic;
@@ -45,17 +49,38 @@ class DeepAgentTest {
4549
void setUp() {
4650

4751
Mockito.when(settings.getSettingAs("poll.timer", Integer.class)).thenReturn(1010);
52+
Mockito.when(settings.getSettingAs(Mockito.anyString(), Mockito.eq(String.class))).thenReturn("");
53+
Mockito.when(settings.getSettingAs(Mockito.anyString(), Mockito.eq(Boolean.class))).thenReturn(false);
54+
Mockito.when(settings.getServiceHost()).thenReturn("localhost");
55+
4856
try (MockedStatic<Callback> callback = Mockito.mockStatic(Callback.class, "init")) {
4957
deepAgent = new DeepAgent(settings, tracepointInstrumentationService);
5058
callback.verify(() -> Callback.init(Mockito.any(), Mockito.any(), Mockito.any()), times(1));
5159
}
5260
}
5361

5462
@Test
55-
void start_shouldSetPluginsAndResource() {
63+
void start_shouldSetPluginsAndResource() throws IOException {
64+
65+
// find a free port
66+
int port;
67+
try (ServerSocket socket = new ServerSocket(0)) {
68+
port = socket.getLocalPort();
69+
}
70+
Mockito.when(settings.getServicePort()).thenReturn(port);
71+
72+
final Server server = ServerBuilder.forPort(port).build();
73+
74+
server.start();
75+
5676
deepAgent.start();
77+
78+
server.shutdownNow();
79+
deepAgent.shutdown();
80+
5781
Mockito.verify(settings).setPlugins(Mockito.anyCollection());
5882
Mockito.verify(settings).setResource(Mockito.any());
83+
5984
}
6085

6186
@Test

agent/src/test/java/com/intergral/deep/agent/grpc/GrpcServiceTest.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import io.grpc.ServerBuilder;
3737
import io.grpc.ServerInterceptors;
3838
import io.grpc.stub.StreamObserver;
39-
import java.io.IOException;
4039
import java.net.ServerSocket;
4140
import java.util.Collections;
4241
import java.util.HashMap;
@@ -51,6 +50,7 @@
5150

5251
class GrpcServiceTest {
5352

53+
private GrpcService grpcService;
5454
private Server server;
5555
private CountDownLatch pollLatch;
5656
private final AtomicReference<PollRequest> pollRequest = new AtomicReference<>();
@@ -104,8 +104,11 @@ void setUp() throws Exception {
104104
}
105105

106106
@AfterEach
107-
void tearDown() throws IOException {
107+
void tearDown() throws Exception {
108+
this.grpcService.shutdown();
109+
108110
this.server.shutdownNow();
111+
this.server.awaitTermination();
109112
}
110113

111114
@Test
@@ -116,9 +119,7 @@ void serverCanConnect_poll() throws InterruptedException {
116119
map.put(ISettings.KEY_SERVICE_SECURE, "false");
117120
map.put(ISettings.KEY_AUTH_PROVIDER, MockAuthProvider.class.getName());
118121

119-
final GrpcService grpcService = new GrpcService(Settings.build(map));
120-
121-
new Thread(grpcService::start).start();
122+
grpcService = new GrpcService(Settings.build(map));
122123

123124
final PollResponse pollResponse = grpcService.pollService().poll(PollRequest.newBuilder().setTsNanos(101010L).build());
124125
assertEquals(202020L, pollResponse.getTsNanos());
@@ -138,9 +139,8 @@ void serverCanConnect_snapshot() throws InterruptedException {
138139
map.put(ISettings.KEY_SERVICE_SECURE, "false");
139140
map.put(ISettings.KEY_AUTH_PROVIDER, MockAuthProvider.class.getName());
140141

141-
final GrpcService grpcService = new GrpcService(Settings.build(map));
142+
grpcService = new GrpcService(Settings.build(map));
142143

143-
new Thread(grpcService::start).start();
144144
final CountDownLatch responseLatch = new CountDownLatch(1);
145145
final AtomicReference<SnapshotResponse> responseAtomicReference = new AtomicReference<>();
146146
grpcService.snapshotService().send(Snapshot.newBuilder().build(),

agent/src/test/java/com/intergral/deep/agent/poll/DriftAwareThreadTest.java

+7
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.concurrent.CountDownLatch;
2424
import org.junit.jupiter.api.BeforeEach;
2525
import org.junit.jupiter.api.Test;
26+
import org.mockito.Mockito;
2627

2728
class DriftAwareThreadTest {
2829

@@ -65,6 +66,12 @@ public long callback(final long duration, final long next) {
6566
assertTrue((lwrap.now + 10000) >= currentTimeMillis);
6667
}
6768

69+
@Test
70+
void errorLogs() {
71+
final DriftAwareThread spy = Mockito.spy(task);
72+
spy.error("test error", new RuntimeException("test exception"));
73+
Mockito.verify(spy, Mockito.times(1)).getName();
74+
}
6875

6976
@Test
7077
public void testCheckForEarlyWakeUp() throws Exception {

agent/src/test/java/com/intergral/deep/agent/poll/LongPollServiceTest.java

+17-11
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import java.util.Collection;
5151
import java.util.Collections;
5252
import java.util.HashMap;
53+
import java.util.concurrent.atomic.AtomicReference;
5354
import org.junit.jupiter.api.AfterEach;
5455
import org.junit.jupiter.api.BeforeEach;
5556
import org.junit.jupiter.api.Test;
@@ -62,15 +63,16 @@ class LongPollServiceTest {
6263
private Server server;
6364
private LongPollService longPollService;
6465

65-
private PollRequest request;
66+
private final AtomicReference<PollRequest> request = new AtomicReference<>(null);
6667
private PollResponse response;
6768
private Throwable responseError;
6869
private int port;
70+
private GrpcService grpcService;
6971

7072
@BeforeEach
7173
void setUp() throws IOException {
7274
final TestPollService testPollService = new TestPollService((req, responseObserver) -> {
73-
request = req;
75+
request.set(req);
7476
if (responseError != null) {
7577
responseObserver.onError(responseError);
7678
} else {
@@ -92,14 +94,18 @@ void setUp() throws IOException {
9294
agentArgs.put(ISettings.KEY_SERVICE_URL, "localhost:" + port);
9395
agentArgs.put(ISettings.KEY_SERVICE_SECURE, "false");
9496
final Settings settings = Settings.build(agentArgs);
97+
settings.setActive(true);
9598
settings.setResource(Resource.create(Collections.singletonMap("test", "resource")));
96-
final GrpcService grpcService = new GrpcService(settings);
99+
grpcService = new GrpcService(settings);
97100
longPollService = new LongPollService(settings, grpcService);
98101
}
99102

100103
@AfterEach
101-
void tearDown() {
104+
void tearDown() throws Exception {
102105
server.shutdownNow();
106+
server.awaitTermination();
107+
108+
grpcService.shutdown();
103109
}
104110

105111
@Test
@@ -148,15 +154,15 @@ void propagateHashOnNextCall() {
148154

149155
longPollService.run(100);
150156

151-
assertEquals("", request.getCurrentHash());
152-
assertEquals(100, request.getTsNanos());
157+
assertEquals("", request.get().getCurrentHash());
158+
assertEquals(100, request.get().getTsNanos());
153159

154160
response = PollResponse.newBuilder().setResponseType(ResponseType.UPDATE).setCurrentHash("321").build();
155161

156162
longPollService.run(101);
157163

158-
assertEquals("123", request.getCurrentHash());
159-
assertEquals(101, request.getTsNanos());
164+
assertEquals("123", request.get().getCurrentHash());
165+
assertEquals(101, request.get().getTsNanos());
160166
verify(instrumentationService, times(2)).processBreakpoints(Mockito.anyCollection());
161167
}
162168

@@ -204,8 +210,8 @@ void doesSendResourceOnRequest() {
204210

205211
longPollService.run(100);
206212

207-
assertNotNull(request.getResource());
208-
assertEquals("test", request.getResource().getAttributes(0).getKey());
209-
assertEquals("resource", request.getResource().getAttributes(0).getValue().getStringValue());
213+
assertNotNull(request.get().getResource());
214+
assertEquals("test", request.get().getResource().getAttributes(0).getKey());
215+
assertEquals("resource", request.get().getResource().getAttributes(0).getValue().getStringValue());
210216
}
211217
}

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
</modules>
8686

8787
<properties>
88-
<grpc.version>1.55.1</grpc.version>
88+
<grpc.version>1.57.0</grpc.version>
8989
<tcnative.version>2.0.51.Final</tcnative.version>
9090
<asm.version>8.0.1</asm.version>
9191

0 commit comments

Comments
 (0)