Skip to content

Commit b7a048c

Browse files
committed
Added support for posting data without auth
1 parent a8290d3 commit b7a048c

File tree

14 files changed

+510
-16
lines changed

14 files changed

+510
-16
lines changed

example-definitions/cli-target-grpc.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
authMode: "basic"
12
username: "your-username"
23
password: "your-password"
34
grpchost: "target-gRPC-host"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
authMode: "basic"
12
username: "your-username"
23
password: "your-password"
34
restURL: "target-rest-URL"

src/main/java/io/opentelemetry/contrib/generator/telemetry/cli/CLIProcessor.java

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
import io.opentelemetry.contrib.generator.telemetry.dto.GeneratorInput;
2121
import io.opentelemetry.contrib.generator.telemetry.transport.PayloadHandler;
2222
import io.opentelemetry.contrib.generator.telemetry.TelemetryGenerator;
23+
import io.opentelemetry.contrib.generator.telemetry.transport.auth.AuthHandler;
2324
import io.opentelemetry.contrib.generator.telemetry.transport.auth.BasicAuthHandler;
25+
import io.opentelemetry.contrib.generator.telemetry.transport.auth.NoAuthHandler;
2426
import io.opentelemetry.contrib.generator.telemetry.transport.implementations.grpc.GRPCPayloadHandler;
2527
import io.opentelemetry.contrib.generator.telemetry.transport.implementations.rest.RESTPayloadHandler;
2628
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -60,27 +62,32 @@ public static void main(String[] args) throws ParseException {
6062
private static Options getOptions() {
6163
Option entityDefinition = Option.builder("e")
6264
.argName("entityDefinition")
65+
.longOpt("entityDefinition")
6366
.desc("Path to the entity definition YAML")
6467
.hasArg()
6568
.required()
6669
.build();
6770
Option metricDefinition = Option.builder("m")
6871
.argName("metricDefinition")
72+
.longOpt("metricDefinition")
6973
.desc("Path to the metric definition YAML")
7074
.hasArg()
7175
.build();
7276
Option logsDefinition = Option.builder("l")
7377
.argName("logDefinition")
78+
.longOpt("logDefinition")
7479
.desc("Path to the log definition YAML")
7580
.hasArg()
7681
.build();
7782
Option traceDefinition = Option.builder("s")
7883
.argName("spanDefinition")
84+
.longOpt("spanDefinition")
7985
.desc("Path to the trace definition YAML")
8086
.hasArg()
8187
.build();
8288
Option targetEnvYAML = Option.builder("t")
8389
.argName("target")
90+
.longOpt("target")
8491
.desc("Path to the YAML containing details of the environment to target")
8592
.hasArg()
8693
.required()
@@ -99,31 +106,36 @@ private static Options getOptions() {
99106

100107
private static PayloadHandler getPayloadHandler(String targetEnvYAML) {
101108
TargetEnvironmentDetails targetEnvironmentDetails = getTargetEnvDetails(targetEnvYAML);
102-
if (StringUtils.defaultString(targetEnvironmentDetails.getUsername()).isBlank()) {
103-
throw new GeneratorException("Missing username in environment target YAML");
104-
}
105-
if (StringUtils.defaultString(targetEnvironmentDetails.getPassword()).isBlank()) {
106-
throw new GeneratorException("Missing password in environment target YAML");
107-
}
108109
String nonNullRestURL = StringUtils.defaultString(targetEnvironmentDetails.getRestURL());
109110
String nonNullGRPCHost = StringUtils.defaultString(targetEnvironmentDetails.getGRPCHost());
110111
String nonNullGRPCPort = StringUtils.defaultString(targetEnvironmentDetails.getGRPCPort());
111112
if (nonNullRestURL.isBlank() && (nonNullGRPCHost.isBlank() || nonNullGRPCPort.isBlank())) {
112113
throw new GeneratorException("Either restURL (for REST endpoint) or gRPCHost & gRPCPort (for gRPC endpoint) " +
113114
"must be provided in environment target YAML");
114115
}
116+
AuthHandler authHandler;
117+
if (targetEnvironmentDetails.getAuthMode().equalsIgnoreCase("NONE")) {
118+
authHandler = new NoAuthHandler();
119+
} else {
120+
if (StringUtils.defaultString(targetEnvironmentDetails.getUsername()).isBlank()) {
121+
throw new GeneratorException("Missing username in environment target YAML");
122+
}
123+
if (StringUtils.defaultString(targetEnvironmentDetails.getPassword()).isBlank()) {
124+
throw new GeneratorException("Missing password in environment target YAML");
125+
}
126+
authHandler = new BasicAuthHandler(targetEnvironmentDetails.getUsername(),
127+
targetEnvironmentDetails.getPassword());
128+
}
115129
if (!nonNullRestURL.isBlank()) {
116-
return new RESTPayloadHandler(nonNullRestURL,
117-
new BasicAuthHandler(targetEnvironmentDetails.getUsername(), targetEnvironmentDetails.getPassword()));
130+
return new RESTPayloadHandler(nonNullRestURL, authHandler);
118131
}
119132
int gRPCPort;
120133
try {
121134
gRPCPort = Integer.parseInt(nonNullGRPCPort);
122135
} catch (NumberFormatException numberFormatException) {
123136
throw new GeneratorException("Invalid gRPC port " + nonNullGRPCPort + " provided in environment target YAML");
124137
}
125-
return new GRPCPayloadHandler(nonNullGRPCHost, gRPCPort,
126-
new BasicAuthHandler(targetEnvironmentDetails.getUsername(), targetEnvironmentDetails.getPassword()));
138+
return new GRPCPayloadHandler(nonNullGRPCHost, gRPCPort, authHandler);
127139
}
128140

129141
private static TargetEnvironmentDetails getTargetEnvDetails(String targetEnvYAML) {

src/main/java/io/opentelemetry/contrib/generator/telemetry/cli/TargetEnvironmentDetails.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@
2121
@Data
2222
public class TargetEnvironmentDetails {
2323

24-
private String username;
25-
private String password;
2624
private String restURL;
2725
private String gRPCHost;
2826
private String gRPCPort;
27+
private String authMode;
28+
private String username;
29+
private String password;
2930
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2022 AppDynamics Inc.
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+
17+
package io.opentelemetry.contrib.generator.telemetry.transport.auth;
18+
19+
public class NoAuthHandler implements AuthHandler {
20+
21+
@Override
22+
public String getAuthString() {
23+
return "NO_AUTH";
24+
}
25+
}

src/main/java/io/opentelemetry/contrib/generator/telemetry/transport/implementations/grpc/GRPCPayloadHandler.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import io.opentelemetry.contrib.generator.telemetry.transport.auth.AuthHandler;
2121
import com.google.protobuf.GeneratedMessageV3;
2222
import io.grpc.*;
23+
import io.opentelemetry.contrib.generator.telemetry.transport.auth.NoAuthHandler;
2324
import io.opentelemetry.proto.collector.logs.v1.ExportLogsServiceRequest;
2425
import io.opentelemetry.proto.collector.logs.v1.ExportLogsServiceResponse;
2526
import io.opentelemetry.proto.collector.logs.v1.LogsServiceGrpc;
@@ -41,6 +42,7 @@ public class GRPCPayloadHandler implements PayloadHandler {
4142
private final String HOST;
4243
private final int gRPCPORT;
4344
private final AuthHandler authHandler;
45+
private final boolean isAuthEnabled;
4446
private ManagedChannel managedChannel;
4547
private MetricsServiceGrpc.MetricsServiceBlockingStub metricsServiceBlockingStub;
4648
private LogsServiceGrpc.LogsServiceBlockingStub logsServiceBlockingStub;
@@ -50,6 +52,7 @@ public GRPCPayloadHandler(String host, int gRPCPort, AuthHandler authHandler) {
5052
this.HOST = host;
5153
this.gRPCPORT = gRPCPort;
5254
this.authHandler = authHandler;
55+
isAuthEnabled = authHandler instanceof NoAuthHandler;
5356
}
5457

5558
@Override
@@ -113,6 +116,6 @@ public void start(Listener<RespT> responseListener, Metadata headers) {
113116
}
114117

115118
protected String[] getHeadersPostData() {
116-
return new String[] {HttpHeaders.AUTHORIZATION, authHandler.getAuthString()};
119+
return isAuthEnabled ? new String[] {HttpHeaders.AUTHORIZATION, authHandler.getAuthString()} : new String[0];
117120
}
118121
}

src/main/java/io/opentelemetry/contrib/generator/telemetry/transport/implementations/rest/RESTPayloadHandler.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import io.opentelemetry.contrib.generator.telemetry.transport.PayloadHandler;
2020
import io.opentelemetry.contrib.generator.telemetry.transport.auth.AuthHandler;
2121
import com.google.protobuf.GeneratedMessageV3;
22+
import io.opentelemetry.contrib.generator.telemetry.transport.auth.NoAuthHandler;
2223
import io.opentelemetry.contrib.generator.telemetry.transport.implementations.HTTPClient;
2324
import io.opentelemetry.proto.collector.logs.v1.ExportLogsServiceRequest;
2425
import io.opentelemetry.proto.collector.metrics.v1.ExportMetricsServiceRequest;
@@ -28,13 +29,16 @@
2829
import org.apache.commons.lang3.StringUtils;
2930

3031
import javax.ws.rs.core.HttpHeaders;
32+
import java.util.ArrayList;
33+
import java.util.List;
3134

3235
@Slf4j
3336
public class RESTPayloadHandler implements PayloadHandler {
3437

3538
private final String ENDPOINT_URL;
3639
private final AuthHandler authHandler;
3740
private final HTTPClient httpClient;
41+
private final boolean isAuthEnabled;
3842
@Getter
3943
@Setter
4044
private String metricsURL = "";
@@ -51,6 +55,7 @@ public RESTPayloadHandler(String endpointURL, AuthHandler authHandler) {
5155
this.ENDPOINT_URL = endpointURL;
5256
httpClient = new HTTPClient();
5357
this.authHandler = authHandler;
58+
isAuthEnabled = authHandler instanceof NoAuthHandler;
5459
}
5560

5661
@Override
@@ -73,8 +78,15 @@ public boolean postPayload(GeneratedMessageV3 message) {
7378
}
7479

7580
protected String[] getHeadersPostData() {
76-
return new String[] {HttpHeaders.AUTHORIZATION, authHandler.getAuthString(),
77-
HttpHeaders.CONTENT_TYPE, "application/x-protobuf",
78-
HttpHeaders.ACCEPT, "application/x-protobuf"};
81+
List<String> defaultHeaders = new ArrayList<>();
82+
defaultHeaders.add(HttpHeaders.CONTENT_TYPE);
83+
defaultHeaders.add("application/x-protobuf");
84+
defaultHeaders.add(HttpHeaders.ACCEPT);
85+
defaultHeaders.add("application/x-protobuf");
86+
if (isAuthEnabled) {
87+
defaultHeaders.add(HttpHeaders.AUTHORIZATION);
88+
defaultHeaders.add(authHandler.getAuthString());
89+
}
90+
return defaultHeaders.toArray(new String[4]);
7991
}
8092
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2022 AppDynamics Inc.
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+
17+
package io.opentelemetry.contrib.generator.telemetry;
18+
19+
import io.opentelemetry.contrib.generator.telemetry.cli.CLIProcessor;
20+
import org.apache.commons.cli.ParseException;
21+
import org.testng.annotations.Test;
22+
23+
import java.nio.file.Paths;
24+
25+
public class TestCLIProcessor {
26+
27+
private final String cliResourcesPath = Paths.get(System.getProperty("user.dir"), "src", "test", "resources",
28+
"test-definitions", "cli").toString();
29+
private final String ENTITIES_YAML = Paths.get(cliResourcesPath, "entity-definition.yaml").toString();
30+
private final String METRICS_YAML = Paths.get(cliResourcesPath, "metrics-cli-test.yaml").toString();
31+
private final String LOGS_YAML = Paths.get(cliResourcesPath, "logs-cli-test.yaml").toString();
32+
private final String TRACES_YAML = Paths.get(cliResourcesPath, "traces-cli-test.yaml").toString();
33+
34+
@Test
35+
public void testAllGeneratorsNoAuthREST() throws ParseException {
36+
String noAuthTargetYAML = Paths.get(cliResourcesPath, "target-noauth.yaml").toString();
37+
String[] cliArgs = new String[] {
38+
"-e", ENTITIES_YAML, "-m", METRICS_YAML, "-l", LOGS_YAML, "-s", TRACES_YAML, "-t", noAuthTargetYAML
39+
};
40+
CLIProcessor.main(cliArgs);
41+
}
42+
43+
@Test
44+
public void testMetricsTracesBasicAuthGRPC() throws ParseException {
45+
String basicAuthTargetYAML = Paths.get(cliResourcesPath, "target-basicauth.yaml").toString();
46+
String[] cliArgs = new String[] {
47+
"--entityDefinition", ENTITIES_YAML, "--metricDefinition", METRICS_YAML,
48+
"--spanDefinition", TRACES_YAML, "--target", basicAuthTargetYAML
49+
};
50+
CLIProcessor.main(cliArgs);
51+
}
52+
53+
}

0 commit comments

Comments
 (0)