Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ replay_pid*
.DS_Store
# Ignore Gradle build output directory
build

# Intellij
.idea
6 changes: 5 additions & 1 deletion lib/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
plugins {
id 'java-library'
id "io.freefair.lombok" version "8.1.0"
}

repositories {
Expand All @@ -19,9 +20,12 @@ dependencies {

// This dependency is used internally, and not exposed to consumers on their own compile classpath.
implementation 'com.google.guava:guava:31.1-jre'
implementation 'info.picocli:picocli:4.7.4'
implementation 'com.google.guava:guava:31.1-jre'
implementation "io.grpc:grpc-protobuf:1.57.0"
implementation "io.grpc:grpc-stub:1.57.0"
implementation "io.grpc:grpc-services:1.57.0"
implementation "io.grpc:grpc-testing:1.15.1"
implementation 'com.cloudquery:plugin-pb-java:0.0.2'
}

Expand All @@ -38,6 +42,6 @@ testing {
// Apply a specific Java toolchain to ease working on different environments.
java {
toolchain {
languageVersion = JavaLanguageVersion.of(18)
languageVersion = JavaLanguageVersion.of(20)
}
}
29 changes: 0 additions & 29 deletions lib/src/main/java/cloudquery/plugin/sdk/DiscoveryServer.java

This file was deleted.

29 changes: 0 additions & 29 deletions lib/src/main/java/cloudquery/plugin/sdk/PluginServer.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.cloudquery.internal.servers.discovery.v1;

import cloudquery.discovery.v1.DiscoveryGrpc.DiscoveryImplBase;
import cloudquery.discovery.v1.DiscoveryOuterClass.GetVersions.Request;
import cloudquery.discovery.v1.DiscoveryOuterClass.GetVersions.Response;
import io.grpc.stub.StreamObserver;

import java.util.List;

public class DiscoverServer extends DiscoveryImplBase {
private final List<Integer> versions;

public DiscoverServer(List<Integer> versions) {
this.versions = versions;
}

@Override
public void getVersions(Request request, StreamObserver<Response> responseObserver) {
responseObserver.onNext(Response.newBuilder().addAllVersions(versions).build());
responseObserver.onCompleted();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.cloudquery.internal.servers.plugin.v3;

import cloudquery.plugin.v3.PluginGrpc.PluginImplBase;
import io.cloudquery.plugin.Plugin;

public class PluginServer extends PluginImplBase {
private final Plugin plugin;

public PluginServer(Plugin plugin) {
this.plugin = plugin;
}
}
18 changes: 18 additions & 0 deletions lib/src/main/java/io/cloudquery/plugin/Plugin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.cloudquery.plugin;

import lombok.Builder;
import lombok.Getter;
import lombok.NonNull;

@Builder(builderMethodName = "innerBuilder")
@Getter
public class Plugin {
public static PluginBuilder builder(String name, String version) {
return innerBuilder().name(name).verion(version);
}

@NonNull
private final String name;
@NonNull
private final String verion;
}
22 changes: 22 additions & 0 deletions lib/src/main/java/io/cloudquery/server/AddressConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.cloudquery.server;

import picocli.CommandLine.ITypeConverter;

public class AddressConverter implements ITypeConverter<AddressConverter.Address> {
public static class AddressParseException extends RuntimeException {

}
public record Address(String host, int port) {
}

@Override
public Address convert(String rawAddress) throws Exception {
String[] components = rawAddress.split(":");
if (components.length != 2) {
throw new AddressParseException();
}
return new Address(components[0], Integer.parseInt(components[1]));
}


}
7 changes: 7 additions & 0 deletions lib/src/main/java/io/cloudquery/server/DocCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.cloudquery.server;

import picocli.CommandLine;

@CommandLine.Command
public class DocCommand {
}
33 changes: 33 additions & 0 deletions lib/src/main/java/io/cloudquery/server/PluginServe.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.cloudquery.server;

import io.cloudquery.plugin.Plugin;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.NonNull;
import picocli.CommandLine;

import java.util.ArrayList;
import java.util.List;

@Builder(access = AccessLevel.PUBLIC)
public class PluginServe {
@NonNull
private final Plugin plugin;
@Builder.Default
private List<String> args = new ArrayList<>();
private boolean destinationV0V1Server;
private String sentryDSN;
private boolean testListener;
//TODO: Allow a test listener to be passed in
// testListenerConn *bufconn.Listener

public void Serve() throws ServerException {
int exitStatus = new CommandLine(new RootCommand()).
addSubcommand("serve", new ServeCommand(plugin)).
addSubcommand("doc", new DocCommand()).
execute(args.toArray(new String[]{}));
if (exitStatus != 0) {
throw new ServerException("error processing command line exit status = "+exitStatus);
}
}
}
7 changes: 7 additions & 0 deletions lib/src/main/java/io/cloudquery/server/RootCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.cloudquery.server;

import picocli.CommandLine;

@CommandLine.Command
public class RootCommand {
}
82 changes: 82 additions & 0 deletions lib/src/main/java/io/cloudquery/server/ServeCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package io.cloudquery.server;

import io.cloudquery.internal.servers.discovery.v1.DiscoverServer;
import io.cloudquery.internal.servers.plugin.v3.PluginServer;
import io.cloudquery.plugin.Plugin;
import io.cloudquery.server.AddressConverter.Address;
import io.grpc.Grpc;
import io.grpc.InsecureServerCredentials;
import io.grpc.Server;
import io.grpc.protobuf.services.ProtoReflectionService;
import lombok.ToString;

import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.logging.Level;
import java.util.logging.Logger;

import static picocli.CommandLine.Command;
import static picocli.CommandLine.Option;

@Command
@ToString
public class ServeCommand implements Callable<Integer> {
private static final Logger logger = Logger.getLogger(ServeCommand.class.getName());
public static final List<Integer> DISCOVERY_VERSIONS = List.of(3);

@Option(names = "--address", converter = AddressConverter.class, description = "address to serve on. can be tcp: localhost:7777 or unix socket: `/tmp/plugin.rpc.sock` (default \"${DEFAULT-VALUE}\")")
private Address address = new Address("localhost", 7777);

@Option(names = "--log-format", description = "log format. one of: text,json (default \"${DEFAULT-VALUE}\")")
private String logFormat = "text";

@Option(names = "--log-level", description = "log level. one of: trace,debug,info,warn,error (default \"${DEFAULT-VALUE}\")")
private String logLevel = "info";

@Option(names = "--network", description = "the network must be \"tcp\", \"tcp4\", \"tcp6\", \"unix\" or \"unixpacket\" (default \"${DEFAULT-VALUE}\")")
private String network = "tcp";

@Option(names = "--disable-sentry", description = "disable sentry")
private Boolean disableSentry = false;

@Option(names = "--otel-endpoint", description = "Open Telemetry HTTP collector endpoint")
private String otelEndpoint = "";

@Option(names = "--otel-endpoint-insecure", description = "use Open Telemetry HTTP endpoint (for development only)")
private Boolean otelEndpointInsecure = false;

private final Plugin plugin;

public ServeCommand(Plugin plugin) {
this.plugin = plugin;
}

@Override
public Integer call() throws Exception {
// Initialize a logger

// Configure open telemetry

// Configure test listener

// Configure gRPC server
Server server = Grpc.newServerBuilderForPort(address.port(), InsecureServerCredentials.create()).
addService(new DiscoverServer(DISCOVERY_VERSIONS)).
addService(new PluginServer(plugin)).
addService(ProtoReflectionService.newInstance()).
executor(Executors.newFixedThreadPool(10)).
build();

// Configure sentry

// Log we are listening on address and port

// Run gRPC server and block
server.start();
logger.log(Level.INFO, "Started server on {0}", address);
server.awaitTermination();
return 0;
}

}
7 changes: 7 additions & 0 deletions lib/src/main/java/io/cloudquery/server/ServerException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.cloudquery.server;

public class ServerException extends Exception {
public ServerException(String message) {
super(message);
}
}

This file was deleted.

13 changes: 0 additions & 13 deletions lib/src/test/java/cloudquery/plugin/sdk/PluginServerTest.java

This file was deleted.

36 changes: 36 additions & 0 deletions lib/src/test/java/io/cloudquery/server/AddressTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.cloudquery.server;

import io.cloudquery.server.AddressConverter.Address;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class AddressTest {

private AddressConverter addressConverter;

@Before
public void setUp() {
addressConverter = new AddressConverter();
}

@Test
public void shouldParseAddressFromString() throws Exception {
String rawAddress = "127.0.0.1:12345";

Address address = addressConverter.convert(rawAddress);

assertEquals(new Address("127.0.0.1", 12345), address);
}

@Test
public void shouldThrowExceptionIfAddressNotFormattedCorrectly() {
String rawAddress = "bad address";

AddressConverter addressConverter = new AddressConverter();

Assert.assertThrows(AddressConverter.AddressParseException.class, () -> addressConverter.convert(rawAddress));
}
}
Loading