Skip to content

Commit e931da8

Browse files
committed
Tested changes and modified accordingly
1 parent 4862b9f commit e931da8

File tree

1 file changed

+35
-12
lines changed

1 file changed

+35
-12
lines changed

sdk/identity/azure-identity/src/main/java/com/azure/identity/WorkloadIdentityCredential.java

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ public class WorkloadIdentityCredential implements TokenCredential {
7272
IdentityClientOptions identityClientOptions) {
7373
ValidationUtil.validateTenantIdCharacterRange(tenantId, LOGGER);
7474

75+
if (identityClientOptions == null) {
76+
throw LOGGER.logExceptionAsError(new IllegalArgumentException("identityClientOptions cannot be null"));
77+
}
78+
7579
Configuration configuration = identityClientOptions.getConfiguration() == null
7680
? Configuration.getGlobalConfiguration().clone()
7781
: identityClientOptions.getConfiguration();
@@ -90,20 +94,35 @@ public class WorkloadIdentityCredential implements TokenCredential {
9094
|| CoreUtils.isNullOrEmpty(federatedTokenFilePathInput)
9195
|| CoreUtils.isNullOrEmpty(clientIdInput)
9296
|| 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)
96105
.clientId(clientIdInput)
97106
.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());
105110
}
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+
107126
clientAssertionCredential = builder.build();
108127
this.clientId = clientIdInput;
109128
} else {
@@ -146,8 +165,12 @@ String getClientId() {
146165
* This token will be used as a client assertion for authentication.
147166
*/
148167
private String readFederatedTokenFromFile(String filePath) {
168+
if (filePath == null) {
169+
throw LOGGER.logExceptionAsError(new IllegalArgumentException("Federated token file path cannot be null"));
170+
}
149171
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();
151174
} catch (IOException e) {
152175
throw LOGGER.logExceptionAsError(new RuntimeException("Failed to read federated token from file. ", e));
153176
}

0 commit comments

Comments
 (0)