■ ■ ■ ■ ■ ■
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 | + | |