From 84fc6c4bbc955b55d273d92a85c07850080d79df Mon Sep 17 00:00:00 2001 From: Simon Walker Date: Wed, 14 Feb 2024 14:41:36 +0000 Subject: [PATCH] Update localstack plugin to use LOCALSTACK_AUTH_TOKEN --- plugins/localstack/api_key.go | 3 +- plugins/localstack/auth_token.go | 37 ++++++++++++++++++++++++ plugins/localstack/auth_token_test.go | 41 +++++++++++++++++++++++++++ plugins/localstack/localstack.go | 2 +- plugins/localstack/plugin.go | 5 +++- 5 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 plugins/localstack/auth_token.go create mode 100644 plugins/localstack/auth_token_test.go diff --git a/plugins/localstack/api_key.go b/plugins/localstack/api_key.go index 5b168a054..c37b8bfff 100644 --- a/plugins/localstack/api_key.go +++ b/plugins/localstack/api_key.go @@ -36,5 +36,6 @@ func APIKey() schema.CredentialType { } var defaultEnvVarMapping = map[string]sdk.FieldName{ - "LOCALSTACK_API_KEY": fieldname.APIKey, + "LOCALSTACK_API_KEY": fieldname.APIKey, + "LOCALSTACK_AUTH_TOKEN": fieldname.AuthToken, } diff --git a/plugins/localstack/auth_token.go b/plugins/localstack/auth_token.go new file mode 100644 index 000000000..285da9060 --- /dev/null +++ b/plugins/localstack/auth_token.go @@ -0,0 +1,37 @@ +package localstack + +import ( + "github.com/1Password/shell-plugins/sdk" + "github.com/1Password/shell-plugins/sdk/importer" + "github.com/1Password/shell-plugins/sdk/provision" + "github.com/1Password/shell-plugins/sdk/schema" + "github.com/1Password/shell-plugins/sdk/schema/credname" + "github.com/1Password/shell-plugins/sdk/schema/fieldname" +) + +func AuthToken() schema.CredentialType { + return schema.CredentialType{ + Name: credname.AuthToken, + DocsURL: sdk.URL("https://docs.localstack.cloud/getting-started/auth-token/"), + ManagementURL: sdk.URL("https://app.localstack.cloud/workspace/auth-token"), + Fields: []schema.CredentialField{ + { + Name: fieldname.AuthToken, + MarkdownDescription: "Auth token used to authenticate to LocalStack.", + Secret: true, + Composition: &schema.ValueComposition{ + Length: 39, + Charset: schema.Charset{ + Uppercase: true, + Lowercase: true, + Digits: true, + Specific: []rune{'-'}, + }, + }, + }, + }, + DefaultProvisioner: provision.EnvVars(defaultEnvVarMapping), + Importer: importer.TryAll( + importer.TryEnvVarPair(defaultEnvVarMapping), + )} +} diff --git a/plugins/localstack/auth_token_test.go b/plugins/localstack/auth_token_test.go new file mode 100644 index 000000000..c2ebcc57e --- /dev/null +++ b/plugins/localstack/auth_token_test.go @@ -0,0 +1,41 @@ +package localstack + +import ( + "testing" + + "github.com/1Password/shell-plugins/sdk" + "github.com/1Password/shell-plugins/sdk/plugintest" + "github.com/1Password/shell-plugins/sdk/schema/fieldname" +) + +func TestAuthTokenProvisioner(t *testing.T) { + plugintest.TestProvisioner(t, AuthToken().DefaultProvisioner, map[string]plugintest.ProvisionCase{ + "default": { + ItemFields: map[sdk.FieldName]string{ + fieldname.AuthToken: "ls-02b523ae-52f2-4905-b46a-0a7d7c2947aa", + }, + ExpectedOutput: sdk.ProvisionOutput{ + Environment: map[string]string{ + "LOCALSTACK_AUTH_TOKEN": "ls-02b523ae-52f2-4905-b46a-0a7d7c2947aa", + }, + }, + }, + }) +} + +func TestAuthTokenImporter(t *testing.T) { + plugintest.TestImporter(t, AuthToken().Importer, map[string]plugintest.ImportCase{ + "environment": { + Environment: map[string]string{ + "LOCALSTACK_AUTH_TOKEN": "ls-02b523ae-52f2-4905-b46a-0a7d7c2947aa", + }, + ExpectedCandidates: []sdk.ImportCandidate{ + { + Fields: map[sdk.FieldName]string{ + fieldname.AuthToken: "ls-02b523ae-52f2-4905-b46a-0a7d7c2947aa", + }, + }, + }, + }, + }) +} diff --git a/plugins/localstack/localstack.go b/plugins/localstack/localstack.go index 07337a253..4a1dad63a 100644 --- a/plugins/localstack/localstack.go +++ b/plugins/localstack/localstack.go @@ -18,7 +18,7 @@ func LocalStackCLI() schema.Executable { ), Uses: []schema.CredentialUsage{ { - Name: credname.APIKey, + Name: credname.AuthToken, }, }, } diff --git a/plugins/localstack/plugin.go b/plugins/localstack/plugin.go index d588dff29..233f7dc7b 100644 --- a/plugins/localstack/plugin.go +++ b/plugins/localstack/plugin.go @@ -12,8 +12,11 @@ func New() schema.Plugin { Name: "LocalStack", Homepage: sdk.URL("https://localstack.cloud"), }, + // TODO: LocalStack accepts both auth token and api key. When multiple + // credentials types are supported, update this list to include both + // options. Credentials: []schema.CredentialType{ - APIKey(), + AuthToken(), }, Executables: []schema.Executable{ LocalStackCLI(),