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
17 changes: 9 additions & 8 deletions internal/sync/api/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,15 @@ func (client Client) SyncDirections(

// 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.
// that LaunchDarkly has no setting for is left out. Any number of resources
// can be read; they are asked for one page at a time.
func (client Client) ResolveOwnership(
projectKey string,
ids []syncdomain.ResourceID,
) (map[syncdomain.ResourceID]syncdomain.Ownership, error) {
if len(ids) == 0 {
return map[syncdomain.ResourceID]syncdomain.Ownership{}, nil
}
if len(ids) > 100 {
return nil, fmt.Errorf("read sync settings: at most 100 resources are allowed")
}
byKind := map[syncdomain.Kind][]syncdomain.ResourceID{}
for _, id := range ids {
if id.ProjectKey != projectKey {
Expand All @@ -66,12 +64,15 @@ func (client Client) ResolveOwnership(
// 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.
// The endpoint accepts at most one page of resources per request.
for _, kind := range slices.Sorted(maps.Keys(byKind)) {
ownership, err := client.ownershipOfKind(projectKey, byKind[kind])
if err != nil {
return nil, err
for batch := range slices.Chunk(byKind[kind], syncSettingsPageLimit) {
ownership, err := client.ownershipOfKind(projectKey, batch)
if err != nil {
return nil, err
}
maps.Copy(result, ownership)
}
maps.Copy(result, ownership)
}
return result, nil
}
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 @@ -155,3 +155,28 @@ func TestResolveOwnershipReportsTheMatchedRule(t *testing.T) {
empty: {Direction: syncdomain.SyncDirectionServerCanonical},
}, ownership)
}

// The endpoint accepts at most 100 resources per request, so a read of more
// asks for them 100 at a time and answers for all of them.
func TestResolveOwnershipOfMoreThan100ResourcesReadsThemInBatches(t *testing.T) {
ids := make([]syncdomain.ResourceID, 0, 101)
items := make([]string, 0, 101)
for index := range 101 {
key := fmt.Sprintf("support/v%03d", index)
ids = append(ids, syncdomain.VariationID("project", "support", fmt.Sprintf("v%03d", index)))
items = append(items, fmt.Sprintf(`{"resourceKind":"variation","resourceLookupKey":%q,"direction":"server_canonical"}`, key))
}
transport := &recordingClient{Responses: [][]byte{
[]byte(`{"items": [` + strings.Join(items[:100], ",") + `], "totalCount": 100}`),
[]byte(`{"items": [` + items[100] + `], "totalCount": 1}`),
}}
client := NewClient(transport, "token", "https://example.com")

ownership, err := client.ResolveOwnership("project", ids)

require.NoError(t, err)
assert.Len(t, ownership, 101)
require.Len(t, transport.Requests, 2)
assert.Len(t, transport.Requests[0].Query["resources"], 100)
assert.Equal(t, []string{"variation:support/v100"}, transport.Requests[1].Query["resources"])
}
10 changes: 9 additions & 1 deletion internal/sync/api/settings_write.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api
import (
"fmt"
"net/http"
"strings"

syncdomain "github.com/launchdarkly/ldcli/internal/sync"
)
Expand Down Expand Up @@ -32,7 +33,9 @@ func (client Client) ReturnToLaunchDarkly(projectKey string, ids []syncdomain.Re
}

// setSyncDirection upserts one exact rule per resource, in batches of at most
// syncSettingsWriteLimit rules. It checks every resource before it writes.
// syncSettingsWriteLimit rules. It checks every resource before it writes:
// each key becomes part of a rule pattern, so a key that is not a
// LaunchDarkly key, such as *, would write a wildcard rule.
func (client Client) setSyncDirection(
projectKey string, ids []syncdomain.ResourceID, direction syncdomain.SyncDirection,
) error {
Expand All @@ -41,6 +44,11 @@ func (client Client) setSyncDirection(
if id.ProjectKey != projectKey {
return fmt.Errorf("update sync settings: %s is not in project %q", id.Label(), projectKey)
}
for _, key := range append([]string{id.ProjectKey}, strings.Split(id.LookupKey, "/")...) {
if !syncdomain.IsLaunchDarklyKey(key) {
return fmt.Errorf("update sync settings: %s has key %q, which is not a LaunchDarkly key", id.Label(), key)
}
}
rules = append(rules, syncSettingRule{ResourceKind: id.Kind, ResourcePattern: id.LookupKey, Direction: direction})
}
for start := 0; start < len(rules); start += syncSettingsWriteLimit {
Expand Down
35 changes: 35 additions & 0 deletions internal/sync/api/settings_write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,38 @@ func TestSyncSettingsWritesSendCLIUserAgent(t *testing.T) {
assert.Equal(t, "/api/v2/projects/project/configs/sync/settings", path)
assert.Equal(t, "launchdarkly-cli/v"+version, userAgent)
}

// A key from a repo file reaches the rule pattern as is, so a pattern
// character such as * would write a wildcard rule. Every key must be a
// LaunchDarkly key, and one bad key sends nothing for the whole batch.
func TestSetSyncDirectionRejectsAKeyThatIsNotALaunchDarklyKey(t *testing.T) {
for name, bad := range map[string]struct {
projectKey string
id syncdomain.ResourceID
key string
}{
"config key": {"project", syncdomain.VariationID("project", "*", "default"), "*"},
"variation key": {"project", syncdomain.VariationID("project", "support", "de*"), "de*"},
"project key": {"proj*", syncdomain.VariationID("proj*", "support", "default"), "proj*"},
} {
for direction, write := range map[string]func(Client, string, []syncdomain.ResourceID) error{
"claim": Client.ClaimForCode,
"return": Client.ReturnToLaunchDarkly,
} {
t.Run(name+" "+direction, func(t *testing.T) {
transport := &recordingClient{}
client := NewClient(transport, "token", "https://example.com")
ids := []syncdomain.ResourceID{bad.id}
if bad.projectKey == "project" {
ids = append([]syncdomain.ResourceID{syncdomain.VariationID("project", "support", "concise")}, ids...)
}

err := write(client, bad.projectKey, ids)

require.Error(t, err)
assert.Contains(t, err.Error(), fmt.Sprintf("%q", bad.key))
assert.Empty(t, transport.Requests)
})
}
}
}
13 changes: 11 additions & 2 deletions internal/sync/bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,15 +456,24 @@ type ownershipDecision struct {
}

// decideProject reads the ownership of the new variations of one project
// and decides which ones to claim. It claims nothing.
// and decides which ones to claim. It claims nothing. A variation that the
// read answers without an entry for fails closed.
func decideProject(settings Settings, projectKey string, ids []syncdomain.ResourceID) (ownershipDecision, error) {
ownership, err := settings.ResolveOwnership(projectKey, ids)
if err != nil {
return ownershipDecision{}, err
}
var decision ownershipDecision
for _, id := range ids {
switch syncdomain.DecideOwnership(id, ownership[id]) {
owner, ok := ownership[id]
if !ok {
return ownershipDecision{}, syncdomain.NewProblem(
syncdomain.ErrorSyncSettingUnavailable,
"LaunchDarkly returned no sync setting",
"Configure the resource sync setting in LaunchDarkly, then add it again.",
).WithResource(id)
}
switch syncdomain.DecideOwnership(id, owner) {
case syncdomain.OwnershipClaim:
decision.claims = append(decision.claims, id)
case syncdomain.OwnershipManagedInLaunchDarkly:
Expand Down
26 changes: 24 additions & 2 deletions internal/sync/bootstrap/ownership_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@ import (
var _ Settings = syncapi.Client{}

// recordingSettings stands in for the LaunchDarkly sync settings API. It
// answers ResolveOwnership from ownership, fails the reads and claims of the
// projects in resolveErrs and claimErrs, records every claim, and records
// answers ResolveOwnership from ownership, leaves out the resources in
// omitted, fails the reads and claims of the projects in resolveErrs and
// claimErrs, records every claim, and records
// whether a claimed variation's file was already on disk when it was claimed.
type recordingSettings struct {
root string
ownership map[syncdomain.ResourceID]syncdomain.Ownership
resolveErrs map[string]error
claimErrs map[string]error
omitted []syncdomain.ResourceID

claims []claim
}
Expand All @@ -57,6 +59,9 @@ func (settings *recordingSettings) ResolveOwnership(
if id.ProjectKey != projectKey {
return nil, fmt.Errorf("%s is not in project %q", id.LookupKey, projectKey)
}
if slices.Contains(settings.omitted, id) {
continue
}
// A variation without a stored rule resolves to the project default
// with no matched pattern.
owner, ok := settings.ownership[id]
Expand Down Expand Up @@ -301,6 +306,23 @@ func TestRunWritesNothingWhenTheSettingsFail(t *testing.T) {
run.assertNothingWritten(t)
}

// A sync settings read that answers without an entry for a new variation
// fails closed: nothing is claimed or written.
func TestRunFailsClosedWhenTheSyncSettingIsMissing(t *testing.T) {
run, id, options := singleAdd(t)
run.settings.omitted = []syncdomain.ResourceID{id}
options.Yes = true

err := Run(options)

problem, ok := syncdomain.ProblemFrom(err)
require.True(t, ok, "want a sync problem, got %v", err)
assert.Equal(t, syncdomain.ErrorSyncSettingUnavailable, problem.Code)
assert.Equal(t, "LaunchDarkly returned no sync setting", problem.Message)
assert.Empty(t, run.settings.claims)
run.assertNothingWritten(t)
}

func TestRunWarnsAndContinuesWithYesWhenSettingsAreUnavailable(t *testing.T) {
run, id, options := singleAdd(t)
run.settings.resolveErrs["project"] = settingsNotFound()
Expand Down
17 changes: 13 additions & 4 deletions internal/sync/detach/detach.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,9 @@ func (options Options) interactive() bool {
// variation in the sync settings, with one write per project. LaunchDarkly
// keeps a detached tool or skill without a rule. It reads the sync settings
// of every project before it writes any rule, so a project without sync
// settings, or a read that fails, leaves no rule behind. When a project has
// no sync settings, it warns, and asks whether to detach anyway unless Yes is
// set. The bool result is false when the user declines.
// settings, a read that fails, or a read without an entry for a variation,
// leaves no rule behind. When a project has no sync settings, it warns, and
// asks whether to detach anyway unless Yes is set. The bool result is false when the user declines.
func returnToLaunchDarkly(options Options, selected []syncdomain.ResourceID) (bool, error) {
if options.Settings == nil {
return true, nil
Expand All @@ -275,7 +275,7 @@ func returnToLaunchDarkly(options Options, selected []syncdomain.ResourceID) (bo

var available, unavailable []string
for _, projectKey := range projectKeys {
_, err := options.Settings.ResolveOwnership(projectKey, byProject[projectKey])
ownership, err := options.Settings.ResolveOwnership(projectKey, byProject[projectKey])
switch {
case syncapi.IsNotFound(err):
unavailable = append(unavailable, projectKey)
Expand All @@ -285,6 +285,15 @@ func returnToLaunchDarkly(options Options, selected []syncdomain.ResourceID) (bo
case err != nil:
return false, fmt.Errorf("read the sync settings of project %s, so %s: %w", projectKey, detachConsequence(nil), err)
}
for _, id := range byProject[projectKey] {
if _, ok := ownership[id]; !ok {
return false, syncdomain.NewProblem(
syncdomain.ErrorSyncSettingUnavailable,
"LaunchDarkly returned no sync setting",
"Configure the resource sync setting in LaunchDarkly, then run detach again.",
).WithResource(id)
}
}
available = append(available, projectKey)
}
if len(unavailable) != 0 {
Expand Down
27 changes: 26 additions & 1 deletion internal/sync/detach/ownership_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"io"
"slices"
"strings"
"testing"
"testing/iotest"
Expand All @@ -19,7 +20,8 @@ import (
)

// recordingSettings stands in for the LaunchDarkly sync settings API. Every
// variation resolves to the project default with no matched rule. It fails
// variation resolves to the project default with no matched rule, except the
// ones in omitted, which the read leaves out. It fails
// the reads and writes of the projects in resolveErrs and returnErrs, and
// logs every read and write in order. When workspace is set, each write
// records whether every variation it names still has its local file and
Expand All @@ -28,6 +30,7 @@ type recordingSettings struct {
workspace *archiveWorkspace
resolveErrs map[string]error
returnErrs map[string]error
omitted []syncdomain.ResourceID
log []string
calls []settingsCall
}
Expand All @@ -54,6 +57,9 @@ func (settings *recordingSettings) ResolveOwnership(
if id.ProjectKey != projectKey {
return nil, fmt.Errorf("%s is not in project %q", id.Label(), projectKey)
}
if slices.Contains(settings.omitted, id) {
continue
}
result[id] = syncdomain.Ownership{Direction: syncdomain.SyncDirectionServerCanonical}
}
return result, nil
Expand Down Expand Up @@ -228,6 +234,25 @@ func TestRunDoesNotWriteSyncSettingsForToolsAndSkills(t *testing.T) {
assert.Empty(t, settings.calls)
}

// A sync settings read that answers without an entry for a selected
// variation fails closed: no return rule is written and nothing is detached.
func TestRunFailsClosedWhenTheSyncSettingIsMissing(t *testing.T) {
workspace, settings, options := singleDetach(t)
lockBefore := workspace.baselines.manifest.Clone()
settings.omitted = []syncdomain.ResourceID{syncdomain.VariationID("project", "config", "prompt")}
options.Yes = true

err := Run(options)

problem, ok := syncdomain.ProblemFrom(err)
require.True(t, ok, "want a sync problem, got %v", err)
assert.Equal(t, syncdomain.ErrorSyncSettingUnavailable, problem.Code)
assert.Equal(t, "LaunchDarkly returned no sync setting", problem.Message)
assert.Empty(t, settings.calls, "no return rule")
workspace.requireTracked(t, "prompt", true)
assert.Equal(t, lockBefore, workspace.baselines.manifest)
}

// A 403 describes returning ownership to LaunchDarkly, not recording code
// ownership, and names the missing permission. Nothing is detached.
func TestRunKeepsEverythingWhenTheSyncSettingsWriteIsForbidden(t *testing.T) {
Expand Down
11 changes: 10 additions & 1 deletion internal/sync/prompt/conflict_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,11 +504,20 @@ func (api *conflictAPI) MakeRequest(
method string,
path string,
_ string,
_ url.Values,
query url.Values,
body []byte,
_ bool,
) ([]byte, error) {
api.requests = append(api.requests, method)
if strings.HasSuffix(path, "/configs/sync/settings") && method == "GET" {
// Every resource resolves to the default setting, with no rule.
items := make([]map[string]any, 0, len(query["resources"]))
for _, resource := range query["resources"] {
kind, lookupKey, _ := strings.Cut(resource, ":")
items = append(items, map[string]any{"resourceKind": kind, "resourceLookupKey": lookupKey, "direction": "both"})
}
return json.Marshal(map[string]any{"items": items})
}
if strings.Contains(path, "/ai-tools/") {
if method == "PATCH" {
var tool syncdomain.Tool
Expand Down
Loading
Loading