@@ -72,6 +72,10 @@ public class WorkloadIdentityCredential implements TokenCredential {
72
72
IdentityClientOptions identityClientOptions ) {
73
73
ValidationUtil .validateTenantIdCharacterRange (tenantId , LOGGER );
74
74
75
+ if (identityClientOptions == null ) {
76
+ throw LOGGER .logExceptionAsError (new IllegalArgumentException ("identityClientOptions cannot be null" ));
77
+ }
78
+
75
79
Configuration configuration = identityClientOptions .getConfiguration () == null
76
80
? Configuration .getGlobalConfiguration ().clone ()
77
81
: identityClientOptions .getConfiguration ();
@@ -90,20 +94,35 @@ public class WorkloadIdentityCredential implements TokenCredential {
90
94
|| CoreUtils .isNullOrEmpty (federatedTokenFilePathInput )
91
95
|| CoreUtils .isNullOrEmpty (clientIdInput )
92
96
|| CoreUtils .isNullOrEmpty (identityClientOptions .getAuthorityHost ()))) {
93
-
94
- ClientAssertionCredentialBuilder builder = new ClientAssertionCredentialBuilder ()
95
- .tenantId (tenantIdInput )
97
+
98
+ if (tenantIdInput == null || clientIdInput == null || federatedTokenFilePathInput == null ) {
99
+ throw LOGGER .logExceptionAsError (
100
+ new IllegalStateException ("Required parameters cannot be null: tenantId=" + tenantIdInput
101
+ + ", clientId=" + clientIdInput + ", federatedTokenFilePath=" + federatedTokenFilePathInput ));
102
+ }
103
+
104
+ ClientAssertionCredentialBuilder builder = new ClientAssertionCredentialBuilder ().tenantId (tenantIdInput )
96
105
.clientId (clientIdInput )
97
106
.clientAssertion (() -> readFederatedTokenFromFile (federatedTokenFilePathInput ));
98
- builder .authorityHost (identityClientOptions .getAuthorityHost ())
99
- .httpClient (identityClientOptions .getHttpClient ())
100
- .maxRetry (identityClientOptions .getMaxRetry ())
101
- .retryTimeout (identityClientOptions .getRetryTimeout ());
102
-
103
- if (identityClientOptions .getAdditionallyAllowedTenants () != null ) {
104
- builder .additionallyAllowedTenants (identityClientOptions .getAdditionallyAllowedTenants ().toArray (new String [0 ]));
107
+
108
+ if (identityClientOptions .getAuthorityHost () != null ) {
109
+ builder .authorityHost (identityClientOptions .getAuthorityHost ());
105
110
}
106
-
111
+ builder .maxRetry (identityClientOptions .getMaxRetry ());
112
+
113
+ if (identityClientOptions .getHttpClient () != null ) {
114
+ builder .httpClient (identityClientOptions .getHttpClient ());
115
+ }
116
+ if (identityClientOptions .getRetryTimeout () != null ) {
117
+ builder .retryTimeout (identityClientOptions .getRetryTimeout ());
118
+ }
119
+
120
+ if (identityClientOptions .getAdditionallyAllowedTenants () != null
121
+ && !identityClientOptions .getAdditionallyAllowedTenants ().isEmpty ()) {
122
+ builder .additionallyAllowedTenants (
123
+ identityClientOptions .getAdditionallyAllowedTenants ().toArray (new String [0 ]));
124
+ }
125
+
107
126
clientAssertionCredential = builder .build ();
108
127
this .clientId = clientIdInput ;
109
128
} else {
@@ -146,8 +165,12 @@ String getClientId() {
146
165
* This token will be used as a client assertion for authentication.
147
166
*/
148
167
private String readFederatedTokenFromFile (String filePath ) {
168
+ if (filePath == null ) {
169
+ throw LOGGER .logExceptionAsError (new IllegalArgumentException ("Federated token file path cannot be null" ));
170
+ }
149
171
try {
150
- return Files .readString (Paths .get (filePath ), StandardCharsets .UTF_8 ).trim ();
172
+ byte [] bytes = Files .readAllBytes (Paths .get (filePath ));
173
+ return new String (bytes , StandardCharsets .UTF_8 ).trim ();
151
174
} catch (IOException e ) {
152
175
throw LOGGER .logExceptionAsError (new RuntimeException ("Failed to read federated token from file. " , e ));
153
176
}
0 commit comments