11package io .temporal .envconfig ;
22
3- import com .fasterxml .jackson .annotation .JsonIgnoreProperties ;
4- import com .fasterxml .jackson .annotation .JsonInclude ;
5- import com .fasterxml .jackson .annotation .JsonProperty ;
63import com .fasterxml .jackson .databind .ObjectReader ;
74import com .fasterxml .jackson .dataformat .toml .TomlMapper ;
8- import io .grpc .Metadata ;
95import io .temporal .common .Experimental ;
106import java .io .*;
11- import java .util .Collections ;
12- import java .util .HashMap ;
137import java .util .Map ;
14- import javax .annotation .Nullable ;
158
9+ /** ClientConfig represents a client config file. */
1610@ Experimental
1711public class ClientConfig {
1812
13+ /** Get the default config file path: $HOME/.config/temporal/temporal.toml */
1914 private static String getDefaultConfigFilePath () {
2015 String userDir = System .getProperty ("user.home" );
2116 if (userDir == null || userDir .isEmpty ()) {
@@ -41,21 +36,29 @@ public static ClientConfig load() throws IOException {
4136 * <p>This does not apply environment variable overrides to the profiles, it only uses an
4237 * environment variable to find the default config file path (TEMPORAL_CONFIG_FILE). To get a
4338 * single profile with environment variables applied, use {@link ClientConfigProfile#load}.
39+ *
40+ * @param options options to control loading the config
41+ * @throws IOException if the config file cannot be read or parsed
4442 */
4543 public static ClientConfig load (LoadClientConfigOptions options ) throws IOException {
46- ObjectReader reader = new TomlMapper ().readerFor (TomlClientConfig .class );
44+ ObjectReader reader = new TomlMapper ().readerFor (ClientConfigToml . TomlClientConfig .class );
4745 if (options .isStrictConfigFile ()) {
4846 reader =
4947 reader .withFeatures (
5048 com .fasterxml .jackson .databind .DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES );
49+ } else {
50+ reader =
51+ reader .withoutFeatures (
52+ com .fasterxml .jackson .databind .DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES );
5153 }
54+
5255 if (options .getConfigFileData () != null && options .getConfigFileData ().length > 0 ) {
5356 if (options .getConfigFilePath () != null && !options .getConfigFilePath ().isEmpty ()) {
5457 throw new IllegalArgumentException (
5558 "Cannot have both ConfigFileData and ConfigFilePath set" );
5659 }
57- TomlClientConfig result = reader .readValue (options .getConfigFileData ());
58- return new ClientConfig (result );
60+ ClientConfigToml . TomlClientConfig result = reader .readValue (options .getConfigFileData ());
61+ return new ClientConfig (ClientConfigToml . getClientProfiles ( result ) );
5962 } else {
6063 // Get file name which is either set value, env var, or default path
6164 String file = options .getConfigFilePath ();
@@ -71,172 +74,19 @@ public static ClientConfig load(LoadClientConfigOptions options) throws IOExcept
7174 if (file == null || file .isEmpty ()) {
7275 file = getDefaultConfigFilePath ();
7376 }
74- TomlClientConfig result = reader .readValue (file );
75- return new ClientConfig (result );
77+ ClientConfigToml . TomlClientConfig result = reader .readValue (new File ( file ) );
78+ return new ClientConfig (ClientConfigToml . getClientProfiles ( result ) );
7679 }
7780 }
7881
79- private final Map <String , ClientConfigProfile > profiles ;
80-
81- private ClientConfig () {
82- profiles = Collections .emptyMap ();
83- }
84-
85- private String normalizeGrpcMetaKey (String key ) {
86- return key .toLowerCase ().replace ('_' , '-' );
87- }
88-
89- private ClientConfig (TomlClientConfig clientConfig ) {
90- profiles = new HashMap <>(clientConfig .profiles .size ());
91- for (Map .Entry <String , TomlClientConfigProfile > entry : clientConfig .profiles .entrySet ()) {
92- String profileName = entry .getKey ();
93- TomlClientConfigProfile tomlProfile = entry .getValue ();
94- ClientConfigTLS tls = getClientConfigTLS (tomlProfile );
95- Metadata metadata = null ;
96- if (tomlProfile .grpcMeta != null ) {
97- for (Map .Entry <String , String > metaEntry : tomlProfile .grpcMeta .entrySet ()) {
98- if (metadata == null ) {
99- metadata = new Metadata ();
100- }
101- Metadata .Key <String > key =
102- Metadata .Key .of (
103- normalizeGrpcMetaKey (metaEntry .getKey ()), Metadata .ASCII_STRING_MARSHALLER );
104- metadata .put (key , metaEntry .getValue ());
105- }
106- }
107- ClientConfigProfile profile =
108- ClientConfigProfile .newBuilder ()
109- .setAddress (tomlProfile .address )
110- .setNamespace (tomlProfile .namespace )
111- .setApiKey (tomlProfile .apiKey )
112- .setMetadata (metadata )
113- .setTls (tls )
114- .build ();
115- profiles .put (profileName , profile );
116- }
82+ public ClientConfig (Map <String , ClientConfigProfile > profiles ) {
83+ this .profiles = profiles ;
11784 }
11885
119- @ Nullable
120- private static ClientConfigTLS getClientConfigTLS (TomlClientConfigProfile tomlProfile ) {
121- ClientConfigTLS tls = null ;
122- if (tomlProfile .tls != null ) {
123- tls =
124- new ClientConfigTLS (
125- tomlProfile .tls .disabled ,
126- tomlProfile .tls .clientCertPath ,
127- tomlProfile .tls .clientCertData .getBytes (),
128- tomlProfile .tls .clientKeyPath ,
129- tomlProfile .tls .clientKeyData .getBytes (),
130- tomlProfile .tls .serverCACertPath ,
131- tomlProfile .tls .serverCACertData .getBytes (),
132- tomlProfile .tls .serverName ,
133- tomlProfile .tls .disableHostVerification );
134- }
135- return tls ;
136- }
86+ private final Map <String , ClientConfigProfile > profiles ;
13787
88+ /** All profiles loaded from the config file, may be empty but never null. */
13889 public Map <String , ClientConfigProfile > getProfiles () {
13990 return profiles ;
14091 }
141-
142- @ JsonIgnoreProperties (ignoreUnknown = true )
143- @ JsonInclude (JsonInclude .Include .NON_EMPTY )
144- static class TomlClientConfig {
145- @ JsonProperty ("profile" )
146- public Map <String , TomlClientConfigProfile > profiles ;
147-
148- protected TomlClientConfig () {}
149-
150- TomlClientConfig (Map <String , TomlClientConfigProfile > profiles ) {
151- this .profiles = profiles ;
152- }
153- }
154-
155- @ JsonIgnoreProperties (ignoreUnknown = true )
156- @ JsonInclude (JsonInclude .Include .NON_EMPTY )
157- static class TomlClientConfigProfile {
158- @ JsonProperty ("address" )
159- public String address ;
160-
161- @ JsonProperty ("namespace" )
162- public String namespace ;
163-
164- @ JsonProperty ("api_key" )
165- public String apiKey ;
166-
167- @ JsonProperty ("tls" )
168- public TomlClientConfigTLS tls ;
169-
170- @ JsonProperty ("grpc_meta" )
171- public Map <String , String > grpcMeta ;
172-
173- protected TomlClientConfigProfile () {}
174-
175- TomlClientConfigProfile (
176- String address ,
177- String namespace ,
178- String apiKey ,
179- TomlClientConfigTLS tls ,
180- Map <String , String > grpcMeta ) {
181- this .address = address ;
182- this .namespace = namespace ;
183- this .apiKey = apiKey ;
184- this .tls = tls ;
185- this .grpcMeta = grpcMeta ;
186- }
187- }
188-
189- @ JsonIgnoreProperties (ignoreUnknown = true )
190- @ JsonInclude (JsonInclude .Include .NON_EMPTY )
191- static class TomlClientConfigTLS {
192- @ JsonProperty ("disabled" )
193- public boolean disabled ;
194-
195- @ JsonProperty ("client_cert_path" )
196- public String clientCertPath ;
197-
198- @ JsonProperty ("client_cert_data" )
199- public String clientCertData ;
200-
201- @ JsonProperty ("client_key_path" )
202- public String clientKeyPath ;
203-
204- @ JsonProperty ("client_key_data" )
205- public String clientKeyData ;
206-
207- @ JsonProperty ("server_ca_cert_path" )
208- public String serverCACertPath ;
209-
210- @ JsonProperty ("server_ca_cert_data" )
211- public String serverCACertData ;
212-
213- @ JsonProperty ("server_name" )
214- public String serverName ;
215-
216- @ JsonProperty ("disable_host_verification" )
217- public boolean disableHostVerification ;
218-
219- protected TomlClientConfigTLS () {}
220-
221- TomlClientConfigTLS (
222- boolean disabled ,
223- String clientCertPath ,
224- String clientCertData ,
225- String clientKeyPath ,
226- String clientKeyData ,
227- String serverCACertPath ,
228- String serverCACertData ,
229- String serverName ,
230- boolean disableHostVerification ) {
231- this .disabled = disabled ;
232- this .clientCertPath = clientCertPath ;
233- this .clientCertData = clientCertData ;
234- this .clientKeyPath = clientKeyPath ;
235- this .clientKeyData = clientKeyData ;
236- this .serverCACertPath = serverCACertPath ;
237- this .serverCACertData = serverCACertData ;
238- this .serverName = serverName ;
239- this .disableHostVerification = disableHostVerification ;
240- }
241- }
24292}
0 commit comments