237 lines
7.2 KiB
Go
237 lines
7.2 KiB
Go
package lifecycle
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/kandev/kandev/internal/common/logger"
|
|
"github.com/kandev/kandev/internal/secrets"
|
|
)
|
|
|
|
// inMemorySecretStore implements secrets.SecretStore for testing.
|
|
type inMemorySecretStore struct {
|
|
store map[string]*secrets.SecretWithValue
|
|
err error
|
|
}
|
|
|
|
var _ secrets.SecretStore = (*inMemorySecretStore)(nil)
|
|
var _ secrets.ScopedSecretStore = (*inMemorySecretStore)(nil)
|
|
|
|
func newInMemorySecretStore() *inMemorySecretStore {
|
|
return &inMemorySecretStore{store: make(map[string]*secrets.SecretWithValue)}
|
|
}
|
|
|
|
func (s *inMemorySecretStore) Create(_ context.Context, secret *secrets.SecretWithValue) error {
|
|
if s.err != nil {
|
|
return s.err
|
|
}
|
|
if secret.ID == "" {
|
|
secret.ID = fmt.Sprintf("secret-%d", len(s.store)+1)
|
|
}
|
|
s.store[secret.ID] = secret
|
|
return nil
|
|
}
|
|
|
|
func (s *inMemorySecretStore) Get(_ context.Context, id string) (*secrets.Secret, error) {
|
|
if sw, ok := s.store[id]; ok {
|
|
return &sw.Secret, nil
|
|
}
|
|
return nil, fmt.Errorf("not found")
|
|
}
|
|
|
|
func (s *inMemorySecretStore) Reveal(_ context.Context, id string) (string, error) {
|
|
if sw, ok := s.store[id]; ok {
|
|
return sw.Value, nil
|
|
}
|
|
return "", fmt.Errorf("not found")
|
|
}
|
|
|
|
func (s *inMemorySecretStore) Update(_ context.Context, _ string, _ *secrets.UpdateSecretRequest) error {
|
|
return nil
|
|
}
|
|
func (s *inMemorySecretStore) Delete(_ context.Context, _ string) error { return nil }
|
|
func (s *inMemorySecretStore) List(_ context.Context) ([]*secrets.SecretListItem, error) {
|
|
return nil, nil
|
|
}
|
|
func (s *inMemorySecretStore) Close() error { return nil }
|
|
|
|
func (s *inMemorySecretStore) ListScoped(_ context.Context, opts secrets.SecretListOptions) ([]*secrets.SecretListItem, error) {
|
|
items := make([]*secrets.SecretListItem, 0, len(s.store))
|
|
for _, stored := range s.store {
|
|
scope := stored.Scope
|
|
if scope == "" {
|
|
scope = secrets.ScopeGlobal
|
|
}
|
|
if opts.Scope == secrets.ScopeWorkspace && scope != secrets.ScopeWorkspace {
|
|
continue
|
|
}
|
|
if opts.Scope == secrets.ScopeGlobal && scope != secrets.ScopeGlobal {
|
|
continue
|
|
}
|
|
if scope == secrets.ScopeWorkspace && stored.WorkspaceID != opts.WorkspaceID {
|
|
continue
|
|
}
|
|
items = append(items, &secrets.SecretListItem{ID: stored.ID, Name: stored.Name, Scope: scope})
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
func (s *inMemorySecretStore) GetForWorkspace(_ context.Context, id, workspaceID string) (*secrets.Secret, error) {
|
|
secret, err := s.Get(context.Background(), id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if secret.Scope == secrets.ScopeWorkspace && secret.WorkspaceID != workspaceID {
|
|
return nil, fmt.Errorf("workspace secret unavailable")
|
|
}
|
|
return secret, nil
|
|
}
|
|
|
|
func (s *inMemorySecretStore) RevealGlobal(ctx context.Context, id string) (string, error) {
|
|
secret, err := s.Get(ctx, id)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if secret.Scope == secrets.ScopeWorkspace {
|
|
return "", fmt.Errorf("workspace secret unavailable")
|
|
}
|
|
return s.Reveal(ctx, id)
|
|
}
|
|
|
|
func (s *inMemorySecretStore) RevealForWorkspace(ctx context.Context, id, workspaceID string) (string, error) {
|
|
if _, err := s.GetForWorkspace(ctx, id, workspaceID); err != nil {
|
|
return "", err
|
|
}
|
|
return s.Reveal(ctx, id)
|
|
}
|
|
|
|
func (s *inMemorySecretStore) DeleteWorkspaceSecrets(_ context.Context, workspaceID string) error {
|
|
for id, stored := range s.store {
|
|
if stored.Scope == secrets.ScopeWorkspace && stored.WorkspaceID == workspaceID {
|
|
delete(s.store, id)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func TestPersistAuthToken(t *testing.T) {
|
|
log, _ := logger.NewLogger(logger.LoggingConfig{Level: "error", Format: "json"})
|
|
|
|
t.Run("stores token and sets metadata", func(t *testing.T) {
|
|
store := newInMemorySecretStore()
|
|
m := &Manager{logger: log, secretStore: store}
|
|
|
|
instance := &ExecutorInstance{
|
|
InstanceID: "exec-123456789012",
|
|
AuthToken: "handshake-token-abc",
|
|
}
|
|
execution := &AgentExecution{
|
|
Metadata: make(map[string]interface{}),
|
|
}
|
|
|
|
m.persistAuthToken(context.Background(), instance, execution)
|
|
|
|
// Verify secret was stored
|
|
if len(store.store) != 1 {
|
|
t.Fatalf("expected 1 secret, got %d", len(store.store))
|
|
}
|
|
|
|
// Verify metadata has the secret ID
|
|
secretID, ok := execution.Metadata[MetadataKeyAuthTokenSecret].(string)
|
|
if !ok || secretID == "" {
|
|
t.Fatalf("expected secret ID in metadata, got %v", execution.Metadata[MetadataKeyAuthTokenSecret])
|
|
}
|
|
|
|
// Verify we can retrieve the token
|
|
revealed, err := store.Reveal(context.Background(), secretID)
|
|
if err != nil {
|
|
t.Fatalf("failed to reveal: %v", err)
|
|
}
|
|
if revealed != "handshake-token-abc" {
|
|
t.Fatalf("expected handshake-token-abc, got %q", revealed)
|
|
}
|
|
})
|
|
|
|
t.Run("no-op when auth token is empty", func(t *testing.T) {
|
|
store := newInMemorySecretStore()
|
|
m := &Manager{logger: log, secretStore: store}
|
|
|
|
instance := &ExecutorInstance{InstanceID: "exec-123456789012"}
|
|
execution := &AgentExecution{Metadata: make(map[string]interface{})}
|
|
|
|
m.persistAuthToken(context.Background(), instance, execution)
|
|
|
|
if len(store.store) != 0 {
|
|
t.Fatal("expected no secrets stored")
|
|
}
|
|
if _, ok := execution.Metadata[MetadataKeyAuthTokenSecret]; ok {
|
|
t.Fatal("expected no metadata key")
|
|
}
|
|
})
|
|
|
|
t.Run("no-op when secret store is nil", func(t *testing.T) {
|
|
m := &Manager{logger: log}
|
|
|
|
instance := &ExecutorInstance{
|
|
InstanceID: "exec-123456789012",
|
|
AuthToken: "some-token",
|
|
}
|
|
execution := &AgentExecution{Metadata: make(map[string]interface{})}
|
|
|
|
// Should not panic
|
|
m.persistAuthToken(context.Background(), instance, execution)
|
|
})
|
|
|
|
t.Run("handles nil metadata in execution", func(t *testing.T) {
|
|
store := newInMemorySecretStore()
|
|
m := &Manager{logger: log, secretStore: store}
|
|
|
|
instance := &ExecutorInstance{
|
|
InstanceID: "exec-123456789012",
|
|
AuthToken: "token-xyz",
|
|
}
|
|
execution := &AgentExecution{}
|
|
|
|
m.persistAuthToken(context.Background(), instance, execution)
|
|
|
|
if execution.Metadata == nil {
|
|
t.Fatal("expected metadata to be initialized")
|
|
}
|
|
if _, ok := execution.Metadata[MetadataKeyAuthTokenSecret]; !ok {
|
|
t.Fatal("expected secret ID in metadata")
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestPersistRuntimeSecrets(t *testing.T) {
|
|
log, _ := logger.NewLogger(logger.LoggingConfig{Level: "error", Format: "json"})
|
|
store := newInMemorySecretStore()
|
|
m := &Manager{logger: log, secretStore: store}
|
|
|
|
instance := &ExecutorInstance{
|
|
InstanceID: "exec-123456789012",
|
|
AuthToken: "agentctl-token",
|
|
BootstrapNonce: "bootstrap-nonce",
|
|
}
|
|
execution := &AgentExecution{Metadata: make(map[string]interface{})}
|
|
|
|
m.persistRuntimeSecrets(context.Background(), instance, execution)
|
|
|
|
authSecretID, ok := execution.Metadata[MetadataKeyAuthTokenSecret].(string)
|
|
if !ok || authSecretID == "" {
|
|
t.Fatalf("expected auth token secret ID, got %v", execution.Metadata[MetadataKeyAuthTokenSecret])
|
|
}
|
|
nonceSecretID, ok := execution.Metadata[MetadataKeyBootstrapNonceSecret].(string)
|
|
if !ok || nonceSecretID == "" {
|
|
t.Fatalf("expected bootstrap nonce secret ID, got %v", execution.Metadata[MetadataKeyBootstrapNonceSecret])
|
|
}
|
|
|
|
if got := m.revealRuntimeSecret(context.Background(), execution.Metadata, MetadataKeyAuthTokenSecret); got != "agentctl-token" {
|
|
t.Fatalf("revealed auth token = %q, want agentctl-token", got)
|
|
}
|
|
if got := m.revealRuntimeSecret(context.Background(), execution.Metadata, MetadataKeyBootstrapNonceSecret); got != "bootstrap-nonce" {
|
|
t.Fatalf("revealed bootstrap nonce = %q, want bootstrap-nonce", got)
|
|
}
|
|
}
|