-
Notifications
You must be signed in to change notification settings - Fork 1
fix(cloud): request required organization scopes #164
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
flemzord
wants to merge
1
commit into
main
Choose a base branch
from
fix/cloud-required-scopes
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
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,91 @@ | ||
| package stack | ||
|
|
||
| import ( | ||
| "sort" | ||
| "strings" | ||
|
|
||
| "golang.org/x/mod/semver" | ||
|
|
||
| "github.com/formancehq/fctl/internal/membershipclient/v3/models/components" | ||
| ) | ||
|
|
||
| func sortRegionVersionsByLatest(versions []components.Version) []components.Version { | ||
| sorted := append([]components.Version(nil), versions...) | ||
| sort.SliceStable(sorted, func(i, j int) bool { | ||
| return compareVersionNamesByLatest(sorted[i].GetName(), sorted[j].GetName()) | ||
| }) | ||
| return sorted | ||
| } | ||
|
|
||
| func sortVersionNamesByLatest(versions []string) []string { | ||
| sorted := append([]string(nil), versions...) | ||
| sort.SliceStable(sorted, func(i, j int) bool { | ||
| return compareVersionNamesByLatest(sorted[i], sorted[j]) | ||
| }) | ||
| return sorted | ||
| } | ||
|
|
||
| func compareVersionNamesByLatest(a, b string) bool { | ||
| normalizedA, validA := normalizeSemver(a) | ||
| normalizedB, validB := normalizeSemver(b) | ||
|
|
||
| if validA && validB { | ||
| return semver.Compare(normalizedA, normalizedB) > 0 | ||
| } | ||
| if validA != validB { | ||
| return validA | ||
| } | ||
| return a > b | ||
| } | ||
|
|
||
| func isVersionNewerThanCurrent(candidate, current string) bool { | ||
| normalizedCandidate, validCandidate := normalizeSemver(candidate) | ||
| normalizedCurrent, validCurrent := normalizeSemver(current) | ||
| if validCandidate && validCurrent { | ||
| return semver.Compare(normalizedCandidate, normalizedCurrent) > 0 | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| func normalizeSemver(version string) (string, bool) { | ||
| version = strings.TrimSpace(version) | ||
| if version == "" { | ||
| return "", false | ||
| } | ||
| if !strings.HasPrefix(version, "v") { | ||
| version = "v" + version | ||
| } | ||
| if semver.IsValid(version) { | ||
| return version, true | ||
| } | ||
|
|
||
| suffixStart := len(version) | ||
| for _, separator := range []string{"-", "+"} { | ||
| if index := strings.Index(version, separator); index >= 0 && index < suffixStart { | ||
| suffixStart = index | ||
| } | ||
| } | ||
|
|
||
| core := strings.TrimPrefix(version[:suffixStart], "v") | ||
| suffix := version[suffixStart:] | ||
| parts := strings.Split(core, ".") | ||
| if len(parts) > 3 { | ||
| return "", false | ||
| } | ||
| for len(parts) < 3 { | ||
| parts = append(parts, "0") | ||
| } | ||
| for _, part := range parts { | ||
| if part == "" { | ||
| return "", false | ||
| } | ||
| for _, r := range part { | ||
| if r < '0' || r > '9' { | ||
| return "", false | ||
| } | ||
| } | ||
| } | ||
|
|
||
| normalized := "v" + strings.Join(parts, ".") + suffix | ||
| return normalized, semver.IsValid(normalized) | ||
| } | ||
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,56 @@ | ||
| package stack | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/formancehq/fctl/internal/membershipclient/v3/models/components" | ||
| ) | ||
|
|
||
| func TestSortRegionVersionsByLatest(t *testing.T) { | ||
| versions := []components.Version{ | ||
| {Name: "v1.2.0"}, | ||
| {Name: "1.10.0"}, | ||
| {Name: "v2.0.0-rc.1"}, | ||
| {Name: "v2.0.0"}, | ||
| {Name: "v1.9.0"}, | ||
| } | ||
|
|
||
| sorted := sortRegionVersionsByLatest(versions) | ||
|
|
||
| require.Equal(t, []string{ | ||
| "v2.0.0", | ||
| "v2.0.0-rc.1", | ||
| "1.10.0", | ||
| "v1.9.0", | ||
| "v1.2.0", | ||
| }, []string{ | ||
| sorted[0].GetName(), | ||
| sorted[1].GetName(), | ||
| sorted[2].GetName(), | ||
| sorted[3].GetName(), | ||
| sorted[4].GetName(), | ||
| }) | ||
| require.Equal(t, "v1.2.0", versions[0].GetName()) | ||
| } | ||
|
|
||
| func TestSortVersionNamesByLatest(t *testing.T) { | ||
| require.Equal(t, | ||
| []string{"v1.11.0", "1.10.0", "v1.2.0"}, | ||
| sortVersionNamesByLatest([]string{"v1.2.0", "v1.11.0", "1.10.0"}), | ||
| ) | ||
| } | ||
|
|
||
| func TestSortVersionNamesByLatestWithShortVersions(t *testing.T) { | ||
| require.Equal(t, | ||
| []string{"v3.2-rc", "v3.1", "v3.0", "v2.2", "v2.1"}, | ||
| sortVersionNamesByLatest([]string{"v3.0", "v2.2", "v3.1", "v3.2-rc", "v2.1"}), | ||
| ) | ||
| } | ||
|
|
||
| func TestIsVersionNewerThanCurrent(t *testing.T) { | ||
| require.True(t, isVersionNewerThanCurrent("1.10.0", "v1.9.0")) | ||
| require.False(t, isVersionNewerThanCurrent("v1.9.0", "1.10.0")) | ||
| require.True(t, isVersionNewerThanCurrent("v3.2-rc", "v3.1")) | ||
| } |
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,40 @@ | ||
| package fctl | ||
|
|
||
| import ( | ||
| "reflect" | ||
| "testing" | ||
|
|
||
| "github.com/formancehq/go-libs/v4/oidc" | ||
| ) | ||
|
|
||
| func TestAccessTokenMissingScopes(t *testing.T) { | ||
| token := AccessToken{ | ||
| TokenWithClaims: TokenWithClaims[AccessTokenClaims]{ | ||
| Claims: AccessTokenClaims{ | ||
| Scopes: oidc.SpaceDelimitedArray{ | ||
| "organization:CreateStack", | ||
| "organization:ReadRegion", | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| missingScopes := token.MissingScopes( | ||
| "organization:CreateStack", | ||
| "organization:ListRegions", | ||
| "organization:ReadRegion", | ||
| ) | ||
|
|
||
| expected := []string{"organization:ListRegions"} | ||
| if !reflect.DeepEqual(missingScopes, expected) { | ||
| t.Fatalf("expected missing scopes %v, got %v", expected, missingScopes) | ||
| } | ||
|
|
||
| if token.HasScopes("organization:CreateStack", "organization:ListRegions") { | ||
| t.Fatal("expected HasScopes to return false for missing scope") | ||
| } | ||
|
|
||
| if !token.HasScopes("organization:CreateStack", "organization:ReadRegion") { | ||
| t.Fatal("expected HasScopes to return true when all scopes are present") | ||
| } | ||
| } |
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.
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.
Invalid candidate versions are always treated as newer.
When
candidateis not semver-parseable butcurrentis valid, the function returnstrue, incorrectly presenting invalid versions as upgrade candidates. This could lead to users selecting non-semver versions (e.g., typos, placeholder strings) as upgrade targets.Consider returning
falsewhen the candidate is invalid:🐛 Proposed fix
func isVersionNewerThanCurrent(candidate, current string) bool { normalizedCandidate, validCandidate := normalizeSemver(candidate) normalizedCurrent, validCurrent := normalizeSemver(current) if validCandidate && validCurrent { return semver.Compare(normalizedCandidate, normalizedCurrent) > 0 } - return true + // If candidate is not parseable, don't treat it as newer + // If current is not parseable, any valid candidate is acceptable + return validCandidate }📝 Committable suggestion
🤖 Prompt for AI Agents