~james/core

core/spindle/secrets/manager.go -rw-r--r-- 1.5 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 16 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package secrets

import (
	"context"
	"errors"
	"regexp"
	"time"

	"github.com/bluesky-social/indigo/atproto/syntax"
)

type RepoIdentifier string

type Secret[T any] struct {
	Key       string
	Value     T
	Repo      RepoIdentifier
	CreatedAt time.Time
	CreatedBy syntax.DID
}

// the secret is not present
type LockedSecret = Secret[struct{}]

// the secret is present in plaintext, never expose this publicly,
// only use in the workflow engine
type UnlockedSecret = Secret[string]

type Manager interface {
	AddSecret(ctx context.Context, secret UnlockedSecret) error
	RemoveSecret(ctx context.Context, secret Secret[any]) error
	RemoveAllSecrets(ctx context.Context, repo RepoIdentifier) error
	GetSecretsLocked(ctx context.Context, repo RepoIdentifier) ([]LockedSecret, error)
	GetSecretsUnlocked(ctx context.Context, repo RepoIdentifier) ([]UnlockedSecret, error)
}

// stopper interface for managers that need cleanup
type Stopper interface {
	Stop()
}

var ErrKeyAlreadyPresent = errors.New("key already present")
var ErrInvalidKeyIdent = errors.New("key is not a valid identifier")
var ErrKeyNotFound = errors.New("key not found")

// ensure that we are satisfying the interface
var (
	_ = []Manager{
		&SqliteManager{},
		&OpenBaoManager{},
	}
)

var (
	// bash identifier syntax
	keyIdent = regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_]*$`)
)

func isValidKey(key string) bool {
	if key == "" {
		return false
	}
	return keyIdent.MatchString(key)
}

func ValidateKey(key string) error {
	if !isValidKey(key) {
		return ErrInvalidKeyIdent
	}
	return nil
}