-
Notifications
You must be signed in to change notification settings - Fork 5
/
kms.go
49 lines (36 loc) · 1.1 KB
/
kms.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
package infisical
import (
"encoding/base64"
api "github.com/infisical/go-sdk/packages/api/kms"
)
type KmsEncryptDataOptions = api.KmsEncryptDataV1Request
type KmsDecryptDataOptions = api.KmsDecryptDataV1Request
type KmsInterface interface {
EncryptData(options KmsEncryptDataOptions) (string, error)
DecryptData(options KmsDecryptDataOptions) (string, error)
}
type Kms struct {
client *InfisicalClient
}
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 NewKms(client *InfisicalClient) KmsInterface {
return &Kms{client: client}
}