diff --git a/plugins/atlas/apikey.go b/plugins/atlas/apikey.go new file mode 100644 index 000000000..9ffdd5e80 --- /dev/null +++ b/plugins/atlas/apikey.go @@ -0,0 +1,52 @@ +package atlas + +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 APIKey() schema.CredentialType { + return schema.CredentialType{ + Name: credname.APIKey, + DocsURL: sdk.URL("https://www.mongodb.com/docs/atlas/configure-api-access/"), + ManagementURL: nil, + Fields: []schema.CredentialField{ + { + Name: fieldname.PublicKey, + MarkdownDescription: "Public key used to authenticate to MongoDB Atlas.", + Secret: false, + Composition: &schema.ValueComposition{ + Length: 8, + Charset: schema.Charset{ + Lowercase: true, + }, + }, + }, + { + Name: fieldname.PrivateKey, + MarkdownDescription: "Private key used to authenticate to MongoDB Atlas.", + Secret: true, + Composition: &schema.ValueComposition{ + Length: 36, + Charset: schema.Charset{ + Lowercase: true, + Digits: true, + Specific: []rune{'-'}, + }, + }, + }, + }, + DefaultProvisioner: provision.EnvVars(defaultEnvVarMapping), + Importer: importer.TryAll( + importer.TryEnvVarPair(defaultEnvVarMapping), + )} +} + +var defaultEnvVarMapping = map[string]sdk.FieldName{ + "MONGODB_ATLAS_PUBLIC_API_KEY": fieldname.PublicKey, + "MONGODB_ATLAS_PRIVATE_API_KEY": fieldname.PrivateKey, +} diff --git a/plugins/atlas/apikey_test.go b/plugins/atlas/apikey_test.go new file mode 100644 index 000000000..ec46f0288 --- /dev/null +++ b/plugins/atlas/apikey_test.go @@ -0,0 +1,45 @@ +package atlas + +import ( + "testing" + + "github.com/1Password/shell-plugins/sdk" + "github.com/1Password/shell-plugins/sdk/plugintest" + "github.com/1Password/shell-plugins/sdk/schema/fieldname" +) + +func TestCredentialsProvisioner(t *testing.T) { + plugintest.TestProvisioner(t, APIKey().DefaultProvisioner, map[string]plugintest.ProvisionCase{ + "default": { + ItemFields: map[sdk.FieldName]string{ + fieldname.PublicKey: "eexample", + fieldname.PrivateKey: "qohcbhiu-26ag-wpwf-maqn-cw5xlexample", + }, + ExpectedOutput: sdk.ProvisionOutput{ + Environment: map[string]string{ + "MONGODB_ATLAS_PUBLIC_API_KEY": "eexample", + "MONGODB_ATLAS_PRIVATE_API_KEY": "qohcbhiu-26ag-wpwf-maqn-cw5xlexample", + }, + }, + }, + }) +} + +func TestCredentialsImporter(t *testing.T) { + plugintest.TestImporter(t, APIKey().Importer, map[string]plugintest.ImportCase{ + "environment": { + Environment: map[string]string{ + "MONGODB_ATLAS_PUBLIC_API_KEY": "eexample", + "MONGODB_ATLAS_PRIVATE_API_KEY": "qohcbhiu-26ag-wpwf-maqn-cw5xlexample", + }, + ExpectedCandidates: []sdk.ImportCandidate{ + { + Fields: map[sdk.FieldName]string{ + fieldname.PublicKey: "eexample", + fieldname.PrivateKey: "qohcbhiu-26ag-wpwf-maqn-cw5xlexample", + }, + }, + }, + }, + }) +} diff --git a/plugins/atlas/atlas.go b/plugins/atlas/atlas.go new file mode 100644 index 000000000..a2c69145c --- /dev/null +++ b/plugins/atlas/atlas.go @@ -0,0 +1,25 @@ +package atlas + +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 MongoDBAtlasCLI() schema.Executable { + return schema.Executable{ + Name: "MongoDB Atlas CLI", + Runs: []string{"atlas"}, + DocsURL: sdk.URL("https://www.mongodb.com/docs/atlas/cli/stable/"), + NeedsAuth: needsauth.IfAll( + needsauth.NotForHelpOrVersion(), + needsauth.NotWithoutArgs(), + ), + Uses: []schema.CredentialUsage{ + { + Name: credname.APIKey, + }, + }, + } +} diff --git a/plugins/atlas/plugin.go b/plugins/atlas/plugin.go new file mode 100644 index 000000000..b1927198a --- /dev/null +++ b/plugins/atlas/plugin.go @@ -0,0 +1,22 @@ +package atlas + +import ( + "github.com/1Password/shell-plugins/sdk" + "github.com/1Password/shell-plugins/sdk/schema" +) + +func New() schema.Plugin { + return schema.Plugin{ + Name: "atlas", + Platform: schema.PlatformInfo{ + Name: "MongoDB Atlas", + Homepage: sdk.URL("https://www.mongodb.com/"), + }, + Credentials: []schema.CredentialType{ + APIKey(), + }, + Executables: []schema.Executable{ + MongoDBAtlasCLI(), + }, + } +} diff --git a/sdk/schema/fieldname/names.go b/sdk/schema/fieldname/names.go index 80d6fa252..4932985a1 100644 --- a/sdk/schema/fieldname/names.go +++ b/sdk/schema/fieldname/names.go @@ -36,6 +36,7 @@ const ( Organization = sdk.FieldName("Organization") Password = sdk.FieldName("Password") Port = sdk.FieldName("Port") + PublicKey = sdk.FieldName("Public Key") PrivateKey = sdk.FieldName("Private Key") Region = sdk.FieldName("Region") Secret = sdk.FieldName("Secret") @@ -81,6 +82,7 @@ func ListAll() []sdk.FieldName { Organization, Password, Port, + PublicKey, PrivateKey, Region, Secret,