From f472740fb7e08c93cf0c95113944f48d817cac7b Mon Sep 17 00:00:00 2001 From: Aman Sanger Date: Tue, 16 Jul 2019 10:35:14 -0400 Subject: [PATCH] Created Prober subproject and setup basic ActionHandler and its unit tests (#133) * Initial Commit. * Deleted unfinished features. Added ActionHandler and its Unit Tests. * Included prober subproject in settings.gradle * Added Protocol Class and its Basic Unit Tests * Added Changes Suggested by jianglai * Fixed Gitignore to take out AutoValue generated code * Removed AutoValue java files * Added gitignore within prober * Removed all generated java * Final Changes in .gitignore * Added Ssl and WebWhois Action Handlers and their unit tests in addition to the ProbingAction class * Fixed build.gradle changes requested * Removed Files irrelevant to current pull request * Minor fixes to ActionHandler, as responded in comments, removed package-info, and updated settings.gradle * Fully Updated ActionHandler (missing updated JavaDoc) * Added changed Protocol and both Inbound and Outbound Markers * Removed AutoVaue ignore clause from .gitignore * removed unneccessary dependencies in build.gradle * Fixed Javadoc and comments for ActionHandler * Fixed comments and JavaDoc on other files * EOL added * Removed Unnecessary Files * fixed .gradle files styles * Merge remote-tracking branch 'upstream/master' * Removed outbound message from ActionHandler's fields and renamed Marker Interfaces * Fixed javadoc for Marker Interfaced * Modified Comments on ActionHandler * Removed LocalAddress from Protocol * Fixed Travis Build Issues --- prober/.gitignore | 1 + prober/build.gradle | 49 ++++++++++++ .../monitoring/blackbox/Protocol.java | 58 +++++++++++++++ .../blackbox/handlers/ActionHandler.java | 74 +++++++++++++++++++ .../blackbox/messages/InboundMessageType.java | 21 ++++++ .../messages/OutboundMessageType.java | 21 ++++++ settings.gradle | 1 + 7 files changed, 225 insertions(+) create mode 100644 prober/.gitignore create mode 100644 prober/build.gradle create mode 100644 prober/src/main/java/google/registry/monitoring/blackbox/Protocol.java create mode 100644 prober/src/main/java/google/registry/monitoring/blackbox/handlers/ActionHandler.java create mode 100644 prober/src/main/java/google/registry/monitoring/blackbox/messages/InboundMessageType.java create mode 100644 prober/src/main/java/google/registry/monitoring/blackbox/messages/OutboundMessageType.java diff --git a/prober/.gitignore b/prober/.gitignore new file mode 100644 index 000000000..89f9ac04a --- /dev/null +++ b/prober/.gitignore @@ -0,0 +1 @@ +out/ diff --git a/prober/build.gradle b/prober/build.gradle new file mode 100644 index 000000000..e287bfd1b --- /dev/null +++ b/prober/build.gradle @@ -0,0 +1,49 @@ +// Copyright 2019 The Nomulus Authors. All Rights Reserved. +// +// 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. + +apply plugin: 'java' + +createUberJar('deployJar', 'prober', 'google.registry.monitoring.blackbox.Prober') + +dependencies { + def deps = rootProject.dependencyMap + + compile deps['com.google.auto.value:auto-value-annotations'] + compile deps['com.google.dagger:dagger'] + compile deps['com.google.flogger:flogger'] + compile deps['com.google.guava:guava'] + compile deps['io.netty:netty-buffer'] + compile deps['io.netty:netty-codec-http'] + compile deps['io.netty:netty-codec'] + compile deps['io.netty:netty-common'] + compile deps['io.netty:netty-handler'] + compile deps['io.netty:netty-transport'] + compile deps['javax.inject:javax.inject'] + + runtime deps['com.google.flogger:flogger-system-backend'] + runtime deps['com.google.auto.value:auto-value'] + runtime deps['io.netty:netty-tcnative-boringssl-static'] + + testCompile deps['com.google.truth:truth'] + testCompile deps['junit:junit'] + testCompile deps['org.mockito:mockito-core'] + testCompile project(':third_party') + + // Include auto-value in compile until nebula-lint understands + // annotationProcessor + annotationProcessor deps['com.google.auto.value:auto-value'] + testAnnotationProcessor deps['com.google.auto.value:auto-value'] + annotationProcessor deps['com.google.dagger:dagger-compiler'] + testAnnotationProcessor deps['com.google.dagger:dagger-compiler'] +} diff --git a/prober/src/main/java/google/registry/monitoring/blackbox/Protocol.java b/prober/src/main/java/google/registry/monitoring/blackbox/Protocol.java new file mode 100644 index 000000000..b0071ea44 --- /dev/null +++ b/prober/src/main/java/google/registry/monitoring/blackbox/Protocol.java @@ -0,0 +1,58 @@ +// Copyright 2019 The Nomulus Authors. All Rights Reserved. +// +// 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 google.registry.monitoring.blackbox; + +import com.google.auto.value.AutoValue; +import com.google.common.collect.ImmutableList; +import io.netty.channel.ChannelHandler; +import javax.inject.Provider; + +/** + * {@link AutoValue} class that stores all unchanged variables necessary for type of connection + */ +@AutoValue +public abstract class Protocol { + + abstract String name(); + + public abstract int port(); + + /** The {@link ChannelHandler} providers to use for the protocol, in order. */ + abstract ImmutableList> handlerProviders(); + + /** Boolean that notes if connection associated with Protocol is persistent.*/ + abstract boolean persistentConnection(); + + public abstract Builder toBuilder(); + + public static Builder builder() { + return new AutoValue_Protocol.Builder(); + } + + @AutoValue.Builder + public static abstract class Builder { + + public abstract Builder name(String value); + + public abstract Builder port(int num); + + public abstract Builder handlerProviders(ImmutableList> providers); + + public abstract Builder persistentConnection(boolean value); + + public abstract Protocol build(); + } +} + diff --git a/prober/src/main/java/google/registry/monitoring/blackbox/handlers/ActionHandler.java b/prober/src/main/java/google/registry/monitoring/blackbox/handlers/ActionHandler.java new file mode 100644 index 000000000..dfd88cba1 --- /dev/null +++ b/prober/src/main/java/google/registry/monitoring/blackbox/handlers/ActionHandler.java @@ -0,0 +1,74 @@ +// Copyright 2019 The Nomulus Authors. All Rights Reserved. +// +// 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 google.registry.monitoring.blackbox.handlers; + +import com.google.common.flogger.FluentLogger; +import google.registry.monitoring.blackbox.messages.InboundMessageType; +import google.registry.monitoring.blackbox.messages.OutboundMessageType; +import io.netty.channel.SimpleChannelInboundHandler; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelPromise; + +/** + *Superclass of all {@link ChannelHandler}s placed at end of channel pipeline + * + *

{@code ActionHandler} inherits from {@link SimpleChannelInboundHandler< InboundMessageType >}, as it should only be passed in + * messages that implement the {@link InboundMessageType} interface.

+ * + *

The {@code ActionHandler} skeleton exists for a few main purposes. First, it returns a {@link ChannelPromise}, + * which informs the {@link ProbingAction} in charge that a response has been read. Second, it stores the {@link OutboundMessageType} + * passed down the pipeline, so that subclasses can use that information for their own processes. Lastly, with any exception + * thrown, the connection is closed, and the ProbingAction governing this channel is informed of the error. Subclasses + * specify further work to be done for specific kinds of channel pipelines.

+ */ +public abstract class ActionHandler extends SimpleChannelInboundHandler { + + private static final FluentLogger logger = FluentLogger.forEnclosingClass(); + + protected ChannelPromise finished; + + /** Takes in {@link OutboundMessageType} type and saves for subclasses. Then returns initialized {@link ChannelPromise}*/ + public ChannelFuture getFuture() { + return finished; + } + + /** Initializes new {@link ChannelPromise} */ + @Override + public void handlerAdded(ChannelHandlerContext ctx) { + //Once handler is added to channel pipeline, initialize channel and future for this handler + finished = ctx.newPromise(); + } + + @Override + public void channelRead0(ChannelHandlerContext ctx, InboundMessageType inboundMessage) throws Exception { + //simply marks finished as success + finished.setSuccess(); + } + + /** Logs the channel and pipeline that caused error, closes channel, then informs {@link ProbingAction} listeners of error */ + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { + logger.atSevere().withCause(cause).log(String.format( + "Attempted Action was unsuccessful with channel: %s, having pipeline: %s", + ctx.channel().toString(), + ctx.channel().pipeline().toString())); + + finished.setFailure(cause); + ChannelFuture closedFuture = ctx.channel().close(); + closedFuture.addListener(f -> logger.atInfo().log("Unsuccessful channel connection closed")); + } +} + diff --git a/prober/src/main/java/google/registry/monitoring/blackbox/messages/InboundMessageType.java b/prober/src/main/java/google/registry/monitoring/blackbox/messages/InboundMessageType.java new file mode 100644 index 000000000..0c1260bec --- /dev/null +++ b/prober/src/main/java/google/registry/monitoring/blackbox/messages/InboundMessageType.java @@ -0,0 +1,21 @@ +// Copyright 2019 The Nomulus Authors. All Rights Reserved. +// +// 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 google.registry.monitoring.blackbox.messages; + +/** + * Marker Interface that is implemented by all classes that serve as {@code inboundMessages} in channel pipeline + */ +public interface InboundMessageType {} + diff --git a/prober/src/main/java/google/registry/monitoring/blackbox/messages/OutboundMessageType.java b/prober/src/main/java/google/registry/monitoring/blackbox/messages/OutboundMessageType.java new file mode 100644 index 000000000..0dd17e9d4 --- /dev/null +++ b/prober/src/main/java/google/registry/monitoring/blackbox/messages/OutboundMessageType.java @@ -0,0 +1,21 @@ +// Copyright 2019 The Nomulus Authors. All Rights Reserved. +// +// 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 google.registry.monitoring.blackbox.messages; + +/** + * Marker Interface that is implemented by all classes that serve as {@code outboundMessages} in channel pipeline + */ +public interface OutboundMessageType {} + diff --git a/settings.gradle b/settings.gradle index d8f1c959a..9b2d11de8 100644 --- a/settings.gradle +++ b/settings.gradle @@ -28,6 +28,7 @@ if (pluginsUrl) { rootProject.name = 'nomulus' include 'core' +include 'prober' include 'proxy' include 'third_party' include 'util'