-
Notifications
You must be signed in to change notification settings - Fork 51
Add SSS client config #5082
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
+248
−0
Merged
Add SSS client config #5082
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| // Copyright The Linux Foundation and each contributor to CommunityBridge. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package sss | ||
|
|
||
| import ( | ||
| "net/url" | ||
| "strings" | ||
| ) | ||
|
|
||
| // NewClientFromPlatformCredentials builds an SSS client that reuses the shared | ||
| // LFX platform M2M (Auth0) credentials already configured for EasyCLA. | ||
| // | ||
| // oauthTokenURL is the full Auth0 token endpoint used for platform auth | ||
| // (e.g. https://<tenant>/oauth/token); its scheme+host is reused as the SSS | ||
| // client's Auth0 domain, since SSS authenticates with the same client against | ||
| // the same Auth0 tenant - only the requested audience differs. | ||
| // | ||
| // It returns (nil, nil) when baseURL or audience is empty so callers can treat | ||
| // an unconfigured SSS as a disabled, no-op feature rather than an error. | ||
| func NewClientFromPlatformCredentials(baseURL, audience, oauthTokenURL, clientID, clientSecret string) (*Client, error) { | ||
| baseURL = strings.TrimSpace(baseURL) | ||
| audience = strings.TrimSpace(audience) | ||
| if baseURL == "" || audience == "" { | ||
| return nil, nil | ||
| } | ||
|
|
||
| return NewClient(SSSConfig{ | ||
| BaseURL: baseURL, | ||
| Auth0Domain: auth0DomainFromTokenURL(oauthTokenURL), | ||
| Auth0ClientID: strings.TrimSpace(clientID), | ||
| Auth0ClientSecret: strings.TrimSpace(clientSecret), | ||
| Auth0Audience: audience, | ||
| }) | ||
|
lukaszgryglicki marked this conversation as resolved.
|
||
| } | ||
|
|
||
| // auth0DomainFromTokenURL reduces a full Auth0 token endpoint to its scheme+host | ||
| // (e.g. https://tenant.auth0.com), which is what the SSS client expects as its | ||
| // Auth0 domain. It tolerates a missing scheme: url.Parse on a scheme-less value | ||
| // puts the whole string in Path and leaves Host empty, so a value like | ||
| // "tenant.auth0.com/oauth/token" would otherwise be passed through verbatim and | ||
| // produce a doubled "/oauth/token" when the client builds the token URL. | ||
| func auth0DomainFromTokenURL(oauthTokenURL string) string { | ||
| oauthTokenURL = strings.TrimSpace(oauthTokenURL) | ||
| if oauthTokenURL == "" { | ||
| return "" | ||
| } | ||
|
|
||
| parseTarget := oauthTokenURL | ||
| if !strings.Contains(parseTarget, "://") { | ||
| parseTarget = "https://" + parseTarget | ||
| } | ||
| if u, err := url.Parse(parseTarget); err == nil && u.Host != "" { | ||
| return u.Scheme + "://" + u.Host | ||
| } | ||
| return oauthTokenURL | ||
| } | ||
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,128 @@ | ||
| // Copyright The Linux Foundation and each contributor to CommunityBridge. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package sss | ||
|
|
||
| import "testing" | ||
|
|
||
| const ( | ||
| testAuth0Domain = "https://linuxfoundation.auth0.com" | ||
| testAuth0TokenURL = testAuth0Domain + "/oauth/token" | ||
| ) | ||
|
|
||
| func TestNewClientFromPlatformCredentials_DisabledWhenBaseURLMissing(t *testing.T) { | ||
| client, err := NewClientFromPlatformCredentials("", "https://sss.example/", testAuth0TokenURL, "id", "secret") | ||
| if err != nil { | ||
| t.Fatalf("expected no error when disabled, got: %v", err) | ||
| } | ||
| if client != nil { | ||
| t.Fatalf("expected nil client when base URL is empty") | ||
| } | ||
| } | ||
|
|
||
| func TestNewClientFromPlatformCredentials_DisabledWhenAudienceMissing(t *testing.T) { | ||
| client, err := NewClientFromPlatformCredentials("https://sss.example", "", testAuth0TokenURL, "id", "secret") | ||
| if err != nil { | ||
| t.Fatalf("expected no error when disabled, got: %v", err) | ||
| } | ||
| if client != nil { | ||
| t.Fatalf("expected nil client when audience is empty") | ||
| } | ||
| } | ||
|
|
||
| func TestNewClientFromPlatformCredentials_DerivesAuth0DomainFromTokenURL(t *testing.T) { | ||
| client, err := NewClientFromPlatformCredentials( | ||
| "https://sanctions-screening.dev.v2.cluster.linuxfound.info", | ||
| "https://sanctions-screening.dev.v2.cluster.linuxfound.info/", | ||
| testAuth0TokenURL, | ||
| "client-id", | ||
| "client-secret", | ||
| ) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| if client == nil { | ||
| t.Fatal("expected a configured client") | ||
| } | ||
|
|
||
| if got, want := client.cfg.Auth0Domain, testAuth0Domain; got != want { | ||
| t.Errorf("Auth0Domain: got %q, want %q", got, want) | ||
| } | ||
| // The derived domain must yield the original token endpoint, not a doubled path. | ||
| if got, want := client.authTokenURL(), testAuth0TokenURL; got != want { | ||
| t.Errorf("authTokenURL: got %q, want %q", got, want) | ||
| } | ||
| } | ||
|
|
||
| func TestNewClientFromPlatformCredentials_DerivesAuth0DomainFromSchemelessTokenURL(t *testing.T) { | ||
| cases := []struct { | ||
| name string | ||
| tokenURL string | ||
| }{ | ||
| {"scheme-less with path", "linuxfoundation.auth0.com/oauth/token"}, | ||
| {"scheme-less host only", "linuxfoundation.auth0.com"}, | ||
| } | ||
| for _, tc := range cases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| client, err := NewClientFromPlatformCredentials( | ||
| "https://sanctions-screening.dev.v2.cluster.linuxfound.info", | ||
| "https://sanctions-screening.dev.v2.cluster.linuxfound.info/", | ||
| tc.tokenURL, | ||
| "client-id", | ||
| "client-secret", | ||
| ) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| if client == nil { | ||
| t.Fatal("expected a configured client") | ||
| } | ||
| if got, want := client.cfg.Auth0Domain, testAuth0Domain; got != want { | ||
| t.Errorf("Auth0Domain: got %q, want %q", got, want) | ||
| } | ||
| // A scheme-less token URL must not double the "/oauth/token" path. | ||
| if got, want := client.authTokenURL(), testAuth0TokenURL; got != want { | ||
| t.Errorf("authTokenURL: got %q, want %q", got, want) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestNewClientFromPlatformCredentials_MissingCredentialsErrors(t *testing.T) { | ||
| // base URL + audience present but no Auth0 client credentials -> NewClient validates and errors. | ||
| client, err := NewClientFromPlatformCredentials("https://sss.example", "https://sss.example/", testAuth0TokenURL, "", "") | ||
| if err == nil { | ||
| t.Fatal("expected an error when client credentials are missing") | ||
| } | ||
| if client != nil { | ||
| t.Fatal("expected nil client on error") | ||
| } | ||
| } | ||
|
lukaszgryglicki marked this conversation as resolved.
|
||
|
|
||
| func TestNewClientFromPlatformCredentials_TrimsValues(t *testing.T) { | ||
| client, err := NewClientFromPlatformCredentials( | ||
| " https://sanctions-screening.dev.v2.cluster.linuxfound.info ", | ||
| " https://sanctions-screening.dev.v2.cluster.linuxfound.info/ ", | ||
| " https://linuxfoundation.auth0.com/oauth/token ", | ||
| " client-id ", | ||
| " client-secret ", | ||
| ) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| if client == nil { | ||
| t.Fatal("expected a configured client") | ||
| } | ||
| if got, want := client.cfg.BaseURL, "https://sanctions-screening.dev.v2.cluster.linuxfound.info"; got != want { | ||
| t.Errorf("BaseURL not trimmed: got %q, want %q", got, want) | ||
| } | ||
| if got, want := client.cfg.Auth0Audience, "https://sanctions-screening.dev.v2.cluster.linuxfound.info/"; got != want { | ||
| t.Errorf("Auth0Audience not trimmed: got %q, want %q", got, want) | ||
| } | ||
| if got, want := client.cfg.Auth0ClientID, "client-id"; got != want { | ||
| t.Errorf("Auth0ClientID not trimmed: got %q, want %q", got, want) | ||
| } | ||
| if got, want := client.cfg.Auth0ClientSecret, "client-secret"; got != want { | ||
| t.Errorf("Auth0ClientSecret not trimmed: got %q, want %q", got, want) | ||
| } | ||
| } | ||
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.