-
Notifications
You must be signed in to change notification settings - Fork 24
OIDC datagatherer #758
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
Open
inteon
wants to merge
2
commits into
master
Choose a base branch
from
oidc_datagatherer
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
OIDC datagatherer #758
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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
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,16 @@ | ||
| # one-shot-oidc.yaml | ||
| # | ||
| # An example configuration file which can be used for local testing. | ||
| # For example: | ||
| # | ||
| # go run . agent \ | ||
| # --agent-config-file examples/one-shot-oidc.yaml \ | ||
| # --one-shot \ | ||
| # --output-path output.json | ||
| # | ||
| organization_id: "my-organization" | ||
| cluster_id: "my_cluster" | ||
| period: 1m | ||
| data-gatherers: | ||
| - kind: "oidc" | ||
| name: "ark/oidc" |
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
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,124 @@ | ||
| package oidc | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
|
|
||
| "k8s.io/client-go/rest" | ||
|
|
||
| "github.com/jetstack/preflight/pkg/datagatherer" | ||
| "github.com/jetstack/preflight/pkg/kubeconfig" | ||
| ) | ||
|
|
||
| // OIDCDiscovery contains the configuration for the k8s-discovery data-gatherer | ||
| type OIDCDiscovery struct { | ||
| // KubeConfigPath is the path to the kubeconfig file. If empty, will assume it runs in-cluster. | ||
| KubeConfigPath string `yaml:"kubeconfig"` | ||
| } | ||
|
|
||
| // UnmarshalYAML unmarshals the Config resolving GroupVersionResource. | ||
| func (c *OIDCDiscovery) UnmarshalYAML(unmarshal func(any) error) error { | ||
| aux := struct { | ||
| KubeConfigPath string `yaml:"kubeconfig"` | ||
| }{} | ||
| err := unmarshal(&aux) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| c.KubeConfigPath = aux.KubeConfigPath | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (c *OIDCDiscovery) NewDataGatherer(ctx context.Context) (datagatherer.DataGatherer, error) { | ||
| cl, err := kubeconfig.NewDiscoveryClient(c.KubeConfigPath) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return &DataGathererOIDC{ | ||
| cl: cl.RESTClient(), | ||
| }, nil | ||
| } | ||
|
|
||
| // DataGathererOIDC stores the config for a k8s-discovery datagatherer | ||
| type DataGathererOIDC struct { | ||
| cl rest.Interface | ||
| } | ||
|
|
||
| var _ datagatherer.DataGatherer = &DataGathererOIDC{} | ||
|
|
||
| func (g *DataGathererOIDC) Run(ctx context.Context) error { | ||
| return nil | ||
| } | ||
|
|
||
| func (g *DataGathererOIDC) WaitForCacheSync(ctx context.Context) error { | ||
| // no async functionality, see Fetch | ||
| return nil | ||
| } | ||
|
|
||
| // Fetch will fetch the OIDC discovery document and JWKS from the cluster API server. | ||
| func (g *DataGathererOIDC) Fetch() (any, int, error) { | ||
| ctx := context.Background() | ||
|
|
||
| oidcResponse, oidcErr := g.fetchOIDCConfig(ctx) | ||
| jwksResponse, jwksErr := g.fetchJWKS(ctx) | ||
|
|
||
| errToString := func(err error) string { | ||
| if err != nil { | ||
| return err.Error() | ||
| } | ||
| return "" | ||
| } | ||
|
|
||
| return OIDCDiscoveryData{ | ||
| OIDCConfig: oidcResponse, | ||
| OIDCConfigError: errToString(oidcErr), | ||
| JWKS: jwksResponse, | ||
| JWKSError: errToString(jwksErr), | ||
| }, 1 /* we have 1 result, so return 1 as count */, nil | ||
| } | ||
|
|
||
| type OIDCDiscoveryData struct { | ||
| OIDCConfig map[string]any `json:"openid_configuration,omitempty"` | ||
| OIDCConfigError string `json:"openid_configuration_error,omitempty"` | ||
| JWKS map[string]any `json:"jwks,omitempty"` | ||
| JWKSError string `json:"jwks_error,omitempty"` | ||
| } | ||
|
|
||
| func (g *DataGathererOIDC) fetchOIDCConfig(ctx context.Context) (map[string]any, error) { | ||
| // Fetch the OIDC discovery document from the well-known endpoint. | ||
| bytes, err := g.cl.Get().AbsPath("/.well-known/openid-configuration").Do(ctx).Raw() | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to get OIDC discovery document: %v", err) | ||
| } | ||
|
|
||
| var oidcResponse map[string]any | ||
| if err := json.Unmarshal(bytes, &oidcResponse); err != nil { | ||
| return nil, fmt.Errorf("failed to unmarshal OIDC discovery document: %v", err) | ||
| } | ||
|
|
||
| return oidcResponse, nil | ||
| } | ||
|
|
||
| func (g *DataGathererOIDC) fetchJWKS(ctx context.Context) (map[string]any, error) { | ||
| // Fetch the JWKS from the default /openid/v1/jwks endpoint. | ||
| // We are not using the jwks_uri from the OIDC config because: | ||
| // - on hybrid OpenShift clusters, we saw it pointed to a non-existent URL | ||
| // - on fully private AWS EKS clusters, the URL is still public and might not | ||
| // be reachable from within the cluster (https://github.com/aws/containers-roadmap/issues/2038) | ||
| // So we are using the default path instead, which we think should work in most cases. | ||
| bytes, err := g.cl.Get().AbsPath("/openid/v1/jwks").Do(ctx).Raw() | ||
inteon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to get JWKS from jwks_uri: %v", err) | ||
| } | ||
|
|
||
| var jwksResponse map[string]any | ||
| if err := json.Unmarshal(bytes, &jwksResponse); err != nil { | ||
| return nil, fmt.Errorf("failed to unmarshal JWKS response: %v", err) | ||
| } | ||
|
|
||
| return jwksResponse, 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,119 @@ | ||
| package oidc | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "net/url" | ||
| "testing" | ||
|
|
||
| "k8s.io/client-go/discovery" | ||
| "k8s.io/client-go/rest" | ||
| ) | ||
|
|
||
| func makeRESTClient(t *testing.T, ts *httptest.Server) rest.Interface { | ||
| t.Helper() | ||
| u, err := url.Parse(ts.URL) | ||
| if err != nil { | ||
| t.Fatalf("parse server url: %v", err) | ||
| } | ||
|
|
||
| cfg := &rest.Config{ | ||
| Host: u.Host, | ||
| } | ||
|
|
||
| discoveryClient, err := discovery.NewDiscoveryClientForConfigAndClient(cfg, ts.Client()) | ||
| if err != nil { | ||
| t.Fatalf("new discovery client: %v", err) | ||
| } | ||
|
|
||
| return discoveryClient.RESTClient() | ||
| } | ||
|
|
||
| func TestFetch_Success(t *testing.T) { | ||
| ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| switch r.URL.Path { | ||
| case "/.well-known/openid-configuration": | ||
| w.Header().Set("Content-Type", "application/json") | ||
| _, _ = w.Write([]byte(`{"issuer":"https://example"}`)) | ||
| case "/openid/v1/jwks": | ||
| w.Header().Set("Content-Type", "application/json") | ||
| _, _ = w.Write([]byte(`{"keys":[]}`)) | ||
| default: | ||
| http.NotFound(w, r) | ||
| } | ||
| })) | ||
| defer ts.Close() | ||
|
|
||
| rc := makeRESTClient(t, ts) | ||
| g := &DataGathererOIDC{cl: rc} | ||
|
|
||
| anyRes, count, err := g.Fetch() | ||
| if err != nil { | ||
| t.Fatalf("Fetch returned error: %v", err) | ||
| } | ||
| if count != 1 { | ||
| t.Fatalf("expected count 1, got %d", count) | ||
| } | ||
|
|
||
| res, ok := anyRes.(OIDCDiscoveryData) | ||
| if !ok { | ||
| t.Fatalf("unexpected result type: %T", anyRes) | ||
| } | ||
|
|
||
| if res.OIDCConfig == nil { | ||
| t.Fatalf("expected OIDCConfig, got nil") | ||
| } | ||
| if iss, _ := res.OIDCConfig["issuer"].(string); iss != "https://example" { | ||
| t.Fatalf("unexpected issuer: %v", res.OIDCConfig["issuer"]) | ||
| } | ||
|
|
||
| if res.JWKS == nil { | ||
| t.Fatalf("expected JWKS, got nil") | ||
| } | ||
| if _, ok := res.JWKS["keys"].([]any); !ok { | ||
| t.Fatalf("expected keys to be a slice, got %#v", res.JWKS["keys"]) | ||
| } | ||
| } | ||
|
|
||
| func TestFetch_Errors(t *testing.T) { | ||
| ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| switch r.URL.Path { | ||
| case "/.well-known/openid-configuration": | ||
| // return server error | ||
| http.Error(w, "boom", http.StatusInternalServerError) | ||
| case "/openid/v1/jwks": | ||
| // return invalid JSON | ||
| w.Header().Set("Content-Type", "application/json") | ||
| _, _ = w.Write([]byte(`}{`)) | ||
| default: | ||
| http.NotFound(w, r) | ||
| } | ||
| })) | ||
| defer ts.Close() | ||
|
|
||
| rc := makeRESTClient(t, ts) | ||
| g := &DataGathererOIDC{cl: rc} | ||
|
|
||
| anyRes, _, err := g.Fetch() | ||
| if err != nil { | ||
| t.Fatalf("Fetch returned error: %v", err) | ||
| } | ||
|
|
||
| res, ok := anyRes.(OIDCDiscoveryData) | ||
| if !ok { | ||
| t.Fatalf("unexpected result type: %T", anyRes) | ||
| } | ||
|
|
||
| if res.OIDCConfig != nil { | ||
| t.Fatalf("expected nil OIDCConfig on error, got %#v", res.OIDCConfig) | ||
| } | ||
| if res.OIDCConfigError != "failed to get OIDC discovery document: an error on the server (\"boom\") has prevented the request from succeeding" { | ||
| t.Fatalf("unexpected OIDCConfigError: %q", res.OIDCConfigError) | ||
| } | ||
| if res.JWKS != nil { | ||
| t.Fatalf("expected nil JWKS on malformed JSON, got %#v", res.JWKS) | ||
| } | ||
| if res.JWKSError != "failed to unmarshal JWKS response: invalid character '}' looking for beginning of value" { | ||
| t.Fatalf("unexpected JWKSError: %q", res.JWKSError) | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: probably worth having a comment here explaining why it does nothing!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Am I missing the change here? It still seems like there's no comment?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added
var _ datagatherer.DataGatherer = &DataGathererOIDC{}to make it clear that this struct is implementing an interface.