Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions internal/sync/api/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,35 @@ func responseStatusCode(err error) (int, bool) {
return int(status), ok && status != 0
}

// responseError finds the JSON response body in an error chain. The resources
// responseError finds the JSON response body in an error tree. The resources
// client reports an API failure as an error whose text is the response body.
func responseError(err error) (map[string]any, bool) {
for current := err; current != nil; current = errors.Unwrap(current) {
var response map[string]any
if json.Unmarshal([]byte(current.Error()), &response) == nil && len(response) != 0 {
return response, true
if err == nil {
return nil, false
}
var response map[string]any
if json.Unmarshal([]byte(err.Error()), &response) == nil && len(response) != 0 {
return response, true
}
switch wrapped := err.(type) {
case interface{ Unwrap() error }:
return responseError(wrapped.Unwrap())
case interface{ Unwrap() []error }:
for _, inner := range wrapped.Unwrap() {
if response, ok := responseError(inner); ok {
return response, true
}
}
}
return nil, false
}

// ErrSyncSettingsForbidden marks a sync settings write the caller's role does
// not allow.
var ErrSyncSettingsForbidden = errors.New("sync settings write forbidden")

// IsForbidden reports whether a LaunchDarkly API error has a 403 status.
func IsForbidden(err error) bool {
status, ok := responseStatusCode(err)
return ok && status == http.StatusForbidden
}
41 changes: 31 additions & 10 deletions internal/sync/api/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ type resolvedSetting struct {
ResourceKind syncdomain.Kind `json:"resourceKind"`
ResourceLookupKey string `json:"resourceLookupKey"`
Direction syncdomain.SyncDirection `json:"direction"`
// MatchedResourcePattern is the stored rule pattern that produced the
// direction. It is absent or empty when no rule matched.
MatchedResourcePattern string `json:"matchedResourcePattern,omitempty"`
}

// SyncDirections returns the effective LaunchDarkly setting for each
Expand All @@ -28,8 +31,26 @@ func (client Client) SyncDirections(
projectKey string,
ids []syncdomain.ResourceID,
) (map[syncdomain.ResourceID]syncdomain.SyncDirection, error) {
ownership, err := client.ResolveOwnership(projectKey, ids)
if err != nil {
return nil, err
}
result := make(map[syncdomain.ResourceID]syncdomain.SyncDirection, len(ownership))
for id, owner := range ownership {
result[id] = owner.Direction
}
return result, nil
}

// ResolveOwnership returns the effective LaunchDarkly setting of each
// resource and the stored rule pattern that matched it. A resource of a kind
// that LaunchDarkly has no setting for is left out.
func (client Client) ResolveOwnership(
projectKey string,
ids []syncdomain.ResourceID,
) (map[syncdomain.ResourceID]syncdomain.Ownership, error) {
if len(ids) == 0 {
return map[syncdomain.ResourceID]syncdomain.SyncDirection{}, nil
return map[syncdomain.ResourceID]syncdomain.Ownership{}, nil
}
if len(ids) > 100 {
return nil, fmt.Errorf("read sync settings: at most 100 resources are allowed")
Expand All @@ -41,31 +62,31 @@ func (client Client) SyncDirections(
}
byKind[id.Kind] = append(byKind[id.Kind], id)
}
result := make(map[syncdomain.ResourceID]syncdomain.SyncDirection, len(ids))
result := make(map[syncdomain.ResourceID]syncdomain.Ownership, len(ids))
// The endpoint rejects a request that names a kind it has no setting for,
// and the rejection fails every resource of the request. Each kind travels
// alone, so a tool or a skill cannot take the variations down with it.
for _, kind := range slices.Sorted(maps.Keys(byKind)) {
directions, err := client.syncDirectionsOfKind(projectKey, byKind[kind])
ownership, err := client.ownershipOfKind(projectKey, byKind[kind])
if err != nil {
return nil, err
}
maps.Copy(result, directions)
maps.Copy(result, ownership)
}
return result, nil
}

// syncDirectionsOfKind reads the settings of resources of one kind. A kind
// that LaunchDarkly rejects as unsupported has no settings.
func (client Client) syncDirectionsOfKind(
// ownershipOfKind reads the settings of resources of one kind. A kind that
// LaunchDarkly rejects as unsupported has no settings.
func (client Client) ownershipOfKind(
projectKey string,
ids []syncdomain.ResourceID,
) (map[syncdomain.ResourceID]syncdomain.SyncDirection, error) {
) (map[syncdomain.ResourceID]syncdomain.Ownership, error) {
query := url.Values{}
for _, id := range ids {
query.Add("resources", string(id.Kind)+":"+id.LookupKey)
}
result := make(map[syncdomain.ResourceID]syncdomain.SyncDirection, len(ids))
result := make(map[syncdomain.ResourceID]syncdomain.Ownership, len(ids))
// The endpoint pages its items. Read every page, so that a batch of more
// than one page of resources does not look like missing settings.
for offset := 0; ; {
Expand All @@ -87,7 +108,7 @@ func (client Client) syncDirectionsOfKind(
"Configure a supported sync setting in LaunchDarkly.",
).WithResource(id)
}
result[id] = item.Direction
result[id] = syncdomain.Ownership{Direction: item.Direction, MatchedPattern: item.MatchedResourcePattern}
}
offset += len(page.Items)
// The total count ends the paging when the response has one. Without
Expand Down
25 changes: 25 additions & 0 deletions internal/sync/api/settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,28 @@ func TestSyncDirectionsPagesWithoutATotalCount(t *testing.T) {
require.Len(t, transport.Requests, 2, "a full page without a total count is followed by a request for the next page")
assert.Equal(t, []string{"100"}, transport.Requests[1].Query["offset"])
}

// ResolveOwnership keeps the stored rule pattern that matched each resource.
// A missing or empty pattern means that no rule is stored.
func TestResolveOwnershipReportsTheMatchedRule(t *testing.T) {
transport := &recordingClient{Responses: [][]byte{
[]byte(`{"items": [
{"resourceKind":"variation","resourceLookupKey":"support/wild","direction":"code_canonical","matchedResourcePattern":"support/*"},
{"resourceKind":"variation","resourceLookupKey":"support/absent","direction":"both"},
{"resourceKind":"variation","resourceLookupKey":"support/empty","direction":"server_canonical","matchedResourcePattern":""}
], "totalCount": 3}`),
}}
client := NewClient(transport, "token", "https://example.com")
wild := syncdomain.VariationID("project", "support", "wild")
absent := syncdomain.VariationID("project", "support", "absent")
empty := syncdomain.VariationID("project", "support", "empty")

ownership, err := client.ResolveOwnership("project", []syncdomain.ResourceID{wild, absent, empty})

require.NoError(t, err)
assert.Equal(t, map[syncdomain.ResourceID]syncdomain.Ownership{
wild: {Direction: syncdomain.SyncDirectionCodeCanonical, MatchedPattern: "support/*"},
absent: {Direction: syncdomain.SyncDirectionBoth},
empty: {Direction: syncdomain.SyncDirectionServerCanonical},
}, ownership)
}
64 changes: 64 additions & 0 deletions internal/sync/api/settings_write.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package api

import (
"fmt"
"net/http"

syncdomain "github.com/launchdarkly/ldcli/internal/sync"
)

// syncSettingsWriteLimit is the most rules one sync settings write accepts.
const syncSettingsWriteLimit = 100

type syncSettingsPatch struct {
Upserts []syncSettingRule `json:"upserts"`
Deletions []syncSettingRule `json:"deletions"`
}

type syncSettingRule struct {
ResourceKind syncdomain.Kind `json:"resourceKind"`
ResourcePattern string `json:"resourcePattern"`
Direction syncdomain.SyncDirection `json:"direction"`
}

// ClaimForCode makes code the owner of each resource.
func (client Client) ClaimForCode(projectKey string, ids []syncdomain.ResourceID) error {
return client.setSyncDirection(projectKey, ids, syncdomain.SyncDirectionCodeCanonical)
}

// ReturnToLaunchDarkly makes LaunchDarkly the owner of each resource.
func (client Client) ReturnToLaunchDarkly(projectKey string, ids []syncdomain.ResourceID) error {
return client.setSyncDirection(projectKey, ids, syncdomain.SyncDirectionServerCanonical)
}

// setSyncDirection upserts one exact rule per resource, in batches of at most
// syncSettingsWriteLimit rules. It checks every resource before it writes.
func (client Client) setSyncDirection(
projectKey string, ids []syncdomain.ResourceID, direction syncdomain.SyncDirection,
) error {
rules := make([]syncSettingRule, 0, len(ids))
for _, id := range ids {
if id.ProjectKey != projectKey {
return fmt.Errorf("update sync settings: %s is not in project %q", id.Label(), projectKey)
}
rules = append(rules, syncSettingRule{ResourceKind: id.Kind, ResourcePattern: id.LookupKey, Direction: direction})
}
for start := 0; start < len(rules); start += syncSettingsWriteLimit {
end := min(start+syncSettingsWriteLimit, len(rules))
_, err := client.mutate(mutation{
method: http.MethodPatch, action: "update", resource: "sync settings in project", key: projectKey,
body: syncSettingsPatch{Upserts: rules[start:end], Deletions: []syncSettingRule{}},
}, projectPath(projectKey, "configs", "sync", "settings")...)
if IsForbidden(err) {
return fmt.Errorf(
"%w: your role needs the updateSyncSettings action on proj/%s:ai-code-sync/*; "+
"ask a LaunchDarkly admin to grant it: %w",
ErrSyncSettingsForbidden, projectKey, err,
)
}
if err != nil {
return err
}
}
return nil
}
163 changes: 163 additions & 0 deletions internal/sync/api/settings_write_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
package api

import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/launchdarkly/ldcli/internal/resources"
syncdomain "github.com/launchdarkly/ldcli/internal/sync"
)

func TestClaimForCodeUpsertsCodeCanonicalRules(t *testing.T) {
transport := &recordingClient{}
client := NewClient(transport, "token", "https://example.com")

err := client.ClaimForCode("project", []syncdomain.ResourceID{
syncdomain.VariationID("project", "support", "default"),
syncdomain.VariationID("project", "support", "concise"),
})

require.NoError(t, err)
require.Len(t, transport.Requests, 1)
request := transport.Requests[0]
assert.Equal(t, "token", request.AccessToken)
assert.Equal(t, http.MethodPatch, request.Method)
assert.Equal(t, "https://example.com/api/v2/projects/project/configs/sync/settings", request.Path)
assert.Equal(t, "application/json", request.ContentType)
assert.Empty(t, request.Query)
assert.False(t, request.IsBeta)
assert.JSONEq(t, `{
"upserts": [
{"resourceKind": "variation", "resourcePattern": "support/default", "direction": "code_canonical"},
{"resourceKind": "variation", "resourcePattern": "support/concise", "direction": "code_canonical"}
],
"deletions": []
}`, string(request.Body))
}

func TestReturnToLaunchDarklyUpsertsServerCanonicalRules(t *testing.T) {
transport := &recordingClient{}
client := NewClient(transport, "token", "https://example.com")

err := client.ReturnToLaunchDarkly("project", []syncdomain.ResourceID{
syncdomain.VariationID("project", "support", "default"),
})

require.NoError(t, err)
require.Len(t, transport.Requests, 1)
request := transport.Requests[0]
assert.Equal(t, http.MethodPatch, request.Method)
assert.Equal(t, "https://example.com/api/v2/projects/project/configs/sync/settings", request.Path)
assert.JSONEq(t, `{
"upserts": [
{"resourceKind": "variation", "resourcePattern": "support/default", "direction": "server_canonical"}
],
"deletions": []
}`, string(request.Body))
}

func TestClaimForCodeSendsAtMostOneHundredRulesPerRequest(t *testing.T) {
transport := &recordingClient{}
client := NewClient(transport, "token", "https://example.com")
ids := make([]syncdomain.ResourceID, 0, 150)
for index := range 150 {
ids = append(ids, syncdomain.VariationID("project", "support", fmt.Sprintf("variation-%03d", index)))
}

require.NoError(t, client.ClaimForCode("project", ids))

require.Len(t, transport.Requests, 2)
var patterns []string
for index, size := range []int{100, 50} {
var body struct {
Upserts []struct {
ResourcePattern string `json:"resourcePattern"`
} `json:"upserts"`
}
require.NoError(t, json.Unmarshal(transport.Requests[index].Body, &body))
require.Len(t, body.Upserts, size)
for _, rule := range body.Upserts {
patterns = append(patterns, rule.ResourcePattern)
}
}
expected := make([]string, 0, len(ids))
for _, id := range ids {
expected = append(expected, id.LookupKey)
}
assert.Equal(t, expected, patterns, "every rule is sent once, in order")
}

// The rule pattern holds only the lookup key, so a resource of another
// project would get a rule in the wrong project.
func TestClaimForCodeRejectsResourceOfAnotherProject(t *testing.T) {
transport := &recordingClient{}
client := NewClient(transport, "token", "https://example.com")

err := client.ClaimForCode("project", []syncdomain.ResourceID{
syncdomain.VariationID("project", "support", "default"),
syncdomain.VariationID("other", "support", "default"),
})

require.Error(t, err)
assert.Contains(t, err.Error(), `"project"`)
assert.Empty(t, transport.Requests)
}

// A 403 names the missing permission. Any other failure must not say that a
// permission is missing.
func TestClaimForCodeExplainsForbidden403(t *testing.T) {
ids := []syncdomain.ResourceID{syncdomain.VariationID("project", "support", "default")}

t.Run("403", func(t *testing.T) {
transport := &recordingClient{Err: errors.New(`{"code":"forbidden","statusCode":403}`)}

err := NewClient(transport, "token", "https://example.com").ClaimForCode("project", ids)

require.Error(t, err)
assert.ErrorIs(t, err, ErrSyncSettingsForbidden)
assert.True(t, IsForbidden(err), "IsForbidden(%v)", err)
assert.Contains(t, err.Error(), "updateSyncSettings")
assert.Contains(t, err.Error(), "proj/project:ai-code-sync/*")
assert.Contains(t, strings.ToLower(err.Error()), "ask a launchdarkly admin")
})
t.Run("500", func(t *testing.T) {
transport := &recordingClient{Err: errors.New(`{"code":"internal_server_error","statusCode":500}`)}

err := NewClient(transport, "token", "https://example.com").ClaimForCode("project", ids)

require.Error(t, err)
assert.NotErrorIs(t, err, ErrSyncSettingsForbidden)
assert.False(t, IsForbidden(err))
assert.NotContains(t, err.Error(), "updateSyncSettings")
})
}

// The server identifies sync writes by the ldcli User-Agent, so the settings
// write must go through the shared resources client. The test server stands
// in for the LaunchDarkly API, which can't run locally.
func TestSyncSettingsWritesSendCLIUserAgent(t *testing.T) {
const version = "9.8.7-test"
var method, path, userAgent string
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
method, path, userAgent = r.Method, r.URL.Path, r.Header.Get("User-Agent")
w.WriteHeader(http.StatusNoContent)
}))
t.Cleanup(server.Close)
client := NewClient(resources.NewClient(version), "api-token", server.URL)

require.NoError(t, client.ClaimForCode("project", []syncdomain.ResourceID{
syncdomain.VariationID("project", "support", "default"),
}))

assert.Equal(t, http.MethodPatch, method)
assert.Equal(t, "/api/v2/projects/project/configs/sync/settings", path)
assert.Equal(t, "launchdarkly-cli/v"+version, userAgent)
}
Loading
Loading