Skip to content

Commit 1d56474

Browse files
authored
Improve AzurePipelinesCredential errors (Azure#22958)
1 parent b2f6b84 commit 1d56474

File tree

4 files changed

+14
-7
lines changed

4 files changed

+14
-7
lines changed

sdk/azidentity/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
### Bugs Fixed
1212

1313
### Other Changes
14+
* Added more details to `AzurePipelinesCredential` error messages
1415

1516
## 1.6.0-beta.4 (2024-05-14)
1617

sdk/azidentity/azure_pipelines_credential.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,27 +98,33 @@ func (a *AzurePipelinesCredential) getAssertion(ctx context.Context) (string, er
9898
url := a.oidcURI + "?api-version=" + oidcAPIVersion + "&serviceConnectionId=" + a.connectionID
9999
url, err := runtime.EncodeQueryParams(url)
100100
if err != nil {
101-
return "", err
101+
return "", newAuthenticationFailedError(credNameAzurePipelines, "couldn't encode OIDC URL: "+err.Error(), nil, nil)
102102
}
103103
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, nil)
104104
if err != nil {
105-
return "", err
105+
return "", newAuthenticationFailedError(credNameAzurePipelines, "couldn't create OIDC token request: "+err.Error(), nil, nil)
106106
}
107107
req.Header.Set("Authorization", "Bearer "+a.systemAccessToken)
108108
res, err := doForClient(a.cred.client.azClient, req)
109109
if err != nil {
110-
return "", err
110+
return "", newAuthenticationFailedError(credNameAzurePipelines, "couldn't send OIDC token request: "+err.Error(), nil, nil)
111+
}
112+
if res.StatusCode != http.StatusOK {
113+
msg := res.Status + " response from the OIDC endpoint. Check service connection ID and Pipeline configuration"
114+
// include the response because its body, if any, probably contains an error message.
115+
// OK responses aren't included with errors because they probably contain secrets
116+
return "", newAuthenticationFailedError(credNameAzurePipelines, msg, res, nil)
111117
}
112118
b, err := runtime.Payload(res)
113119
if err != nil {
114-
return "", err
120+
return "", newAuthenticationFailedError(credNameAzurePipelines, "couldn't read OIDC response content: "+err.Error(), nil, nil)
115121
}
116122
var r struct {
117123
OIDCToken string `json:"oidcToken"`
118124
}
119125
err = json.Unmarshal(b, &r)
120126
if err != nil {
121-
return "", err
127+
return "", newAuthenticationFailedError(credNameAzurePipelines, "unexpected response from OIDC endpoint", nil, nil)
122128
}
123129
return r.OIDCToken, nil
124130
}

sdk/azidentity/errors.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func (e *AuthenticationFailedError) Error() string {
5353
return e.credType + ": " + e.message
5454
}
5555
msg := &bytes.Buffer{}
56-
fmt.Fprintf(msg, e.credType+" authentication failed\n")
56+
fmt.Fprintf(msg, "%s authentication failed. %s\n", e.credType, e.message)
5757
if e.RawResponse.Request != nil {
5858
fmt.Fprintf(msg, "%s %s://%s%s\n", e.RawResponse.Request.Method, e.RawResponse.Request.URL.Scheme, e.RawResponse.Request.URL.Host, e.RawResponse.Request.URL.Path)
5959
} else {

sdk/azidentity/managed_identity_client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ func (c *managedIdentityClient) authenticate(ctx context.Context, id ManagedIDKi
238238
}
239239
}
240240

241-
return azcore.AccessToken{}, newAuthenticationFailedError(credNameManagedIdentity, "authentication failed", resp, nil)
241+
return azcore.AccessToken{}, newAuthenticationFailedError(credNameManagedIdentity, "", resp, nil)
242242
}
243243

244244
func (c *managedIdentityClient) createAccessToken(res *http.Response) (azcore.AccessToken, error) {

0 commit comments

Comments
 (0)