Skip to content

Commit c21eb19

Browse files
Use static errors for OIDC Token fetcher (#64)
Allows structured error handling in the consumer
1 parent a6c950a commit c21eb19

1 file changed

Lines changed: 26 additions & 9 deletions

File tree

actions.go

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,27 @@ import (
3333
"time"
3434
)
3535

36-
var (
37-
// osExit allows `os.Exit()` to be stubbed during testing.
38-
osExit = os.Exit
39-
)
36+
// osExit allows `os.Exit()` to be stubbed during testing.
37+
var osExit = os.Exit
38+
39+
// EnvMissingError is returned when a required environment variable is missing.
40+
type EnvMissingError struct {
41+
Name string
42+
}
43+
44+
func (e *EnvMissingError) Error() string {
45+
return fmt.Sprintf("missing %s in environment", e.Name)
46+
}
47+
48+
// RequestFailedError is returned when a request to the GitHub Actions runtime fails.
49+
type RequestFailedError struct {
50+
StatusCode int
51+
Body string
52+
}
53+
54+
func (e *RequestFailedError) Error() string {
55+
return fmt.Sprintf("non-successful response from minting OIDC token (status %d): %s", e.StatusCode, e.Body)
56+
}
4057

4158
const (
4259
addMaskCmd = "add-mask"
@@ -63,7 +80,7 @@ const (
6380
warningCmd = "warning"
6481
errorCmd = "error"
6582

66-
errFileCmdFmt = "unable to write command to the environment file: %s"
83+
errFileCmdFmt = "unable to write command to the environment file: %w"
6784
)
6885

6986
// New creates a new wrapper with helpers for outputting information in GitHub
@@ -131,7 +148,7 @@ func (c *Action) issueFileCommand(cmd *Command) (retErr error) {
131148

132149
filepath := c.getenv(e)
133150
msg := []byte(cmd.Message + EOF)
134-
f, err := os.OpenFile(filepath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
151+
f, err := os.OpenFile(filepath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
135152
if err != nil {
136153
retErr = fmt.Errorf(errFileCmdFmt, err)
137154
return
@@ -396,12 +413,12 @@ type idTokenResponse struct {
396413
func (c *Action) GetIDToken(ctx context.Context, audience string) (string, error) {
397414
requestURL := c.getenv("ACTIONS_ID_TOKEN_REQUEST_URL")
398415
if requestURL == "" {
399-
return "", fmt.Errorf("missing ACTIONS_ID_TOKEN_REQUEST_URL in environment")
416+
return "", &EnvMissingError{Name: "ACTIONS_ID_TOKEN_REQUEST_URL"}
400417
}
401418

402419
requestToken := c.getenv("ACTIONS_ID_TOKEN_REQUEST_TOKEN")
403420
if requestToken == "" {
404-
return "", fmt.Errorf("missing ACTIONS_ID_TOKEN_REQUEST_TOKEN in environment")
421+
return "", &EnvMissingError{Name: "ACTIONS_ID_TOKEN_REQUEST_TOKEN"}
405422
}
406423

407424
u, err := url.Parse(requestURL)
@@ -435,7 +452,7 @@ func (c *Action) GetIDToken(ctx context.Context, audience string) (string, error
435452
body = bytes.TrimSpace(body)
436453

437454
if resp.StatusCode != 200 {
438-
return "", fmt.Errorf("non-successful response from minting OIDC token: %s", body)
455+
return "", &RequestFailedError{StatusCode: resp.StatusCode, Body: string(body)}
439456
}
440457

441458
var tokenResp idTokenResponse

0 commit comments

Comments
 (0)