-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from Infisical/feat/add-kms-operations
feat: add kms operations
- Loading branch information
Showing
5 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package api | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/go-resty/resty/v2" | ||
"github.com/infisical/go-sdk/packages/errors" | ||
) | ||
|
||
const callKmsDecryptDataOperationV1 = "CallKmsDecryptDataV1" | ||
|
||
func CallKmsDecryptDataV1(httpClient *resty.Client, request KmsDecryptDataV1Request) (KmsDecryptDataV1Response, error) { | ||
kmsDecryptDataResponse := KmsDecryptDataV1Response{} | ||
|
||
res, err := httpClient.R(). | ||
SetResult(&kmsDecryptDataResponse). | ||
SetBody(request). | ||
Post(fmt.Sprintf("/v1/kms/keys/%s/decrypt", request.KeyId)) | ||
|
||
if err != nil { | ||
return KmsDecryptDataV1Response{}, errors.NewRequestError(callKmsDecryptDataOperationV1, err) | ||
} | ||
|
||
if res.IsError() { | ||
return KmsDecryptDataV1Response{}, errors.NewAPIErrorWithResponse(callKmsDecryptDataOperationV1, res) | ||
} | ||
|
||
return kmsDecryptDataResponse, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package api | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/go-resty/resty/v2" | ||
"github.com/infisical/go-sdk/packages/errors" | ||
) | ||
|
||
const callKmsEncryptDataOperationV1 = "CallKmsEncryptDataV1" | ||
|
||
func CallKmsEncryptDataV1(httpClient *resty.Client, request KmsEncryptDataV1Request) (KmsEncryptDataV1Response, error) { | ||
kmsEncryptDataResponse := KmsEncryptDataV1Response{} | ||
|
||
res, err := httpClient.R(). | ||
SetResult(&kmsEncryptDataResponse). | ||
SetBody(request). | ||
Post(fmt.Sprintf("/v1/kms/keys/%s/encrypt", request.KeyId)) | ||
|
||
if err != nil { | ||
return KmsEncryptDataV1Response{}, errors.NewRequestError(callKmsEncryptDataOperationV1, err) | ||
} | ||
|
||
if res.IsError() { | ||
return KmsEncryptDataV1Response{}, errors.NewAPIErrorWithResponse(callKmsEncryptDataOperationV1, res) | ||
} | ||
|
||
return kmsEncryptDataResponse, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package api | ||
|
||
type KmsEncryptDataV1Request struct { | ||
KeyId string | ||
Plaintext string `json:"plaintext"` | ||
} | ||
|
||
type KmsEncryptDataV1Response struct { | ||
Ciphertext string `json:"ciphertext"` | ||
} | ||
|
||
type KmsDecryptDataV1Request struct { | ||
KeyId string | ||
Ciphertext string `json:"ciphertext"` | ||
} | ||
|
||
type KmsDecryptDataV1Response struct { | ||
Plaintext string `json:"plaintext"` | ||
} |