-
Notifications
You must be signed in to change notification settings - Fork 193
/
keyvault.go
174 lines (149 loc) · 4.77 KB
/
keyvault.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
package provider
import (
"context"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/azcertificates"
"github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/azkeys"
"github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/azsecrets"
"github.com/Azure/go-autorest/autorest/date"
"github.com/Azure/secrets-store-csi-driver-provider-azure/pkg/provider/types"
)
type KeyVault interface {
GetSecret(ctx context.Context, name, version string) (*azsecrets.SecretBundle, error)
GetSecretVersions(ctx context.Context, name string) ([]types.KeyVaultObjectVersion, error)
GetKey(ctx context.Context, name, version string) (*azkeys.KeyBundle, error)
GetKeyVersions(ctx context.Context, name string) ([]types.KeyVaultObjectVersion, error)
GetCertificate(ctx context.Context, name, version string) (*azcertificates.CertificateBundle, error)
GetCertificateVersions(ctx context.Context, name string) ([]types.KeyVaultObjectVersion, error)
}
// TODO(aramase): add user agent
type client struct {
secrets *azsecrets.Client
keys *azkeys.Client
certs *azcertificates.Client
}
// NewClient creates a new KeyVault client
func NewClient(cred azcore.TokenCredential, vaultURI string) (KeyVault, error) {
secrets, err := azsecrets.NewClient(vaultURI, cred, nil)
if err != nil {
return nil, err
}
keys, err := azkeys.NewClient(vaultURI, cred, nil)
if err != nil {
return nil, err
}
certs, err := azcertificates.NewClient(vaultURI, cred, nil)
if err != nil {
return nil, err
}
return &client{
secrets: secrets,
keys: keys,
certs: certs,
}, nil
}
func (c *client) GetSecret(ctx context.Context, name, version string) (*azsecrets.SecretBundle, error) {
resp, err := c.secrets.GetSecret(ctx, name, version, &azsecrets.GetSecretOptions{})
if err != nil {
return nil, err
}
return &resp.SecretBundle, nil
}
func (c *client) GetKey(ctx context.Context, name, version string) (*azkeys.KeyBundle, error) {
resp, err := c.keys.GetKey(ctx, name, version, &azkeys.GetKeyOptions{})
if err != nil {
return nil, err
}
return &resp.KeyBundle, nil
}
func (c *client) GetCertificate(ctx context.Context, name, version string) (*azcertificates.CertificateBundle, error) {
resp, err := c.certs.GetCertificate(ctx, name, version, &azcertificates.GetCertificateOptions{})
if err != nil {
return nil, err
}
return &resp.CertificateBundle, nil
}
func (c *client) GetSecretVersions(ctx context.Context, name string) ([]types.KeyVaultObjectVersion, error) {
pager := c.secrets.NewListSecretVersionsPager(name, &azsecrets.ListSecretVersionsOptions{})
var versions []types.KeyVaultObjectVersion
for pager.More() {
page, err := pager.NextPage(ctx)
if err != nil {
return nil, err
}
for _, secret := range page.SecretListResult.Value {
if secret.Attributes == nil {
continue
}
if secret.Attributes.Enabled != nil && !*secret.Attributes.Enabled {
continue
}
id := *secret.ID
created := date.UnixEpoch()
if secret.Attributes.Created != nil {
created = *secret.Attributes.Created
}
versions = append(versions, types.KeyVaultObjectVersion{
Version: id.Version(),
Created: created,
})
}
}
return versions, nil
}
func (c *client) GetKeyVersions(ctx context.Context, name string) ([]types.KeyVaultObjectVersion, error) {
pager := c.keys.NewListKeyVersionsPager(name, &azkeys.ListKeyVersionsOptions{})
var versions []types.KeyVaultObjectVersion
for pager.More() {
page, err := pager.NextPage(ctx)
if err != nil {
return nil, err
}
for _, key := range page.KeyListResult.Value {
if key.Attributes == nil {
continue
}
if key.Attributes.Enabled != nil && !*key.Attributes.Enabled {
continue
}
id := *key.KID
created := date.UnixEpoch()
if key.Attributes.Created != nil {
created = *key.Attributes.Created
}
versions = append(versions, types.KeyVaultObjectVersion{
Version: id.Version(),
Created: created,
})
}
}
return versions, nil
}
func (c *client) GetCertificateVersions(ctx context.Context, name string) ([]types.KeyVaultObjectVersion, error) {
pager := c.certs.NewListCertificateVersionsPager(name, &azcertificates.ListCertificateVersionsOptions{})
var versions []types.KeyVaultObjectVersion
for pager.More() {
page, err := pager.NextPage(ctx)
if err != nil {
return nil, err
}
for _, cert := range page.CertificateListResult.Value {
if cert.Attributes == nil {
continue
}
if cert.Attributes.Enabled != nil && !*cert.Attributes.Enabled {
continue
}
id := *cert.ID
created := date.UnixEpoch()
if cert.Attributes.Created != nil {
created = *cert.Attributes.Created
}
versions = append(versions, types.KeyVaultObjectVersion{
Version: id.Version(),
Created: created,
})
}
}
return versions, nil
}