-
Notifications
You must be signed in to change notification settings - Fork 278
/
root_key.go
84 lines (74 loc) · 2.12 KB
/
root_key.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
package kms
import (
"context"
"github.com/hashicorp/boundary/internal/db"
"github.com/hashicorp/boundary/internal/errors"
"github.com/hashicorp/boundary/internal/kms/store"
"google.golang.org/protobuf/proto"
)
const (
DefaultRootKeyTableName = "kms_root_key"
)
type RootKey struct {
*store.RootKey
tableName string `gorm:"-"`
}
// NewRootKey creates a new in memory root key. ScopeId must be
// for a global or org scope, but the scope type validation will be deferred
// until the in memory root key is written to the database. No options
// are currently supported.
func NewRootKey(scopeId string, _ ...Option) (*RootKey, error) {
const op = "kms.NewRootKey"
if scopeId == "" {
return nil, errors.New(errors.InvalidParameter, op, "missing scope id")
}
c := &RootKey{
RootKey: &store.RootKey{
ScopeId: scopeId,
},
}
return c, nil
}
// AllocRootKey will allocate a root key
func AllocRootKey() RootKey {
return RootKey{
RootKey: &store.RootKey{},
}
}
// Clone creates a clone of the RootKey
func (k *RootKey) Clone() interface{} {
cp := proto.Clone(k.RootKey)
return &RootKey{
RootKey: cp.(*store.RootKey),
}
}
// VetForWrite implements db.VetForWrite() interface and validates the key
// before it's written.
func (k *RootKey) VetForWrite(_ context.Context, _ db.Reader, opType db.OpType, _ ...db.Option) error {
const op = "kms.(RootKey).VetForWrite"
if k.PrivateId == "" {
return errors.New(errors.InvalidParameter, op, "missing private id")
}
switch opType {
case db.CreateOp:
if k.ScopeId == "" {
return errors.New(errors.InvalidParameter, op, "missing scope id")
}
case db.UpdateOp:
return errors.New(errors.InvalidParameter, op, "key is immutable")
}
return nil
}
// TableName returns the tablename to override the default gorm table name
func (k *RootKey) TableName() string {
if k.tableName != "" {
return k.tableName
}
return DefaultRootKeyTableName
}
// SetTableName sets the tablename and satisfies the ReplayableMessage
// interface. If the caller attempts to set the name to "" the name will be
// reset to the default name.
func (c *RootKey) SetTableName(n string) {
c.tableName = n
}