| 1 |
|
| 2 |
|
| 3 |
|
| 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 |
|
| 17 |
var Labels = []string{"restore"} |
| 18 |
|
| 19 |
const ( |
| 20 |
|
| 21 |
|
| 22 |
Disabled = "disabled" |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
Inactive = "inactive" |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
Active = "active" |
| 32 |
) |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
type Status struct { |
| 37 |
State string `json:"state"` |
| 38 |
Summary map[string]keycache.ActiveUser |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
type Store interface { |
| 44 |
Blob() []byte |
| 45 |
Policy() string |
| 46 |
Users() []string |
| 47 |
Store([]byte) error |
| 48 |
Load() error |
| 49 |
Status() *Status |
| 50 |
|
| 51 |
Persist() |
| 52 |
Delegate(record passvault.PasswordRecord, name, password string, users, labels []string, uses int, slot, durationString string) error |
| 53 |
|
| 54 |
|
| 55 |
Cache() *keycache.Cache |
| 56 |
|
| 57 |
Purge() error |
| 58 |
} |
| 59 |
|
| 60 |
|
| 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 |
|
| 71 |
|
| 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 |
|
| 90 |
|
| 91 |
var ErrInvalidConfig = errors.New("persist: invalid configuration") |