diff --git a/plugins/poetry/api_token.go b/plugins/poetry/api_token.go new file mode 100644 index 00000000..e9e5477c --- /dev/null +++ b/plugins/poetry/api_token.go @@ -0,0 +1,43 @@ +package poetry + +import ( + "github.com/1Password/shell-plugins/sdk" + "github.com/1Password/shell-plugins/sdk/importer" + "github.com/1Password/shell-plugins/sdk/schema" + "github.com/1Password/shell-plugins/sdk/schema/credname" + "github.com/1Password/shell-plugins/sdk/schema/fieldname" +) + +func APIToken() schema.CredentialType { + return schema.CredentialType{ + Name: credname.APIToken, + DocsURL: sdk.URL("https://pypi.org/help/#apitoken"), + ManagementURL: sdk.URL("https://pypi.org/manage/account/#api-tokens"), + Fields: []schema.CredentialField{ + { + Name: fieldname.Token, + MarkdownDescription: "API token used to authenticate to PyPI.", + Secret: true, + Composition: &schema.ValueComposition{ + Prefix: "pypi-", + Charset: schema.Charset{ + Uppercase: true, + Lowercase: true, + Digits: true, + Specific: []rune{'-', '_'}, + }, + }, + }, + { + Name: fieldname.Repository, + MarkdownDescription: "Poetry repository name", + }, + }, + DefaultProvisioner: PoetryProvisioner(), + Importer: importer.TryAll( + TryPoetryEnvVars(), + importer.LinuxOnly(TryPoetryConfigFile("~/.config/pypoetry/auth.toml")), + importer.MacOnly(TryPoetryConfigFile("~/Library/Application Support/pypoetry/auth.toml")), + ), + } +} diff --git a/plugins/poetry/api_token_test.go b/plugins/poetry/api_token_test.go new file mode 100644 index 00000000..8061605a --- /dev/null +++ b/plugins/poetry/api_token_test.go @@ -0,0 +1,123 @@ +package poetry + +import ( + "testing" + + "github.com/1Password/shell-plugins/sdk" + "github.com/1Password/shell-plugins/sdk/plugintest" + "github.com/1Password/shell-plugins/sdk/schema/fieldname" +) + +func TestAPITokenImporter(t *testing.T) { + plugintest.TestImporter(t, APIToken().Importer, map[string]plugintest.ImportCase{ + "POETRY_PYPI_TOKEN_PYPI environment variable": { + Environment: map[string]string{ + "POETRY_PYPI_TOKEN_PYPI": "pypi-AgEIcHlwaS5vcmc", + }, + ExpectedCandidates: []sdk.ImportCandidate{ + { + Fields: map[sdk.FieldName]string{ + fieldname.Token: "pypi-AgEIcHlwaS5vcmc", + fieldname.Repository: "pypi", + }, + NameHint: "pypi", + }, + }, + }, + "POETRY_PYPI_TOKEN_TESTPYPI environment variable": { + Environment: map[string]string{ + "POETRY_PYPI_TOKEN_TESTPYPI": "pypi-flit123abc", + }, + ExpectedCandidates: []sdk.ImportCandidate{ + { + Fields: map[sdk.FieldName]string{ + fieldname.Token: "pypi-flit123abc", + fieldname.Repository: "testpypi", + }, + NameHint: "testpypi", + }, + }, + }, + "environment variable with dash": { + Environment: map[string]string{ + "POETRY_PYPI_TOKEN_MY_REPO": "pypi-flit123abc", + }, + ExpectedCandidates: []sdk.ImportCandidate{ + { + Fields: map[sdk.FieldName]string{ + fieldname.Token: "pypi-flit123abc", + fieldname.Repository: "my_repo", + }, + NameHint: "my_repo", + }, + { + Fields: map[sdk.FieldName]string{ + fieldname.Token: "pypi-flit123abc", + fieldname.Repository: "my-repo", + }, + NameHint: "my-repo", + }, + }, + }, + "config file macos": { + OS: "darwin", + Files: map[string]string{ + "~/Library/Application Support/pypoetry/auth.toml": plugintest.LoadFixture(t, "auth.toml"), + }, + ExpectedCandidates: []sdk.ImportCandidate{ + { + Fields: map[sdk.FieldName]string{ + fieldname.Token: "pypi-fake_token_value", + fieldname.Repository: "pypi", + }, + NameHint: "pypi", + }, + { + Fields: map[sdk.FieldName]string{ + fieldname.Token: "pypi-another_fake_token_value", + fieldname.Repository: "testpypi", + }, + NameHint: "testpypi", + }, + }, + }, + "config file linux": { + OS: "linux", + Files: map[string]string{ + "~/.config/pypoetry/auth.toml": plugintest.LoadFixture(t, "auth.toml"), + }, + ExpectedCandidates: []sdk.ImportCandidate{ + { + Fields: map[sdk.FieldName]string{ + fieldname.Token: "pypi-fake_token_value", + fieldname.Repository: "pypi", + }, + NameHint: "pypi", + }, + { + Fields: map[sdk.FieldName]string{ + fieldname.Token: "pypi-another_fake_token_value", + fieldname.Repository: "testpypi", + }, + NameHint: "testpypi", + }, + }, + }, + }) +} + +func TestAPITokenProvisioner(t *testing.T) { + plugintest.TestProvisioner(t, APIToken().DefaultProvisioner, map[string]plugintest.ProvisionCase{ + "default provisioner sets Poetry env vars": { + ItemFields: map[sdk.FieldName]string{ + fieldname.Token: "pypi-AgEIcHlwaS5vcmc", + fieldname.Repository: "mysecretpypi", + }, + ExpectedOutput: sdk.ProvisionOutput{ + Environment: map[string]string{ + "POETRY_PYPI_TOKEN_MYSECRETPYPI": "pypi-AgEIcHlwaS5vcmc", + }, + }, + }, + }) +} diff --git a/plugins/poetry/importer.go b/plugins/poetry/importer.go new file mode 100644 index 00000000..7658cc2e --- /dev/null +++ b/plugins/poetry/importer.go @@ -0,0 +1,77 @@ +package poetry + +import ( + "context" + "os" + "strings" + + "github.com/1Password/shell-plugins/sdk" + "github.com/1Password/shell-plugins/sdk/importer" + "github.com/1Password/shell-plugins/sdk/schema/fieldname" +) + +func TryPoetryEnvVars() sdk.Importer { + return func(ctx context.Context, in sdk.ImportInput, out *sdk.ImportOutput) { + prefix := "POETRY_PYPI_TOKEN_" + + for _, envVar := range os.Environ() { + // Split into key and value + key := envVar + value := "" + if before, after, ok := strings.Cut(envVar, "="); ok { + key = before + value = after + } + + if after, ok := strings.CutPrefix(key, prefix); ok { + repo := strings.ToLower(after) + attempt := out.NewAttempt(importer.SourceEnvVars(key)) + attempt.AddCandidate(sdk.ImportCandidate{ + Fields: map[sdk.FieldName]string{ + fieldname.Token: value, + fieldname.Repository: repo, + }, + NameHint: importer.SanitizeNameHint(repo), + }) + repoWithDashes := strings.ReplaceAll(repo, "_", "-") + if repoWithDashes != repo { + attempt.AddCandidate(sdk.ImportCandidate{ + Fields: map[sdk.FieldName]string{ + fieldname.Token: value, + fieldname.Repository: repoWithDashes, + }, + NameHint: importer.SanitizeNameHint(repoWithDashes), + }) + } + } + } + } +} + +// TryPoetryConfigFile returns an importer that scans the poetry CLI configuration file for API tokens. +func TryPoetryConfigFile(path string) sdk.Importer { + return importer.TryFile(path, func(ctx context.Context, contents importer.FileContents, in sdk.ImportInput, out *sdk.ImportAttempt) { + var config map[string]interface{} + if err := contents.ToTOML(&config); err != nil { + out.AddError(err) + return + } + + pypiToken := config["pypi-token"].(map[string]interface{}) + + for repo, token := range pypiToken { + tokenStr := token.(string) + if tokenStr == "" { + continue + } + + out.AddCandidate(sdk.ImportCandidate{ + Fields: map[sdk.FieldName]string{ + fieldname.Token: tokenStr, + fieldname.Repository: repo, + }, + NameHint: importer.SanitizeNameHint(repo), + }) + } + }) +} diff --git a/plugins/poetry/plugin.go b/plugins/poetry/plugin.go new file mode 100644 index 00000000..e15b55f9 --- /dev/null +++ b/plugins/poetry/plugin.go @@ -0,0 +1,22 @@ +package poetry + +import ( + "github.com/1Password/shell-plugins/sdk" + "github.com/1Password/shell-plugins/sdk/schema" +) + +func New() schema.Plugin { + return schema.Plugin{ + Name: "poetry", + Platform: schema.PlatformInfo{ + Name: "Poetry", + Homepage: sdk.URL("https://python-poetry.org"), + }, + Credentials: []schema.CredentialType{ + APIToken(), + }, + Executables: []schema.Executable{ + PoetryCLI(), + }, + } +} diff --git a/plugins/poetry/poetry.go b/plugins/poetry/poetry.go new file mode 100644 index 00000000..63bfcc4e --- /dev/null +++ b/plugins/poetry/poetry.go @@ -0,0 +1,26 @@ +package poetry + +import ( + "github.com/1Password/shell-plugins/sdk" + "github.com/1Password/shell-plugins/sdk/needsauth" + "github.com/1Password/shell-plugins/sdk/schema" + "github.com/1Password/shell-plugins/sdk/schema/credname" +) + +func PoetryCLI() schema.Executable { + return schema.Executable{ + Name: "Poetry", + Runs: []string{"poetry"}, + DocsURL: sdk.URL("https://python-poetry.org/docs/"), + NeedsAuth: needsauth.IfAll( + needsauth.NotForHelpOrVersion(), + needsauth.ForCommand("publish"), + ), + Uses: []schema.CredentialUsage{ + { + Name: credname.APIToken, + Provisioner: PoetryProvisioner(), + }, + }, + } +} diff --git a/plugins/poetry/poetry_test.go b/plugins/poetry/poetry_test.go new file mode 100644 index 00000000..efa520a1 --- /dev/null +++ b/plugins/poetry/poetry_test.go @@ -0,0 +1,53 @@ +package poetry + +import ( + "testing" + + "github.com/1Password/shell-plugins/sdk" + "github.com/1Password/shell-plugins/sdk/plugintest" + "github.com/1Password/shell-plugins/sdk/schema/fieldname" +) + +func TestPoetryCLIProvisioner(t *testing.T) { + plugintest.TestProvisioner(t, PoetryCLI().Uses[0].Provisioner, map[string]plugintest.ProvisionCase{ + "sets Poetry env vars": { + ItemFields: map[sdk.FieldName]string{ + fieldname.Token: "pypi-AgEIcHlwaS5vcmc", + fieldname.Repository: "pypi", + }, + ExpectedOutput: sdk.ProvisionOutput{ + Environment: map[string]string{ + "POETRY_PYPI_TOKEN_PYPI": "pypi-AgEIcHlwaS5vcmc", + }, + }, + }, + "repository name with a dash": { + ItemFields: map[sdk.FieldName]string{ + fieldname.Token: "pypi-AgEIcHlwaS5vcmc", + fieldname.Repository: "my-repo", + }, + ExpectedOutput: sdk.ProvisionOutput{ + Environment: map[string]string{ + "POETRY_PYPI_TOKEN_MY_REPO": "pypi-AgEIcHlwaS5vcmc", + }, + }, + }, + }) +} + +func TestPoetryCLINeedsAuth(t *testing.T) { + plugintest.TestNeedsAuth(t, PoetryCLI().NeedsAuth, map[string]plugintest.NeedsAuthCase{ + "requires auth for publish": { + Args: []string{"publish"}, + ExpectedNeedsAuth: true, + }, + "skips auth for help": { + Args: []string{"--help"}, + ExpectedNeedsAuth: false, + }, + "skips auth for version": { + Args: []string{"--version"}, + ExpectedNeedsAuth: false, + }, + }) +} diff --git a/plugins/poetry/provisioner.go b/plugins/poetry/provisioner.go new file mode 100644 index 00000000..be2d43ba --- /dev/null +++ b/plugins/poetry/provisioner.go @@ -0,0 +1,42 @@ +package poetry + +import ( + "context" + "fmt" + "strings" + + "github.com/1Password/shell-plugins/sdk" + "github.com/1Password/shell-plugins/sdk/schema/fieldname" +) + +// poetryTokenEnvVarPrefix is the prefix of the environment variable that the poetry CLI uses to +// provision the API token for a package repository, e.g. "pypi" or "testpypi". +const poetryTokenEnvVarPrefix = "POETRY_PYPI_TOKEN_" + +// poetryTokenEnvVar returns the name of the environment variable that the poetry CLI uses to +// provision the API token for the package repository with the given name, e.g. "pypi" or "testpypi". +func poetryTokenEnvVar(repositoryName string) string { + return fmt.Sprintf("%s%s", poetryTokenEnvVarPrefix, strings.ToUpper(strings.ReplaceAll(repositoryName, "-", "_"))) +} + +type poetryProvisioner struct{} + +// PoetryProvisioner provisions the API token for the supported package repositories via the +// environment variables that the poetry CLI understands. +func PoetryProvisioner() sdk.Provisioner { + return poetryProvisioner{} +} + +func (p poetryProvisioner) Description() string { + return "Provision Poetry API token via POETRY_PYPI_TOKEN_* environment variables" +} + +func (p poetryProvisioner) Provision(ctx context.Context, in sdk.ProvisionInput, out *sdk.ProvisionOutput) { + token := in.ItemFields[fieldname.Token] + repo := in.ItemFields[fieldname.Repository] + out.AddEnvVar(poetryTokenEnvVar(repo), token) +} + +func (p poetryProvisioner) Deprovision(ctx context.Context, in sdk.DeprovisionInput, out *sdk.DeprovisionOutput) { + // Environment variables are automatically cleaned up when the process ends. +} diff --git a/plugins/poetry/test-fixtures/auth.toml b/plugins/poetry/test-fixtures/auth.toml new file mode 100644 index 00000000..7db60c33 --- /dev/null +++ b/plugins/poetry/test-fixtures/auth.toml @@ -0,0 +1,7 @@ +[pypi-token] +pypi = "pypi-fake_token_value" +testpypi = "pypi-another_fake_token_value" + +[http-basic.mypypi] +username = "fake_user" +password = "fake_password" diff --git a/sdk/schema/fieldname/names.go b/sdk/schema/fieldname/names.go index f2256903..81faa10a 100644 --- a/sdk/schema/fieldname/names.go +++ b/sdk/schema/fieldname/names.go @@ -51,6 +51,7 @@ const ( ProjectID = sdk.FieldName("Project ID") Project = sdk.FieldName("Project") Region = sdk.FieldName("Region") + Repository = sdk.FieldName("Repository") SSOAccountID = sdk.FieldName("SSO Account ID") SSORegion = sdk.FieldName("SSO Region") SSORoleName = sdk.FieldName("SSO Role Name")