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 Nirmata,cloud-native application management #298

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions plugins/nirmata/api_token.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package nirmata

import (
"context"

"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 APIToken() schema.CredentialType {
return schema.CredentialType{
Name: credname.APIToken,
DocsURL: sdk.URL("https://downloads.nirmata.io/nctl/downloads/"),
ManagementURL: sdk.URL("https://www.nirmata.io/security/login.html"),
Fields: []schema.CredentialField{
{
Name: fieldname.Token,
MarkdownDescription: "Token used to authenticate to Nirmata.",
Secret: true,
Composition: &schema.ValueComposition{
Length: 116,
Charset: schema.Charset{
Lowercase: true,
Digits: true,
},
},
},
{
Name: fieldname.Email,
MarkdownDescription: "Email address registered in Nirmata.",
Comment on lines +33 to +34
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the email required in the item, since it's not provisioned?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But it should be used for this command nctl login which prompts for all these fields including email

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this shell plugin, the user should no longer have to run nctl login. From what I can find in the documentation (Alternatively, you can use global flags or set the NIRMATA_TOKEN and NIRMATA_URL environment variables.), this is an alternative to the email-based authentication.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay in that way makes sense

Copy link
Contributor Author

@itsCheithanya itsCheithanya Jun 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So should we now make the URL field required and not optional?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, since the docs state that the URL has a default:

      --url string     the URL address for Nirmata (env NIRMATA_URL) (default "https://nirmata.io")

so not provisioning it would not break the users' workflows.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh okay so should I only import token from the config file?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's okay to also import url from the config file. At provisioning time, if there's nothing to provision for the url, then we can use the default value

Optional: true,
},
{
Name: fieldname.Address,
MarkdownDescription: "Url address of Nirmata[https://nirmata.io].",
Optional: true,
},
},
DefaultProvisioner: provision.EnvVars(defaultEnvVarMapping),

AndyTitu marked this conversation as resolved.
Show resolved Hide resolved
Importer: importer.TryAll(
importer.TryEnvVarPair(defaultEnvVarMapping),
TryNirmataConfigFile(),
)}
}

var defaultEnvVarMapping = map[string]sdk.FieldName{
"NIRMATA_TOKEN": fieldname.Token,
"NIRMATA_URL": fieldname.Address,
}

func TryNirmataConfigFile() sdk.Importer {
return importer.TryFile("~/.nirmata/config", func(ctx context.Context, contents importer.FileContents, in sdk.ImportInput, out *sdk.ImportAttempt) {

credentialsFile, err := contents.ToINI()
if err != nil {
out.AddError(err)
return
}
for _, section := range credentialsFile.Sections() {
fields := make(map[sdk.FieldName]string)
if section.HasKey("address") && section.Key("address").Value() != "" {
fields[fieldname.Address] = section.Key("address").Value()
}
// if section.HasKey("email") && section.Key("email").Value() != "" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's completely remove this block instead of leaving them as commented sections.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@itsCheithanya this is still commented

// fields[fieldname.Email] = section.Key("email").Value()
// }
if section.HasKey("token") && section.Key("token").Value() != "" {
fields[fieldname.Token] = section.Key("token").Value()
}
if fields[fieldname.Token] != "" {
out.AddCandidate(sdk.ImportCandidate{
Fields: fields,
})
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of empty lines in this file as well.

}

})
}

type Config struct {
Token string
Address string
}
59 changes: 59 additions & 0 deletions plugins/nirmata/api_token_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package nirmata

import (
"testing"

"github.com/1Password/shell-plugins/sdk"
"github.com/1Password/shell-plugins/sdk/plugintest"
"github.com/1Password/shell-plugins/sdk/schema/fieldname"
)

func TestAPITokenProvisioner(t *testing.T) {
plugintest.TestProvisioner(t, APIToken().DefaultProvisioner, map[string]plugintest.ProvisionCase{
"default": {
ItemFields: map[sdk.FieldName]string{
fieldname.Token: "fw90xpbsq8d1nzmdmbie0bcwk99x9rodqx72wfwif7y2hfbhq3gjg4bhcluw8b5qto5hwzfagsztgibbjs4rm8vswu6ppez8za9vhc2ozv5trexample",
fieldname.Address: "https://nirmata.io",
},
ExpectedOutput: sdk.ProvisionOutput{
Environment: map[string]string{
"NIRMATA_TOKEN": "fw90xpbsq8d1nzmdmbie0bcwk99x9rodqx72wfwif7y2hfbhq3gjg4bhcluw8b5qto5hwzfagsztgibbjs4rm8vswu6ppez8za9vhc2ozv5trexample",
"NIRMATA_URL": "https://nirmata.io",
},
},
},
})
}

func TestAPITokenImporter(t *testing.T) {
plugintest.TestImporter(t, APIToken().Importer, map[string]plugintest.ImportCase{
"environment": {
Environment: map[string]string{
"NIRMATA_TOKEN": "fw90xpbsq8d1nzmdmbie0bcwk99x9rodqx72wfwif7y2hfbhq3gjg4bhcluw8b5qto5hwzfagsztgibbjs4rm8vswu6ppez8za9vhc2ozv5trexample",
"NIRMATA_URL": "https://nirmata.io",
},
ExpectedCandidates: []sdk.ImportCandidate{
{
Fields: map[sdk.FieldName]string{
fieldname.Token: "fw90xpbsq8d1nzmdmbie0bcwk99x9rodqx72wfwif7y2hfbhq3gjg4bhcluw8b5qto5hwzfagsztgibbjs4rm8vswu6ppez8za9vhc2ozv5trexample",
fieldname.Address: "https://nirmata.io",
},
},
},
},

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An empty line here. Let's remove it.

"config file": {
Files: map[string]string{
"~/.nirmata/config": plugintest.LoadFixture(t, "config"),
},
ExpectedCandidates: []sdk.ImportCandidate{
{
Fields: map[sdk.FieldName]string{
fieldname.Token: "fw90xpbsq8d1nzmdmbie0bcwk99x9rodqx72wfwif7y2hfbhq3gjg4bhcluw8b5qto5hwzfagsztgibbjs4rm8vswu6ppez8za9vhc2ozv5trexample",
fieldname.Address: "https://nirmata.io",
},
},
},
},
})
}
26 changes: 26 additions & 0 deletions plugins/nirmata/nctl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package nirmata

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 NirmataCLI() schema.Executable {
return schema.Executable{
Name: "Nirmata CLI",
Runs: []string{"nctl"},
DocsURL: sdk.URL("https://nirmata.com/docs/cli"), // TODO: Replace with actual URL
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This todo as well.

NeedsAuth: needsauth.IfAll(
needsauth.NotForHelpOrVersion(),
needsauth.NotWithoutArgs(),
Comment on lines +16 to +17
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's opt out of authentication for nctl login as well. 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yess getit!!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I'm not familiar with the Nirmata CLI, are there any other commands which do not require authentication?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe version does not require authentication, but could you confirm the rest, @itsCheithanya?

needsauth.NotForExactArgs("login"),
),
Uses: []schema.CredentialUsage{
{
Name: credname.APIToken,
},
},
}
}
22 changes: 22 additions & 0 deletions plugins/nirmata/plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package nirmata

import (
"github.com/1Password/shell-plugins/sdk"
"github.com/1Password/shell-plugins/sdk/schema"
)

func New() schema.Plugin {
return schema.Plugin{
Name: "nirmata",
Platform: schema.PlatformInfo{
Name: "Nirmata",
Homepage: sdk.URL("https://nirmata.com"), // TODO: Check if this is correct
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove the TODO from here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yess into it

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@itsCheithanya This does not seem to be removed yet, could we make sure to remove it, please?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove the TODO from here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like nirmata.com and nirmata.io are two different products/services, but from the same company? Could you share some specifics of what you know about Nirmata? That'd help us reviewers jumpstart the process, vs having to understand the Nirmata concepts manually. Thanks @itsCheithanya!

},
Credentials: []schema.CredentialType{
APIToken(),
},
Executables: []schema.Executable{
NirmataCLI(),
},
}
}
2 changes: 2 additions & 0 deletions plugins/nirmata/test-fixtures/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
address: https://nirmata.io
token: fw90xpbsq8d1nzmdmbie0bcwk99x9rodqx72wfwif7y2hfbhq3gjg4bhcluw8b5qto5hwzfagsztgibbjs4rm8vswu6ppez8za9vhc2ozv5trexample