Skip to content

Commit ba97aed

Browse files
authored
Merge pull request #394 from 1Password/amanda/cratedb
Amanda/cratedb
2 parents 00a6fca + be6edff commit ba97aed

5 files changed

Lines changed: 154 additions & 0 deletions

File tree

plugins/cratedb/crash.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package cratedb
2+
3+
import (
4+
"github.com/1Password/shell-plugins/sdk"
5+
"github.com/1Password/shell-plugins/sdk/needsauth"
6+
"github.com/1Password/shell-plugins/sdk/schema"
7+
"github.com/1Password/shell-plugins/sdk/schema/credname"
8+
)
9+
10+
func CrateDBCLI() schema.Executable {
11+
return schema.Executable{
12+
Name: "CrateDB Shell",
13+
Runs: []string{"crash"},
14+
DocsURL: sdk.URL("https://crate.io/docs/crate/crash/en/latest/"),
15+
NeedsAuth: needsauth.NotForHelpOrVersion(),
16+
Uses: []schema.CredentialUsage{
17+
{
18+
Name: credname.DatabaseCredentials,
19+
Provisioner: CrateArgsProvisioner{},
20+
},
21+
},
22+
}
23+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package cratedb
2+
3+
import (
4+
"github.com/1Password/shell-plugins/sdk"
5+
"github.com/1Password/shell-plugins/sdk/importer"
6+
"github.com/1Password/shell-plugins/sdk/provision"
7+
"github.com/1Password/shell-plugins/sdk/schema"
8+
"github.com/1Password/shell-plugins/sdk/schema/credname"
9+
"github.com/1Password/shell-plugins/sdk/schema/fieldname"
10+
)
11+
12+
func DatabaseCredentials() schema.CredentialType {
13+
return schema.CredentialType{
14+
Name: credname.DatabaseCredentials,
15+
DocsURL: sdk.URL("https://crate.io/docs/crate/crash/en/latest/run.html#environment-variables"),
16+
ManagementURL: sdk.URL("https://console.cratedb.cloud/account/settings"),
17+
Fields: []schema.CredentialField{
18+
{
19+
Name: fieldname.Host,
20+
MarkdownDescription: "CrateDB host to connect to.",
21+
Optional: false,
22+
},
23+
{
24+
Name: fieldname.Username,
25+
MarkdownDescription: "CrateDB user to authenticate as.",
26+
Optional: false,
27+
},
28+
{
29+
Name: fieldname.Password,
30+
MarkdownDescription: "Password used to authenticate to CrateDB.",
31+
Secret: true,
32+
},
33+
},
34+
DefaultProvisioner: provision.NoOp(),
35+
Importer: importer.TryEnvVarPair(defaultEnvVarMapping),
36+
}
37+
}
38+
39+
var defaultEnvVarMapping = map[string]sdk.FieldName{
40+
"CRATEPW": fieldname.Password,
41+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package cratedb
2+
3+
import (
4+
"testing"
5+
6+
"github.com/1Password/shell-plugins/sdk"
7+
"github.com/1Password/shell-plugins/sdk/plugintest"
8+
"github.com/1Password/shell-plugins/sdk/schema/fieldname"
9+
)
10+
11+
func TestDatabaseCredentialsProvisioner(t *testing.T) {
12+
plugintest.TestProvisioner(t, CrateArgsProvisioner{}, map[string]plugintest.ProvisionCase{
13+
"default": {
14+
ItemFields: map[sdk.FieldName]string{
15+
16+
fieldname.Host: "https://love.aks1.eastus2.azure.cratedb.net:4200",
17+
fieldname.Username: "admin",
18+
fieldname.Password: "1<34&f0rg3t@me",
19+
},
20+
CommandLine: []string{"crash"},
21+
ExpectedOutput: sdk.ProvisionOutput{
22+
Environment: map[string]string{
23+
"CRATEPW": "1<34&f0rg3t@me",
24+
},
25+
CommandLine: []string{
26+
"crash",
27+
"--username",
28+
"admin",
29+
"--hosts",
30+
"https://love.aks1.eastus2.azure.cratedb.net:4200",
31+
},
32+
},
33+
},
34+
})
35+
}

plugins/cratedb/plugin.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package cratedb
2+
3+
import (
4+
"github.com/1Password/shell-plugins/sdk"
5+
"github.com/1Password/shell-plugins/sdk/schema"
6+
)
7+
8+
func New() schema.Plugin {
9+
return schema.Plugin{
10+
Name: "cratedb",
11+
Platform: schema.PlatformInfo{
12+
Name: "CrateDB",
13+
Homepage: sdk.URL("https://crate.io/"),
14+
},
15+
Credentials: []schema.CredentialType{
16+
DatabaseCredentials(),
17+
},
18+
Executables: []schema.Executable{
19+
CrateDBCLI(),
20+
},
21+
}
22+
}

plugins/cratedb/provisioner.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package cratedb
2+
3+
import (
4+
"context"
5+
6+
"github.com/1Password/shell-plugins/sdk"
7+
"github.com/1Password/shell-plugins/sdk/schema/fieldname"
8+
)
9+
10+
type CrateArgsProvisioner struct {
11+
}
12+
13+
func (p CrateArgsProvisioner) Provision(ctx context.Context, in sdk.ProvisionInput, out *sdk.ProvisionOutput) {
14+
if value, ok := in.ItemFields[fieldname.Password]; ok {
15+
out.AddEnvVar("CRATEPW", value)
16+
}
17+
18+
user, userFound := in.ItemFields[fieldname.Username]
19+
host, hostFound := in.ItemFields[fieldname.Host]
20+
if userFound && hostFound {
21+
commandLine := []string{out.CommandLine[0], "--username", user, "--hosts", host}
22+
commandLine = append(commandLine, out.CommandLine[1:]...)
23+
out.CommandLine = commandLine
24+
}
25+
}
26+
27+
func (p CrateArgsProvisioner) Deprovision(ctx context.Context, in sdk.DeprovisionInput, out *sdk.DeprovisionOutput) {
28+
// Nothing to do here: credentials get wiped automatically when the process exits.
29+
}
30+
31+
func (p CrateArgsProvisioner) Description() string {
32+
return "Provision CrateDB username, host as command-line arguments && Password as Env ."
33+
}

0 commit comments

Comments
 (0)