diff --git a/flaps/actions.go b/flaps/actions.go index 19ea783..00388d1 100644 --- a/flaps/actions.go +++ b/flaps/actions.go @@ -27,6 +27,10 @@ const ( machineCordon machineUncordon machineSuspend + secretCreate + secretDelete + secretGenerate + secretsList volumeList volumeCreate volumetUpdate diff --git a/flaps/flaps_secrets.go b/flaps/flaps_secrets.go new file mode 100644 index 0000000..1d387b4 --- /dev/null +++ b/flaps/flaps_secrets.go @@ -0,0 +1,58 @@ +package flaps + +import ( + "context" + "fmt" + "net/http" + + fly "github.com/superfly/fly-go" +) + +func (f *Client) sendRequestSecrets(ctx context.Context, method, endpoint string, in, out interface{}, headers map[string][]string) error { + endpoint = fmt.Sprintf("/apps/%s/secrets%s", f.appName, endpoint) + return f._sendRequest(ctx, method, endpoint, in, out, headers) +} + +func (f *Client) ListSecrets(ctx context.Context) ([]fly.ListSecret, error) { + ctx = contextWithAction(ctx, secretsList) + + out := make([]fly.ListSecret, 0) + if err := f.sendRequestSecrets(ctx, http.MethodGet, "", nil, &out, nil); err != nil { + return nil, fmt.Errorf("failed to list secrets: %w", err) + } + + return out, nil +} + +func (f *Client) CreateSecret(ctx context.Context, sLabel, sType string, in fly.CreateSecretRequest) error { + ctx = contextWithAction(ctx, secretCreate) + + path := fmt.Sprintf("/%s/type/%s", sLabel, sType) + if err := f.sendRequestSecrets(ctx, http.MethodPost, path, in, nil, nil); err != nil { + return fmt.Errorf("failed to create secret: %w", err) + } + + return nil +} + +func (f *Client) GenerateSecret(ctx context.Context, sLabel, sType string) error { + ctx = contextWithAction(ctx, secretGenerate) + + path := fmt.Sprintf("/%s/type/%s/generate", sLabel, sType) + if err := f.sendRequestSecrets(ctx, http.MethodPost, path, nil, nil, nil); err != nil { + return fmt.Errorf("failed to create secret: %w", err) + } + + return nil +} + +func (f *Client) DeleteSecret(ctx context.Context, label string) error { + ctx = contextWithAction(ctx, secretDelete) + + endpoint := fmt.Sprintf("/%s", label) + if err := f.sendRequestSecrets(ctx, http.MethodDelete, endpoint, nil, nil, nil); err != nil { + return fmt.Errorf("failed to delete secret: %w", err) + } + + return nil +} diff --git a/flaps/flapsaction_string.go b/flaps/flapsaction_string.go index 662079e..7d55cae 100644 --- a/flaps/flapsaction_string.go +++ b/flaps/flapsaction_string.go @@ -29,22 +29,26 @@ func _() { _ = x[machineCordon-18] _ = x[machineUncordon-19] _ = x[machineSuspend-20] - _ = x[volumeList-21] - _ = x[volumeCreate-22] - _ = x[volumetUpdate-23] - _ = x[volumeGet-24] - _ = x[volumeSnapshotCreate-25] - _ = x[volumeSnapshotList-26] - _ = x[volumeExtend-27] - _ = x[volumeDelete-28] - _ = x[metadataSet-29] - _ = x[metadataGet-30] - _ = x[metadataDel-31] + _ = x[secretCreate-21] + _ = x[secretDelete-22] + _ = x[secretGenerate-23] + _ = x[secretsList-24] + _ = x[volumeList-25] + _ = x[volumeCreate-26] + _ = x[volumetUpdate-27] + _ = x[volumeGet-28] + _ = x[volumeSnapshotCreate-29] + _ = x[volumeSnapshotList-30] + _ = x[volumeExtend-31] + _ = x[volumeDelete-32] + _ = x[metadataSet-33] + _ = x[metadataGet-34] + _ = x[metadataDel-35] } -const _flapsAction_name = "noneappCreatemachineLaunchmachineUpdatemachineStartmachineWaitmachineStopmachineRestartmachineGetmachineListmachineDestroymachineKillmachineFindLeasemachineAcquireLeasemachineRefreshLeasemachineReleaseLeasemachineExecmachinePsmachineCordonmachineUncordonmachineSuspendvolumeListvolumeCreatevolumetUpdatevolumeGetvolumeSnapshotCreatevolumeSnapshotListvolumeExtendvolumeDeletemetadataSetmetadataGetmetadataDel" +const _flapsAction_name = "noneappCreatemachineLaunchmachineUpdatemachineStartmachineWaitmachineStopmachineRestartmachineGetmachineListmachineDestroymachineKillmachineFindLeasemachineAcquireLeasemachineRefreshLeasemachineReleaseLeasemachineExecmachinePsmachineCordonmachineUncordonmachineSuspendsecretCreatesecretDeletesecretGeneratesecretsListvolumeListvolumeCreatevolumetUpdatevolumeGetvolumeSnapshotCreatevolumeSnapshotListvolumeExtendvolumeDeletemetadataSetmetadataGetmetadataDel" -var _flapsAction_index = [...]uint16{0, 4, 13, 26, 39, 51, 62, 73, 87, 97, 108, 122, 133, 149, 168, 187, 206, 217, 226, 239, 254, 268, 278, 290, 303, 312, 332, 350, 362, 374, 385, 396, 407} +var _flapsAction_index = [...]uint16{0, 4, 13, 26, 39, 51, 62, 73, 87, 97, 108, 122, 133, 149, 168, 187, 206, 217, 226, 239, 254, 268, 280, 292, 306, 317, 327, 339, 352, 361, 381, 399, 411, 423, 434, 445, 456} func (i flapsAction) String() string { if i < 0 || i >= flapsAction(len(_flapsAction_index)-1) { diff --git a/secrets_types.go b/secrets_types.go new file mode 100644 index 0000000..ce42b1c --- /dev/null +++ b/secrets_types.go @@ -0,0 +1,24 @@ +package fly + +const ( + // Secret types + AppSecret = "AppSecret" + VolumeEncryptionKey = "VolumeEncryptionKey" + SECRET_TYPE_KMS_HS256 = "SECRET_TYPE_KMS_HS256" + SECRET_TYPE_KMS_HS384 = "SECRET_TYPE_KMS_HS384" + SECRET_TYPE_KMS_HS512 = "SECRET_TYPE_KMS_HS512" + SECRET_TYPE_KMS_XAES256GCM = "SECRET_TYPE_KMS_XAES256GCM" + SECRET_TYPE_KMS_NACL_AUTH = "SECRET_TYPE_KMS_NACL_AUTH" + SECRET_TYPE_KMS_NACL_BOX = "SECRET_TYPE_KMS_NACL_BOX" + SECRET_TYPE_KMS_NACL_SECRETBOX = "SECRET_TYPE_KMS_NACL_SECRETBOX" + SECRET_TYPE_KMS_NACL_SIGN = "SECRET_TYPE_KMS_NACL_SIGN" +) + +type ListSecret struct { + Label string `json:"label"` + Type string `json:"type"` +} + +type CreateSecretRequest struct { + Value []byte `json:"value,omitempty"` +}