/
secret_doc.go
67 lines (56 loc) · 1.58 KB
/
secret_doc.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
package secret
import (
"fmt"
"github.com/google/uuid"
"github.com/stephenzsy/small-kms/backend/base"
)
type SecretKeyVaultStore struct {
Name string `json:"name"`
ID string `json:"id"`
}
type SecretDoc struct {
base.BaseDoc
Policy base.DocLocator `json:"policy"`
Created base.NumericDate `json:"iat"`
NotBefore *base.NumericDate `json:"nbf,omitempty"`
NotAfter *base.NumericDate `json:"exp,omitempty"`
KeyVaultStore SecretKeyVaultStore `json:"keyVaultStore"`
}
const (
secretDocQueryColumnVersion = "c.version"
secretDocQueryColumnCreated = "c.created"
)
func (d *SecretDoc) PopulateModelRef(r *SecretRef) {
if d == nil || r == nil {
return
}
d.BaseDoc.PopulateModelRef(&r.ResourceReference)
r.Iat = d.Created
r.Exp = d.NotAfter
}
// PopulateModel implements base.ModelPopulater.
func (d *SecretDoc) PopulateModel(r *Secret) {
if d == nil || r == nil {
return
}
d.PopulateModelRef(&r.SecretRef)
r.ContentType = "text/plain"
r.Sid = string(d.KeyVaultStore.ID)
}
func GetKeyStoreName(nsKind base.NamespaceKind, nsID base.ID, policyID base.ID) string {
return fmt.Sprintf("s-%s-%s-%s", nsKind, nsID, policyID)
}
func (d *SecretDoc) init(
nsKind base.NamespaceKind,
nsID base.ID,
pDoc *SecretPolicyDoc) error {
secretUUID, err := uuid.NewRandom()
if err != nil {
return err
}
d.BaseDoc.Init(nsKind, nsID, base.ResourceKindSecret, base.IDFromUUID(secretUUID))
d.Policy = pDoc.GetStorageFullIdentifier()
d.KeyVaultStore.Name = GetKeyStoreName(nsKind, nsID, pDoc.ID)
return nil
}
var _ base.ModelPopulater[Secret] = (*SecretDoc)(nil)