Skip to content

Commit 2f434c2

Browse files
committed
refactor: simplify API usage with higher level methods
Signed-off-by: Pierre-Henri Symoneaux <[email protected]>
1 parent 258a014 commit 2f434c2

17 files changed

+324
-170
lines changed

apis.go

+5-11
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// ANY KIND, either express or implied. See the License for the specific language
88
// governing permissions and limitations under the License.
99

10+
// Package okms is a client for interacting with OVHcloud KMS REST-API.
1011
package okms
1112

1213
import (
@@ -16,17 +17,18 @@ import (
1617
"github.com/ovh/okms-sdk-go/types"
1718
)
1819

19-
var _ Client = (*RestAPIClient)(nil)
20+
var _ API = (*Client)(nil)
2021

21-
// Client is the interface abstracting the KMS clients methods.
22-
type Client interface {
22+
// API is the interface abstracting the KMS clients methods.
23+
type API interface {
2324
// RandomApi
2425
DataKeyApi
2526
SignatureApi
2627
EncryptionApi
2728
ServiceKeyApi
2829
// SecretApi
2930
// Ping(ctx context.Context) error
31+
SetCustomHeader(key, value string)
3032
}
3133

3234
// type RandomApi interface {
@@ -74,18 +76,10 @@ type ServiceKeyApi interface {
7476
// ListServiceKeys returns a page of service keys. The response contains a continuationToken that must be passed to the
7577
// subsequent calls in order to get the next page. The state parameter when no nil is used to query keys having a specific state.
7678
ListServiceKeys(ctx context.Context, continuationToken *string, maxKey *int32, state *types.KeyStates) (*types.ListServiceKeysResponse, error)
77-
// ListAllServiceKeys returns an iterator to go through all the keys without having to deal with pagination.
78-
ListAllServiceKeys(pageSize *int32, state *types.KeyStates) KeyIter
7979
// UpdateServiceKey updates some service key metadata.
8080
UpdateServiceKey(ctx context.Context, keyId uuid.UUID, body types.PatchServiceKeyRequest) (*types.GetServiceKeyResponse, error)
8181
}
8282

83-
// type ServiceKeyApi2 interface {
84-
// GenerateSymmetricKey(ctx context.Context, name string, size types.KeySizes, usage ...types.CryptographicUsages) (*types.GetServiceKeyResponse, error)
85-
// GenerateRSAKeyPair(ctx context.Context, name string, size types.KeySizes, usage ...types.CryptographicUsages) (*types.GetServiceKeyResponse, error)
86-
// GenerateECDSAKeyPair(ctx context.Context, name string, curve types.Curves, usage ...types.CryptographicUsages) (*types.GetServiceKeyResponse, error)
87-
// }
88-
8983
// type SecretApi interface {
9084
// GetSecretsMetadata(ctx context.Context, path string, list bool) (*types.GetMetadataResponse, error)
9185
// PatchSecretMetadata(ctx context.Context, path string, body types.SecretUpdatableMetadata) error

client.go

+202-51
Large diffs are not rendered by default.

datakey.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,15 @@ import (
2020
"math"
2121

2222
"github.com/google/uuid"
23+
"github.com/ovh/okms-sdk-go/internal/utils"
2324
"github.com/ovh/okms-sdk-go/internal/xcrypto"
2425
)
2526

27+
// DataKeys creates a new datakey provider for the given service key.
28+
func (client *Client) DataKeys(serviceKeyID uuid.UUID) *DataKeyProvider {
29+
return newDataKeyProvider(client, serviceKeyID)
30+
}
31+
2632
// DataKeyProvider is a helper provider that wraps an API client
2733
// and provides helpers functions to repeatedly generate or decrypt datakeys
2834
// protected by the same service key.
@@ -33,9 +39,9 @@ type DataKeyProvider struct {
3339
keyId uuid.UUID
3440
}
3541

36-
// NewDataKeyProvider creates a new datakey provider for the given service key,
42+
// newDataKeyProvider creates a new datakey provider for the given service key,
3743
// using the given [DataKeyApi] api client.
38-
func NewDataKeyProvider(api DataKeyApi, keyId uuid.UUID) *DataKeyProvider {
44+
func newDataKeyProvider(api DataKeyApi, keyId uuid.UUID) *DataKeyProvider {
3945
return &DataKeyProvider{
4046
api: api,
4147
keyId: keyId,
@@ -52,8 +58,7 @@ func (sk *DataKeyProvider) GenerateDataKey(ctx context.Context, name string, siz
5258
return nil, nil, errors.New("key size is out of bound")
5359
}
5460
// Let's first ask the KMS to generate a new DK
55-
//nolint:gosec // integer bounds are checked right before
56-
plain, encryptedKey, err := sk.api.GenerateDataKey(ctx, sk.keyId, name, int32(size))
61+
plain, encryptedKey, err := sk.api.GenerateDataKey(ctx, sk.keyId, name, utils.ToInt32(size))
5762
if err != nil {
5863
return nil, nil, err
5964
}

errors.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"errors"
1515
"fmt"
1616

17+
"github.com/ovh/okms-sdk-go/internal/utils"
1718
"github.com/ovh/okms-sdk-go/types"
1819
)
1920

@@ -204,7 +205,7 @@ func newKmsErrorFromRestResponse(resp types.ErrorResponse) *KmsError {
204205
kmsErr.ErrorId = *resp.ErrorId
205206
}
206207
if resp.ErrorCode != nil {
207-
kmsErr.ErrorCode = ErrorCode(*resp.ErrorCode)
208+
kmsErr.ErrorCode = ErrorCode(utils.ToUint32(*resp.ErrorCode))
208209
}
209210
if resp.Errors != nil {
210211
for _, er := range *resp.Errors {

example_test.go

+27-27
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ func ExampleNewRestAPIClientWithHttp() {
4848
}
4949

5050
// Generate an 256 bits AES key
51-
func ExampleRestAPIClient_CreateImportServiceKey_generateAES() {
52-
var kmsClient *okms.RestAPIClient // Initialize client
51+
func ExampleClient_CreateImportServiceKey_generateAES() {
52+
var kmsClient *okms.Client // Initialize client
5353
kType := types.Oct
5454
kSize := types.N256
5555
ops := []types.CryptographicUsages{types.Encrypt, types.Decrypt, types.WrapKey, types.UnwrapKey}
@@ -67,13 +67,13 @@ func ExampleRestAPIClient_CreateImportServiceKey_generateAES() {
6767
}
6868

6969
// Generate a 2048 bits RSA key pair
70-
func ExampleRestAPIClient_CreateImportServiceKey_generateRSA() {
71-
var kmsClient *okms.RestAPIClient // Initialize client
70+
func ExampleClient_CreateImportServiceKey_generateRSA() {
71+
var kmsClient *okms.Client // Initialize client
7272
kType := types.RSA
7373
kSize := types.N2048
7474
ops := []types.CryptographicUsages{types.Sign, types.Verify}
7575
// Create a new RSA 2048 key-pair
76-
respAes, err := kmsClient.CreateImportServiceKey(context.Background(), nil, types.CreateImportServiceKeyRequest{
76+
respRSA, err := kmsClient.CreateImportServiceKey(context.Background(), nil, types.CreateImportServiceKeyRequest{
7777
Name: "RSA key-pair example",
7878
Type: &kType,
7979
Size: &kSize,
@@ -82,17 +82,17 @@ func ExampleRestAPIClient_CreateImportServiceKey_generateRSA() {
8282
if err != nil {
8383
panic(err)
8484
}
85-
fmt.Println("RSA KEY:", respAes.Id)
85+
fmt.Println("RSA KEY:", respRSA.Id)
8686
}
8787

8888
// Generate an ECDSA key pair on the P-256 curve
89-
func ExampleRestAPIClient_CreateImportServiceKey_generateECDSA() {
90-
var kmsClient *okms.RestAPIClient // Initialize client
89+
func ExampleClient_CreateImportServiceKey_generateECDSA() {
90+
var kmsClient *okms.Client // Initialize client
9191
kType := types.EC
9292
curve := types.P256
9393
ops := []types.CryptographicUsages{types.Sign, types.Verify}
9494
// Create a new ECDSA P-256 key-pair
95-
respAes, err := kmsClient.CreateImportServiceKey(context.Background(), nil, types.CreateImportServiceKeyRequest{
95+
respEC, err := kmsClient.CreateImportServiceKey(context.Background(), nil, types.CreateImportServiceKeyRequest{
9696
Name: "ECDSA key-pair example",
9797
Type: &kType,
9898
Curve: &curve,
@@ -101,23 +101,23 @@ func ExampleRestAPIClient_CreateImportServiceKey_generateECDSA() {
101101
if err != nil {
102102
panic(err)
103103
}
104-
fmt.Println("ECDSA KEY:", respAes.Id)
104+
fmt.Println("ECDSA KEY:", respEC.Id)
105105
}
106106

107-
func ExampleRestAPIClient_Sign() {
108-
var kmsClient *okms.RestAPIClient // Initialize client
109-
data := "Hello World !!!" // Data to sign
107+
func ExampleClient_Sign() {
108+
var kmsClient *okms.Client // Initialize client
109+
data := "Hello World !!!" // Data to sign
110110
signResponse, err := kmsClient.Sign(context.Background(), uuid.MustParse("2dab95dc-d7d3-482b-a07b-6b4dfae89d58"), types.ES256, false, []byte(data))
111111
if err != nil {
112112
panic(err)
113113
}
114114
fmt.Println("Signature:", signResponse)
115115
}
116116

117-
func ExampleRestAPIClient_Verify() {
118-
var kmsClient *okms.RestAPIClient // Initialize client
119-
var signature string // Base64 encoded signature
120-
data := "Hello World !!!" // Data to sign
117+
func ExampleClient_Verify() {
118+
var kmsClient *okms.Client // Initialize client
119+
var signature string // Base64 encoded signature
120+
data := "Hello World !!!" // Data to sign
121121
result, err := kmsClient.Verify(context.Background(), uuid.MustParse("2dab95dc-d7d3-482b-a07b-6b4dfae89d58"), types.ES256, false, []byte(data), signature)
122122
if err != nil {
123123
panic(err)
@@ -126,10 +126,10 @@ func ExampleRestAPIClient_Verify() {
126126
}
127127

128128
func ExampleDataKeyProvider_helpers() {
129-
var kmsClient *okms.RestAPIClient // Initialize client
129+
var kmsClient *okms.Client // Initialize client
130130

131131
data := "Hello World !!!" // Data to encrypt
132-
dkProvider := okms.NewDataKeyProvider(kmsClient, uuid.MustParse("2dab95dc-d7d3-482b-a07b-6b4dfae89d58"))
132+
dkProvider := kmsClient.DataKeys(uuid.MustParse("2dab95dc-d7d3-482b-a07b-6b4dfae89d58"))
133133

134134
// Unless you want to use another algorithm than AES-GCM 256 bits, you can use the 2 following helper methods:
135135
encryptedData, encryptedKey, nonce, err := dkProvider.EncryptGCM(context.Background(), "Example DK", []byte(data), []byte("Some additional data"))
@@ -145,9 +145,9 @@ func ExampleDataKeyProvider_helpers() {
145145
}
146146

147147
func ExampleDataKeyProvider_GenerateDataKey() {
148-
var kmsClient *okms.RestAPIClient // Initialize client
149-
data := "Hello World !!!" // Data to encrypt
150-
dkProvider := okms.NewDataKeyProvider(kmsClient, uuid.MustParse("2dab95dc-d7d3-482b-a07b-6b4dfae89d58"))
148+
var kmsClient *okms.Client // Initialize client
149+
data := "Hello World !!!" // Data to encrypt
150+
dkProvider := kmsClient.DataKeys(uuid.MustParse("2dab95dc-d7d3-482b-a07b-6b4dfae89d58"))
151151

152152
// Generate a new datakey
153153
plain, encrypted, err := dkProvider.GenerateDataKey(context.Background(), "Example DK", 256)
@@ -178,11 +178,11 @@ func ExampleDataKeyProvider_GenerateDataKey() {
178178
}
179179

180180
func ExampleDataKeyProvider_DecryptDataKey() {
181-
var kmsClient *okms.RestAPIClient // Initialize client
182-
var encryptedData []byte // Some encrypted data
183-
var encryptedKey []byte // Encrypted datakey
184-
var nonce []byte // Nonce used for data encryption
185-
dkProvider := okms.NewDataKeyProvider(kmsClient, uuid.MustParse("2dab95dc-d7d3-482b-a07b-6b4dfae89d58"))
181+
var kmsClient *okms.Client // Initialize client
182+
var encryptedData []byte // Some encrypted data
183+
var encryptedKey []byte // Encrypted datakey
184+
var nonce []byte // Nonce used for data encryption
185+
dkProvider := kmsClient.DataKeys(uuid.MustParse("2dab95dc-d7d3-482b-a07b-6b4dfae89d58"))
186186

187187
// Decrypt data key
188188
plain, err := dkProvider.DecryptDataKey(context.Background(), encryptedKey)

examples/datakeys.go

+9-24
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,16 @@ import (
2323
"github.com/ovh/okms-sdk-go/types"
2424
)
2525

26-
func dataKeyEncryptDecrypt(ctx context.Context, kmsClient okms.Client) {
26+
func dataKeyEncryptDecrypt(ctx context.Context, kmsClient *okms.Client) {
2727
// Create a new AES 256 key
28-
respAes, err := kmsClient.CreateImportServiceKey(ctx, nil, types.CreateImportServiceKeyRequest{
29-
Name: "AES key example",
30-
Type: ptrTo(types.Oct),
31-
Size: ptrTo(types.N256),
32-
Operations: ptrTo([]types.CryptographicUsages{types.Encrypt, types.Decrypt, types.WrapKey, types.UnwrapKey}),
33-
})
28+
respAes, err := kmsClient.GenerateSymmetricKey(ctx, types.N256, "AES key example", "", types.WrapKey, types.UnwrapKey)
3429
if err != nil {
3530
panic(err)
3631
}
3732

3833
data := "Hello World !!!" // Data to encrypt
3934

40-
dkProvider := okms.NewDataKeyProvider(kmsClient, respAes.Id)
35+
dkProvider := kmsClient.DataKeys(respAes.Id)
4136

4237
// ENCRYPTION
4338

@@ -108,19 +103,14 @@ func dataKeyEncryptDecrypt(ctx context.Context, kmsClient okms.Client) {
108103
fmt.Println("Decrypted:", string(plainData))
109104
}
110105

111-
func dataKeyEncryptStream(ctx context.Context, kmsClient okms.Client) {
106+
func dataKeyEncryptStream(ctx context.Context, kmsClient *okms.Client) {
112107
// Create a new AES 256 key
113-
respAes, err := kmsClient.CreateImportServiceKey(ctx, nil, types.CreateImportServiceKeyRequest{
114-
Name: "AES key example",
115-
Type: ptrTo(types.Oct),
116-
Size: ptrTo(types.N256),
117-
Operations: ptrTo([]types.CryptographicUsages{types.Encrypt, types.Decrypt, types.WrapKey, types.UnwrapKey}),
118-
})
108+
respAes, err := kmsClient.GenerateSymmetricKey(ctx, types.N256, "AES key example", "", types.WrapKey, types.UnwrapKey)
119109
if err != nil {
120110
panic(err)
121111
}
122112

123-
dkProvider := okms.NewDataKeyProvider(kmsClient, respAes.Id)
113+
dkProvider := kmsClient.DataKeys(respAes.Id)
124114

125115
sourceFile, err := os.Open("10GB_Plain_File.txt")
126116
if err != nil {
@@ -146,19 +136,14 @@ func dataKeyEncryptStream(ctx context.Context, kmsClient okms.Client) {
146136
}
147137
}
148138

149-
func dataKeyDecryptStream(ctx context.Context, kmsClient okms.Client) {
139+
func dataKeyDecryptStream(ctx context.Context, kmsClient *okms.Client) {
150140
// Create a new AES 256 key
151-
respAes, err := kmsClient.CreateImportServiceKey(ctx, nil, types.CreateImportServiceKeyRequest{
152-
Name: "AES key example",
153-
Type: ptrTo(types.Oct),
154-
Size: ptrTo(types.N256),
155-
Operations: ptrTo([]types.CryptographicUsages{types.Encrypt, types.Decrypt, types.WrapKey, types.UnwrapKey}),
156-
})
141+
respAes, err := kmsClient.GenerateSymmetricKey(ctx, types.N256, "AES key example", "", types.WrapKey, types.UnwrapKey)
157142
if err != nil {
158143
panic(err)
159144
}
160145

161-
dkProvider := okms.NewDataKeyProvider(kmsClient, respAes.Id)
146+
dkProvider := kmsClient.DataKeys(respAes.Id)
162147

163148
sourceFile, err := os.Create("Encrypted_File.bin")
164149
if err != nil {

examples/encrypt_decrypt.go

+2-7
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,9 @@ import (
1717
"github.com/ovh/okms-sdk-go/types"
1818
)
1919

20-
func encryptDecrypt(ctx context.Context, kmsClient okms.Client) {
20+
func encryptDecrypt(ctx context.Context, kmsClient *okms.Client) {
2121
// Create a new AES 256 key
22-
respAes, err := kmsClient.CreateImportServiceKey(ctx, nil, types.CreateImportServiceKeyRequest{
23-
Name: "AES key example",
24-
Type: ptrTo(types.Oct),
25-
Size: ptrTo(types.N256),
26-
Operations: ptrTo([]types.CryptographicUsages{types.Encrypt, types.Decrypt, types.WrapKey, types.UnwrapKey}),
27-
})
22+
respAes, err := kmsClient.GenerateSymmetricKey(ctx, types.N256, "AES key example", "", types.Encrypt, types.Decrypt)
2823
if err != nil {
2924
panic(err)
3025
}

examples/generate_keys.go

+4-19
Original file line numberDiff line numberDiff line change
@@ -17,38 +17,23 @@ import (
1717
"github.com/ovh/okms-sdk-go/types"
1818
)
1919

20-
func generateKeys(ctx context.Context, kmsClient okms.Client) {
20+
func generateKeys(ctx context.Context, kmsClient *okms.Client) {
2121
// Create a new AES 256 key
22-
respAes, err := kmsClient.CreateImportServiceKey(ctx, nil, types.CreateImportServiceKeyRequest{
23-
Name: "AES key example",
24-
Type: ptrTo(types.Oct),
25-
Size: ptrTo(types.N256),
26-
Operations: ptrTo([]types.CryptographicUsages{types.Encrypt, types.Decrypt, types.WrapKey, types.UnwrapKey}),
27-
})
22+
respAes, err := kmsClient.GenerateSymmetricKey(ctx, types.N256, "AES key example", "", types.Encrypt, types.Decrypt, types.WrapKey, types.UnwrapKey)
2823
if err != nil {
2924
panic(err)
3025
}
3126
fmt.Println("AES KEY:", respAes.Id)
3227

3328
// Create a new RSA 2048 key-pair
34-
respRSA, err := kmsClient.CreateImportServiceKey(ctx, nil, types.CreateImportServiceKeyRequest{
35-
Name: "RSA key-pair example",
36-
Type: ptrTo(types.RSA),
37-
Size: ptrTo(types.N2048),
38-
Operations: ptrTo([]types.CryptographicUsages{types.Sign, types.Verify}),
39-
})
29+
respRSA, err := kmsClient.GenerateRSAKeyPair(ctx, types.N2048, "RSA key-pair example", "", types.Sign, types.Verify)
4030
if err != nil {
4131
panic(err)
4232
}
4333
fmt.Println("RSA KEY:", respRSA.Id)
4434

4535
// Create a new ECDSA P-256 key-pair
46-
respECDSA, err := kmsClient.CreateImportServiceKey(ctx, nil, types.CreateImportServiceKeyRequest{
47-
Name: "ECDSA key-pair example",
48-
Type: ptrTo(types.EC),
49-
Curve: ptrTo(types.P256),
50-
Operations: ptrTo([]types.CryptographicUsages{types.Sign, types.Verify}),
51-
})
36+
respECDSA, err := kmsClient.GenerateECKeyPair(ctx, types.P256, "ECDSA key-pair example", "", types.Sign, types.Verify)
5237
if err != nil {
5338
panic(err)
5439
}

examples/list_get.go

+3-8
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
"github.com/ovh/okms-sdk-go/types"
1818
)
1919

20-
func listKeys(ctx context.Context, kmsClient okms.Client) {
20+
func listKeys(ctx context.Context, kmsClient *okms.Client) {
2121
it := kmsClient.ListAllServiceKeys(nil, nil)
2222
for it.Next(ctx) {
2323
key, err := it.Value()
@@ -36,14 +36,9 @@ func listKeys(ctx context.Context, kmsClient okms.Client) {
3636
}
3737
}
3838

39-
func getKey(ctx context.Context, kmsClient okms.Client) {
39+
func getKey(ctx context.Context, kmsClient *okms.Client) {
4040
// Create a new AES 256 key
41-
respAes, err := kmsClient.CreateImportServiceKey(ctx, nil, types.CreateImportServiceKeyRequest{
42-
Name: "AES key example",
43-
Type: ptrTo(types.Oct),
44-
Size: ptrTo(types.N256),
45-
Operations: ptrTo([]types.CryptographicUsages{types.Encrypt, types.Decrypt, types.WrapKey, types.UnwrapKey}),
46-
})
41+
respAes, err := kmsClient.GenerateSymmetricKey(ctx, types.N256, "AES key example", "", types.Encrypt, types.Decrypt, types.WrapKey, types.UnwrapKey)
4742
if err != nil {
4843
panic(err)
4944
}

0 commit comments

Comments
 (0)