/
storage_disk.go
73 lines (52 loc) · 1.59 KB
/
storage_disk.go
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
// Arx
// Copyright (C) 2016 Keith Ball
// License: GPL3
package kms
import (
"errors"
"io/ioutil"
"os"
"path/filepath"
"strings"
"golang.org/x/net/context"
)
// DiskStorageProvider is an implementation of a simple disk storage
type DiskStorageProvider struct {
path string
}
// NewDiskStorageProvider ...
func NewDiskStorageProvider(path string) (DiskStorageProvider, error) {
log.Infof("Using DiskStorageProvider - Disk Path: %v", path)
return DiskStorageProvider{path: path}, nil
}
// SaveKey - Persist a key to disk
func (sp DiskStorageProvider) SaveKey(ctx context.Context, keyID string, data []byte, overwrite bool) error {
keyPath := filepath.Join(sp.path, keyID+".key")
log.Infof("SaveKey: %v", keyPath)
_, err := os.Stat(keyPath)
if err == nil && !overwrite {
return errors.New("Already exists")
}
return ioutil.WriteFile(keyPath, data, 0600)
}
// GetKey - Read a key from disk
func (sp DiskStorageProvider) GetKey(ctx context.Context, keyID string) ([]byte, error) {
keyPath := filepath.Join(sp.path, keyID+".key")
log.Infof("GetKey: %v", keyPath)
return ioutil.ReadFile(keyPath)
}
// ListCustomerKeyIDs - List available keys
func (sp DiskStorageProvider) ListCustomerKeyIDs(ctx context.Context) ([]string, error) {
// List the key ids
keyIDs := []string{}
files, _ := ioutil.ReadDir(sp.path)
for _, f := range files {
if !f.IsDir() && strings.HasSuffix(f.Name(), ".key") && !strings.HasPrefix(f.Name(), "kms") {
keyIDs = append(keyIDs, strings.TrimSuffix(f.Name(), ".key"))
}
}
return keyIDs, nil
}
// Close will do nothing
func (sp DiskStorageProvider) Close() {
}