1616/** ClientConfigProfile is profile-level configuration for a client. */
1717@ Experimental
1818public 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 ) {
0 commit comments