diff --git a/plugins/cratedb/crash.go b/plugins/cratedb/crash.go new file mode 100644 index 00000000..c318441e --- /dev/null +++ b/plugins/cratedb/crash.go @@ -0,0 +1,23 @@ +package cratedb + +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 CrateDBCLI() schema.Executable { + return schema.Executable{ + Name: "CrateDB Shell", + Runs: []string{"crash"}, + DocsURL: sdk.URL("https://crate.io/docs/crate/crash/en/latest/"), + NeedsAuth: needsauth.NotForHelpOrVersion(), + Uses: []schema.CredentialUsage{ + { + Name: credname.DatabaseCredentials, + Provisioner: CrateArgsProvisioner{}, + }, + }, + } +} diff --git a/plugins/cratedb/database_credentials.go b/plugins/cratedb/database_credentials.go new file mode 100644 index 00000000..2c08013e --- /dev/null +++ b/plugins/cratedb/database_credentials.go @@ -0,0 +1,41 @@ +package cratedb + +import ( + "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" +) + +func DatabaseCredentials() schema.CredentialType { + return schema.CredentialType{ + Name: credname.DatabaseCredentials, + DocsURL: sdk.URL("https://crate.io/docs/crate/crash/en/latest/run.html#environment-variables"), + ManagementURL: sdk.URL("https://console.cratedb.cloud/account/settings"), + Fields: []schema.CredentialField{ + { + Name: fieldname.Host, + MarkdownDescription: "CrateDB host to connect to.", + Optional: false, + }, + { + Name: fieldname.Username, + MarkdownDescription: "CrateDB user to authenticate as.", + Optional: false, + }, + { + Name: fieldname.Password, + MarkdownDescription: "Password used to authenticate to CrateDB.", + Secret: true, + }, + }, + DefaultProvisioner: provision.NoOp(), + Importer: importer.TryEnvVarPair(defaultEnvVarMapping), + } +} + +var defaultEnvVarMapping = map[string]sdk.FieldName{ + "CRATEPW": fieldname.Password, +} diff --git a/plugins/cratedb/database_credentials_test.go b/plugins/cratedb/database_credentials_test.go new file mode 100644 index 00000000..72938c7a --- /dev/null +++ b/plugins/cratedb/database_credentials_test.go @@ -0,0 +1,35 @@ +package cratedb + +import ( + "testing" + + "github.com/1Password/shell-plugins/sdk" + "github.com/1Password/shell-plugins/sdk/plugintest" + "github.com/1Password/shell-plugins/sdk/schema/fieldname" +) + +func TestDatabaseCredentialsProvisioner(t *testing.T) { + plugintest.TestProvisioner(t, CrateArgsProvisioner{}, map[string]plugintest.ProvisionCase{ + "default": { + ItemFields: map[sdk.FieldName]string{ + + fieldname.Host: "https://love.aks1.eastus2.azure.cratedb.net:4200", + fieldname.Username: "admin", + fieldname.Password: "1<34&f0rg3t@me", + }, + CommandLine: []string{"crash"}, + ExpectedOutput: sdk.ProvisionOutput{ + Environment: map[string]string{ + "CRATEPW": "1<34&f0rg3t@me", + }, + CommandLine: []string{ + "crash", + "--username", + "admin", + "--hosts", + "https://love.aks1.eastus2.azure.cratedb.net:4200", + }, + }, + }, + }) +} diff --git a/plugins/cratedb/plugin.go b/plugins/cratedb/plugin.go new file mode 100644 index 00000000..c390cd22 --- /dev/null +++ b/plugins/cratedb/plugin.go @@ -0,0 +1,22 @@ +package cratedb + +import ( + "github.com/1Password/shell-plugins/sdk" + "github.com/1Password/shell-plugins/sdk/schema" +) + +func New() schema.Plugin { + return schema.Plugin{ + Name: "cratedb", + Platform: schema.PlatformInfo{ + Name: "CrateDB", + Homepage: sdk.URL("https://crate.io/"), + }, + Credentials: []schema.CredentialType{ + DatabaseCredentials(), + }, + Executables: []schema.Executable{ + CrateDBCLI(), + }, + } +} diff --git a/plugins/cratedb/provisioner.go b/plugins/cratedb/provisioner.go new file mode 100644 index 00000000..2ba136d2 --- /dev/null +++ b/plugins/cratedb/provisioner.go @@ -0,0 +1,33 @@ +package cratedb + +import ( + "context" + + "github.com/1Password/shell-plugins/sdk" + "github.com/1Password/shell-plugins/sdk/schema/fieldname" +) + +type CrateArgsProvisioner struct { +} + +func (p CrateArgsProvisioner) Provision(ctx context.Context, in sdk.ProvisionInput, out *sdk.ProvisionOutput) { + if value, ok := in.ItemFields[fieldname.Password]; ok { + out.AddEnvVar("CRATEPW", value) + } + + user, userFound := in.ItemFields[fieldname.Username] + host, hostFound := in.ItemFields[fieldname.Host] + if userFound && hostFound { + commandLine := []string{out.CommandLine[0], "--username", user, "--hosts", host} + commandLine = append(commandLine, out.CommandLine[1:]...) + out.CommandLine = commandLine + } +} + +func (p CrateArgsProvisioner) Deprovision(ctx context.Context, in sdk.DeprovisionInput, out *sdk.DeprovisionOutput) { + // Nothing to do here: credentials get wiped automatically when the process exits. +} + +func (p CrateArgsProvisioner) Description() string { + return "Provision CrateDB username, host as command-line arguments && Password as Env ." +}