Skip to content

Commit

Permalink
Merge pull request #35 from intergral/snyk-fix-61cb39884fcab0939f98c8…
Browse files Browse the repository at this point in the history
…aa57bf8c85

[Snyk] Fix for 1 vulnerabilities
  • Loading branch information
Umaaz committed Sep 25, 2023
2 parents 1e61e89 + f61b023 commit 4631ebb
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 23 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/on_push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ jobs:
- name: Run tests
run: make coverage

- name: Test Reports
uses: actions/upload-artifact@v3
if: always()
with:
name: Test Reports
path: ${{ github.workspace }}/**/target/site

# Optional: Uploads the full dependency graph to GitHub to improve the quality of Dependabot alerts this repository can receive
- name: Update dependency graph
uses: advanced-security/maven-dependency-submission-action@571e99aab1055c2e71a1e2309b9691de18d6b7d6
Expand Down
4 changes: 4 additions & 0 deletions agent/src/main/java/com/intergral/deep/agent/DeepAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,8 @@ public synchronized void setEnabled(final boolean enabled) {
this.tracepointConfig.configUpdate(0, null, Collections.emptyList());
}
}

public void shutdown() {
this.grpcService.shutdown();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,21 @@ public void start() {
try {
setupChannel();
} catch (Exception e) {
LOGGER.debug("Error setting up GRPC channel", e);
LOGGER.error("Error setting up GRPC channel", e);
}
}

/**
* Shutdown the grpc channel.
*/
public void shutdown() {
if (this.channel == null) {
return;
}
try {
this.channel.shutdown().awaitTermination(10, TimeUnit.SECONDS);
} catch (InterruptedException e) {
LOGGER.debug("Could not shutdown cleanly.", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,11 @@ public void run() {
}

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


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

Expand Down
27 changes: 26 additions & 1 deletion agent/src/test/java/com/intergral/deep/agent/DeepAgentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
import com.intergral.deep.agent.settings.Settings;
import com.intergral.deep.agent.tracepoint.handler.Callback;
import com.intergral.deep.agent.tracepoint.inst.TracepointInstrumentationService;
import io.grpc.Server;
import io.grpc.ServerBuilder;
import java.io.IOException;
import java.net.ServerSocket;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
Expand All @@ -45,17 +49,38 @@ class DeepAgentTest {
void setUp() {

Mockito.when(settings.getSettingAs("poll.timer", Integer.class)).thenReturn(1010);
Mockito.when(settings.getSettingAs(Mockito.anyString(), Mockito.eq(String.class))).thenReturn("");
Mockito.when(settings.getSettingAs(Mockito.anyString(), Mockito.eq(Boolean.class))).thenReturn(false);
Mockito.when(settings.getServiceHost()).thenReturn("localhost");

try (MockedStatic<Callback> callback = Mockito.mockStatic(Callback.class, "init")) {
deepAgent = new DeepAgent(settings, tracepointInstrumentationService);
callback.verify(() -> Callback.init(Mockito.any(), Mockito.any(), Mockito.any()), times(1));
}
}

@Test
void start_shouldSetPluginsAndResource() {
void start_shouldSetPluginsAndResource() throws IOException {

// find a free port
int port;
try (ServerSocket socket = new ServerSocket(0)) {
port = socket.getLocalPort();
}
Mockito.when(settings.getServicePort()).thenReturn(port);

final Server server = ServerBuilder.forPort(port).build();

server.start();

deepAgent.start();

server.shutdownNow();
deepAgent.shutdown();

Mockito.verify(settings).setPlugins(Mockito.anyCollection());
Mockito.verify(settings).setResource(Mockito.any());

}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import io.grpc.ServerBuilder;
import io.grpc.ServerInterceptors;
import io.grpc.stub.StreamObserver;
import java.io.IOException;
import java.net.ServerSocket;
import java.util.Collections;
import java.util.HashMap;
Expand All @@ -51,6 +50,7 @@

class GrpcServiceTest {

private GrpcService grpcService;
private Server server;
private CountDownLatch pollLatch;
private final AtomicReference<PollRequest> pollRequest = new AtomicReference<>();
Expand Down Expand Up @@ -104,8 +104,11 @@ void setUp() throws Exception {
}

@AfterEach
void tearDown() throws IOException {
void tearDown() throws Exception {
this.grpcService.shutdown();

this.server.shutdownNow();
this.server.awaitTermination();
}

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

final GrpcService grpcService = new GrpcService(Settings.build(map));

new Thread(grpcService::start).start();
grpcService = new GrpcService(Settings.build(map));

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

final GrpcService grpcService = new GrpcService(Settings.build(map));
grpcService = new GrpcService(Settings.build(map));

new Thread(grpcService::start).start();
final CountDownLatch responseLatch = new CountDownLatch(1);
final AtomicReference<SnapshotResponse> responseAtomicReference = new AtomicReference<>();
grpcService.snapshotService().send(Snapshot.newBuilder().build(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.concurrent.CountDownLatch;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

class DriftAwareThreadTest {

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

@Test
void errorLogs() {
final DriftAwareThread spy = Mockito.spy(task);
spy.error("test error", new RuntimeException("test exception"));
Mockito.verify(spy, Mockito.times(1)).getName();
}

@Test
public void testCheckForEarlyWakeUp() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.concurrent.atomic.AtomicReference;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -62,15 +63,16 @@ class LongPollServiceTest {
private Server server;
private LongPollService longPollService;

private PollRequest request;
private final AtomicReference<PollRequest> request = new AtomicReference<>(null);
private PollResponse response;
private Throwable responseError;
private int port;
private GrpcService grpcService;

@BeforeEach
void setUp() throws IOException {
final TestPollService testPollService = new TestPollService((req, responseObserver) -> {
request = req;
request.set(req);
if (responseError != null) {
responseObserver.onError(responseError);
} else {
Expand All @@ -92,14 +94,18 @@ void setUp() throws IOException {
agentArgs.put(ISettings.KEY_SERVICE_URL, "localhost:" + port);
agentArgs.put(ISettings.KEY_SERVICE_SECURE, "false");
final Settings settings = Settings.build(agentArgs);
settings.setActive(true);
settings.setResource(Resource.create(Collections.singletonMap("test", "resource")));
final GrpcService grpcService = new GrpcService(settings);
grpcService = new GrpcService(settings);
longPollService = new LongPollService(settings, grpcService);
}

@AfterEach
void tearDown() {
void tearDown() throws Exception {
server.shutdownNow();
server.awaitTermination();

grpcService.shutdown();
}

@Test
Expand Down Expand Up @@ -148,15 +154,15 @@ void propagateHashOnNextCall() {

longPollService.run(100);

assertEquals("", request.getCurrentHash());
assertEquals(100, request.getTsNanos());
assertEquals("", request.get().getCurrentHash());
assertEquals(100, request.get().getTsNanos());

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

longPollService.run(101);

assertEquals("123", request.getCurrentHash());
assertEquals(101, request.getTsNanos());
assertEquals("123", request.get().getCurrentHash());
assertEquals(101, request.get().getTsNanos());
verify(instrumentationService, times(2)).processBreakpoints(Mockito.anyCollection());
}

Expand Down Expand Up @@ -204,8 +210,8 @@ void doesSendResourceOnRequest() {

longPollService.run(100);

assertNotNull(request.getResource());
assertEquals("test", request.getResource().getAttributes(0).getKey());
assertEquals("resource", request.getResource().getAttributes(0).getValue().getStringValue());
assertNotNull(request.get().getResource());
assertEquals("test", request.get().getResource().getAttributes(0).getKey());
assertEquals("resource", request.get().getResource().getAttributes(0).getValue().getStringValue());
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
</modules>

<properties>
<grpc.version>1.55.1</grpc.version>
<grpc.version>1.57.0</grpc.version>
<tcnative.version>2.0.51.Final</tcnative.version>
<asm.version>8.0.1</asm.version>

Expand Down

0 comments on commit 4631ebb

Please sign in to comment.