Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions plugins/poetry/api_token.go
Original file line number Diff line number Diff line change
@@ -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")),
),
}
}
123 changes: 123 additions & 0 deletions plugins/poetry/api_token_test.go
Original file line number Diff line number Diff line change
@@ -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",
},
},
},
})
}
77 changes: 77 additions & 0 deletions plugins/poetry/importer.go
Original file line number Diff line number Diff line change
@@ -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),
})
}
})
}
22 changes: 22 additions & 0 deletions plugins/poetry/plugin.go
Original file line number Diff line number Diff line change
@@ -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(),
},
}
}
26 changes: 26 additions & 0 deletions plugins/poetry/poetry.go
Original file line number Diff line number Diff line change
@@ -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(),
},
},
}
}
53 changes: 53 additions & 0 deletions plugins/poetry/poetry_test.go
Original file line number Diff line number Diff line change
@@ -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,
},
})
}
42 changes: 42 additions & 0 deletions plugins/poetry/provisioner.go
Original file line number Diff line number Diff line change
@@ -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, "-", "_")))
}
Comment thread
acourtneybrown marked this conversation as resolved.
Resolved

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.
}
Loading