From d44a749ba7d89e729a7a11366af6c3ac67cfd943 Mon Sep 17 00:00:00 2001 From: Floris van der Grinten Date: Tue, 8 Nov 2022 13:47:08 +0100 Subject: [PATCH] Add built-in support for XDG_CONFIG_HOME imports --- plugins/circleci/personal_api_token.go | 6 +++--- plugins/gitlab/personal_access_token.go | 6 +++--- plugins/heroku/api_key.go | 7 +++---- plugins/okta/api_token.go | 4 ++-- plugins/snyk/api_token.go | 6 +++--- plugins/vault/auth_token.go | 4 ++-- sdk/importer.go | 3 ++- sdk/importer/file_importer.go | 6 ++++-- 8 files changed, 22 insertions(+), 20 deletions(-) diff --git a/plugins/circleci/personal_api_token.go b/plugins/circleci/personal_api_token.go index 4be271219..32c055885 100644 --- a/plugins/circleci/personal_api_token.go +++ b/plugins/circleci/personal_api_token.go @@ -37,7 +37,7 @@ func PersonalAPIToken() schema.CredentialType { importer.TryEnvVarPair(map[string]string{ fieldname.Token: "CIRCLECI_CLI_TOKEN", }), - TryCircleCIConfigFile(), + TryCircleCIConfigFile("~/.circleci/cli.yml"), ), } } @@ -46,8 +46,8 @@ type Config struct { Token string `yaml:"token"` } -func TryCircleCIConfigFile() sdk.Importer { - return importer.TryFile("~/.circleci/cli.yml", func(ctx context.Context, contents importer.FileContents, in sdk.ImportInput, out *sdk.ImportOutput) { +func TryCircleCIConfigFile(path string) sdk.Importer { + return importer.TryFile(path, func(ctx context.Context, contents importer.FileContents, in sdk.ImportInput, out *sdk.ImportOutput) { var config Config if err := contents.ToYAML(&config); err != nil { out.AddError(err) diff --git a/plugins/gitlab/personal_access_token.go b/plugins/gitlab/personal_access_token.go index 6bfaf4d42..9455522f3 100644 --- a/plugins/gitlab/personal_access_token.go +++ b/plugins/gitlab/personal_access_token.go @@ -43,7 +43,7 @@ func PersonalAccessToken() schema.CredentialType { Provisioner: provision.EnvVars(defaultEnvVarMapping), Importer: importer.TryAll( importer.TryEnvVarPair(defaultEnvVarMapping), - TryGlabConfigFile(), + TryGlabConfigFile("$XDG_CONFIG_HOME/glab-cli/config.yml"), ), } } @@ -54,7 +54,7 @@ var defaultEnvVarMapping = map[string]string{ fieldname.APIHost: "GITLAB_API_HOST", } -func TryGlabConfigFile() sdk.Importer { - // TODO: Try importing token from ~/.config/glab-cli/config.yml +func TryGlabConfigFile(path string) sdk.Importer { + // TODO: Try importing token from $XDG_CONFIG_HOME/glab-cli/config.yml return importer.NoOp() } diff --git a/plugins/heroku/api_key.go b/plugins/heroku/api_key.go index d2fc3f620..695aebcdf 100644 --- a/plugins/heroku/api_key.go +++ b/plugins/heroku/api_key.go @@ -35,14 +35,13 @@ func APIKey() schema.CredentialType { }), Importer: importer.TryAll( importer.TryAllEnvVars(fieldname.APIKey, "HEROKU_API_KEY"), - TryNetrcFile(), + TryNetrcFile("~/.netrc"), ), } } -// TryNetrcFile tries to find Heroku API keys in the ~/.netrc file -func TryNetrcFile() sdk.Importer { - return importer.TryFile("~/.netrc", func(ctx context.Context, contents importer.FileContents, in sdk.ImportInput, out *sdk.ImportOutput) { +func TryNetrcFile(path string) sdk.Importer { + return importer.TryFile(path, func(ctx context.Context, contents importer.FileContents, in sdk.ImportInput, out *sdk.ImportOutput) { // TODO: Iterate over 'machine' entries to look for 'api.heroku.com' or 'git.heroku.com' // Example contents: // diff --git a/plugins/okta/api_token.go b/plugins/okta/api_token.go index ea21eb5f6..7002b2fb0 100644 --- a/plugins/okta/api_token.go +++ b/plugins/okta/api_token.go @@ -36,7 +36,7 @@ func APIToken() schema.CredentialType { Provisioner: provision.EnvVars(defaultEnvVarMapping), Importer: importer.TryAll( importer.TryEnvVarPair(defaultEnvVarMapping), - TryOktaConfigFile(), + TryOktaConfigFile("~/.okta/okta.yaml"), ), } } @@ -48,7 +48,7 @@ var defaultEnvVarMapping = map[string]string{ const FieldNameOrgURL = "Org URL" -func TryOktaConfigFile() sdk.Importer { +func TryOktaConfigFile(path string) sdk.Importer { // TODO: Try importing from ~/.okta/okta.yaml return importer.NoOp() } diff --git a/plugins/snyk/api_token.go b/plugins/snyk/api_token.go index aa81d9094..7d927aa70 100644 --- a/plugins/snyk/api_token.go +++ b/plugins/snyk/api_token.go @@ -31,7 +31,7 @@ func APIToken() schema.CredentialType { Provisioner: provision.EnvVars(defaultEnvVarMapping), Importer: importer.TryAll( importer.TryEnvVarPair(defaultEnvVarMapping), - TrySnykConfigFile(), + TrySnykConfigFile("$XDG_CONFIG_HOME/configstore/snyk.json"), )} } @@ -39,7 +39,7 @@ var defaultEnvVarMapping = map[string]string{ fieldname.Token: "SNYK_TOKEN", } -func TrySnykConfigFile() sdk.Importer { - // TODO: Import `api` field from ~/.config/configstore/snyk.json +func TrySnykConfigFile(path string) sdk.Importer { + // TODO: Import `api` field from $XDG_CONFIG_HOME/configstore/snyk.json return importer.NoOp() } diff --git a/plugins/vault/auth_token.go b/plugins/vault/auth_token.go index b371f371a..a96dd4003 100644 --- a/plugins/vault/auth_token.go +++ b/plugins/vault/auth_token.go @@ -34,7 +34,7 @@ func AuthToken() schema.CredentialType { Provisioner: provision.EnvVars(defaultEnvVarMapping), Importer: importer.TryAll( importer.TryEnvVarPair(defaultEnvVarMapping), - TryVaultTokenFile(), + TryVaultTokenFile("~/.vault-token"), ), } } @@ -45,7 +45,7 @@ var defaultEnvVarMapping = map[string]string{ fieldname.Namespace: "VAULT_NAMESPACE", } -func TryVaultTokenFile() sdk.Importer { +func TryVaultTokenFile(path string) sdk.Importer { // TODO: Try importing from ~/.vault-token file return importer.NoOp() } diff --git a/sdk/importer.go b/sdk/importer.go index a8909e738..be43e2913 100644 --- a/sdk/importer.go +++ b/sdk/importer.go @@ -60,7 +60,8 @@ type ImportSource struct { } type ImportInput struct { - HomeDir string + HomeDir string + XDGConfigHome string } type ImportOutput struct { diff --git a/sdk/importer/file_importer.go b/sdk/importer/file_importer.go index 4f6d8cde3..752196042 100644 --- a/sdk/importer/file_importer.go +++ b/sdk/importer/file_importer.go @@ -16,8 +16,10 @@ import ( func TryFile(path string, result func(ctx context.Context, contents FileContents, in sdk.ImportInput, out *sdk.ImportOutput)) sdk.Importer { return func(ctx context.Context, in sdk.ImportInput, out *sdk.ImportOutput) { - if strings.HasPrefix(path, "~/") { - path = filepath.Join(in.HomeDir, strings.TrimPrefix(path, "~/")) + if strings.HasPrefix(path, "~") { + path = filepath.Join(in.HomeDir, strings.TrimPrefix(path, "~")) + } else if strings.HasPrefix(path, "$XDG_CONFIG_HOME") { + path = filepath.Join(in.XDGConfigHome, strings.TrimPrefix(path, "$XDG_CONFIG_HOME")) } contents, err := os.ReadFile(path)