Skip to content

Commit 179bf0d

Browse files
More tests
1 parent fb820a0 commit 179bf0d

6 files changed

Lines changed: 246 additions & 94 deletions

File tree

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

Lines changed: 94 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616
/** ClientConfigProfile is profile-level configuration for a client. */
1717
@Experimental
1818
public class ClientConfigProfile {
19-
/** Creates a new builder to build a ClientConfigProfile. */
19+
/** Creates a new builder to build a {@link ClientConfigProfile}. */
2020
public static Builder newBuilder() {
2121
return new Builder();
2222
}
2323

2424
/**
25-
* Creates a new builder to build a ClientConfigProfile based on an existing profile.
25+
* Creates a new builder to build a {@link ClientConfigProfile} based on an existing profile.
2626
*
2727
* @param profile the existing profile to base the builder on
2828
* @return a new Builder instance
@@ -31,11 +31,72 @@ public static Builder newBuilder(ClientConfigProfile profile) {
3131
return new Builder(profile);
3232
}
3333

34-
/** Returns a default instance of ClientConfigProfile with all fields unset. */
34+
/** Returns a default instance of {@link ClientConfigProfile} with all fields unset. */
3535
public static ClientConfigProfile getDefaultInstance() {
3636
return new Builder().build();
3737
}
3838

39+
/**
40+
* Load a client profile from given sources, applying environment variable overrides.
41+
*
42+
* <p>This is a convenience method for {@link #load(LoadClientConfigProfileOptions)} with default
43+
* options.
44+
*
45+
* @throws IOException if the config file cannot be read or parsed
46+
*/
47+
public static ClientConfigProfile load() throws IOException {
48+
return load(LoadClientConfigProfileOptions.newBuilder().build());
49+
}
50+
51+
/**
52+
* Load a client profile from given sources, applying environment variable overrides.
53+
*
54+
* @param options options to control loading the profile
55+
* @throws IOException if the config file cannot be read or parsed
56+
*/
57+
public static ClientConfigProfile load(LoadClientConfigProfileOptions options)
58+
throws IOException {
59+
if (options.isDisableFile() && options.isDisableEnv()) {
60+
throw new IllegalArgumentException("Cannot have both DisableFile and DisableEnv set to true");
61+
}
62+
Map<String, String> env = System.getenv();
63+
if (options.getEnvOverrides() != null) {
64+
env = options.getEnvOverrides();
65+
}
66+
ClientConfigProfile clientConfigProfile = ClientConfigProfile.getDefaultInstance();
67+
// If file is enabled, load it and find just the profile
68+
if (!options.isDisableFile()) {
69+
ClientConfig conf =
70+
ClientConfig.load(
71+
LoadClientConfigOptions.newBuilder()
72+
.setConfigFilePath(options.getConfigFilePath())
73+
.setConfigFileData(options.getConfigFileData())
74+
.setStrictConfigFile(options.isConfigFileStrict())
75+
.setEnvOverrides(options.getEnvOverrides())
76+
.build());
77+
String profile = options.getConfigFileProfile();
78+
boolean profileUnset = false;
79+
if (profile == null || profile.isEmpty()) {
80+
profile = env.get("TEMPORAL_PROFILE");
81+
}
82+
if (profile == null || profile.isEmpty()) {
83+
profile = "default";
84+
profileUnset = true;
85+
}
86+
ClientConfigProfile fileClientConfigProfile = conf.getProfiles().get(profile);
87+
if (fileClientConfigProfile != null) {
88+
clientConfigProfile = fileClientConfigProfile;
89+
} else if (!profileUnset) {
90+
throw new IllegalArgumentException("Unable to find profile " + profile + " in config data");
91+
}
92+
}
93+
// If env is enabled, apply it on top of an empty profile
94+
if (!options.isDisableEnv()) {
95+
clientConfigProfile.applyEnvOverrides(options.getEnvOverrides());
96+
}
97+
return clientConfigProfile;
98+
}
99+
39100
private String namespace;
40101
private String address;
41102
private String apiKey;
@@ -70,48 +131,64 @@ public WorkflowServiceStubsOptions toWorkflowServiceStubsOptions() {
70131
if (this.metadata != null) {
71132
builder.addGrpcMetadataProvider(() -> this.metadata);
72133
}
73-
if (this.tls != null && !this.tls.isDisabled()) {
134+
if (this.tls != null && !Boolean.FALSE.equals(this.tls.isDisabled())) {
135+
InputStream clientCertStream = null;
136+
InputStream keyFileStream = null;
137+
InputStream trustCertCollectionInputStream = null;
74138
try {
75-
InputStream clientCertStream;
76139
if (this.tls.getClientCertPath() != null && !this.tls.getClientCertPath().isEmpty()) {
77140
clientCertStream = Files.newInputStream(Paths.get(this.tls.getClientCertPath()));
78141
} else if (this.tls.getClientCertData() != null
79142
&& this.tls.getClientCertData().length > 0) {
80143
clientCertStream = new ByteArrayInputStream(this.tls.getClientCertData());
81-
} else {
82-
clientCertStream = null;
83144
}
84145

85-
InputStream keyFile;
86146
if (this.tls.getClientKeyPath() != null && !this.tls.getClientKeyPath().isEmpty()) {
87-
keyFile = Files.newInputStream(Paths.get(this.tls.getClientKeyPath()));
147+
keyFileStream = Files.newInputStream(Paths.get(this.tls.getClientKeyPath()));
88148
} else if (this.tls.getClientKeyData() != null && this.tls.getClientKeyData().length > 0) {
89-
keyFile = new ByteArrayInputStream(this.tls.getClientKeyData());
90-
} else {
91-
keyFile = null;
149+
keyFileStream = new ByteArrayInputStream(this.tls.getClientKeyData());
92150
}
93151

94-
InputStream trustCertCollectionInputStream;
95152
if (this.tls.getServerCACertPath() != null && !this.tls.getServerCACertPath().isEmpty()) {
96153
trustCertCollectionInputStream =
97154
Files.newInputStream(Paths.get(this.tls.getServerCACertPath()));
98155
} else if (this.tls.getServerCACertData() != null
99156
&& this.tls.getServerCACertData().length > 0) {
100157
trustCertCollectionInputStream = new ByteArrayInputStream(this.tls.getServerCACertData());
101-
} else {
102-
trustCertCollectionInputStream = null;
103158
}
104159

105160
SslContextBuilder sslContextBuilder = SslContextBuilder.forClient();
106161
if (trustCertCollectionInputStream != null) {
107162
sslContextBuilder.trustManager(trustCertCollectionInputStream);
108-
} else if (this.tls.isDisableHostVerification()) {
163+
} else if (Boolean.TRUE.equals(this.tls.isDisableHostVerification())) {
109164
sslContextBuilder.trustManager(InsecureTrustManagerFactory.INSTANCE);
110165
}
111-
sslContextBuilder.keyManager(clientCertStream, keyFile);
166+
sslContextBuilder.keyManager(clientCertStream, keyFileStream);
112167
builder.setSslContext(sslContextBuilder.build());
113168
} catch (IOException e) {
114169
throw new RuntimeException("Unable to create SSL context", e);
170+
} finally {
171+
if (clientCertStream != null) {
172+
try {
173+
clientCertStream.close();
174+
} catch (IOException e) {
175+
// Ignore
176+
}
177+
}
178+
if (keyFileStream != null) {
179+
try {
180+
keyFileStream.close();
181+
} catch (IOException e) {
182+
// Ignore
183+
}
184+
}
185+
if (trustCertCollectionInputStream != null) {
186+
try {
187+
trustCertCollectionInputStream.close();
188+
} catch (IOException e) {
189+
// Ignore
190+
}
191+
}
115192
}
116193
if (this.tls.getServerName() != null && !this.tls.getServerName().isEmpty()) {
117194
builder.setChannelInitializer(c -> c.overrideAuthority(this.tls.getServerName()));
@@ -137,53 +214,6 @@ public WorkflowClientOptions toWorkflowClientOptions() {
137214
return builder.build();
138215
}
139216

140-
public static ClientConfigProfile load() throws IOException {
141-
return load(LoadClientConfigProfileOptions.newBuilder().build());
142-
}
143-
144-
public static ClientConfigProfile load(LoadClientConfigProfileOptions options)
145-
throws IOException {
146-
if (options.isDisableFile() && options.isDisableEnv()) {
147-
throw new IllegalArgumentException("Cannot have both DisableFile and DisableEnv set to true");
148-
}
149-
Map<String, String> env = System.getenv();
150-
if (options.getEnvOverrides() != null) {
151-
env = options.getEnvOverrides();
152-
}
153-
ClientConfigProfile clientConfigProfile = ClientConfigProfile.getDefaultInstance();
154-
// If file is enabled, load it and find just the profile
155-
if (!options.isDisableFile()) {
156-
ClientConfig conf =
157-
ClientConfig.load(
158-
LoadClientConfigOptions.newBuilder()
159-
.setConfigFilePath(options.getConfigFilePath())
160-
.setConfigFileData(options.getConfigFileData())
161-
.setStrictConfigFile(options.isConfigFileStrict())
162-
.setEnvOverrides(options.getEnvOverrides())
163-
.build());
164-
String profile = options.getConfigFileProfile();
165-
boolean profileUnset = false;
166-
if (profile == null || profile.isEmpty()) {
167-
profile = env.get("TEMPORAL_PROFILE");
168-
}
169-
if (profile == null || profile.isEmpty()) {
170-
profile = "default";
171-
profileUnset = true;
172-
}
173-
ClientConfigProfile fileClientConfigProfile = conf.getProfiles().get(profile);
174-
if (fileClientConfigProfile != null) {
175-
clientConfigProfile = fileClientConfigProfile;
176-
} else if (!profileUnset) {
177-
throw new IllegalArgumentException("Unable to find profile " + profile + " in config data");
178-
}
179-
}
180-
// If env is enabled, apply it on top of an empty profile
181-
if (!options.isDisableEnv()) {
182-
clientConfigProfile.applyEnvOverrides(options.getEnvOverrides());
183-
}
184-
return clientConfigProfile;
185-
}
186-
187217
private void applyEnvOverrides(Map<String, String> overrideEnvVars) {
188218
Map<String, String> env = System.getenv();
189219
if (overrideEnvVars != null) {

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

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,31 @@ public static Builder newBuilder(ClientConfigTLS config) {
1515
return new Builder(config);
1616
}
1717

18-
private final boolean disabled;
18+
/** Returns a default instance of {@link ClientConfigTLS} with all fields unset. */
19+
public static ClientConfigTLS getDefaultInstance() {
20+
return new Builder().build();
21+
}
22+
23+
private final Boolean disabled;
1924
private final String clientCertPath;
2025
private final byte[] clientCertData;
2126
private final String clientKeyPath;
2227
private final byte[] clientKeyData;
2328
private final String serverCACertPath;
2429
private final byte[] serverCACertData;
2530
private final String serverName;
26-
private final boolean disableHostVerification;
31+
private final Boolean disableHostVerification;
2732

2833
private ClientConfigTLS(
29-
boolean disabled,
34+
Boolean disabled,
3035
String clientCertPath,
3136
byte[] clientCertData,
3237
String clientKeyPath,
3338
byte[] clientKeyData,
3439
String serverCACertPath,
3540
byte[] serverCACertData,
3641
String serverName,
37-
boolean disableHostVerification) {
42+
Boolean disableHostVerification) {
3843
this.disabled = disabled;
3944
this.clientCertPath = clientCertPath;
4045
this.clientCertData = clientCertData;
@@ -46,7 +51,7 @@ private ClientConfigTLS(
4651
this.disableHostVerification = disableHostVerification;
4752
}
4853

49-
public boolean isDisabled() {
54+
public Boolean isDisabled() {
5055
return disabled;
5156
}
5257

@@ -78,7 +83,7 @@ public String getServerName() {
7883
return serverName;
7984
}
8085

81-
public boolean isDisableHostVerification() {
86+
public Boolean isDisableHostVerification() {
8287
return disableHostVerification;
8388
}
8489

@@ -93,9 +98,9 @@ public static class Builder {
9398
private byte[] clientKeyData;
9499
private String serverCACertPath;
95100
private byte[] serverCACertData;
96-
private boolean disabled;
101+
private Boolean disabled;
97102
private String serverName;
98-
private boolean disableHostVerification;
103+
private Boolean disableHostVerification;
99104

100105
private Builder() {}
101106

@@ -112,7 +117,7 @@ private Builder(ClientConfigTLS clientConfigTLS) {
112117
}
113118

114119
/** Disable TLS. Default: false. */
115-
public Builder setDisabled(boolean disabled) {
120+
public Builder setDisabled(Boolean disabled) {
116121
this.disabled = disabled;
117122
return this;
118123
}
@@ -163,7 +168,7 @@ public Builder setServerCACertData(byte[] serverCACertData) {
163168
}
164169

165170
/** Disable server host verification. Default: false */
166-
public Builder setDisableHostVerification(boolean disableHostVerification) {
171+
public Builder setDisableHostVerification(Boolean disableHostVerification) {
167172
this.disableHostVerification = disableHostVerification;
168173
return this;
169174
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ protected TomlClientConfigProfile() {}
6565
@JsonInclude(JsonInclude.Include.NON_EMPTY)
6666
static class TomlClientConfigTLS {
6767
@JsonProperty("disabled")
68-
public boolean disabled;
68+
public Boolean disabled;
6969

7070
@JsonProperty("client_cert_path")
7171
public String clientCertPath;
@@ -89,20 +89,20 @@ static class TomlClientConfigTLS {
8989
public String serverName;
9090

9191
@JsonProperty("disable_host_verification")
92-
public boolean disableHostVerification;
92+
public Boolean disableHostVerification;
9393

9494
protected TomlClientConfigTLS() {}
9595

9696
TomlClientConfigTLS(
97-
boolean disabled,
97+
Boolean disabled,
9898
String clientCertPath,
9999
String clientCertData,
100100
String clientKeyPath,
101101
String clientKeyData,
102102
String serverCACertPath,
103103
String serverCACertData,
104104
String serverName,
105-
boolean disableHostVerification) {
105+
Boolean disableHostVerification) {
106106
this.disabled = disabled;
107107
this.clientCertPath = clientCertPath;
108108
this.clientCertData = clientCertData;

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,27 @@
66
/** Options for loading a client config via {@link ClientConfig#load(LoadClientConfigOptions)} */
77
@Experimental
88
public class LoadClientConfigOptions {
9+
/** Create a builder for {@link LoadClientConfigOptions}. */
910
public static Builder newBuilder() {
1011
return new Builder();
1112
}
1213

14+
/** Create a builder from an existing {@link LoadClientConfigOptions}. */
1315
public static Builder newBuilder(LoadClientConfigOptions options) {
1416
return new Builder(options);
1517
}
1618

19+
/** Returns a default instance of {@link LoadClientConfigOptions} with all fields unset. */
20+
public static LoadClientConfigOptions getDefaultInstance() {
21+
return new Builder().build();
22+
}
23+
1724
private final String configFilePath;
1825
private final byte[] configFileData;
1926
private final boolean strictConfigFile;
2027
private final Map<String, String> envOverrides;
2128

22-
public LoadClientConfigOptions(
29+
private LoadClientConfigOptions(
2330
String configFilePath,
2431
byte[] configFileData,
2532
boolean strictConfigFile,
@@ -61,11 +68,6 @@ private Builder(LoadClientConfigOptions options) {
6168
this.envOverrides = options.envOverrides;
6269
}
6370

64-
public LoadClientConfigOptions build() {
65-
return new LoadClientConfigOptions(
66-
configFilePath, configFileData, strictConfigFile, envOverrides);
67-
}
68-
6971
/** If true, will error if there are unrecognized keys. Defaults to false. */
7072
public Builder setStrictConfigFile(boolean configFileStrict) {
7173
this.strictConfigFile = configFileStrict;
@@ -100,5 +102,10 @@ public Builder setConfigFilePath(String configFilePath) {
100102
this.configFilePath = configFilePath;
101103
return this;
102104
}
105+
106+
public LoadClientConfigOptions build() {
107+
return new LoadClientConfigOptions(
108+
configFilePath, configFileData, strictConfigFile, envOverrides);
109+
}
103110
}
104111
}

0 commit comments

Comments
 (0)