-
Notifications
You must be signed in to change notification settings - Fork 475
/
disk.go
179 lines (146 loc) · 4.31 KB
/
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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package disk
import (
"context"
"crypto/x509"
"encoding/json"
"os"
"path/filepath"
"sync"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/hcl"
keymanagerv1 "github.com/spiffe/spire-plugin-sdk/proto/spire/plugin/agent/keymanager/v1"
configv1 "github.com/spiffe/spire-plugin-sdk/proto/spire/service/common/config/v1"
keymanagerbase "github.com/spiffe/spire/pkg/agent/plugin/keymanager/base"
catalog "github.com/spiffe/spire/pkg/common/catalog"
"github.com/spiffe/spire/pkg/common/diskutil"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type Generator = keymanagerbase.Generator
func BuiltIn() catalog.BuiltIn {
return asBuiltIn(newKeyManager(nil))
}
func TestBuiltIn(generator Generator) catalog.BuiltIn {
return asBuiltIn(newKeyManager(generator))
}
func asBuiltIn(p *KeyManager) catalog.BuiltIn {
return catalog.MakeBuiltIn("disk",
keymanagerv1.KeyManagerPluginServer(p),
configv1.ConfigServiceServer(p))
}
type configuration struct {
Directory string `hcl:"directory"`
}
type KeyManager struct {
*keymanagerbase.Base
configv1.UnimplementedConfigServer
log hclog.Logger
mu sync.Mutex
config *configuration
}
func newKeyManager(generator Generator) *KeyManager {
m := &KeyManager{}
m.Base = keymanagerbase.New(keymanagerbase.Config{
Generator: generator,
WriteEntries: m.writeEntries,
})
return m
}
func (m *KeyManager) SetLogger(log hclog.Logger) {
m.log = log
}
func (m *KeyManager) Configure(_ context.Context, req *configv1.ConfigureRequest) (*configv1.ConfigureResponse, error) {
config := new(configuration)
if err := hcl.Decode(config, req.HclConfiguration); err != nil {
return nil, status.Errorf(codes.InvalidArgument, "unable to decode configuration: %v", err)
}
if config.Directory == "" {
return nil, status.Error(codes.InvalidArgument, "directory must be configured")
}
m.mu.Lock()
defer m.mu.Unlock()
if err := m.configure(config); err != nil {
return nil, err
}
return &configv1.ConfigureResponse{}, nil
}
func (m *KeyManager) configure(config *configuration) error {
// Only load entry information on first configure
if m.config == nil {
if err := m.loadEntries(config.Directory); err != nil {
return err
}
}
m.config = config
return nil
}
func (m *KeyManager) loadEntries(dir string) error {
// Load the entries from the keys file.
entries, err := loadEntries(keysPath(dir))
if err != nil {
return err
}
m.Base.SetEntries(entries)
return nil
}
func (m *KeyManager) writeEntries(_ context.Context, allEntries []*keymanagerbase.KeyEntry, _ *keymanagerbase.KeyEntry) error {
m.mu.Lock()
config := m.config
m.mu.Unlock()
if config == nil {
return status.Error(codes.FailedPrecondition, "not configured")
}
return writeEntries(keysPath(config.Directory), allEntries)
}
type entriesData struct {
Keys map[string][]byte `json:"keys"`
}
func loadEntries(path string) ([]*keymanagerbase.KeyEntry, error) {
jsonBytes, err := os.ReadFile(path)
if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, err
}
data := new(entriesData)
if err := json.Unmarshal(jsonBytes, data); err != nil {
return nil, status.Errorf(codes.Internal, "unable to decode keys JSON: %v", err)
}
var entries []*keymanagerbase.KeyEntry
for id, keyBytes := range data.Keys {
key, err := x509.ParsePKCS8PrivateKey(keyBytes)
if err != nil {
return nil, status.Errorf(codes.Internal, "unable to parse key %q: %v", id, err)
}
entry, err := keymanagerbase.MakeKeyEntryFromKey(id, key)
if err != nil {
return nil, status.Errorf(codes.Internal, "unable to make entry %q: %v", id, err)
}
entries = append(entries, entry)
}
return entries, nil
}
func writeEntries(path string, entries []*keymanagerbase.KeyEntry) error {
data := &entriesData{
Keys: make(map[string][]byte),
}
for _, entry := range entries {
keyBytes, err := x509.MarshalPKCS8PrivateKey(entry.PrivateKey)
if err != nil {
return err
}
data.Keys[entry.Id] = keyBytes
}
jsonBytes, err := json.MarshalIndent(data, "", "\t")
if err != nil {
return status.Errorf(codes.Internal, "unable to marshal entries: %v", err)
}
if err := diskutil.AtomicWritePrivateFile(path, jsonBytes); err != nil {
return status.Errorf(codes.Internal, "unable to write entries: %v", err)
}
return nil
}
func keysPath(dir string) string {
return filepath.Join(dir, "keys.json")
}