Skip to content

Commit 7fe87a1

Browse files
clean up
1 parent 75578a8 commit 7fe87a1

5 files changed

Lines changed: 126 additions & 43 deletions

File tree

temporal-envconfig/src/main/java/io/temporal/envconfig/ClientConfigProfile.java

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import io.grpc.Metadata;
44
import io.grpc.netty.shaded.io.netty.handler.ssl.SslContextBuilder;
5+
import io.grpc.netty.shaded.io.netty.handler.ssl.util.InsecureTrustManagerFactory;
56
import io.temporal.client.WorkflowClientOptions;
67
import io.temporal.common.Experimental;
78
import io.temporal.serviceclient.WorkflowServiceStubsOptions;
@@ -30,6 +31,7 @@ public static Builder newBuilder(ClientConfigProfile profile) {
3031
return new Builder(profile);
3132
}
3233

34+
/** Returns a default instance of ClientConfigProfile with all fields unset. */
3335
public static ClientConfigProfile getDefaultInstance() {
3436
return new Builder().build();
3537
}
@@ -100,11 +102,14 @@ public WorkflowServiceStubsOptions toWorkflowServiceStubsOptions() {
100102
trustCertCollectionInputStream = null;
101103
}
102104

103-
builder.setSslContext(
104-
SslContextBuilder.forClient()
105-
.trustManager(trustCertCollectionInputStream)
106-
.keyManager(clientCertStream, keyFile)
107-
.build());
105+
SslContextBuilder sslContextBuilder = SslContextBuilder.forClient();
106+
if (trustCertCollectionInputStream != null) {
107+
sslContextBuilder.trustManager(trustCertCollectionInputStream);
108+
} else if (this.tls.isDisableHostVerification()) {
109+
sslContextBuilder.trustManager(InsecureTrustManagerFactory.INSTANCE);
110+
}
111+
sslContextBuilder.keyManager(clientCertStream, keyFile);
112+
builder.setSslContext(sslContextBuilder.build());
108113
} catch (IOException e) {
109114
throw new RuntimeException("Unable to create SSL context", e);
110115
}
@@ -273,15 +278,8 @@ private void applyEnvOverrides(Map<String, String> overrideEnvVars) {
273278
if (tlsBuilder == null) {
274279
tlsBuilder = ClientConfigTLS.newBuilder();
275280
}
276-
// tlsBuilder.setDisableHostVerification(v);
277-
}
278-
}
279-
if (env.containsKey("TEMPORAL_TLS_SERVER_NAME")) {
280-
String s = env.get("TEMPORAL_TLS_SERVER_NAME");
281-
if (tlsBuilder == null) {
282-
tlsBuilder = ClientConfigTLS.newBuilder();
281+
tlsBuilder.setDisableHostVerification(v);
283282
}
284-
tlsBuilder.setServerName(s);
285283
}
286284
// Apply the TLS changes if any
287285
if (tlsBuilder != null) {
@@ -335,9 +333,9 @@ public static final class Builder {
335333
private Metadata metadata;
336334
private ClientConfigTLS tls;
337335

338-
public Builder() {}
336+
private Builder() {}
339337

340-
public Builder(ClientConfigProfile profile) {
338+
private Builder(ClientConfigProfile profile) {
341339
this.namespace = profile.namespace;
342340
this.address = profile.address;
343341
this.apiKey = profile.apiKey;

temporal-envconfig/src/main/java/io/temporal/envconfig/ClientConfigTLS.java

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,16 @@
55
/** TLS configuration for a client. */
66
@Experimental
77
public class ClientConfigTLS {
8+
/** Create a builder for {@link ClientConfigTLS}. */
89
public static Builder newBuilder() {
910
return new Builder();
1011
}
1112

13+
/** Create a builder from an existing {@link ClientConfigTLS}. */
14+
public static Builder newBuilder(ClientConfigTLS config) {
15+
return new Builder(config);
16+
}
17+
1218
private final boolean disabled;
1319
private final String clientCertPath;
1420
private final byte[] clientCertData;
@@ -17,8 +23,9 @@ public static Builder newBuilder() {
1723
private final String serverCACertPath;
1824
private final byte[] serverCACertData;
1925
private final String serverName;
26+
private final boolean disableHostVerification;
2027

21-
public ClientConfigTLS(
28+
private ClientConfigTLS(
2229
boolean disabled,
2330
String clientCertPath,
2431
byte[] clientCertData,
@@ -36,6 +43,7 @@ public ClientConfigTLS(
3643
this.serverCACertPath = serverCACertPath;
3744
this.serverCACertData = serverCACertData;
3845
this.serverName = serverName;
46+
this.disableHostVerification = disableHostVerification;
3947
}
4048

4149
public boolean isDisabled() {
@@ -70,19 +78,37 @@ public String getServerName() {
7078
return serverName;
7179
}
7280

81+
public boolean isDisableHostVerification() {
82+
return disableHostVerification;
83+
}
84+
7385
public Builder toBuilder() {
7486
return new Builder(this);
7587
}
7688

7789
public static class Builder {
90+
private String clientCertPath;
91+
private byte[] clientCertData;
92+
private String clientKeyPath;
93+
private byte[] clientKeyData;
94+
private String serverCACertPath;
95+
private byte[] serverCACertData;
7896
private boolean disabled;
7997
private String serverName;
98+
private boolean disableHostVerification;
8099

81-
public Builder() {}
100+
private Builder() {}
82101

83-
public Builder(ClientConfigTLS clientConfigTLS) {
102+
private Builder(ClientConfigTLS clientConfigTLS) {
84103
this.disabled = clientConfigTLS.disabled;
85104
this.serverName = clientConfigTLS.serverName;
105+
this.clientCertPath = clientConfigTLS.clientCertPath;
106+
this.clientCertData = clientConfigTLS.clientCertData;
107+
this.clientKeyPath = clientConfigTLS.clientKeyPath;
108+
this.clientKeyData = clientConfigTLS.clientKeyData;
109+
this.serverCACertPath = clientConfigTLS.serverCACertPath;
110+
this.serverCACertData = clientConfigTLS.serverCACertData;
111+
this.disableHostVerification = clientConfigTLS.disableHostVerification;
86112
}
87113

88114
/** Disable TLS. Default: false. */
@@ -102,36 +128,57 @@ public Builder setServerName(String serverName) {
102128

103129
/** Path to client mTLS certificate. Mutually exclusive with ClientCertData. */
104130
public Builder setClientCertPath(String clientCertPath) {
131+
this.clientCertPath = clientCertPath;
105132
return this;
106133
}
107134

108135
/** PEM bytes for client mTLS certificate. Mutually exclusive with ClientCertPath. */
109136
public Builder setClientCertData(byte[] bytes) {
137+
this.clientCertData = bytes;
110138
return this;
111139
}
112140

113141
/** Path to client mTLS key. Mutually exclusive with ClientKeyData. */
114142
public Builder setClientKeyPath(String clientKeyPath) {
143+
this.clientKeyPath = clientKeyPath;
115144
return this;
116145
}
117146

118147
/** PEM bytes for client mTLS key. Mutually exclusive with ClientKeyPath. */
119-
public Builder setClientKeyData(byte[] bytes) {
148+
public Builder setClientKeyData(byte[] clientKeyData) {
149+
this.clientKeyData = clientKeyData;
120150
return this;
121151
}
122152

123153
/** Path to server CA cert override. Mutually exclusive with ServerCACertData. */
124-
public Builder setServerCACertPath(String s) {
154+
public Builder setServerCACertPath(String serverCACertPath) {
155+
this.serverCACertPath = serverCACertPath;
125156
return this;
126157
}
127158

128159
/** PEM bytes for server CA cert override. Mutually exclusive with ServerCACertPath. */
129-
public Builder setServerCACertData(byte[] bytes) {
160+
public Builder setServerCACertData(byte[] serverCACertData) {
161+
this.serverCACertData = serverCACertData;
162+
return this;
163+
}
164+
165+
/** Disable server host verification. Default: false */
166+
public Builder setDisableHostVerification(boolean disableHostVerification) {
167+
this.setDisableHostVerification(disableHostVerification);
130168
return this;
131169
}
132170

133171
public ClientConfigTLS build() {
134-
return new ClientConfigTLS(disabled, null, null, null, null, null, null, serverName, false);
172+
return new ClientConfigTLS(
173+
disabled,
174+
clientCertPath,
175+
clientCertData,
176+
clientKeyPath,
177+
clientKeyData,
178+
serverCACertPath,
179+
serverCACertData,
180+
serverName,
181+
disableHostVerification);
135182
}
136183
}
137184
}

temporal-envconfig/src/main/java/io/temporal/envconfig/ClientConfigToml.java

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.fasterxml.jackson.annotation.JsonProperty;
55
import io.grpc.Metadata;
66
import io.temporal.common.Experimental;
7+
import java.nio.charset.StandardCharsets;
78
import java.util.HashMap;
89
import java.util.Map;
910
import javax.annotation.Nullable;
@@ -119,16 +120,26 @@ static ClientConfigTLS getClientConfigTLS(ClientConfigToml.TomlClientConfigProfi
119120
ClientConfigTLS tls = null;
120121
if (tomlProfile.tls != null) {
121122
tls =
122-
new ClientConfigTLS(
123-
tomlProfile.tls.disabled,
124-
tomlProfile.tls.clientCertPath,
125-
tomlProfile.tls.clientCertData.getBytes(),
126-
tomlProfile.tls.clientKeyPath,
127-
tomlProfile.tls.clientKeyData.getBytes(),
128-
tomlProfile.tls.serverCACertPath,
129-
tomlProfile.tls.serverCACertData.getBytes(),
130-
tomlProfile.tls.serverName,
131-
tomlProfile.tls.disableHostVerification);
123+
ClientConfigTLS.newBuilder()
124+
.setClientCertData(
125+
tomlProfile.tls.clientCertData != null
126+
? tomlProfile.tls.clientCertData.getBytes(StandardCharsets.UTF_8)
127+
: null)
128+
.setClientCertPath(tomlProfile.tls.clientCertPath)
129+
.setClientKeyData(
130+
tomlProfile.tls.clientKeyData != null
131+
? tomlProfile.tls.clientKeyData.getBytes(StandardCharsets.UTF_8)
132+
: null)
133+
.setClientKeyPath(tomlProfile.tls.clientKeyPath)
134+
.setServerCACertData(
135+
tomlProfile.tls.serverCACertData != null
136+
? tomlProfile.tls.serverCACertData.getBytes(StandardCharsets.UTF_8)
137+
: null)
138+
.setServerCACertPath(tomlProfile.tls.serverCACertPath)
139+
.setDisabled(tomlProfile.tls.disabled)
140+
.setServerName(tomlProfile.tls.serverName)
141+
.setDisableHostVerification(tomlProfile.tls.disableHostVerification)
142+
.build();
132143
}
133144
return tls;
134145
}

temporal-envconfig/src/main/java/io/temporal/envconfig/LoadClientConfigOptions.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ public static Builder newBuilder() {
1010
return new Builder();
1111
}
1212

13+
public static Builder newBuilder(LoadClientConfigOptions options) {
14+
return new Builder(options);
15+
}
16+
1317
private final String configFilePath;
1418
private final byte[] configFileData;
1519
private final boolean strictConfigFile;
@@ -48,7 +52,14 @@ public static class Builder {
4852
private boolean strictConfigFile;
4953
private Map<String, String> envOverrides;
5054

51-
public Builder() {}
55+
private Builder() {}
56+
57+
private Builder(LoadClientConfigOptions options) {
58+
this.configFilePath = options.configFilePath;
59+
this.configFileData = options.configFileData;
60+
this.strictConfigFile = options.strictConfigFile;
61+
this.envOverrides = options.envOverrides;
62+
}
5263

5364
public LoadClientConfigOptions build() {
5465
return new LoadClientConfigOptions(

temporal-envconfig/src/main/java/io/temporal/envconfig/LoadClientConfigProfileOptions.java

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ public static Builder newBuilder() {
1313
return new Builder();
1414
}
1515

16+
public static Builder newBuilder(LoadClientConfigProfileOptions options) {
17+
return new Builder(options);
18+
}
19+
1620
private final String configFileProfile;
1721
private final String configFilePath;
1822
private final byte[] configFileData;
@@ -21,7 +25,7 @@ public static Builder newBuilder() {
2125
private final boolean disableEnv;
2226
private final Map<String, String> envOverrides;
2327

24-
public LoadClientConfigProfileOptions(
28+
private LoadClientConfigProfileOptions(
2529
String configFileProfile,
2630
String configFilePath,
2731
byte[] configFileData,
@@ -75,15 +79,16 @@ public static class Builder {
7579
private boolean disableEnv;
7680
private Map<String, String> envOverrides;
7781

78-
public LoadClientConfigProfileOptions build() {
79-
return new LoadClientConfigProfileOptions(
80-
configFileProfile,
81-
configFilePath,
82-
configFileData,
83-
configFileStrict,
84-
disableFile,
85-
disableEnv,
86-
envOverrides);
82+
private Builder() {}
83+
84+
private Builder(LoadClientConfigProfileOptions options) {
85+
this.configFileProfile = options.configFileProfile;
86+
this.configFilePath = options.configFilePath;
87+
this.configFileData = options.configFileData;
88+
this.configFileStrict = options.configFileStrict;
89+
this.disableFile = options.disableFile;
90+
this.disableEnv = options.disableEnv;
91+
this.envOverrides = options.envOverrides;
8792
}
8893

8994
/** If true, will error if there are unrecognized keys. Defaults to false. */
@@ -150,5 +155,16 @@ public Builder setEnvOverrides(Map<String, String> envOverrides) {
150155
this.envOverrides = envOverrides;
151156
return this;
152157
}
158+
159+
public LoadClientConfigProfileOptions build() {
160+
return new LoadClientConfigProfileOptions(
161+
configFileProfile,
162+
configFilePath,
163+
configFileData,
164+
configFileStrict,
165+
disableFile,
166+
disableEnv,
167+
envOverrides);
168+
}
153169
}
154170
}

0 commit comments

Comments
 (0)