-
Notifications
You must be signed in to change notification settings - Fork 47
Enable additional identity providers for machine accounts (GitHub Actions enablement) #5385
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
evankanderson
merged 14 commits into
mindersec:main
from
evankanderson:github-actions-auth
Feb 3, 2025
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
61864cd
Commit working spike
evankanderson 3e08526
Relax requirement for users to be in Minder DB in two other places in…
evankanderson dbc09df
Merge remote-tracking branch 'upstream/main' into auth-token-experime…
evankanderson ae53ffb
Merge remote-tracking branch 'upstream/main' into auth-token-experime…
evankanderson 1c955b9
Fix stacklok/minder --> mindersec/minder
evankanderson d41e578
Fix mocks in tests added after this PR started
evankanderson a8c5b09
Merge remote-tracking branch 'upstream/main' into auth-token-experime…
evankanderson cde854a
Clean up spike from #4317 for merge
evankanderson e10fca5
Fix lint and test errors, simplify interfaces
evankanderson edf9294
Fix internal/authz and internal/controlplane tests
evankanderson 452a379
Address feedback from eleftherias
evankanderson 241b927
Fix lint errors
evankanderson 3c6d028
Merge remote-tracking branch 'upstream/main' into github-actions-auth
evankanderson d92257f
Avoid using <> tag-like constructs in proto comments
evankanderson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// SPDX-FileCopyrightText: Copyright 2025 The Minder Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package auth | ||
|
||
import "context" | ||
|
||
type idContextKeyType struct{} | ||
|
||
var idContextKey idContextKeyType | ||
|
||
// WithIdentityContext stores the identity in the context. | ||
func WithIdentityContext(ctx context.Context, identity *Identity) context.Context { | ||
return context.WithValue(ctx, idContextKey, identity) | ||
} | ||
|
||
// IdentityFromContext retrieves the caller's identity from the context. | ||
// This may return `nil` or an empty Identity if the user is not authenticated. | ||
func IdentityFromContext(ctx context.Context) *Identity { | ||
id, ok := ctx.Value(idContextKey).(*Identity) | ||
if !ok { | ||
return nil | ||
} | ||
return id | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// SPDX-FileCopyrightText: Copyright 2025 The Minder Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// Package githubactions provides an implementation of the GitHub IdentityProvider. | ||
package githubactions | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"net/url" | ||
"strings" | ||
|
||
"github.com/lestrrat-go/jwx/v2/jwt" | ||
|
||
"github.com/mindersec/minder/internal/auth" | ||
) | ||
|
||
// GitHubActions is an implementation of the auth.IdentityProvider interface. | ||
type GitHubActions struct { | ||
} | ||
|
||
var _ auth.IdentityProvider = (*GitHubActions)(nil) | ||
var _ auth.Resolver = (*GitHubActions)(nil) | ||
|
||
var ghIssuerUrl = url.URL{ | ||
Scheme: "https", | ||
Host: "token.actions.githubusercontent.com", | ||
} | ||
|
||
// String implements auth.IdentityProvider. | ||
func (_ *GitHubActions) String() string { | ||
return "githubactions" | ||
} | ||
|
||
// URL implements auth.IdentityProvider. | ||
func (_ *GitHubActions) URL() url.URL { | ||
return ghIssuerUrl | ||
} | ||
|
||
// Resolve implements auth.IdentityProvider. | ||
func (gha *GitHubActions) Resolve(_ context.Context, id string) (*auth.Identity, error) { | ||
// GitHub Actions subjects look like: | ||
// repo:evankanderson/actions-id-token-testing:ref:refs/heads/main | ||
// however, OpenFGA does not allow the "#" or ":" characters in the subject: | ||
// https://github.com/openfga/openfga/blob/main/pkg/tuple/tuple.go#L34 | ||
return &auth.Identity{ | ||
UserID: strings.ReplaceAll(id, ":", "+"), | ||
HumanName: strings.ReplaceAll(id, "+", ":"), | ||
Provider: gha, | ||
}, nil | ||
} | ||
|
||
// Validate implements auth.IdentityProvider. | ||
func (gha *GitHubActions) Validate(_ context.Context, token jwt.Token) (*auth.Identity, error) { | ||
expectedUrl := gha.URL() | ||
if token.Issuer() != expectedUrl.String() { | ||
return nil, errors.New("token issuer is not the expected issuer") | ||
} | ||
return &auth.Identity{ | ||
UserID: strings.ReplaceAll(token.Subject(), ":", "+"), | ||
HumanName: token.Subject(), | ||
Provider: gha, | ||
}, nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// SPDX-FileCopyrightText: Copyright 2025 The Minder Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// Package githubactions provides an implementation of the GitHub IdentityProvider. | ||
package githubactions | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/lestrrat-go/jwx/v2/jwt" | ||
|
||
"github.com/mindersec/minder/internal/auth" | ||
) | ||
|
||
func TestGitHubActions_Resolve(t *testing.T) { | ||
t.Parallel() | ||
tests := []struct { | ||
name string | ||
identity string | ||
want *auth.Identity | ||
}{{ | ||
name: "Resolve from storage", | ||
identity: "repo+evankanderson/actions-id-token-testing+ref+refs/heads/main", | ||
want: &auth.Identity{ | ||
HumanName: "repo:evankanderson/actions-id-token-testing:ref:refs/heads/main", | ||
UserID: "repo+evankanderson/actions-id-token-testing+ref+refs/heads/main", | ||
}, | ||
}, { | ||
name: "Resolve from human input", | ||
identity: "repo:evankanderson/actions-id-token-testing:ref:refs/heads/main", | ||
want: &auth.Identity{ | ||
HumanName: "repo:evankanderson/actions-id-token-testing:ref:refs/heads/main", | ||
UserID: "repo+evankanderson/actions-id-token-testing+ref+refs/heads/main", | ||
}, | ||
}} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
gha := &GitHubActions{} | ||
|
||
got, err := gha.Resolve(context.Background(), tt.identity) | ||
if err != nil { | ||
t.Errorf("GitHubActions.Resolve() error = %v", err) | ||
} | ||
|
||
tt.want.Provider = gha | ||
if tt.want.String() != got.String() { | ||
t.Errorf("GitHubActions.Resolve() = %v, want %v", got.String(), tt.want.String()) | ||
} | ||
if tt.want.Human() != got.Human() { | ||
t.Errorf("GitHubActions.Resolve() = %v, want %v", got.Human(), tt.want.Human()) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestGitHubActions_Validate(t *testing.T) { | ||
t.Parallel() | ||
tests := []struct { | ||
name string | ||
input func() jwt.Token | ||
want *auth.Identity | ||
wantErr bool | ||
}{{ | ||
name: "Validate token", | ||
input: func() jwt.Token { | ||
tok := jwt.New() | ||
_ = tok.Set("iss", "https://token.actions.githubusercontent.com") | ||
_ = tok.Set("sub", "repo:evankanderson/actions-id-token-testing:ref:refs/heads/main") | ||
return tok | ||
}, | ||
want: &auth.Identity{ | ||
HumanName: "repo:evankanderson/actions-id-token-testing:ref:refs/heads/main", | ||
UserID: "repo+evankanderson/actions-id-token-testing+ref+refs/heads/main", | ||
}, | ||
}, { | ||
name: "Validate token with invalid issuer", | ||
input: func() jwt.Token { | ||
tok := jwt.New() | ||
_ = tok.Set("iss", "https://issuer.minder.com/") | ||
_ = tok.Set("sub", "repo:evankanderson/actions-id-token-testing:ref:refs/heads/main") | ||
return tok | ||
}, | ||
want: nil, | ||
wantErr: true, | ||
}} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
gha := &GitHubActions{} | ||
got, err := gha.Validate(context.Background(), tt.input()) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("GitHubActions.Validate() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
|
||
if !tt.wantErr { | ||
tt.want.Provider = gha | ||
} | ||
if tt.want.String() != got.String() { | ||
t.Errorf("GitHubActions.Validate() = %v, want %v", got.String(), tt.want.String()) | ||
} | ||
if tt.want.Human() != got.Human() { | ||
t.Errorf("GitHubActions.Validate() = %v, want %v", got.Human(), tt.want.Human()) | ||
} | ||
}) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.