-
Notifications
You must be signed in to change notification settings - Fork 3
fix(auth): Self-hosted instances dead-end with no usable credential #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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
06f2e00
refactor(auth): give the missing discovery document a sentinel
khvn26 e6f0a28
feat(api): read an instance's version from /version
khvn26 1ece7b1
fix(cmd): name the credential variable that is actually read
khvn26 7639566
fix(cmd): nudge to a Master API key on a pre-OAuth instance
khvn26 7d8a2da
fix(cmd): report an unscoped credential as ignored, not as no credential
khvn26 166ccb6
docs: document the host-scoped credential variables
khvn26 a4a53d8
fix(cmd): scope the SDK credential hints to the SDK surface
khvn26 bf7edc9
fix(auth): let the caller name the variable a bad key came from
khvn26 d65e144
fix(auth): treat only a 404 as an absent discovery document
khvn26 50d3952
fix(cmd): keep IPv6 instances out of unexportable variable names
khvn26 eeaee68
fix(cmd): name a usable variable before an instance is resolved
khvn26 ae5d42f
docs: state the host-scoping rule instead of one example of it
khvn26 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,42 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "net/http" | ||
| "strings" | ||
| ) | ||
|
|
||
| // ServerVersion reports the version of the Flagsmith serving apiURL, from the | ||
| // unauthenticated /version endpoint — it answers what an instance supports | ||
| // before there is any credential to ask with. The value is the deployed image | ||
| // tag, which self-hosted instances often set to something that is not a version | ||
| // at all ("latest", a commit sha), so callers must read an unparseable result as | ||
| // unknown rather than old. | ||
| func ServerVersion(ctx context.Context, httpClient *http.Client, apiURL string) (string, error) { | ||
| u := strings.TrimRight(apiURL, "/") + "/version/" | ||
| req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| resp, err := httpClient.Do(req) | ||
| if err != nil { | ||
| return "", fmt.Errorf("reaching %s: %w", u, err) | ||
| } | ||
| defer resp.Body.Close() | ||
| if resp.StatusCode != http.StatusOK { | ||
| return "", fmt.Errorf("%s returned %s", u, resp.Status) | ||
| } | ||
| var doc struct { | ||
| ImageTag string `json:"image_tag"` | ||
| } | ||
| if err := json.NewDecoder(resp.Body).Decode(&doc); err != nil { | ||
| return "", fmt.Errorf("decoding %s: %w", u, err) | ||
| } | ||
| if doc.ImageTag == "" { | ||
| return "", errors.New(u + " carries no image tag") | ||
| } | ||
| return doc.ImageTag, 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,79 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestServerVersion(t *testing.T) { | ||
| t.Run("reports the image tag", func(t *testing.T) { | ||
| // Given | ||
| srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| if r.URL.Path != "/version/" { | ||
| t.Errorf("path = %q", r.URL.Path) | ||
| } | ||
| fmt.Fprint(w, `{"ci_commit_sha":"67853f3","image_tag":"2.262.0","is_saas":true,"package_versions":{".":"2.262.0"}}`) | ||
| })) | ||
| defer srv.Close() | ||
|
|
||
| // When | ||
| got, err := ServerVersion(context.Background(), srv.Client(), srv.URL+"/") | ||
|
|
||
| // Then | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if got != "2.262.0" { | ||
| t.Errorf("ServerVersion = %q, want 2.262.0", got) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("errors when the endpoint is absent", func(t *testing.T) { | ||
| // Given | ||
| srv := httptest.NewServer(http.NotFoundHandler()) | ||
| defer srv.Close() | ||
|
|
||
| // When | ||
| _, err := ServerVersion(context.Background(), srv.Client(), srv.URL) | ||
|
|
||
| // Then | ||
| if err == nil { | ||
| t.Error("err = nil, want an error") | ||
| } | ||
| }) | ||
|
|
||
| t.Run("errors when the body is not the version document", func(t *testing.T) { | ||
| // Given | ||
| srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| fmt.Fprint(w, "<html>nope</html>") | ||
| })) | ||
| defer srv.Close() | ||
|
|
||
| // When | ||
| _, err := ServerVersion(context.Background(), srv.Client(), srv.URL) | ||
|
|
||
| // Then | ||
| if err == nil { | ||
| t.Error("err = nil, want an error") | ||
| } | ||
| }) | ||
|
|
||
| t.Run("errors when the tag is missing", func(t *testing.T) { | ||
| // Given | ||
| srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| fmt.Fprint(w, `{"ci_commit_sha":"67853f3"}`) | ||
| })) | ||
| defer srv.Close() | ||
|
|
||
| // When | ||
| _, err := ServerVersion(context.Background(), srv.Client(), srv.URL) | ||
|
|
||
| // Then | ||
| if err == nil { | ||
| t.Error("err = nil, want an error") | ||
| } | ||
| }) | ||
| } |
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
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.