Skip to content

Commit c95bf76

Browse files
authored
Merge pull request #198 from joqim/new/atlas
Add support for MongoDB Atlas CLI, with environment variable based importing and provisioning
2 parents 7911a75 + 6132da4 commit c95bf76

5 files changed

Lines changed: 146 additions & 0 deletions

File tree

plugins/atlas/apikey.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package atlas
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 APIKey() schema.CredentialType {
13+
return schema.CredentialType{
14+
Name: credname.APIKey,
15+
DocsURL: sdk.URL("https://www.mongodb.com/docs/atlas/configure-api-access/"),
16+
ManagementURL: nil,
17+
Fields: []schema.CredentialField{
18+
{
19+
Name: fieldname.PublicKey,
20+
MarkdownDescription: "Public key used to authenticate to MongoDB Atlas.",
21+
Secret: false,
22+
Composition: &schema.ValueComposition{
23+
Length: 8,
24+
Charset: schema.Charset{
25+
Lowercase: true,
26+
},
27+
},
28+
},
29+
{
30+
Name: fieldname.PrivateKey,
31+
MarkdownDescription: "Private key used to authenticate to MongoDB Atlas.",
32+
Secret: true,
33+
Composition: &schema.ValueComposition{
34+
Length: 36,
35+
Charset: schema.Charset{
36+
Lowercase: true,
37+
Digits: true,
38+
Specific: []rune{'-'},
39+
},
40+
},
41+
},
42+
},
43+
DefaultProvisioner: provision.EnvVars(defaultEnvVarMapping),
44+
Importer: importer.TryAll(
45+
importer.TryEnvVarPair(defaultEnvVarMapping),
46+
)}
47+
}
48+
49+
var defaultEnvVarMapping = map[string]sdk.FieldName{
50+
"MONGODB_ATLAS_PUBLIC_API_KEY": fieldname.PublicKey,
51+
"MONGODB_ATLAS_PRIVATE_API_KEY": fieldname.PrivateKey,
52+
}

plugins/atlas/apikey_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package atlas
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 TestCredentialsProvisioner(t *testing.T) {
12+
plugintest.TestProvisioner(t, APIKey().DefaultProvisioner, map[string]plugintest.ProvisionCase{
13+
"default": {
14+
ItemFields: map[sdk.FieldName]string{
15+
fieldname.PublicKey: "eexample",
16+
fieldname.PrivateKey: "qohcbhiu-26ag-wpwf-maqn-cw5xlexample",
17+
},
18+
ExpectedOutput: sdk.ProvisionOutput{
19+
Environment: map[string]string{
20+
"MONGODB_ATLAS_PUBLIC_API_KEY": "eexample",
21+
"MONGODB_ATLAS_PRIVATE_API_KEY": "qohcbhiu-26ag-wpwf-maqn-cw5xlexample",
22+
},
23+
},
24+
},
25+
})
26+
}
27+
28+
func TestCredentialsImporter(t *testing.T) {
29+
plugintest.TestImporter(t, APIKey().Importer, map[string]plugintest.ImportCase{
30+
"environment": {
31+
Environment: map[string]string{
32+
"MONGODB_ATLAS_PUBLIC_API_KEY": "eexample",
33+
"MONGODB_ATLAS_PRIVATE_API_KEY": "qohcbhiu-26ag-wpwf-maqn-cw5xlexample",
34+
},
35+
ExpectedCandidates: []sdk.ImportCandidate{
36+
{
37+
Fields: map[sdk.FieldName]string{
38+
fieldname.PublicKey: "eexample",
39+
fieldname.PrivateKey: "qohcbhiu-26ag-wpwf-maqn-cw5xlexample",
40+
},
41+
},
42+
},
43+
},
44+
})
45+
}

plugins/atlas/atlas.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package atlas
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 MongoDBAtlasCLI() schema.Executable {
11+
return schema.Executable{
12+
Name: "MongoDB Atlas CLI",
13+
Runs: []string{"atlas"},
14+
DocsURL: sdk.URL("https://www.mongodb.com/docs/atlas/cli/stable/"),
15+
NeedsAuth: needsauth.IfAll(
16+
needsauth.NotForHelpOrVersion(),
17+
needsauth.NotWithoutArgs(),
18+
),
19+
Uses: []schema.CredentialUsage{
20+
{
21+
Name: credname.APIKey,
22+
},
23+
},
24+
}
25+
}

plugins/atlas/plugin.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package atlas
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: "atlas",
11+
Platform: schema.PlatformInfo{
12+
Name: "MongoDB Atlas",
13+
Homepage: sdk.URL("https://www.mongodb.com/"),
14+
},
15+
Credentials: []schema.CredentialType{
16+
APIKey(),
17+
},
18+
Executables: []schema.Executable{
19+
MongoDBAtlasCLI(),
20+
},
21+
}
22+
}

sdk/schema/fieldname/names.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ const (
4040
Organization = sdk.FieldName("Organization")
4141
Password = sdk.FieldName("Password")
4242
Port = sdk.FieldName("Port")
43+
PublicKey = sdk.FieldName("Public Key")
4344
PrivateKey = sdk.FieldName("Private Key")
4445
Region = sdk.FieldName("Region")
4546
Secret = sdk.FieldName("Secret")
@@ -89,6 +90,7 @@ func ListAll() []sdk.FieldName {
8990
Organization,
9091
Password,
9192
Port,
93+
PublicKey,
9294
PrivateKey,
9395
Region,
9496
Secret,

0 commit comments

Comments
 (0)