🤬
  • Add RemoteVulnDetectorLoadingModule to load plugins from language servers.

    PiperOrigin-RevId: 461661699
    Change-Id: I9c3d4490db2e4123bff8eb3ec1f91e70dc4d6d70
  • Loading...
  • John Y. Kim committed with Copybara-Service 2 years ago
    ed40cfca
    1 parent 09bd2a8b
Revision indexing in progress... (symbol navigation in revisions will be accurate after indexed)
  • ■ ■ ■ ■ ■ ■
    plugin/src/main/java/com/google/tsunami/plugin/RemoteVulnDetectorLoadingModule.java
     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.base.Preconditions.checkNotNull;
     19 + 
     20 +import com.google.auto.value.AutoAnnotation;
     21 +import com.google.auto.value.AutoBuilder;
     22 +import com.google.common.annotations.VisibleForTesting;
     23 +import com.google.common.collect.ImmutableList;
     24 +import com.google.inject.AbstractModule;
     25 +import com.google.inject.multibindings.MapBinder;
     26 +import com.google.tsunami.plugin.annotations.PluginInfo;
     27 +import io.grpc.Channel;
     28 + 
     29 +/** A Guice module that loads all {@link RemoteVulnDetector RemoteVulnDetectors} at runtime. */
     30 +public final class RemoteVulnDetectorLoadingModule extends AbstractModule {
     31 + 
     32 + private final ImmutableList<Channel> availableChannels;
     33 + 
     34 + public RemoteVulnDetectorLoadingModule(ImmutableList<Channel> channels) {
     35 + this.availableChannels = checkNotNull(channels);
     36 + }
     37 + 
     38 + @Override
     39 + protected void configure() {
     40 + MapBinder<PluginDefinition, TsunamiPlugin> tsunamiPluginBinder =
     41 + MapBinder.newMapBinder(binder(), PluginDefinition.class, TsunamiPlugin.class);
     42 + availableChannels.forEach(
     43 + channel ->
     44 + tsunamiPluginBinder
     45 + .addBinding(getRemoteVulnDetectorPluginDefinition(channel.hashCode()))
     46 + .toInstance(new RemoteVulnDetectorImpl(channel)));
     47 + }
     48 + 
     49 + // TODO(b/239095108): Change channelIds to something more meaningful to identify
     50 + // RemoteVulnDetectors.
     51 + @VisibleForTesting
     52 + static PluginDefinition getRemoteVulnDetectorPluginDefinition(int channelId) {
     53 + return PluginDefinition.forRemotePlugin(
     54 + pluginInfoBuilder()
     55 + .type(PluginType.REMOTE_VULN_DETECTION)
     56 + .name("RemoteVulnDetector" + channelId)
     57 + .description("Synthetic PluginInfo for RemoteVulnDetectors")
     58 + .author("Tsunami")
     59 + .version("0.0.1")
     60 + .bootstrapModule(RemoteVulnDetectorBootstrapLoadingModule.class)
     61 + .build());
     62 + }
     63 + 
     64 + /** Builder to build a {@link PluginInfo} annotation at runtime for RemoteVulnDetectors. */
     65 + @AutoBuilder(callMethod = "pluginInfo")
     66 + interface PluginInfoBuilder {
     67 + PluginInfoBuilder type(PluginType type);
     68 + 
     69 + PluginInfoBuilder name(String name);
     70 + 
     71 + PluginInfoBuilder description(String description);
     72 + 
     73 + PluginInfoBuilder author(String author);
     74 + 
     75 + PluginInfoBuilder version(String version);
     76 + 
     77 + PluginInfoBuilder bootstrapModule(Class<? extends PluginBootstrapModule> bootstrapModule);
     78 + 
     79 + PluginInfo build();
     80 + }
     81 + 
     82 + // Used by {@link AutoBuilder}
     83 + @SuppressWarnings("unused")
     84 + @AutoAnnotation
     85 + static PluginInfo pluginInfo(
     86 + PluginType type,
     87 + String name,
     88 + String description,
     89 + String author,
     90 + String version,
     91 + Class<? extends PluginBootstrapModule> bootstrapModule) {
     92 + return new AutoAnnotation_RemoteVulnDetectorLoadingModule_pluginInfo(
     93 + type, name, description, author, version, bootstrapModule);
     94 + }
     95 + 
     96 + static PluginInfoBuilder pluginInfoBuilder() {
     97 + return new AutoBuilder_RemoteVulnDetectorLoadingModule_PluginInfoBuilder();
     98 + }
     99 + 
     100 + private static class RemoteVulnDetectorBootstrapLoadingModule extends PluginBootstrapModule {
     101 + @Override
     102 + protected void configurePlugin() {}
     103 + }
     104 +}
     105 + 
  • ■ ■ ■ ■ ■ ■
    plugin/src/test/java/com/google/tsunami/plugin/RemoteVulnDetectorLoadingModuleTest.java
     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.Truth.assertThat;
     19 + 
     20 +import com.google.common.collect.ImmutableList;
     21 +import com.google.inject.Guice;
     22 +import com.google.inject.Key;
     23 +import com.google.inject.util.Types;
     24 +import io.grpc.Channel;
     25 +import io.grpc.inprocess.InProcessChannelBuilder;
     26 +import io.grpc.inprocess.InProcessServerBuilder;
     27 +import java.util.Map;
     28 +import org.junit.Test;
     29 +import org.junit.runner.RunWith;
     30 +import org.junit.runners.JUnit4;
     31 + 
     32 +@RunWith(JUnit4.class)
     33 +public final class RemoteVulnDetectorLoadingModuleTest {
     34 + 
     35 + @SuppressWarnings("unchecked")
     36 + private static final Key<Map<PluginDefinition, TsunamiPlugin>> PLUGIN_BINDING_KEY =
     37 + (Key<Map<PluginDefinition, TsunamiPlugin>>)
     38 + Key.get(Types.mapOf(PluginDefinition.class, TsunamiPlugin.class));
     39 + 
     40 + private static String generateServerName() {
     41 + return InProcessServerBuilder.generateName();
     42 + }
     43 + 
     44 + private static Channel generateChannel(String serverName) {
     45 + return InProcessChannelBuilder.forName(serverName).directExecutor().build();
     46 + }
     47 + 
     48 + @Test
     49 + public void configure_whenNoChannelsRegistered_loadsNoRemotePlugins() {
     50 + Map<PluginDefinition, TsunamiPlugin> remotePlugins =
     51 + Guice.createInjector(new RemoteVulnDetectorLoadingModule(ImmutableList.of()))
     52 + .getInstance(PLUGIN_BINDING_KEY);
     53 + 
     54 + assertThat(remotePlugins).isEmpty();
     55 + }
     56 + 
     57 + @Test
     58 + public void configure_always_loadsAllRemotePlugins() {
     59 + var channelName0 = generateChannel(generateServerName());
     60 + var channelName1 = generateChannel(generateServerName());
     61 + Map<PluginDefinition, TsunamiPlugin> remotePlugins =
     62 + Guice.createInjector(
     63 + new RemoteVulnDetectorLoadingModule(ImmutableList.of(channelName0, channelName1)))
     64 + .getInstance(PLUGIN_BINDING_KEY);
     65 + 
     66 + assertThat(remotePlugins).hasSize(2);
     67 + assertThat(remotePlugins.keySet())
     68 + .containsExactly(
     69 + RemoteVulnDetectorLoadingModule.getRemoteVulnDetectorPluginDefinition(
     70 + channelName0.hashCode()),
     71 + RemoteVulnDetectorLoadingModule.getRemoteVulnDetectorPluginDefinition(
     72 + channelName1.hashCode()));
     73 + }
     74 +}
     75 + 
Please wait...
Page is in error, reload to recover