Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for MongoDB Atlas CLI, with environment variable based importing and provisioning #198

Merged
merged 5 commits into from Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions 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.PrivateKeyPair,
},
},
}
}
22 changes: 22 additions & 0 deletions 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{
PrivateKeyPair(),
},
Executables: []schema.Executable{
MongoDBAtlasCLI(),
},
}
}
52 changes: 52 additions & 0 deletions plugins/atlas/privatekeypair.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 PrivateKeyPair() schema.CredentialType {
return schema.CredentialType{
Name: credname.PrivateKeyPair,
florisvdg marked this conversation as resolved.
Show resolved Hide resolved
DocsURL: sdk.URL("https://www.mongodb.com/docs/atlas/cli/stable/atlas-cli-env-variables/"),
ManagementURL: nil,
Fields: []schema.CredentialField{
{
Name: fieldname.PublicKey,
MarkdownDescription: "Public key used to authenticate to MongoDB Atlas.",
Secret: true,
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,
}
45 changes: 45 additions & 0 deletions plugins/atlas/privatekeypair_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, PrivateKeyPair().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, PrivateKeyPair().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",
},
},
},
},
})
}
2 changes: 2 additions & 0 deletions sdk/schema/credname/names.go
Expand Up @@ -18,6 +18,7 @@ const (
LoginDetails = sdk.CredentialName("Login Details")
PersonalAPIToken = sdk.CredentialName("Personal API Token")
PersonalAccessToken = sdk.CredentialName("Personal Access Token")
PrivateKeyPair = sdk.CredentialName("Private Key Pair")
RegistryCredentials = sdk.CredentialName("Registry Credentials")
SecretKey = sdk.CredentialName("Secret Key")
)
Expand All @@ -38,6 +39,7 @@ func ListAll() []sdk.CredentialName {
LoginDetails,
PersonalAPIToken,
PersonalAccessToken,
PrivateKeyPair,
RegistryCredentials,
SecretKey,
}
Expand Down
2 changes: 2 additions & 0 deletions sdk/schema/fieldname/names.go
Expand Up @@ -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")
Expand Down Expand Up @@ -81,6 +82,7 @@ func ListAll() []sdk.FieldName {
Organization,
Password,
Port,
PublicKey,
PrivateKey,
Region,
Secret,
Expand Down