Skip to content
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

[v14] Fix backends with prefixes #47490

Merged
merged 1 commit into from
Oct 15, 2024
Merged
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
2 changes: 1 addition & 1 deletion lib/backend/dynamo/dynamodbbk.go
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,7 @@ const (
// prependPrefix adds leading 'teleport/' to the key for backwards compatibility
// with previous implementation of DynamoDB backend
func prependPrefix(key backend.Key) string {
return keyPrefix + string(key)
return keyPrefix + key.String()
}

// trimPrefix removes leading 'teleport' from the key
Expand Down
19 changes: 19 additions & 0 deletions lib/backend/dynamo/dynamodbbk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/gravitational/trace"
"github.com/jonboulle/clockwork"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/gravitational/teleport/lib/backend"
Expand Down Expand Up @@ -214,3 +215,21 @@ func TestCreateTable(t *testing.T) {
})
}
}

func TestKeyPrefix(t *testing.T) {
t.Run("leading separator in key", func(t *testing.T) {
prefixed := prependPrefix(backend.NewKey("test", "llama"))
assert.Equal(t, "teleport/test/llama", prefixed)

key := trimPrefix(prefixed)
assert.Equal(t, "/test/llama", key.String())
})

t.Run("no leading separator in key", func(t *testing.T) {
prefixed := prependPrefix(backend.Key(".locks/test/llama"))
assert.Equal(t, "teleport.locks/test/llama", prefixed)

key := trimPrefix(prefixed)
assert.Equal(t, ".locks/test/llama", key.String())
})
}
6 changes: 3 additions & 3 deletions lib/backend/etcdbk/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -1016,10 +1016,10 @@ func fromType(eventType mvccpb.Event_EventType) types.OpType {
}
}

func (b *EtcdBackend) trimPrefix(in backend.Key) backend.Key {
return in.TrimPrefix(backend.Key(b.cfg.Key))
func (b *EtcdBackend) trimPrefix(in []byte) backend.Key {
return backend.Key(in).TrimPrefix(backend.Key(b.cfg.Key))
}

func (b *EtcdBackend) prependPrefix(in backend.Key) string {
return b.cfg.Key + string(in)
return b.cfg.Key + in.String()
}
27 changes: 27 additions & 0 deletions lib/backend/etcdbk/etcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

"github.com/gravitational/trace"
"github.com/jonboulle/clockwork"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/gravitational/teleport/lib/backend"
Expand Down Expand Up @@ -255,3 +256,29 @@ func etcdTestEndpoint() string {
}
return "https://127.0.0.1:2379"
}

func TestKeyPrefix(t *testing.T) {
prefixes := []string{"teleport", "/teleport", "/teleport/"}

for _, prefix := range prefixes {
t.Run("prefix="+prefix, func(t *testing.T) {
bk := EtcdBackend{cfg: &Config{Key: prefix}}

t.Run("leading separator in key", func(t *testing.T) {
prefixed := bk.prependPrefix(backend.NewKey("test", "llama"))
assert.Equal(t, prefix+"/test/llama", prefixed)

key := bk.trimPrefix([]byte(prefixed))
assert.Equal(t, "/test/llama", key.String())
})

t.Run("no leading separator in key", func(t *testing.T) {
prefixed := bk.prependPrefix(backend.Key(".locks/test/llama"))
assert.Equal(t, prefix+".locks/test/llama", prefixed)

key := bk.trimPrefix([]byte(prefixed))
assert.Equal(t, ".locks/test/llama", key.String())
})
})
}
}
File renamed without changes.
15 changes: 15 additions & 0 deletions lib/backend/helpers_test.go → lib/backend/lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,24 @@ import (
"time"

"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestLockKey(t *testing.T) {
t.Run("empty parts", func(t *testing.T) {
key := lockKey()
assert.Equal(t, ".locks", key.String())
assert.Equal(t, [][]byte{[]byte(".locks")}, key.Components())
})

t.Run("with parts", func(t *testing.T) {
key := lockKey("test", "llama")
assert.Equal(t, ".locks/test/llama", key.String())
assert.Equal(t, [][]byte{[]byte(".locks"), []byte("test"), []byte("llama")}, key.Components())
})
}

func TestLockConfiguration_CheckAndSetDefaults(t *testing.T) {
type mockBackend struct {
Backend
Expand Down
Loading