Debian Codegraph @

persist/persist.go

raw symbols SCIP
1 // Package persist implements delegation persistence. It is primarily
2 // concerned with configuration and serialisation; encryption and
3 // decryption is done by the cryptor package.
4 package persist
5
6 import (
7 "errors"
8
9 "github.com/cloudflare/redoctober/config"
10 "github.com/cloudflare/redoctober/keycache"
11 "github.com/cloudflare/redoctober/passvault"
12 )
13
14 var defaultStore Store = &File{}
15
16 // Labels are the labels that the keycache should be encrypted with.
17 var Labels = []string{"restore"}
18
19 const (
20 // Disabled indicates that the persistence store will never
21 // persist active delegations.
22 Disabled = "disabled"
23
24 // Inactive indicates that the persistence store requires
25 // more delegations to unlock, and isn't currently persisting
26 // the store.
27 Inactive = "inactive"
28
29 // Active indicates that the persistence store is
30 // actively persisting delegations.
31 Active = "active"
32 )
33
34 // Status contains information on the current status of a persistence
35 // store.
36 type Status struct {
37 State string `json:"state"`
38 Summary map[string]keycache.ActiveUser
39 }
40
41 // Store is a persistence store interface that handles delegations,
42 // serialising the persistence store, and writing the store to disk.
43 type Store interface {
44 Blob() []byte
45 Policy() string
46 Users() []string
47 Store([]byte) error
48 Load() error
49 Status() *Status
50 // Persist tells the Store to start actively persisting.
51 Persist()
52 Delegate(record passvault.PasswordRecord, name, password string, users, labels []string, uses int, slot, durationString string) error
53 // This is not the main keycache. This is the keycache for
54 // users that can decrypt the store.
55 Cache() *keycache.Cache
56 // Purge clears the persisted keys.
57 Purge() error
58 }
59
60 // FileMechanism indicates that the persistence mechanism is a file.
61 const FileMechanism = "file"
62
63 type mechanism func(*config.Delegations) (Store, error)
64
65 var stores = map[string]mechanism{
66 "": newNull,
67 FileMechanism: newFile,
68 }
69
70 // New attempts to create a new persistence store from the
71 // configuration.
72 func New(config *config.Delegations) (Store, error) {
73 if config == nil {
74 return nil, errors.New("persist: nil configuration")
75 }
76
77 if !config.Persist {
78 return newNull(config)
79 }
80
81 constructor, ok := stores[config.Mechanism]
82 if !ok {
83 return nil, errors.New("persist: invalid persistence mechanism")
84 }
85
86 return constructor(config)
87 }
88
89 // ErrInvalidConfig is returned when the configuration is invalid for
90 // the type of persistence store in use.
91 var ErrInvalidConfig = errors.New("persist: invalid configuration")