-
Notifications
You must be signed in to change notification settings - Fork 896
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support compact RunRequest so requests sent to heavy plugin services …
…still fit in the default gRPC message limit. PiperOrigin-RevId: 656270995 Change-Id: I3216fe01796ff866caa464aad1d49d1c0dd35256
- Loading branch information
1 parent
b4b2089
commit 67f5d79
Showing
6 changed files
with
309 additions
and
12 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
common/src/main/java/com/google/tsunami/common/server/CompactRunRequestHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.google.tsunami.common.server; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.google.tsunami.proto.MatchedPlugin; | ||
import com.google.tsunami.proto.NetworkService; | ||
import com.google.tsunami.proto.RunCompactRequest; | ||
import com.google.tsunami.proto.RunCompactRequest.PluginNetworkServiceTarget; | ||
import com.google.tsunami.proto.RunRequest; | ||
import java.util.HashMap; | ||
|
||
/** | ||
* CompactRunRequestHelper is a helper class to compress/uncompress the RunRequest into/from the | ||
* compact representation. | ||
*/ | ||
public final class CompactRunRequestHelper { | ||
|
||
private CompactRunRequestHelper() {} | ||
|
||
public static RunCompactRequest compress(RunRequest runRequest) { | ||
var builder = RunCompactRequest.newBuilder().setTarget(runRequest.getTarget()); | ||
HashMap<NetworkService, Integer> serviceIndexMap = new HashMap<>(); | ||
int pluginIndex = -1; | ||
for (MatchedPlugin matchedPlugin : runRequest.getPluginsList()) { | ||
pluginIndex++; | ||
builder.addPlugins(matchedPlugin.getPlugin()); | ||
for (NetworkService service : matchedPlugin.getServicesList()) { | ||
Integer serviceIndex = serviceIndexMap.get(service); | ||
if (serviceIndex == null) { | ||
serviceIndex = serviceIndexMap.size(); | ||
serviceIndexMap.put(service, serviceIndex); | ||
builder.addServices(service); | ||
} | ||
|
||
builder.addScanTargets( | ||
PluginNetworkServiceTarget.newBuilder() | ||
.setPluginIndex(pluginIndex) | ||
.setServiceIndex(serviceIndex) | ||
.build()); | ||
} | ||
} | ||
return builder.build(); | ||
} | ||
|
||
public static RunRequest uncompress(RunCompactRequest runCompactRequest) { | ||
ImmutableList.Builder<MatchedPlugin> matchedPlugins = ImmutableList.builder(); | ||
for (var target : runCompactRequest.getScanTargetsList()) { | ||
var plugin = runCompactRequest.getPlugins(target.getPluginIndex()); | ||
var networkService = runCompactRequest.getServices(target.getServiceIndex()); | ||
matchedPlugins.add( | ||
MatchedPlugin.newBuilder().setPlugin(plugin).addServices(networkService).build()); | ||
} | ||
|
||
return RunRequest.newBuilder() | ||
.setTarget(runCompactRequest.getTarget()) | ||
.addAllPlugins(matchedPlugins.build()) | ||
.build(); | ||
} | ||
} |
110 changes: 110 additions & 0 deletions
110
common/src/test/java/com/google/tsunami/common/server/CompactRunRequestHelperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.google.tsunami.common.server; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.google.tsunami.proto.Hostname; | ||
import com.google.tsunami.proto.MatchedPlugin; | ||
import com.google.tsunami.proto.NetworkEndpoint; | ||
import com.google.tsunami.proto.NetworkService; | ||
import com.google.tsunami.proto.PluginDefinition; | ||
import com.google.tsunami.proto.PluginInfo; | ||
import com.google.tsunami.proto.RunCompactRequest; | ||
import com.google.tsunami.proto.RunCompactRequest.PluginNetworkServiceTarget; | ||
import com.google.tsunami.proto.RunRequest; | ||
import com.google.tsunami.proto.TargetInfo; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
@RunWith(JUnit4.class) | ||
public final class CompactRunRequestHelperTest { | ||
|
||
@Test | ||
public void compressingRunRequest_isMoreCompact() { | ||
NetworkService service1 = NetworkService.newBuilder().setServiceName("service1").build(); | ||
NetworkService service2 = NetworkService.newBuilder().setServiceName("service2").build(); | ||
PluginDefinition plugin1 = | ||
PluginDefinition.newBuilder() | ||
.setInfo(PluginInfo.newBuilder().setName("plugin1").build()) | ||
.build(); | ||
PluginDefinition plugin2 = | ||
PluginDefinition.newBuilder() | ||
.setInfo(PluginInfo.newBuilder().setName("plugin2").build()) | ||
.build(); | ||
PluginDefinition plugin3 = | ||
PluginDefinition.newBuilder() | ||
.setInfo(PluginInfo.newBuilder().setName("plugin3").build()) | ||
.build(); | ||
MatchedPlugin matchedPlugin1 = | ||
MatchedPlugin.newBuilder().addServices(service1).setPlugin(plugin1).build(); | ||
MatchedPlugin matchedPlugin2 = | ||
MatchedPlugin.newBuilder().addServices(service2).setPlugin(plugin2).build(); | ||
MatchedPlugin matchedPlugin3 = | ||
MatchedPlugin.newBuilder().addServices(service1).setPlugin(plugin3).build(); | ||
ImmutableList<MatchedPlugin> expectedMatchedPlugins = | ||
ImmutableList.of(matchedPlugin1, matchedPlugin2, matchedPlugin3); | ||
TargetInfo expectedTargetInfo = | ||
TargetInfo.newBuilder() | ||
.addNetworkEndpoints( | ||
NetworkEndpoint.newBuilder() | ||
.setHostname(Hostname.newBuilder().setName("example.com").build()) | ||
.build()) | ||
.build(); | ||
RunRequest expectedUncompressedRunRequest = | ||
RunRequest.newBuilder() | ||
.setTarget(expectedTargetInfo) | ||
.addAllPlugins(expectedMatchedPlugins) | ||
.build(); | ||
var actualCompressedRunRequest = | ||
CompactRunRequestHelper.compress(expectedUncompressedRunRequest); | ||
|
||
var expectedCompressedRunRequest = | ||
RunCompactRequest.newBuilder() | ||
.setTarget(expectedTargetInfo) | ||
.addServices(service1) | ||
.addServices(service2) | ||
.addPlugins(plugin1) | ||
.addPlugins(plugin2) | ||
.addPlugins(plugin3) | ||
.addScanTargets( | ||
PluginNetworkServiceTarget.newBuilder() | ||
.setPluginIndex(0) | ||
.setServiceIndex(0) | ||
.build()) | ||
.addScanTargets( | ||
PluginNetworkServiceTarget.newBuilder() | ||
.setPluginIndex(1) | ||
.setServiceIndex(1) | ||
.build()) | ||
.addScanTargets( | ||
PluginNetworkServiceTarget.newBuilder() | ||
.setPluginIndex(2) | ||
.setServiceIndex(0) | ||
.build()) | ||
.build(); | ||
assertThat(actualCompressedRunRequest).isEqualTo(expectedCompressedRunRequest); | ||
|
||
// And now uncompressing it again: | ||
var actualUncompressedRunRequest = | ||
CompactRunRequestHelper.uncompress(actualCompressedRunRequest); | ||
|
||
// It should match the original setup | ||
assertThat(actualUncompressedRunRequest).isEqualTo(expectedUncompressedRunRequest); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.