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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
package persist
import (
"io/ioutil"
"os"
"github.com/cloudflare/redoctober/config"
"github.com/cloudflare/redoctober/keycache"
"github.com/cloudflare/redoctober/passvault"
)
// File implements a file-backed persistence store.
type File struct {
config *config.Delegations
cache *keycache.Cache
state string
blob []byte
}
// Valid ensures the configuration is valid for a file store. Note
// that it won't validate the policy, it will just ensure that one
// is present.
func (f *File) Valid() bool {
if f.config.Persist == false {
return false
}
if f.config.Policy == "" {
return false
}
if len(f.config.Users) == 0 {
return false
}
if f.config.Mechanism != FileMechanism {
return false
}
if f.config.Location == "" {
return false
}
return true
}
// newFile returns a new file-backed persistence store.
func newFile(config *config.Delegations) (Store, error) {
cache := keycache.NewCache()
file := &File{
config: config,
cache: &cache,
state: Inactive,
}
if !file.Valid() {
return nil, ErrInvalidConfig
}
err := file.Load()
if err != nil {
return nil, err
}
return file, nil
}
func (f *File) Blob() []byte {
return f.blob
}
func (f *File) Policy() string {
return f.config.Policy
}
func (f *File) Users() []string {
return f.config.Users
}
func (f *File) Store(blob []byte) error {
if f.state == Active {
f.blob = blob
return ioutil.WriteFile(f.config.Location, blob, 0644)
}
return nil
}
func (f *File) Load() error {
if fi, err := os.Stat(f.config.Location); err != nil {
// If the file doesn't exist, it can be persisted
// immediately.
if os.IsNotExist(err) {
f.state = Active
return nil
}
return err
} else if fi.Size() == 0 {
f.state = Active
return nil
}
in, err := ioutil.ReadFile(f.config.Location)
if err != nil {
return err
}
f.state = Inactive
f.blob = in
return nil
}
func (f *File) Persist() {
f.state = Active
}
func (f *File) Cache() *keycache.Cache {
return f.cache
}
func (f *File) Delegate(record passvault.PasswordRecord, name, password string, users, labels []string, uses int, slot, durationString string) error {
return f.cache.AddKeyFromRecord(record, name, password, users, labels, uses, slot, durationString)
}
func (f *File) Status() *Status {
return &Status{
State: f.state,
Summary: f.cache.GetSummary(),
}
}
func (f *File) Purge() error {
f.state = Active
f.blob = nil
if err := os.Remove(f.config.Location); err != nil {
return err
}
return nil
}
|