-
Notifications
You must be signed in to change notification settings - Fork 1
Add SSO connection routing for org-scoped login and pc target re-auth #86
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
austin-denoble
wants to merge
4
commits into
main
Choose a base branch
from
adenoble/sso-support
base: main
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
befaf31
Add SSO connection routing for org-scoped login and pc target re-auth
austin-denoble b539a5f
call login.FetchSSOConnection before oauth.Logout() for both login an…
austin-denoble b44267b
extract ResolveSSOConnection
austin-denoble 413f932
pass opts.OrgId and ops.SSOCOnnection to getAndSetAccessTokenJSON
austin-denoble 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
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,105 @@ | ||
| package login | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "io" | ||
| "net/http" | ||
|
|
||
| "github.com/pinecone-io/cli/internal/pkg/utils/configuration/config" | ||
| "github.com/pinecone-io/cli/internal/pkg/utils/environment" | ||
| "github.com/pinecone-io/cli/internal/pkg/utils/log" | ||
| "github.com/pinecone-io/cli/internal/pkg/utils/oauth" | ||
| ) | ||
|
|
||
| // dashboardOrg is the subset of the dashboard API org response needed for SSO lookup. | ||
| type dashboardOrg struct { | ||
| Id string `json:"id"` | ||
| SSOConnectionName string `json:"sso_connection_name"` | ||
| EnforceSSO bool `json:"enforce_sso_authentication"` | ||
| } | ||
|
|
||
| type dashboardOrgsResponse struct { | ||
| NewOrgs []dashboardOrg `json:"newOrgs"` | ||
| } | ||
|
|
||
| // ResolveSSOConnection is a convenience wrapper around FetchSSOConnection that | ||
| // returns a pointer to the connection name when SSO is enforced for the org, or | ||
| // nil otherwise. Errors are logged at debug level and treated as "no SSO". | ||
| func ResolveSSOConnection(ctx context.Context, orgId string) *string { | ||
| conn, err := FetchSSOConnection(ctx, orgId) | ||
| if err != nil { | ||
| log.Debug().Err(err).Str("orgId", orgId).Msg("SSO connection lookup failed, proceeding without connection param") | ||
| } | ||
| if conn == "" { | ||
| return nil | ||
| } | ||
| return &conn | ||
| } | ||
|
|
||
| // FetchSSOConnection calls the private dashboard API to retrieve the Auth0 | ||
| // connection name for the given orgId. It returns ("", nil) when the org has | ||
| // no SSO configured, enforce_sso_authentication is false, or any error occurs. | ||
| // Errors are non-fatal: the caller should proceed with a normal login URL. | ||
| func FetchSSOConnection(ctx context.Context, orgId string) (string, error) { | ||
| token, err := oauth.Token(ctx) | ||
| if err != nil || token == nil || token.AccessToken == "" { | ||
| log.Debug().Str("orgId", orgId).Msg("SSO lookup skipped: no valid token available") | ||
| return "", nil | ||
| } | ||
|
|
||
| envConfig, err := environment.GetEnvConfig(config.Environment.Get()) | ||
| if err != nil { | ||
| return "", nil | ||
| } | ||
|
|
||
| return fetchSSOConnectionFromURL(ctx, orgId, token.AccessToken, http.DefaultClient, envConfig.DashboardUrl) | ||
| } | ||
|
|
||
| // fetchSSOConnectionFromURL is the testable core: it takes an explicit HTTP | ||
| // client and dashboard base URL so tests can inject a local httptest.Server. | ||
| func fetchSSOConnectionFromURL(ctx context.Context, orgId string, accessToken string, client *http.Client, dashboardURL string) (string, error) { | ||
| url := dashboardURL + "/v2/dashboard/organizations" | ||
| req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) | ||
| if err != nil { | ||
| return "", nil | ||
| } | ||
| req.Header.Set("Authorization", "Bearer "+accessToken) | ||
|
|
||
| resp, err := client.Do(req) | ||
| if err != nil { | ||
| log.Debug().Err(err).Str("orgId", orgId).Msg("SSO lookup: dashboard API request failed") | ||
| return "", nil | ||
| } | ||
| defer resp.Body.Close() | ||
|
|
||
| if resp.StatusCode < 200 || resp.StatusCode >= 300 { | ||
| log.Debug().Int("status", resp.StatusCode).Str("orgId", orgId).Msg("SSO lookup: dashboard API returned non-2xx") | ||
| return "", nil | ||
| } | ||
|
|
||
| body, err := io.ReadAll(io.LimitReader(resp.Body, 1<<20)) | ||
| if err != nil { | ||
| log.Debug().Err(err).Str("orgId", orgId).Msg("SSO lookup: failed to read dashboard API response") | ||
| return "", nil | ||
| } | ||
|
|
||
| var orgsResp dashboardOrgsResponse | ||
| if err := json.Unmarshal(body, &orgsResp); err != nil { | ||
| log.Debug().Err(err).Str("orgId", orgId).Msg("SSO lookup: failed to decode dashboard API response") | ||
| return "", nil | ||
| } | ||
|
|
||
| for _, org := range orgsResp.NewOrgs { | ||
| if org.Id == orgId { | ||
| if org.EnforceSSO && org.SSOConnectionName != "" { | ||
| log.Debug().Str("orgId", orgId).Str("connection", org.SSOConnectionName).Msg("SSO lookup: found connection") | ||
| return org.SSOConnectionName, nil | ||
| } | ||
| return "", nil | ||
| } | ||
| } | ||
|
|
||
| log.Debug().Str("orgId", orgId).Msg("SSO lookup: org not found in dashboard response") | ||
| return "", nil | ||
| } |
Oops, something went wrong.
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.