Skip to content

Commit

Permalink
Add support for oidc aws session tags
Browse files Browse the repository at this point in the history
  • Loading branch information
sj26 committed Sep 24, 2024
1 parent 65ed6aa commit 243b1a8
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 15 deletions.
23 changes: 13 additions & 10 deletions api/oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,24 @@ type OIDCToken struct {
}

type OIDCTokenRequest struct {
Job string
Audience string
Lifetime int
Claims []string
Job string
Audience string
Lifetime int
Claims []string
AwsSessionTags []string
}

func (c *Client) OIDCToken(ctx context.Context, methodReq *OIDCTokenRequest) (*OIDCToken, *Response, error) {
m := &struct {
Audience string `json:"audience,omitempty"`
Lifetime int `json:"lifetime,omitempty"`
Claims []string `json:"claims,omitempty"`
Audience string `json:"audience,omitempty"`
Lifetime int `json:"lifetime,omitempty"`
Claims []string `json:"claims,omitempty"`
AwsSessionTags []string `json:"aws_session_tags,omitempty"`
}{
Audience: methodReq.Audience,
Lifetime: methodReq.Lifetime,
Claims: methodReq.Claims,
Audience: methodReq.Audience,
Lifetime: methodReq.Lifetime,
Claims: methodReq.Claims,
AwsSessionTags: methodReq.AwsSessionTags,
}

u := fmt.Sprintf("jobs/%s/oidc/tokens", railsPathEscape(methodReq.Job))
Expand Down
18 changes: 18 additions & 0 deletions api/oidc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,24 @@ func TestOIDCToken(t *testing.T) {
ExpectedBody: []byte(fmt.Sprintf(`{"lifetime":%d}`+"\n", lifetime)),
OIDCToken: &api.OIDCToken{Token: oidcToken},
},
{
AccessToken: accessToken,
OIDCTokenRequest: &api.OIDCTokenRequest{
Job: jobID,
Claims: []string{"organization_id", "pipeline_id"},
},
ExpectedBody: []byte(`{"claims":["organization_id","pipeline_id"]}` + "\n"),
OIDCToken: &api.OIDCToken{Token: oidcToken},
},
{
AccessToken: accessToken,
OIDCTokenRequest: &api.OIDCTokenRequest{
Job: jobID,
AwsSessionTags: []string{"organization_id", "pipeline_id"},
},
ExpectedBody: []byte(`{"aws_session_tags":["organization_id","pipeline_id"]}` + "\n"),
OIDCToken: &api.OIDCToken{Token: oidcToken},
},
}

for _, test := range tests {
Expand Down
19 changes: 14 additions & 5 deletions clicommand/oidc_request_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ type OIDCTokenConfig struct {
Lifetime int `cli:"lifetime"`
Job string `cli:"job" validate:"required"`
// TODO: enumerate possible values, perhaps by adding a link to the documentation
Claims []string `cli:"claim" normalize:"list"`
Claims []string `cli:"claim" normalize:"list"`
AwsSessionTags []string `cli:"aws-session-tag" normalize:"list"`

// Global flags
Debug bool `cli:"debug"`
Expand Down Expand Up @@ -79,6 +80,13 @@ var OIDCRequestTokenCommand = cli.Command{
EnvVar: "BUILDKITE_OIDC_TOKEN_CLAIMS",
},

cli.StringSliceFlag{
Name: "aws-session-tag",
Value: &cli.StringSlice{},
Usage: "Add claims as AWS Session Tags",
EnvVar: "BUILDKITE_OIDC_TOKEN_AWS_SESSION_TAGS",
},

// API Flags
AgentAccessTokenFlag,
EndpointFlag,
Expand Down Expand Up @@ -112,10 +120,11 @@ var OIDCRequestTokenCommand = cli.Command{
)
token, err := roko.DoFunc(ctx, r, func(r *roko.Retrier) (*api.OIDCToken, error) {
req := &api.OIDCTokenRequest{
Job: cfg.Job,
Audience: cfg.Audience,
Lifetime: cfg.Lifetime,
Claims: cfg.Claims,
Job: cfg.Job,
Audience: cfg.Audience,
Lifetime: cfg.Lifetime,
Claims: cfg.Claims,
AwsSessionTags: cfg.AwsSessionTags,
}

token, resp, err := client.OIDCToken(ctx, req)
Expand Down

0 comments on commit 243b1a8

Please sign in to comment.