| 1 | + | /* |
| 2 | + | * Copyright 2022 Google LLC |
| 3 | + | * |
| 4 | + | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + | * you may not use this file except in compliance with the License. |
| 6 | + | * You may obtain a copy of the License at |
| 7 | + | * |
| 8 | + | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + | * |
| 10 | + | * Unless required by applicable law or agreed to in writing, software |
| 11 | + | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + | * See the License for the specific language governing permissions and |
| 14 | + | * limitations under the License. |
| 15 | + | */ |
| 16 | + | package com.google.tsunami.plugin; |
| 17 | + | |
| 18 | + | import static com.google.common.truth.extensions.proto.ProtoTruth.assertThat; |
| 19 | + | import static org.junit.Assert.assertThrows; |
| 20 | + | |
| 21 | + | import com.google.common.collect.ImmutableList; |
| 22 | + | import com.google.inject.AbstractModule; |
| 23 | + | import com.google.inject.Guice; |
| 24 | + | import com.google.tsunami.common.data.NetworkEndpointUtils; |
| 25 | + | import com.google.tsunami.proto.DetectionReport; |
| 26 | + | import com.google.tsunami.proto.DetectionReportList; |
| 27 | + | import com.google.tsunami.proto.ListPluginsRequest; |
| 28 | + | import com.google.tsunami.proto.ListPluginsResponse; |
| 29 | + | import com.google.tsunami.proto.MatchedPlugin; |
| 30 | + | import com.google.tsunami.proto.NetworkService; |
| 31 | + | import com.google.tsunami.proto.PluginDefinition; |
| 32 | + | import com.google.tsunami.proto.PluginInfo; |
| 33 | + | import com.google.tsunami.proto.PluginServiceGrpc.PluginServiceImplBase; |
| 34 | + | import com.google.tsunami.proto.RunRequest; |
| 35 | + | import com.google.tsunami.proto.RunResponse; |
| 36 | + | import com.google.tsunami.proto.TargetInfo; |
| 37 | + | import com.google.tsunami.proto.TransportProtocol; |
| 38 | + | import io.grpc.health.v1.HealthCheckRequest; |
| 39 | + | import io.grpc.health.v1.HealthCheckResponse; |
| 40 | + | import io.grpc.health.v1.HealthCheckResponse.ServingStatus; |
| 41 | + | import io.grpc.health.v1.HealthGrpc.HealthImplBase; |
| 42 | + | import io.grpc.inprocess.InProcessChannelBuilder; |
| 43 | + | import io.grpc.inprocess.InProcessServerBuilder; |
| 44 | + | import io.grpc.stub.StreamObserver; |
| 45 | + | import io.grpc.testing.GrpcCleanupRule; |
| 46 | + | import io.grpc.util.MutableHandlerRegistry; |
| 47 | + | import org.junit.Before; |
| 48 | + | import org.junit.Rule; |
| 49 | + | import org.junit.Test; |
| 50 | + | import org.junit.runner.RunWith; |
| 51 | + | import org.junit.runners.JUnit4; |
| 52 | + | |
| 53 | + | @RunWith(JUnit4.class) |
| 54 | + | public final class RemoteVulnDetectorImplTest { |
| 55 | + | |
| 56 | + | private static final String PLUGIN_VERSION = "0.0.1"; |
| 57 | + | private static final String PLUGIN_DESCRIPTION = "test description"; |
| 58 | + | private static final String PLUGIN_AUTHOR = "tester"; |
| 59 | + | |
| 60 | + | private final MutableHandlerRegistry serviceRegistry = new MutableHandlerRegistry(); |
| 61 | + | |
| 62 | + | @Rule public final GrpcCleanupRule grpcCleanup = new GrpcCleanupRule(); |
| 63 | + | |
| 64 | + | @Before |
| 65 | + | public void setUp() throws Exception { |
| 66 | + | serviceRegistry.addService( |
| 67 | + | new PluginServiceImplBase() { |
| 68 | + | @Override |
| 69 | + | public void run(RunRequest request, StreamObserver<RunResponse> responseObserver) { |
| 70 | + | DetectionReportList.Builder reportListBuilder = DetectionReportList.newBuilder(); |
| 71 | + | for (MatchedPlugin plugin : request.getPluginsList()) { |
| 72 | + | reportListBuilder.addDetectionReports( |
| 73 | + | DetectionReport.newBuilder() |
| 74 | + | .setTargetInfo(request.getTarget()) |
| 75 | + | .setNetworkService(plugin.getServices(0))); |
| 76 | + | } |
| 77 | + | responseObserver.onNext(RunResponse.newBuilder().setReports(reportListBuilder).build()); |
| 78 | + | responseObserver.onCompleted(); |
| 79 | + | } |
| 80 | + | }); |
| 81 | + | } |
| 82 | + | |
| 83 | + | @Test |
| 84 | + | public void detect_withServingServer_returnsSuccessfulDetectionReportList() throws Exception { |
| 85 | + | registerHealthCheckWithStatus(ServingStatus.SERVING); |
| 86 | + | |
| 87 | + | RemoteVulnDetector pluginToTest = getNewRemoteVulnDetectorInstance(); |
| 88 | + | var endpointToTest = NetworkEndpointUtils.forIpAndPort("1.1.1.1", 80); |
| 89 | + | var serviceToTest = |
| 90 | + | NetworkService.newBuilder() |
| 91 | + | .setNetworkEndpoint(endpointToTest) |
| 92 | + | .setTransportProtocol(TransportProtocol.TCP) |
| 93 | + | .setServiceName("http") |
| 94 | + | .build(); |
| 95 | + | |
| 96 | + | TargetInfo testTarget = TargetInfo.newBuilder().addNetworkEndpoints(endpointToTest).build(); |
| 97 | + | pluginToTest.addMatchedPluginToDetect( |
| 98 | + | MatchedPlugin.newBuilder() |
| 99 | + | .addServices(serviceToTest) |
| 100 | + | .setPlugin(createSinglePluginDefinitionWithName("test")) |
| 101 | + | .build()); |
| 102 | + | assertThat(pluginToTest.detect(testTarget, ImmutableList.of()).getDetectionReportsList()) |
| 103 | + | .comparingExpectedFieldsOnly() |
| 104 | + | .containsExactly( |
| 105 | + | DetectionReport.newBuilder() |
| 106 | + | .setTargetInfo(testTarget) |
| 107 | + | .setNetworkService(serviceToTest) |
| 108 | + | .build()); |
| 109 | + | } |
| 110 | + | |
| 111 | + | @Test |
| 112 | + | public void detect_withNonServingServer_returnsEmptyDetectionReportList() throws Exception { |
| 113 | + | registerHealthCheckWithStatus(ServingStatus.NOT_SERVING); |
| 114 | + | |
| 115 | + | RemoteVulnDetector pluginToTest = getNewRemoteVulnDetectorInstance(); |
| 116 | + | var endpointToTest = NetworkEndpointUtils.forIpAndPort("1.1.1.1", 80); |
| 117 | + | var serviceToTest = |
| 118 | + | NetworkService.newBuilder() |
| 119 | + | .setNetworkEndpoint(endpointToTest) |
| 120 | + | .setTransportProtocol(TransportProtocol.TCP) |
| 121 | + | .setServiceName("http") |
| 122 | + | .build(); |
| 123 | + | |
| 124 | + | TargetInfo testTarget = TargetInfo.newBuilder().addNetworkEndpoints(endpointToTest).build(); |
| 125 | + | pluginToTest.addMatchedPluginToDetect( |
| 126 | + | MatchedPlugin.newBuilder() |
| 127 | + | .addServices(serviceToTest) |
| 128 | + | .setPlugin(createSinglePluginDefinitionWithName("test")) |
| 129 | + | .build()); |
| 130 | + | assertThat(pluginToTest.detect(testTarget, ImmutableList.of()).getDetectionReportsList()) |
| 131 | + | .isEmpty(); |
| 132 | + | } |
| 133 | + | |
| 134 | + | @Test |
| 135 | + | public void detect_withRpcError_throwsLanguageServerException() throws Exception { |
| 136 | + | registerHealthCheckWithError(); |
| 137 | + | |
| 138 | + | assertThrows( |
| 139 | + | LanguageServerException.class, |
| 140 | + | () -> |
| 141 | + | getNewRemoteVulnDetectorInstance() |
| 142 | + | .detect(TargetInfo.getDefaultInstance(), ImmutableList.of())); |
| 143 | + | } |
| 144 | + | |
| 145 | + | @Test |
| 146 | + | public void getAllPlugins_withServingServer_returnsSuccessfulList() throws Exception { |
| 147 | + | registerHealthCheckWithStatus(ServingStatus.SERVING); |
| 148 | + | |
| 149 | + | var plugin = createSinglePluginDefinitionWithName("test"); |
| 150 | + | RemoteVulnDetector pluginToTest = getNewRemoteVulnDetectorInstance(); |
| 151 | + | serviceRegistry.addService( |
| 152 | + | new PluginServiceImplBase() { |
| 153 | + | @Override |
| 154 | + | public void listPlugins( |
| 155 | + | ListPluginsRequest request, StreamObserver<ListPluginsResponse> responseObserver) { |
| 156 | + | responseObserver.onNext(ListPluginsResponse.newBuilder().addPlugins(plugin).build()); |
| 157 | + | responseObserver.onCompleted(); |
| 158 | + | } |
| 159 | + | }); |
| 160 | + | |
| 161 | + | assertThat(pluginToTest.getAllPlugins()).containsExactly(plugin); |
| 162 | + | } |
| 163 | + | |
| 164 | + | @Test |
| 165 | + | public void getAllPlugins_withNonServingServer_returnsEmptyList() throws Exception { |
| 166 | + | registerHealthCheckWithStatus(ServingStatus.NOT_SERVING); |
| 167 | + | assertThat(getNewRemoteVulnDetectorInstance().getAllPlugins()).isEmpty(); |
| 168 | + | } |
| 169 | + | |
| 170 | + | @Test |
| 171 | + | public void getAllPlugins_withRpcError_throwsLanguageServerException() throws Exception { |
| 172 | + | registerHealthCheckWithError(); |
| 173 | + | assertThrows(LanguageServerException.class, getNewRemoteVulnDetectorInstance()::getAllPlugins); |
| 174 | + | } |
| 175 | + | |
| 176 | + | private RemoteVulnDetector getNewRemoteVulnDetectorInstance() throws Exception { |
| 177 | + | String serverName = InProcessServerBuilder.generateName(); |
| 178 | + | grpcCleanup.register( |
| 179 | + | InProcessServerBuilder.forName(serverName) |
| 180 | + | .fallbackHandlerRegistry(serviceRegistry) |
| 181 | + | .directExecutor() |
| 182 | + | .build() |
| 183 | + | .start()); |
| 184 | + | |
| 185 | + | return Guice.createInjector( |
| 186 | + | new AbstractModule() { |
| 187 | + | @Override |
| 188 | + | protected void configure() { |
| 189 | + | bind(RemoteVulnDetector.class) |
| 190 | + | .toInstance( |
| 191 | + | new RemoteVulnDetectorImpl( |
| 192 | + | InProcessChannelBuilder.forName(serverName).directExecutor().build())); |
| 193 | + | } |
| 194 | + | }) |
| 195 | + | .getInstance(RemoteVulnDetector.class); |
| 196 | + | } |
| 197 | + | |
| 198 | + | private void registerHealthCheckWithError() { |
| 199 | + | serviceRegistry.addService( |
| 200 | + | new HealthImplBase() { |
| 201 | + | @Override |
| 202 | + | public void check( |
| 203 | + | HealthCheckRequest request, StreamObserver<HealthCheckResponse> responseObserver) { |
| 204 | + | responseObserver.onError(new RuntimeException("Test failure.")); |
| 205 | + | responseObserver.onCompleted(); |
| 206 | + | } |
| 207 | + | }); |
| 208 | + | } |
| 209 | + | |
| 210 | + | private void registerHealthCheckWithStatus(ServingStatus status) { |
| 211 | + | serviceRegistry.addService( |
| 212 | + | new HealthImplBase() { |
| 213 | + | @Override |
| 214 | + | public void check( |
| 215 | + | HealthCheckRequest request, StreamObserver<HealthCheckResponse> responseObserver) { |
| 216 | + | responseObserver.onNext(HealthCheckResponse.newBuilder().setStatus(status).build()); |
| 217 | + | responseObserver.onCompleted(); |
| 218 | + | } |
| 219 | + | }); |
| 220 | + | } |
| 221 | + | |
| 222 | + | private PluginDefinition createSinglePluginDefinitionWithName(String name) { |
| 223 | + | return PluginDefinition.newBuilder() |
| 224 | + | .setInfo( |
| 225 | + | PluginInfo.newBuilder() |
| 226 | + | .setType(PluginInfo.PluginType.VULN_DETECTION) |
| 227 | + | .setName(name) |
| 228 | + | .setVersion(PLUGIN_VERSION) |
| 229 | + | .setDescription(PLUGIN_DESCRIPTION) |
| 230 | + | .setAuthor(PLUGIN_AUTHOR)) |
| 231 | + | .build(); |
| 232 | + | } |
| 233 | + | } |
| 234 | + | |