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 Shodan Command-Line Interface Plugin #400

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
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
58 changes: 58 additions & 0 deletions plugins/shodan/api_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package shodan

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 APIKey() schema.CredentialType {
return schema.CredentialType{
Name: credname.APIKey,
DocsURL: sdk.URL("https://cli.shodan.io"),
ManagementURL: sdk.URL("https://account.shodan.io"),
Fields: []schema.CredentialField{
{
Name: fieldname.APIKey,
MarkdownDescription: "API Key used to authenticate to Shodan.",
Secret: true,
Composition: &schema.ValueComposition{
Length: 32,
Charset: schema.Charset{
Uppercase: true,
Lowercase: true,
Digits: true,
},
},
},
},
DefaultProvisioner: provision.TempFile(
provision.FieldAsFile(fieldname.APIKey),
provision.AtFixedPath("~/.shodan/api_key"),
),
Importer: importer.TryAll(
TryShodanConfigFile("~/.shodan/api_key"),
TryShodanConfigFile("~/.config/shodan/api_key"),
)}
}

func TryShodanConfigFile(configPath string) sdk.Importer {
return importer.TryFile(configPath, func(ctx context.Context, contents importer.FileContents, in sdk.ImportInput, out *sdk.ImportAttempt) {
apiKey := contents.ToString()

if apiKey == "" {
return
}

out.AddCandidate(sdk.ImportCandidate{
Fields: map[sdk.FieldName]string{
fieldname.APIKey: apiKey,
},
})
})
}
41 changes: 41 additions & 0 deletions plugins/shodan/api_key_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package shodan

import (
"testing"

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

func TestAPIKeyProvisioner(t *testing.T) {
plugintest.TestProvisioner(t, APIKey().DefaultProvisioner, map[string]plugintest.ProvisionCase{
"default": {
ItemFields: map[sdk.FieldName]string{
fieldname.APIKey: "ddXfzwQOIjTaaxGMzcxXYR6Q0EXAMPLE",
},
ExpectedOutput: sdk.ProvisionOutput{
Files: map[string]sdk.OutputFile{
"~/.config/shodan/api_key": {Contents: []byte("ddXfzwQOIjTaaxGMzcxXYR6Q0EXAMPLE")},
},
},
},
})
}

func TestAPIKeyImporter(t *testing.T) {
plugintest.TestImporter(t, APIKey().Importer, map[string]plugintest.ImportCase{
"config file": {
Files: map[string]string{
"~/.config/shodan/api_key": plugintest.LoadFixture(t, "api_key"),
},
ExpectedCandidates: []sdk.ImportCandidate{
{
Fields: map[sdk.FieldName]string{
fieldname.APIKey: "ddXfzwQOIjTaaxGMzcxXYR6Q0EXAMPLE",
},
},
},
},
})
}
22 changes: 22 additions & 0 deletions plugins/shodan/plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package shodan

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

func New() schema.Plugin {
return schema.Plugin{
Name: "shodan",
Platform: schema.PlatformInfo{
Name: "Shodan",
Homepage: sdk.URL("https://www.shodan.io"),
},
Credentials: []schema.CredentialType{
APIKey(),
},
Executables: []schema.Executable{
ShodanCLI(),
},
}
}
26 changes: 26 additions & 0 deletions plugins/shodan/shodan.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package shodan

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 ShodanCLI() schema.Executable {
return schema.Executable{
Name: "Shodan Command-Line Interface",
Runs: []string{"shodan"},
DocsURL: sdk.URL("https://cli.shodan.io"),
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.

shall we also exclude shodan init from here?

Copy link
Author

Choose a reason for hiding this comment

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

I appended needsauth.NotForExactArgs("init") to the arguments of needsauth.IfAll().

needsauth.NotForExactArgs("init"),
),
Uses: []schema.CredentialUsage{
{
Name: credname.APIKey,
},
},
}
}
1 change: 1 addition & 0 deletions plugins/shodan/test-fixtures/api_key
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ddXfzwQOIjTaaxGMzcxXYR6Q0EXAMPLE