forked from Azure/azure-sdk-for-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
1496 lines (1260 loc) · 51.3 KB
/
client.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//go:build go1.16
// +build go1.16
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
package azkeys
import (
"context"
"errors"
"net/http"
"time"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
"github.com/Azure/azure-sdk-for-go/sdk/keyvault/azkeys/internal/generated"
shared "github.com/Azure/azure-sdk-for-go/sdk/keyvault/internal"
)
// Client is the struct for interacting with a KeyVault Keys instance
type Client struct {
kvClient *generated.KeyVaultClient
vaultUrl string
}
// ClientOptions are the configurable options on a Client.
type ClientOptions struct {
azcore.ClientOptions
}
// converts ClientOptions to generated *generated.ConnectionOptions
func (c *ClientOptions) toConnectionOptions() *policy.ClientOptions {
if c == nil {
return nil
}
return &policy.ClientOptions{
Logging: c.Logging,
Retry: c.Retry,
Telemetry: c.Telemetry,
Transport: c.Transport,
PerCallPolicies: c.PerCallPolicies,
PerRetryPolicies: c.PerRetryPolicies,
}
}
// NewClient returns a pointer to a Client object affinitized to a vaultUrl.
func NewClient(vaultUrl string, credential azcore.TokenCredential, options *ClientOptions) (*Client, error) {
if options == nil {
options = &ClientOptions{}
}
genOptions := options.toConnectionOptions()
genOptions.PerRetryPolicies = append(
genOptions.PerRetryPolicies,
shared.NewKeyVaultChallengePolicy(credential),
)
pl := runtime.NewPipeline(generated.ModuleName, generated.ModuleVersion, runtime.PipelineOptions{}, genOptions)
return &Client{
kvClient: generated.NewKeyVaultClient(pl),
vaultUrl: vaultUrl,
}, nil
}
// CreateKeyOptions contains the optional parameters for the KeyVaultClient.CreateKey method.
type CreateKeyOptions struct {
// Elliptic curve name. For valid values, see JsonWebKeyCurveName.
Curve *JSONWebKeyCurveName `json:"crv,omitempty"`
// The attributes of a key managed by the key vault service.
KeyAttributes *KeyAttributes `json:"attributes,omitempty"`
KeyOps []*JSONWebKeyOperation `json:"key_ops,omitempty"`
// The key size in bits. For example: 2048, 3072, or 4096 for RSA.
KeySize *int32 `json:"key_size,omitempty"`
// The public exponent for a RSA key.
PublicExponent *int32 `json:"public_exponent,omitempty"`
// Application specific metadata in the form of key-value pairs.
Tags map[string]string `json:"tags,omitempty"`
}
// convert CreateKeyOptions to *generated.KeyVaultClientCreateKeyOptions
func (c *CreateKeyOptions) toGenerated() *generated.KeyVaultClientCreateKeyOptions {
return &generated.KeyVaultClientCreateKeyOptions{}
}
// convert CreateKeyOptions to generated.KeyCreateParameters
func (c *CreateKeyOptions) toKeyCreateParameters(keyType KeyType) generated.KeyCreateParameters {
var attribs *generated.KeyAttributes
if c.KeyAttributes != nil {
attribs = c.KeyAttributes.toGenerated()
}
var ops []*generated.JSONWebKeyOperation
for _, o := range c.KeyOps {
ops = append(ops, (*generated.JSONWebKeyOperation)(o))
}
return generated.KeyCreateParameters{
Kty: keyType.toGenerated(),
Curve: (*generated.JSONWebKeyCurveName)(c.Curve),
KeyAttributes: attribs,
KeyOps: ops,
KeySize: c.KeySize,
PublicExponent: c.PublicExponent,
Tags: convertToGeneratedMap(c.Tags),
}
}
// KeyVaultClientCreateKeyResponse contains the response from method KeyVaultClient.CreateKey.
type CreateKeyResponse struct {
KeyBundle
// RawResponse contains the underlying HTTP response.
RawResponse *http.Response
}
// creates CreateKeyResponse from generated.KeyVaultClient.CreateKeyResponse
func createKeyResponseFromGenerated(g generated.KeyVaultClientCreateKeyResponse) CreateKeyResponse {
return CreateKeyResponse{
RawResponse: g.RawResponse,
KeyBundle: KeyBundle{
Attributes: keyAttributesFromGenerated(g.Attributes),
Key: jsonWebKeyFromGenerated(g.Key),
Tags: convertGeneratedMap(g.Tags),
Managed: g.Managed,
},
}
}
// CreateKey - The create key operation can be used to create any key type in Azure Key Vault.
// If the named key already exists, Azure Key Vault creates
// a new version of the key. It requires the keys/create permission.
func (c *Client) CreateKey(ctx context.Context, name string, keyType KeyType, options *CreateKeyOptions) (CreateKeyResponse, error) {
if options == nil {
options = &CreateKeyOptions{}
}
resp, err := c.kvClient.CreateKey(ctx, c.vaultUrl, name, options.toKeyCreateParameters(keyType), options.toGenerated())
if err != nil {
return CreateKeyResponse{}, err
}
return createKeyResponseFromGenerated(resp), nil
}
// CreateECKeyOptions contains the optional parameters for the KeyVaultClient.CreateECKey method
type CreateECKeyOptions struct {
// Elliptic curve name. For valid values, see JsonWebKeyCurveName.
CurveName *JSONWebKeyCurveName `json:"crv,omitempty"`
// Application specific metadata in the form of key-value pairs.
Tags map[string]string `json:"tags,omitempty"`
// Whether to create an EC key with HSM protection
HardwareProtected bool
}
// convert CreateECKeyOptions to generated.KeyCreateParameters
func (c *CreateECKeyOptions) toKeyCreateParameters(keyType KeyType) generated.KeyCreateParameters {
return generated.KeyCreateParameters{
Kty: keyType.toGenerated(),
Curve: (*generated.JSONWebKeyCurveName)(c.CurveName),
Tags: convertToGeneratedMap(c.Tags),
}
}
// CreateECKeyResponse contains the response from method Client.CreateECKey.
type CreateECKeyResponse struct {
KeyBundle
// RawResponse contains the underlying HTTP response.
RawResponse *http.Response
}
// convert the generated.KeyVaultClientCreateKeyResponse to CreateECKeyResponse
func createECKeyResponseFromGenerated(g generated.KeyVaultClientCreateKeyResponse) CreateECKeyResponse {
return CreateECKeyResponse{
RawResponse: g.RawResponse,
KeyBundle: KeyBundle{
Attributes: keyAttributesFromGenerated(g.Attributes),
Key: jsonWebKeyFromGenerated(g.Key),
Tags: convertGeneratedMap(g.Tags),
Managed: g.Managed,
},
}
}
// CreateKey - The create key operation can be used to create a new elliptic key curve in Azure Key Vault.
// If the named key already exists, Azure Key Vault creates
// a new version of the key. It requires the keys/create permission.
func (c *Client) CreateECKey(ctx context.Context, name string, options *CreateECKeyOptions) (CreateECKeyResponse, error) {
keyType := EC
if options != nil && options.HardwareProtected {
keyType = ECHSM
} else if options == nil {
options = &CreateECKeyOptions{}
}
resp, err := c.kvClient.CreateKey(ctx, c.vaultUrl, name, options.toKeyCreateParameters(keyType), &generated.KeyVaultClientCreateKeyOptions{})
if err != nil {
return CreateECKeyResponse{}, err
}
return createECKeyResponseFromGenerated(resp), nil
}
// CreateOCTKeyOptions contains the optional parameters for the Client.CreateOCTKey method
type CreateOCTKeyOptions struct {
// Hardware Protected OCT Key
HardwareProtected bool
// The key size in bits. For example: 2048, 3072, or 4096 for RSA.
KeySize *int32 `json:"key_size,omitempty"`
// Application specific metadata in the form of key-value pairs.
Tags map[string]string `json:"tags,omitempty"`
}
// conver the CreateOCTKeyOptions to generated.KeyCreateParameters
func (c *CreateOCTKeyOptions) toKeyCreateParameters(keyType KeyType) generated.KeyCreateParameters {
return generated.KeyCreateParameters{
Kty: keyType.toGenerated(),
KeySize: c.KeySize,
Tags: convertToGeneratedMap(c.Tags),
}
}
// CreateOCTKeyResponse contains the response from method Client.CreateOCTKey.
type CreateOCTKeyResponse struct {
KeyBundle
// RawResponse contains the underlying HTTP response.
RawResponse *http.Response
}
// convert generated response to CreateOCTKeyResponse
func createOCTKeyResponseFromGenerated(i generated.KeyVaultClientCreateKeyResponse) CreateOCTKeyResponse {
return CreateOCTKeyResponse{
RawResponse: i.RawResponse,
KeyBundle: KeyBundle{
Attributes: keyAttributesFromGenerated(i.Attributes),
Key: jsonWebKeyFromGenerated(i.Key),
Tags: convertGeneratedMap(i.Tags),
Managed: i.Managed,
},
}
}
// CreateKey - The create key operation can be used to create a new octet sequence (symmetric) key in Azure Key Vault.
// If the named key already exists, Azure Key Vault creates
// a new version of the key. It requires the keys/create permission.
func (c *Client) CreateOCTKey(ctx context.Context, name string, options *CreateOCTKeyOptions) (CreateOCTKeyResponse, error) {
keyType := Oct
if options != nil && options.HardwareProtected {
keyType = OctHSM
} else if options == nil {
options = &CreateOCTKeyOptions{}
}
resp, err := c.kvClient.CreateKey(ctx, c.vaultUrl, name, options.toKeyCreateParameters(keyType), &generated.KeyVaultClientCreateKeyOptions{})
if err != nil {
return CreateOCTKeyResponse{}, err
}
return createOCTKeyResponseFromGenerated(resp), nil
}
// CreateRSAKeyOptions contains the optional parameters for the Client.CreateRSAKey method.
type CreateRSAKeyOptions struct {
// Hardware Protected OCT Key
HardwareProtected bool
// The key size in bits. For example: 2048, 3072, or 4096 for RSA.
KeySize *int32 `json:"key_size,omitempty"`
// The public exponent for a RSA key.
PublicExponent *int32 `json:"public_exponent,omitempty"`
// Application specific metadata in the form of key-value pairs.
Tags map[string]string `json:"tags,omitempty"`
}
// convert CreateRSAKeyOptions to generated.KeyCreateParameters
func (c CreateRSAKeyOptions) toKeyCreateParameters(k KeyType) generated.KeyCreateParameters {
return generated.KeyCreateParameters{
Kty: k.toGenerated(),
KeySize: c.KeySize,
PublicExponent: c.PublicExponent,
Tags: convertToGeneratedMap(c.Tags),
}
}
// CreateRSAKeyResponse contains the response from method Client.CreateRSAKey.
type CreateRSAKeyResponse struct {
KeyBundle
// RawResponse contains the underlying HTTP response.
RawResponse *http.Response
}
// convert internal response to CreateRSAKeyResponse
func createRSAKeyResponseFromGenerated(i generated.KeyVaultClientCreateKeyResponse) CreateRSAKeyResponse {
return CreateRSAKeyResponse{
RawResponse: i.RawResponse,
KeyBundle: KeyBundle{
Attributes: keyAttributesFromGenerated(i.Attributes),
Key: jsonWebKeyFromGenerated(i.Key),
Tags: convertGeneratedMap(i.Tags),
Managed: i.Managed,
},
}
}
// CreateKey - The create key operation can be used to create a new RSA key in Azure Key Vault.
// If the named key already exists, Azure Key Vault creates
// a new version of the key. It requires the keys/create permission.
func (c *Client) CreateRSAKey(ctx context.Context, name string, options *CreateRSAKeyOptions) (CreateRSAKeyResponse, error) {
keyType := RSA
if options != nil && options.HardwareProtected {
keyType = RSAHSM
} else if options == nil {
options = &CreateRSAKeyOptions{}
}
resp, err := c.kvClient.CreateKey(ctx, c.vaultUrl, name, options.toKeyCreateParameters(keyType), &generated.KeyVaultClientCreateKeyOptions{})
if err != nil {
return CreateRSAKeyResponse{}, err
}
return createRSAKeyResponseFromGenerated(resp), nil
}
// ListKeysPager is a Pager for the Client.ListSecrets operation
type ListKeysPager interface {
// PageResponse returns the current ListKeysPage
PageResponse() ListKeysPage
// Err returns true if there is another page of data available, false if not
Err() error
// NextPage returns true if there is another page of data available, false if not
NextPage(context.Context) bool
}
// listKeysPager implements the ListKeysPager interface
type listKeysPager struct {
genPager generated.KeyVaultClientGetKeysPager
}
// PageResponse returns the results from the page most recently fetched from the service
func (l *listKeysPager) PageResponse() ListKeysPage {
return listKeysPageFromGenerated(l.genPager.PageResponse())
}
// Err returns an error value if the most recent call to NextPage was not successful, else nil
func (l *listKeysPager) Err() error {
return l.genPager.Err()
}
// NextPage fetches the next available page of results from the service. If the fetched page
// contains results, the return value is true, else false. Results fetched from the service
// can be evaluated by calling PageResponse on this Pager.
func (l *listKeysPager) NextPage(ctx context.Context) bool {
return l.genPager.NextPage(ctx)
}
// ListKeysOptions contains the optional parameters for the Client.ListKeys method
type ListKeysOptions struct {
MaxResults *int32
}
// convert ListKeysOptions to generated options
func (l ListKeysOptions) toGenerated() *generated.KeyVaultClientGetKeysOptions {
return &generated.KeyVaultClientGetKeysOptions{Maxresults: l.MaxResults}
}
// ListKeysPage contains the current page of results for the Client.ListSecrets operation
type ListKeysPage struct {
// READ-ONLY; The URL to get the next set of keys.
NextLink *string `json:"nextLink,omitempty" azure:"ro"`
// READ-ONLY; A response message containing a list of keys in the key vault along with a link to the next page of keys.
Keys []*KeyItem `json:"value,omitempty" azure:"ro"`
// RawResponse contains the underlying HTTP response.
RawResponse *http.Response
}
// convert internal Response to ListKeysPage
func listKeysPageFromGenerated(i generated.KeyVaultClientGetKeysResponse) ListKeysPage {
var keys []*KeyItem
for _, k := range i.Value {
keys = append(keys, keyItemFromGenerated(k))
}
return ListKeysPage{
RawResponse: i.RawResponse,
NextLink: i.NextLink,
Keys: keys,
}
}
// ListKeys retrieves a list of the keys in the Key Vault as JSON Web Key structures that contain the
// public part of a stored key. The LIST operation is applicable to all key types, however only the
// base key identifier, attributes, and tags are provided in the response. Individual versions of a
// key are not listed in the response. This operation requires the keys/list permission.
func (c *Client) ListKeys(options *ListKeysOptions) ListKeysPager {
if options == nil {
options = &ListKeysOptions{}
}
p := c.kvClient.GetKeys(c.vaultUrl, options.toGenerated())
return &listKeysPager{
genPager: *p,
}
}
// GetKeyOptions contains the options for the Client.GetKey method
type GetKeyOptions struct {
Version string
}
// GetKeyResponse contains the response for the Client.GetResponse method
type GetKeyResponse struct {
KeyBundle
// RawResponse contains the underlying HTTP response.
RawResponse *http.Response
}
// convert internal response to GetKeyResponse
func getKeyResponseFromGenerated(i generated.KeyVaultClientGetKeyResponse) GetKeyResponse {
return GetKeyResponse{
RawResponse: i.RawResponse,
KeyBundle: KeyBundle{
Attributes: keyAttributesFromGenerated(i.Attributes),
Key: jsonWebKeyFromGenerated(i.Key),
Tags: convertGeneratedMap(i.Tags),
Managed: i.Managed,
},
}
}
// GetKey - The get key operation is applicable to all key types. If the requested key is symmetric, then
// no key material is released in the response. This operation requires the keys/get permission.
func (c *Client) GetKey(ctx context.Context, keyName string, options *GetKeyOptions) (GetKeyResponse, error) {
if options == nil {
options = &GetKeyOptions{}
}
resp, err := c.kvClient.GetKey(ctx, c.vaultUrl, keyName, options.Version, &generated.KeyVaultClientGetKeyOptions{})
if err != nil {
return GetKeyResponse{}, err
}
return getKeyResponseFromGenerated(resp), err
}
// GetDeletedKeyOptions contains the optional parameters for the Client.GetDeletedKey method
type GetDeletedKeyOptions struct{}
// convert the GetDeletedKeyOptions to the internal representation
func (g GetDeletedKeyOptions) toGenerated() *generated.KeyVaultClientGetDeletedKeyOptions {
return &generated.KeyVaultClientGetDeletedKeyOptions{}
}
// GetDeletedKeyResponse contains the response from a Client.GetDeletedKey
type GetDeletedKeyResponse struct {
DeletedKeyBundle
// RawResponse contains the underlying HTTP response.
RawResponse *http.Response
}
// convert generated response to GetDeletedKeyResponse
func getDeletedKeyResponseFromGenerated(i generated.KeyVaultClientGetDeletedKeyResponse) GetDeletedKeyResponse {
return GetDeletedKeyResponse{
RawResponse: i.RawResponse,
DeletedKeyBundle: DeletedKeyBundle{
KeyBundle: KeyBundle{
Attributes: keyAttributesFromGenerated(i.Attributes),
Key: jsonWebKeyFromGenerated(i.Key),
Tags: convertGeneratedMap(i.Tags),
Managed: i.Managed,
},
RecoveryID: i.RecoveryID,
DeletedDate: i.DeletedDate,
ScheduledPurgeDate: i.ScheduledPurgeDate,
},
}
}
// GetDeletedKey - The Get Deleted Key operation is applicable for soft-delete enabled vaults.
// While the operation can be invoked on any vault, it will return an error if invoked on a non
// soft-delete enabled vault. This operation requires the keys/get permission.
func (c *Client) GetDeletedKey(ctx context.Context, keyName string, options *GetDeletedKeyOptions) (GetDeletedKeyResponse, error) {
if options == nil {
options = &GetDeletedKeyOptions{}
}
resp, err := c.kvClient.GetDeletedKey(ctx, c.vaultUrl, keyName, options.toGenerated())
if err != nil {
return GetDeletedKeyResponse{}, err
}
return getDeletedKeyResponseFromGenerated(resp), nil
}
// PurgeDeletedKeyOptions is the struct for any future options for Client.PurgeDeletedKey.
type PurgeDeletedKeyOptions struct{}
// convert options to internal options
func (p *PurgeDeletedKeyOptions) toGenerated() *generated.KeyVaultClientPurgeDeletedKeyOptions {
return &generated.KeyVaultClientPurgeDeletedKeyOptions{}
}
// PurgeDeletedKeyResponse contains the response from method Client.PurgeDeletedKey.
type PurgeDeletedKeyResponse struct {
// RawResponse contains the underlying HTTP response.
RawResponse *http.Response
}
// Converts the generated response to the publicly exposed version.
func purgeDeletedKeyResponseFromGenerated(i generated.KeyVaultClientPurgeDeletedKeyResponse) PurgeDeletedKeyResponse {
return PurgeDeletedKeyResponse{
RawResponse: i.RawResponse,
}
}
// PurgeDeletedKey deletes the specified key. The purge deleted key operation removes the key permanently, without the possibility of recovery.
// This operation can only be enabled on a soft-delete enabled vault. This operation requires the key/purge permission.
func (c *Client) PurgeDeletedKey(ctx context.Context, keyName string, options *PurgeDeletedKeyOptions) (PurgeDeletedKeyResponse, error) {
if options == nil {
options = &PurgeDeletedKeyOptions{}
}
resp, err := c.kvClient.PurgeDeletedKey(ctx, c.vaultUrl, keyName, options.toGenerated())
return purgeDeletedKeyResponseFromGenerated(resp), err
}
// DeletedKeyResponse contains the response for a Client.BeginDeleteKey operation.
type DeleteKeyResponse struct {
DeletedKeyBundle
// RawResponse holds the underlying HTTP response
RawResponse *http.Response
}
// convert interal response to DeleteKeyResponse
func deleteKeyResponseFromGenerated(i *generated.KeyVaultClientDeleteKeyResponse) *DeleteKeyResponse {
if i == nil {
return nil
}
return &DeleteKeyResponse{
RawResponse: i.RawResponse,
}
}
// BeginDeleteKeyOptions contains the optional parameters for the Client.BeginDeleteKey method.
type BeginDeleteKeyOptions struct{}
// convert public options to generated options struct
func (b *BeginDeleteKeyOptions) toGenerated() *generated.KeyVaultClientDeleteKeyOptions {
return &generated.KeyVaultClientDeleteKeyOptions{}
}
// DeleteKeyPoller is the interface for the Client.DeleteKey operation.
type DeleteKeyPoller interface {
// Done returns true if the LRO has reached a terminal state
Done() bool
// Poll fetches the latest state of the LRO. It returns an HTTP response or error.
// If the LRO has completed successfully, the poller's state is updated and the HTTP response is returned.
// If the LRO has completed with failure or was cancelled, the poller's state is updated and the error is returned.
Poll(context.Context) (*http.Response, error)
// FinalResponse returns the final response after the operations has finished
FinalResponse(context.Context) (DeleteKeyResponse, error)
}
// The poller returned by the Client.StartDeleteKey operation
type startDeleteKeyPoller struct {
keyName string // This is the key to Poll for in GetDeletedKey
vaultUrl string
client *generated.KeyVaultClient
deleteResponse generated.KeyVaultClientDeleteKeyResponse
lastResponse generated.KeyVaultClientGetDeletedKeyResponse
RawResponse *http.Response
}
// Done returns true if the LRO has reached a terminal state
func (s *startDeleteKeyPoller) Done() bool {
return s.lastResponse.RawResponse != nil
}
// Poll fetches the latest state of the LRO. It returns an HTTP response or error.(
// If the LRO has completed successfully, the poller's state is updated and the HTTP response is returned.
// If the LRO has completed with failure or was cancelled, the poller's state is updated and the error is returned.)
func (s *startDeleteKeyPoller) Poll(ctx context.Context) (*http.Response, error) {
resp, err := s.client.GetDeletedKey(ctx, s.vaultUrl, s.keyName, nil)
if err == nil {
// Service recognizes DeletedKey, operation is done
s.lastResponse = resp
return resp.RawResponse, nil
}
var httpResponseErr *azcore.ResponseError
if errors.As(err, &httpResponseErr) {
if httpResponseErr.StatusCode == http.StatusNotFound {
// This is the expected result
return s.deleteResponse.RawResponse, nil
}
}
return s.deleteResponse.RawResponse, err
}
// FinalResponse returns the final response after the operations has finished
func (s *startDeleteKeyPoller) FinalResponse(ctx context.Context) (DeleteKeyResponse, error) {
return *deleteKeyResponseFromGenerated(&s.deleteResponse), nil
}
// pollUntilDone continually calls the Poll operation until the operation is completed. In between each
// Poll is a wait determined by the t parameter.
func (s *startDeleteKeyPoller) pollUntilDone(ctx context.Context, t time.Duration) (DeleteKeyResponse, error) {
for {
resp, err := s.Poll(ctx)
if err != nil {
return DeleteKeyResponse{}, err
}
s.RawResponse = resp
if s.Done() {
break
}
time.Sleep(t)
}
return DeleteKeyResponse{}, nil
}
// DeleteKeyPollerResponse contains the response from the Client.BeginDeleteKey method
type DeleteKeyPollerResponse struct {
// PollUntilDone will poll the service endpoint until a terminal state is reached or an error occurs
PollUntilDone func(context.Context, time.Duration) (DeleteKeyResponse, error)
// Poller contains an initialized WidgetPoller
Poller DeleteKeyPoller
// RawResponse contains the underlying HTTP response.
RawResponse *http.Response
}
// BeginDeleteKey deletes a key from the keyvault. Delete cannot be applied to an individual version of a key. This operation
// requires the key/delete permission. This response contains a Poller struct that can be used to Poll for a response, or the
// response PollUntilDone function can be used to poll until completion.
func (c *Client) BeginDeleteKey(ctx context.Context, keyName string, options *BeginDeleteKeyOptions) (DeleteKeyPollerResponse, error) {
if options == nil {
options = &BeginDeleteKeyOptions{}
}
resp, err := c.kvClient.DeleteKey(ctx, c.vaultUrl, keyName, options.toGenerated())
if err != nil {
return DeleteKeyPollerResponse{}, err
}
getResp, err := c.kvClient.GetDeletedKey(ctx, c.vaultUrl, keyName, nil)
var httpErr *azcore.ResponseError
if errors.As(err, &httpErr) {
if httpErr.StatusCode != http.StatusNotFound {
return DeleteKeyPollerResponse{}, err
}
}
s := &startDeleteKeyPoller{
vaultUrl: c.vaultUrl,
keyName: keyName,
client: c.kvClient,
deleteResponse: resp,
lastResponse: getResp,
}
return DeleteKeyPollerResponse{
Poller: s,
RawResponse: resp.RawResponse,
PollUntilDone: s.pollUntilDone,
}, nil
}
// BackupKeyOptions contains the optional parameters for the Client.BackupKey method
type BackupKeyOptions struct{}
// convert Options to generated version
func (b BackupKeyOptions) toGenerated() *generated.KeyVaultClientBackupKeyOptions {
return &generated.KeyVaultClientBackupKeyOptions{}
}
// BackupKeyResponse contains the response from the Client.BackupKey method
type BackupKeyResponse struct {
// READ-ONLY; The backup blob containing the backed up key.
Value []byte `json:"value,omitempty" azure:"ro"`
// RawResponse contains the underlying HTTP response.
RawResponse *http.Response
}
// convert internal reponse to BackupKeyResponse
func backupKeyResponseFromGenerated(i generated.KeyVaultClientBackupKeyResponse) BackupKeyResponse {
return BackupKeyResponse{
RawResponse: i.RawResponse,
Value: i.Value,
}
}
// BackupKey - The Key Backup operation exports a key from Azure Key Vault in a protected form.
// Note that this operation does NOT return key material in a form that can be used outside the
// Azure Key Vault system, the returned key material is either protected to a Azure Key Vault
// HSM or to Azure Key Vault itself. The intent of this operation is to allow a client to GENERATE
// a key in one Azure Key Vault instance, BACKUP the key, and then RESTORE it into another Azure
// Key Vault instance. The BACKUP operation may be used to export, in protected form, any key
// type from Azure Key Vault. Individual versions of a key cannot be backed up. BACKUP / RESTORE
// can be performed within geographical boundaries only; meaning that a BACKUP from one geographical
// area cannot be restored to another geographical area. For example, a backup from the US
// geographical area cannot be restored in an EU geographical area. This operation requires the key/backup permission.
func (c *Client) BackupKey(ctx context.Context, keyName string, options *BackupKeyOptions) (BackupKeyResponse, error) {
if options == nil {
options = &BackupKeyOptions{}
}
resp, err := c.kvClient.BackupKey(ctx, c.vaultUrl, keyName, options.toGenerated())
if err != nil {
return BackupKeyResponse{}, err
}
return backupKeyResponseFromGenerated(resp), nil
}
// RecoverDeletedKeyPoller is the interface for the Client.RecoverDeletedKey operation
type RecoverDeletedKeyPoller interface {
// Done returns true if the LRO has reached a terminal state
Done() bool
// Poll fetches the latest state of the LRO. It returns an HTTP response or error.
// If the LRO has completed successfully, the poller's state is updated and the HTTP response is returned.
// If the LRO has completed with failure or was cancelled, the poller's state is updated and the error is returned.
Poll(context.Context) (*http.Response, error)
// FinalResponse returns the final response after the operations has finished
FinalResponse(context.Context) (RecoverDeletedKeyResponse, error)
}
// beginRecoverPoller implements the RecoverDeletedKeyPoller interface
type beginRecoverPoller struct {
keyName string
vaultUrl string
client *generated.KeyVaultClient
recoverResponse generated.KeyVaultClientRecoverDeletedKeyResponse
lastResponse generated.KeyVaultClientGetKeyResponse
RawResponse *http.Response
}
// Done returns true when the polling operation is completed
func (b *beginRecoverPoller) Done() bool {
return b.RawResponse.StatusCode == http.StatusOK
}
// Poll fetches the latest state of the LRO. It returns an HTTP response or error.
// If the LRO has completed successfully, the poller's state is updated and the HTTP response is returned.
// If the LRO has completed with failure or was cancelled, the poller's state is updated and the error is returned.
func (b *beginRecoverPoller) Poll(ctx context.Context) (*http.Response, error) {
resp, err := b.client.GetKey(ctx, b.vaultUrl, b.keyName, "", nil)
b.lastResponse = resp
var httpErr *azcore.ResponseError
if errors.As(err, &httpErr) {
return httpErr.RawResponse, err
}
return resp.RawResponse, nil
}
// FinalResponse returns the final response after the operations has finished
func (b *beginRecoverPoller) FinalResponse(ctx context.Context) (RecoverDeletedKeyResponse, error) {
return recoverDeletedKeyResponseFromGenerated(b.recoverResponse), nil
}
// pollUntilDone is the method for the Response.PollUntilDone struct
func (b *beginRecoverPoller) pollUntilDone(ctx context.Context, t time.Duration) (RecoverDeletedKeyResponse, error) {
for {
resp, err := b.Poll(ctx)
if err != nil {
b.RawResponse = resp
}
if b.Done() {
break
}
b.RawResponse = resp
time.Sleep(t)
}
return recoverDeletedKeyResponseFromGenerated(b.recoverResponse), nil
}
// BeginRecoverDeletedKeyOptions contains the optional parameters for the Client.BeginRecoverDeletedKey operation
type BeginRecoverDeletedKeyOptions struct{}
// Convert the publicly exposed options object to the generated version
func (b BeginRecoverDeletedKeyOptions) toGenerated() *generated.KeyVaultClientRecoverDeletedKeyOptions {
return &generated.KeyVaultClientRecoverDeletedKeyOptions{}
}
// RecoverDeletedKeyResponse is the response object for the Client.RecoverDeletedKey operation.
type RecoverDeletedKeyResponse struct {
KeyBundle
// RawResponse contains the underlying HTTP response.
RawResponse *http.Response
}
// change recover deleted key reponse to the generated version.
func recoverDeletedKeyResponseFromGenerated(i generated.KeyVaultClientRecoverDeletedKeyResponse) RecoverDeletedKeyResponse {
return RecoverDeletedKeyResponse{
RawResponse: i.RawResponse,
KeyBundle: KeyBundle{
Attributes: keyAttributesFromGenerated(i.Attributes),
Key: jsonWebKeyFromGenerated(i.Key),
Tags: convertGeneratedMap(i.Tags),
Managed: i.Managed,
},
}
}
// RecoverDeletedKeyPollerResponse contains the response of the Client.BeginRecoverDeletedKey operations
type RecoverDeletedKeyPollerResponse struct {
// PollUntilDone will poll the service endpoint until a terminal state is reached or an error occurs
PollUntilDone func(context.Context, time.Duration) (RecoverDeletedKeyResponse, error)
// Poller contains an initialized RecoverDeletedKeyPoller
Poller RecoverDeletedKeyPoller
// RawResponse cotains the underlying HTTP response
RawResponse *http.Response
}
// BeginRecoverDeletedKey recovers the deleted key in the specified vault to the latest version.
// This operation can only be performed on a soft-delete enabled vault. This operation requires the keys/recover permission.
func (c *Client) BeginRecoverDeletedKey(ctx context.Context, keyName string, options *BeginRecoverDeletedKeyOptions) (RecoverDeletedKeyPollerResponse, error) {
if options == nil {
options = &BeginRecoverDeletedKeyOptions{}
}
resp, err := c.kvClient.RecoverDeletedKey(ctx, c.vaultUrl, keyName, options.toGenerated())
if err != nil {
return RecoverDeletedKeyPollerResponse{}, err
}
getResp, err := c.kvClient.GetKey(ctx, c.vaultUrl, keyName, "", nil)
var httpErr *azcore.ResponseError
if errors.As(err, &httpErr) {
if httpErr.StatusCode != http.StatusNotFound {
return RecoverDeletedKeyPollerResponse{}, err
}
}
b := &beginRecoverPoller{
lastResponse: getResp,
keyName: keyName,
client: c.kvClient,
vaultUrl: c.vaultUrl,
recoverResponse: resp,
RawResponse: getResp.RawResponse,
}
return RecoverDeletedKeyPollerResponse{
PollUntilDone: b.pollUntilDone,
Poller: b,
RawResponse: getResp.RawResponse,
}, nil
}
// UpdateKeyPropertiesOptions contains the optional parameters for the Client.UpdateKeyProperties method
type UpdateKeyPropertiesOptions struct {
// The version of a key to update
Version string
// The attributes of a key managed by the key vault service.
KeyAttributes *KeyAttributes `json:"attributes,omitempty"`
// Json web key operations. For more information on possible key operations, see JsonWebKeyOperation.
KeyOps []*JSONWebKeyOperation `json:"key_ops,omitempty"`
// Application specific metadata in the form of key-value pairs.
Tags map[string]string `json:"tags,omitempty"`
}
// convert the options to generated.KeyUpdateParameters struct
func (u UpdateKeyPropertiesOptions) toKeyUpdateParameters() generated.KeyUpdateParameters {
var attribs *generated.KeyAttributes
if u.KeyAttributes != nil {
attribs = u.KeyAttributes.toGenerated()
}
var ops []*generated.JSONWebKeyOperation
for _, o := range u.KeyOps {
ops = append(ops, (*generated.JSONWebKeyOperation)(o))
}
return generated.KeyUpdateParameters{
KeyOps: ops,
KeyAttributes: attribs,
Tags: convertToGeneratedMap(u.Tags),
}
}
// convert options to generated options
func (u UpdateKeyPropertiesOptions) toGeneratedOptions() *generated.KeyVaultClientUpdateKeyOptions {
return &generated.KeyVaultClientUpdateKeyOptions{}
}
// UpdateKeyPropertiesResponse contains the response for the Client.UpdateKeyProperties method
type UpdateKeyPropertiesResponse struct {
KeyBundle
// RawResponse contains the underlying HTTP response.
RawResponse *http.Response
}
// convert the internal response to UpdateKeyPropertiesResponse
func updateKeyPropertiesFromGenerated(i generated.KeyVaultClientUpdateKeyResponse) UpdateKeyPropertiesResponse {
return UpdateKeyPropertiesResponse{
RawResponse: i.RawResponse,
KeyBundle: KeyBundle{
Attributes: keyAttributesFromGenerated(i.Attributes),
Key: jsonWebKeyFromGenerated(i.Key),
Tags: convertGeneratedMap(i.Tags),
Managed: i.Managed,
},
}
}
// UpdateKey - In order to perform this operation, the key must already exist in the Key Vault.
// Note: The cryptographic material of a key itself cannot be changed. This operation requires the keys/update permission.
func (c *Client) UpdateKeyProperties(ctx context.Context, keyName string, options *UpdateKeyPropertiesOptions) (UpdateKeyPropertiesResponse, error) {
if options == nil {
options = &UpdateKeyPropertiesOptions{}
}
resp, err := c.kvClient.UpdateKey(
ctx,
c.vaultUrl,
keyName,
options.Version,
options.toKeyUpdateParameters(),
options.toGeneratedOptions(),
)
if err != nil {
return UpdateKeyPropertiesResponse{}, err
}
return updateKeyPropertiesFromGenerated(resp), nil
}
// ListDeletedKeys is the interface for the Client.ListDeletedKeys operation
type ListDeletedKeysPager interface {
// PageResponse returns the current ListDeletedKeysPage
PageResponse() ListDeletedKeysPage
// Err returns true if there is another page of data available, false if not
Err() error
// NextPage returns true if there is another page of data available, false if not
NextPage(context.Context) bool
}
// listDeletedKeysPager is the pager returned by Client.ListDeletedKeys
type listDeletedKeysPager struct {
genPager *generated.KeyVaultClientGetDeletedKeysPager
}
// PageResponse returns the current page of results
func (l *listDeletedKeysPager) PageResponse() ListDeletedKeysPage {
resp := l.genPager.PageResponse()
var values []*DeletedKeyItem
for _, d := range resp.Value {
values = append(values, deletedKeyItemFromGenerated(d))
}
return ListDeletedKeysPage{
RawResponse: resp.RawResponse,
NextLink: resp.NextLink,
DeletedKeys: values,
}
}
// Err returns an error if the last operation resulted in an error.
func (l *listDeletedKeysPager) Err() error {
return l.genPager.Err()
}
// NextPage fetches the next page of results.
func (l *listDeletedKeysPager) NextPage(ctx context.Context) bool {
return l.genPager.NextPage(ctx)
}
// ListDeletedKeysPage holds the data for a single page.
type ListDeletedKeysPage struct {
// RawResponse contains the underlying HTTP response.
RawResponse *http.Response
// READ-ONLY; The URL to get the next set of deleted keys.