|
8 | 8 | import com.uber.m3.tally.RootScopeBuilder;
|
9 | 9 | import com.uber.m3.tally.Scope;
|
10 | 10 | import com.uber.m3.tally.StatsReporter;
|
| 11 | +import io.grpc.Metadata; |
11 | 12 | import io.grpc.netty.shaded.io.netty.handler.ssl.SslContext;
|
| 13 | +import io.grpc.stub.MetadataUtils; |
12 | 14 | import io.micrometer.core.instrument.util.StringUtils;
|
13 | 15 | import io.micrometer.prometheus.PrometheusConfig;
|
14 | 16 | import io.micrometer.prometheus.PrometheusMeterRegistry;
|
|
23 | 25 | import io.temporal.worker.WorkerFactory;
|
24 | 26 | import io.temporal.worker.WorkerFactoryOptions;
|
25 | 27 | import io.temporal.worker.WorkerOptions;
|
| 28 | +import net.logstash.logback.encoder.LogstashEncoder; |
| 29 | +import picocli.CommandLine; |
| 30 | + |
| 31 | +import javax.net.ssl.SSLException; |
26 | 32 | import java.io.FileInputStream;
|
27 | 33 | import java.io.FileNotFoundException;
|
28 | 34 | import java.io.InputStream;
|
29 | 35 | import java.util.ArrayList;
|
30 | 36 | import java.util.Collections;
|
31 | 37 | import java.util.List;
|
32 | 38 | import java.util.concurrent.CountDownLatch;
|
33 |
| -import javax.net.ssl.SSLException; |
34 |
| -import net.logstash.logback.encoder.LogstashEncoder; |
35 |
| -import picocli.CommandLine; |
36 | 39 |
|
37 | 40 | @CommandLine.Command(name = "features", description = "Runs Java features")
|
38 | 41 | public class Main implements Runnable {
|
@@ -86,6 +89,9 @@ public class Main implements Runnable {
|
86 | 89 | @CommandLine.Option(names = "--tls-key-path", description = "Path to a client key for TLS")
|
87 | 90 | private String clientKeyPath;
|
88 | 91 |
|
| 92 | + @CommandLine.Option(names = "--api-key", description = "API key for authentication") |
| 93 | + private String apiKey; |
| 94 | + |
89 | 95 | // Metric parameters
|
90 | 96 | @CommandLine.Option(
|
91 | 97 | names = "--prom-listen-address",
|
@@ -122,29 +128,36 @@ public class Main implements Runnable {
|
122 | 128 |
|
123 | 129 | @Override
|
124 | 130 | public void run() {
|
| 131 | + WorkflowServiceStubsOptions.Builder workflowServiceStubOptionsBuilder = WorkflowServiceStubsOptions.newBuilder(); |
125 | 132 | // Configure TLS
|
126 |
| - SslContext sslContext = null; |
127 |
| - if (StringUtils.isNotEmpty(clientCertPath)) { |
128 |
| - if (StringUtils.isEmpty(clientKeyPath)) { |
| 133 | + if (StringUtils.isNotEmpty(clientCertPath) || StringUtils.isNotEmpty(clientKeyPath)) { |
| 134 | + if (StringUtils.isEmpty(clientKeyPath) || StringUtils.isEmpty(clientCertPath)) { |
129 | 135 | throw new RuntimeException("Client key path must be specified since cert path is");
|
130 | 136 | }
|
131 | 137 |
|
132 | 138 | try {
|
133 | 139 | InputStream clientCert = new FileInputStream(clientCertPath);
|
134 | 140 | InputStream clientKey = new FileInputStream(clientKeyPath);
|
135 |
| - sslContext = SimpleSslContextBuilder.forPKCS8(clientCert, clientKey).build(); |
| 141 | + SslContext sslContext = SimpleSslContextBuilder.forPKCS8(clientCert, clientKey).build(); |
| 142 | + workflowServiceStubOptionsBuilder.setSslContext(sslContext); |
136 | 143 | } catch (FileNotFoundException | SSLException e) {
|
137 | 144 | throw new RuntimeException("Error loading certs", e);
|
138 | 145 | }
|
139 |
| - |
140 |
| - } else if (StringUtils.isNotEmpty(clientKeyPath) && StringUtils.isEmpty(clientCertPath)) { |
141 |
| - throw new RuntimeException("Client cert path must be specified since key path is"); |
142 | 146 | } else if (isTlsEnabled) {
|
143 |
| - try { |
144 |
| - sslContext = SimpleSslContextBuilder.noKeyOrCertChain().build(); |
145 |
| - } catch (SSLException e) { |
146 |
| - throw new RuntimeException(e); |
147 |
| - } |
| 147 | + workflowServiceStubOptionsBuilder.setEnableHttps(true); |
| 148 | + } |
| 149 | + // Configure API key |
| 150 | + if (StringUtils.isNotEmpty(apiKey)) { |
| 151 | + workflowServiceStubOptionsBuilder.addApiKey(() -> apiKey); |
| 152 | + Metadata.Key<String> TEMPORAL_NAMESPACE_HEADER_KEY = |
| 153 | + Metadata.Key.of("temporal-namespace", Metadata.ASCII_STRING_MARSHALLER); |
| 154 | + Metadata metadata = new Metadata(); |
| 155 | + metadata.put(TEMPORAL_NAMESPACE_HEADER_KEY, namespace); |
| 156 | + workflowServiceStubOptionsBuilder.setChannelInitializer( |
| 157 | + (channel) -> { |
| 158 | + channel.intercept(MetadataUtils.newAttachHeadersInterceptor(metadata)); |
| 159 | + }); |
| 160 | + |
148 | 161 | }
|
149 | 162 |
|
150 | 163 | // Configure logging
|
@@ -174,9 +187,8 @@ public void run() {
|
174 | 187 | // Configure client
|
175 | 188 | WorkflowServiceStubs service =
|
176 | 189 | WorkflowServiceStubs.newServiceStubs(
|
177 |
| - WorkflowServiceStubsOptions.newBuilder() |
| 190 | + workflowServiceStubOptionsBuilder |
178 | 191 | .setTarget(serverAddress)
|
179 |
| - .setSslContext(sslContext) |
180 | 192 | .setMetricsScope(scope)
|
181 | 193 | .build());
|
182 | 194 |
|
|
0 commit comments