-
Notifications
You must be signed in to change notification settings - Fork 187
/
keyops.go
221 lines (183 loc) · 6.19 KB
/
keyops.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
package keyvaults
import (
"context"
"fmt"
"net/http"
kvops "github.com/Azure/azure-sdk-for-go/services/keyvault/v7.0/keyvault"
"github.com/Azure/azure-service-operator/api/v1alpha1"
azurev1alpha1 "github.com/Azure/azure-service-operator/api/v1alpha1"
"github.com/Azure/azure-service-operator/pkg/errhelp"
"github.com/Azure/azure-service-operator/pkg/helpers"
"github.com/Azure/azure-service-operator/pkg/resourcemanager"
"github.com/Azure/azure-service-operator/pkg/resourcemanager/config"
"github.com/Azure/go-autorest/autorest/to"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
)
// KeyvaultKeyClient emcompasses the methods needed for the keyops client to fulfill the ARMClient interface
type KeyvaultKeyClient struct {
Creds config.Credentials
KeyvaultClient *AzureKeyVaultManager
}
func NewKeyvaultKeyClient(creds config.Credentials, client *AzureKeyVaultManager) *KeyvaultKeyClient {
return &KeyvaultKeyClient{
Creds: creds,
KeyvaultClient: client,
}
}
// Ensure idempotently implements the user's requested state
func (k *KeyvaultKeyClient) Ensure(ctx context.Context, obj runtime.Object, opts ...resourcemanager.ConfigOption) (bool, error) {
instance, err := k.convert(obj)
if err != nil {
return true, err
}
instance.Status.Provisioning = true
// Check if this KeyVault already exists and its state if it does.
kvopsclient := NewOpsClient(k.Creds, instance.Name)
keyvault, err := k.KeyvaultClient.GetVault(ctx, instance.Spec.ResourceGroup, instance.Spec.KeyVault)
if err != nil {
instance.Status.Message = err.Error()
catch := []string{
errhelp.AsyncOpIncompleteError,
errhelp.ResourceGroupNotFoundErrorCode,
errhelp.ParentNotFoundErrorCode,
errhelp.ResourceNotFound,
}
azerr := errhelp.NewAzureError(err)
if helpers.ContainsString(catch, azerr.Type) {
return false, nil
}
return false, err
}
vaultBaseURL := *keyvault.Properties.VaultURI
// exit successfully if key already exists, @todo, determine how to roll these in a way users would expect
if _, err := kvopsclient.GetKey(ctx, vaultBaseURL, instance.GetName(), ""); err == nil {
instance.Status.Message = resourcemanager.SuccessMsg
instance.Status.Provisioned = true
instance.Status.Provisioning = false
return true, nil
}
katts := kvops.KeyAttributes{
Enabled: to.BoolPtr(true),
}
// default to all operations if none specified
ops := instance.Spec.Operations
if len(ops) == 0 {
ops = kvops.PossibleJSONWebKeyOperationValues()
}
params := kvops.KeyCreateParameters{
Kty: instance.Spec.Type,
KeySize: &instance.Spec.KeySize,
KeyOps: &instance.Spec.Operations,
KeyAttributes: &katts,
}
switch instance.Spec.Type {
case kvops.RSA, kvops.RSAHSM:
if instance.Spec.KeySize == 0 {
instance.Status.Message = "no keysize provided for rsa key"
instance.Status.FailedProvisioning = true
instance.Status.Provisioned = false
instance.Status.Provisioning = false
return true, nil
}
params.KeySize = &instance.Spec.KeySize
case kvops.EC, kvops.ECHSM:
if instance.Spec.Curve == "" {
instance.Status.Message = "no curve provided for ec key"
instance.Status.FailedProvisioning = true
instance.Status.Provisioned = false
instance.Status.Provisioning = false
return true, nil
}
params.Curve = instance.Spec.Curve
}
req, err := kvopsclient.CreateKey(ctx, vaultBaseURL, instance.Name, params)
if err != nil {
instance.Status.Message = err.Error()
// this generally means the operator doesn't have access to the keyvault
// this can be resolved elsewhere so we should keep trying
// See https://github.com/Azure/azure-sdk-for-go/issues/10975 for more details
if req.Response.Response != nil && req.Response.StatusCode == http.StatusForbidden {
return false, nil
}
return false, err
}
instance.Status.Message = resourcemanager.SuccessMsg
instance.Status.Provisioned = true
instance.Status.Provisioning = false
return true, nil
}
// Delete ensures the requested resource is gone from Azure
func (k *KeyvaultKeyClient) Delete(ctx context.Context, obj runtime.Object, opts ...resourcemanager.ConfigOption) (bool, error) {
instance, err := k.convert(obj)
if err != nil {
return true, err
}
keyv, err := k.KeyvaultClient.GetVault(ctx, instance.Spec.ResourceGroup, instance.Spec.KeyVault)
if err != nil {
catch := []string{
errhelp.ResourceGroupNotFoundErrorCode,
errhelp.ParentNotFoundErrorCode,
errhelp.ResourceNotFound,
}
azerr := errhelp.NewAzureError(err)
if helpers.ContainsString(catch, azerr.Type) {
return false, nil
}
return true, err
}
vaultBaseURL := *keyv.Properties.VaultURI
kvopsclient := NewOpsClient(k.Creds, instance.Spec.KeyVault)
req, err := kvopsclient.DeleteKey(ctx, vaultBaseURL, instance.Name)
if err != nil {
if req.Response.StatusCode == http.StatusNotFound {
return false, nil
}
return true, err
}
// @Todo figure out when to purge or if we should ever
// _, err = kvopsclient.PurgeDeletedKey(ctx, vaultBaseURL, instance.Name)
// if err != nil {
// return true, err
// }
return false, nil
}
// GetParents returns the kube resources most likely to be parents to this resource
func (k *KeyvaultKeyClient) GetParents(obj runtime.Object) ([]resourcemanager.KubeParent, error) {
instance, err := k.convert(obj)
if err != nil {
return nil, err
}
return []resourcemanager.KubeParent{
{
Key: types.NamespacedName{
Namespace: instance.Namespace,
Name: instance.Spec.KeyVault,
},
Target: &azurev1alpha1.KeyVault{},
},
{
Key: types.NamespacedName{
Namespace: instance.Namespace,
Name: instance.Spec.ResourceGroup,
},
Target: &azurev1alpha1.ResourceGroup{},
},
}, nil
}
func (g *KeyvaultKeyClient) GetStatus(obj runtime.Object) (*v1alpha1.ASOStatus, error) {
instance, err := g.convert(obj)
if err != nil {
return nil, err
}
return &instance.Status, nil
}
func (k *KeyvaultKeyClient) convert(obj runtime.Object) (*v1alpha1.KeyVaultKey, error) {
local, ok := obj.(*v1alpha1.KeyVaultKey)
if !ok {
return nil, fmt.Errorf("failed type assertion on kind: %s", obj.GetObjectKind().GroupVersionKind().String())
}
return local, nil
}