diff --git a/plugins/yugabytedb/database_credentials.go b/plugins/yugabytedb/database_credentials.go index 2f4f2d8c1..3d34bd15b 100644 --- a/plugins/yugabytedb/database_credentials.go +++ b/plugins/yugabytedb/database_credentials.go @@ -1,12 +1,9 @@ package yugabytedb import ( - "context" - "errors" - "strings" - "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" @@ -42,48 +39,15 @@ func DatabaseCredentials() schema.CredentialType { Optional: true, }, }, - Importer: importer.TryAll( - importer.TryEnvVarPair(DefaultEnvVarMapping), - TryPgPassFile("~/.pgpass"), - ), + DefaultProvisioner: provision.EnvVars(defaultEnvVarMapping), + Importer: importer.TryEnvVarPair(defaultEnvVarMapping), } } -var DefaultEnvVarMapping = map[string]sdk.FieldName{ +var defaultEnvVarMapping = map[string]sdk.FieldName{ "PGHOST": fieldname.Host, "PGPORT": fieldname.Port, "PGUSER": fieldname.Username, + "PGPASSWORD": fieldname.Password, "PGDATABASE": fieldname.Database, } - -func TryPgPassFile(path string) sdk.Importer { - return importer.TryFile("~/.pgpass", func(ctx context.Context, contents importer.FileContents, in sdk.ImportInput, out *sdk.ImportAttempt) { - fileLines := strings.Split(string(contents), "\n") - fields := []string{} - - for _, line := range fileLines { - line = strings.TrimSpace(line) - if line == "" || strings.HasPrefix(line, "#") { - continue - } - - fields = strings.Split(line, ":") - - if len(fields) < 5 { - out.AddError(errors.New("Invalid .pgpass entry: " + line)) - continue - } - - out.AddCandidate(sdk.ImportCandidate{ - NameHint: importer.SanitizeNameHint(fields[2]), - Fields: map[sdk.FieldName]string{ - fieldname.Host: fields[0], - fieldname.Port: fields[1], - fieldname.Database: fields[2], - fieldname.Username: fields[3], - fieldname.Password: fields[4], - }, - }) - } - }) -} diff --git a/plugins/yugabytedb/database_credentials_test.go b/plugins/yugabytedb/database_credentials_test.go index fdb83beaf..1a17b0cab 100644 --- a/plugins/yugabytedb/database_credentials_test.go +++ b/plugins/yugabytedb/database_credentials_test.go @@ -9,23 +9,24 @@ import ( ) func TestDatabaseCredentialsImporter(t *testing.T) { - expectedFields := map[sdk.FieldName]string{ - fieldname.Username: "root", - fieldname.Password: "123456", - fieldname.Database: "test", - fieldname.Port: "5432", - fieldname.Host: "localhost", - } - plugintest.TestImporter(t, DatabaseCredentials().Importer, map[string]plugintest.ImportCase{ - "pgpass": { - Files: map[string]string{ - "~/.pgpass": plugintest.LoadFixture(t, ".pgpass"), + "yugabyte": { + Environment: map[string]string{ + "PGHOST": "localhost", + "PGPORT": "5432", + "PGUSER": "root", + "PGPASSWORD": "123456", + "PGDATABASE": "test", }, ExpectedCandidates: []sdk.ImportCandidate{ { - Fields: expectedFields, - NameHint: "test", + Fields: map[sdk.FieldName]string{ + fieldname.Host: "localhost", + fieldname.Port: "5432", + fieldname.Username: "root", + fieldname.Password: "123456", + fieldname.Database: "test", + }, }, }, }, @@ -33,7 +34,7 @@ func TestDatabaseCredentialsImporter(t *testing.T) { } func TestDatabaseCredentialsProvisioner(t *testing.T) { - plugintest.TestProvisioner(t, YugabyteProvisioner{}, map[string]plugintest.ProvisionCase{ + plugintest.TestProvisioner(t, DatabaseCredentials().DefaultProvisioner, map[string]plugintest.ProvisionCase{ "default": { ItemFields: map[sdk.FieldName]string{ fieldname.Host: "localhost", @@ -47,14 +48,11 @@ func TestDatabaseCredentialsProvisioner(t *testing.T) { "PGHOST": "localhost", "PGPORT": "5432", "PGUSER": "root", + "PGPASSWORD": "123456", "PGDATABASE": "test", }, - Files: map[string]sdk.OutputFile{ - "~/.pgpass": { - Contents: []byte(plugintest.LoadFixture(t, ".pgpass")), - }, - }, }, }, - }) + }, + ) } diff --git a/plugins/yugabytedb/provisioner.go b/plugins/yugabytedb/provisioner.go deleted file mode 100644 index 5cf142d21..000000000 --- a/plugins/yugabytedb/provisioner.go +++ /dev/null @@ -1,61 +0,0 @@ -package yugabytedb - -import ( - "context" - - "github.com/1Password/shell-plugins/sdk" - "github.com/1Password/shell-plugins/sdk/schema/fieldname" -) - -type YugabyteProvisioner struct { -} - -func pgPass(in sdk.ProvisionInput) ([]byte, error) { - content := "" - - if host, ok := in.ItemFields[fieldname.Host]; ok { - content += host + ":" - } - - if port, ok := in.ItemFields[fieldname.Port]; ok { - content += port + ":" - } - - if database, ok := in.ItemFields[fieldname.Database]; ok { - content += database + ":" - } - - if user, ok := in.ItemFields[fieldname.Username]; ok { - content += user + ":" - } - - if password, ok := in.ItemFields[fieldname.Password]; ok { - content += password - } - - return []byte(content), nil -} - -func (p YugabyteProvisioner) Provision(ctx context.Context, in sdk.ProvisionInput, out *sdk.ProvisionOutput) { - for envVarName, fieldName := range DefaultEnvVarMapping { - if value, ok := in.ItemFields[fieldName]; ok && value!="*"{ - out.AddEnvVar(envVarName, value) - } - } - fileData, _ := pgPass(in) - - out.Files = map[string]sdk.OutputFile{ - "~/.pgpass": { - Contents: fileData, - }, - } - -} - -func (p YugabyteProvisioner) Deprovision(ctx context.Context, in sdk.DeprovisionInput, out *sdk.DeprovisionOutput) { - // Nothing to do here: credentials get wiped automatically when the process exits. -} - -func (p YugabyteProvisioner) Description() string { - return "Provision yugabyteDB " -} diff --git a/plugins/yugabytedb/test-fixtures/.pgpass b/plugins/yugabytedb/test-fixtures/.pgpass deleted file mode 100644 index 798143743..000000000 --- a/plugins/yugabytedb/test-fixtures/.pgpass +++ /dev/null @@ -1 +0,0 @@ -localhost:5432:test:root:123456 \ No newline at end of file diff --git a/plugins/yugabytedb/ysqlsh.go b/plugins/yugabytedb/ysqlsh.go index 345e7ab53..f32acb0fc 100644 --- a/plugins/yugabytedb/ysqlsh.go +++ b/plugins/yugabytedb/ysqlsh.go @@ -17,8 +17,7 @@ func YugabyteDBCLI() schema.Executable { ), Uses: []schema.CredentialUsage{ { - Name: credname.DatabaseCredentials, - Provisioner: YugabyteProvisioner{}, + Name: credname.DatabaseCredentials, }, }, }