Skip to content

Commit

Permalink
As of now .pgpass file is not compatible with op, using only envs as …
Browse files Browse the repository at this point in the history
…provisioners as suggestred by @hculea.

Note: 'PGPASSWORD' env is working for yugabyte, but it is not mentioned in ysqlsh docs..
  • Loading branch information
parthiv11 committed Jul 26, 2023
1 parent 6defc98 commit 4972659
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 125 deletions.
46 changes: 5 additions & 41 deletions plugins/yugabytedb/database_credentials.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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],
},
})
}
})
}
38 changes: 18 additions & 20 deletions plugins/yugabytedb/database_credentials_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,32 @@ 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",
},
},
},
},
})
}

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",
Expand All @@ -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")),
},
},
},
},
})
},
)
}
61 changes: 0 additions & 61 deletions plugins/yugabytedb/provisioner.go

This file was deleted.

1 change: 0 additions & 1 deletion plugins/yugabytedb/test-fixtures/.pgpass

This file was deleted.

3 changes: 1 addition & 2 deletions plugins/yugabytedb/ysqlsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ func YugabyteDBCLI() schema.Executable {
),
Uses: []schema.CredentialUsage{
{
Name: credname.DatabaseCredentials,
Provisioner: YugabyteProvisioner{},
Name: credname.DatabaseCredentials,
},
},
}
Expand Down

0 comments on commit 4972659

Please sign in to comment.