-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathkms.go
More file actions
177 lines (130 loc) · 4.33 KB
/
kms.go
File metadata and controls
177 lines (130 loc) · 4.33 KB
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
package infisical
import (
"encoding/base64"
api "github.com/infisical/go-sdk/packages/api/kms"
)
// Options
type KmsEncryptDataOptions = api.KmsEncryptDataV1Request
type KmsDecryptDataOptions = api.KmsDecryptDataV1Request
type KmsSignDataOptions = api.KmsSignDataV1Request
type KmsVerifyDataOptions = api.KmsVerifyDataV1Request
type KmsListSigningAlgorithmsOptions = api.KmsListSigningAlgorithmsV1Request
type KmsGetPublicKeyOptions = api.KmsGetPublicKeyV1Request
type KmsCreateKeyOptions = api.KmsCreateKeyV1Request
type KmsDeleteKeyOptions = api.KmsDeleteKeyV1Request
type KmsGetKeyByNameOptions = api.KmsGetKeyByNameV1Request
// Results
type KmsVerifyDataResult = api.KmsVerifyDataV1Response
type KmsSignDataResult = api.KmsSignDataV1Response
type KmsCreateKeyResult = api.KmsKey
type KmsDeleteKeyResult = api.KmsKey
type KmsGetKeyResult = api.KmsKey
type KmsKeysInterface interface {
Create(options KmsCreateKeyOptions) (KmsCreateKeyResult, error)
Delete(options KmsDeleteKeyOptions) (KmsDeleteKeyResult, error)
GetByName(options KmsGetKeyByNameOptions) (KmsGetKeyResult, error)
}
type KmsSigningInterface interface {
SignData(options KmsSignDataOptions) ([]byte, error)
VerifyData(options KmsVerifyDataOptions) (KmsVerifyDataResult, error)
ListSigningAlgorithms(options KmsListSigningAlgorithmsOptions) ([]string, error)
GetPublicKey(options KmsGetPublicKeyOptions) (string, error)
}
type KmsInterface interface {
EncryptData(options KmsEncryptDataOptions) (string, error)
DecryptData(options KmsDecryptDataOptions) (string, error)
Keys() KmsKeysInterface
Signing() KmsSigningInterface
}
type Kms struct {
client *InfisicalClient
keys *KmsKeys
signing *KmsSigning
}
type KmsKeys struct {
client *InfisicalClient
}
type KmsSigning struct {
client *InfisicalClient
}
func (k *KmsKeys) Create(options KmsCreateKeyOptions) (KmsCreateKeyResult, error) {
res, err := api.CallKmsCreateKeyV1(k.client.httpClient, options)
if err != nil {
return KmsCreateKeyResult{}, err
}
return res.Key, nil
}
func (k *KmsKeys) Delete(options KmsDeleteKeyOptions) (KmsDeleteKeyResult, error) {
res, err := api.CallKmsDeleteKeyV1(k.client.httpClient, options)
if err != nil {
return KmsDeleteKeyResult{}, err
}
return res.Key, nil
}
func (k *KmsKeys) GetByName(options KmsGetKeyByNameOptions) (KmsGetKeyResult, error) {
res, err := api.CallKmsGetKeyByNameV1(k.client.httpClient, options)
if err != nil {
return KmsGetKeyResult{}, err
}
return res.Key, nil
}
func (k *KmsSigning) SignData(options KmsSignDataOptions) ([]byte, error) {
res, err := api.CallKmsSignDataV1(k.client.httpClient, options)
if err != nil {
return nil, err
}
return base64.StdEncoding.DecodeString(res.Signature)
}
func (k *KmsSigning) VerifyData(options KmsVerifyDataOptions) (KmsVerifyDataResult, error) {
res, err := api.CallKmsVerifyDataV1(k.client.httpClient, options)
if err != nil {
return KmsVerifyDataResult{}, err
}
return res, nil
}
func (k *KmsSigning) ListSigningAlgorithms(options KmsListSigningAlgorithmsOptions) ([]string, error) {
res, err := api.CallKmsGetSigningAlgorithmsV1(k.client.httpClient, options)
if err != nil {
return []string{}, err
}
return res.SigningAlgorithms, nil
}
func (k *KmsSigning) GetPublicKey(options KmsGetPublicKeyOptions) (string, error) {
res, err := api.CallKmsGetPublicKeyV1(k.client.httpClient, options)
if err != nil {
return "", err
}
return res.PublicKey, nil
}
func (f *Kms) EncryptData(options KmsEncryptDataOptions) (string, error) {
options.Plaintext = base64.StdEncoding.EncodeToString([]byte(options.Plaintext))
res, err := api.CallKmsEncryptDataV1(f.client.httpClient, options)
if err != nil {
return "", err
}
return res.Ciphertext, nil
}
func (f *Kms) DecryptData(options KmsDecryptDataOptions) (string, error) {
res, err := api.CallKmsDecryptDataV1(f.client.httpClient, options)
if err != nil {
return "", err
}
decodedPlaintext, err := base64.StdEncoding.DecodeString(res.Plaintext)
if err != nil {
return "", err
}
return string(decodedPlaintext), nil
}
func (f *Kms) Keys() KmsKeysInterface {
return &KmsKeys{client: f.client}
}
func (f *Kms) Signing() KmsSigningInterface {
return &KmsSigning{client: f.client}
}
func NewKms(client *InfisicalClient) KmsInterface {
return &Kms{
client: client,
keys: &KmsKeys{client: client},
signing: &KmsSigning{client: client},
}
}