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 6 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
57 changes: 57 additions & 0 deletions plugins/shodan/api_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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://developer.shodan.io/api/requirements"),
Copy link
Member

Choose a reason for hiding this comment

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

This looks to refer to the API, could we replace this by https://cli.shodan.io/?

Copy link
Author

Choose a reason for hiding this comment

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

I replaced the link with the one you provided.

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("~/.config/shodan/api_key"),
),
Importer: importer.TryAll(
TryShodanConfigFile(),
)}
}

func TryShodanConfigFile() sdk.Importer {
return importer.TryFile("~/.config/shodan/api_key", func(ctx context.Context, contents importer.FileContents, in sdk.ImportInput, out *sdk.ImportAttempt) {
Copy link
Member

Choose a reason for hiding this comment

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

For many tools, the equivalent of ~/.config from Linux on MacOS is ~/Library/Application Support.

Is this the case here too?

Copy link
Author

@kusshi94 kusshi94 Mar 23, 2024

Choose a reason for hiding this comment

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

After checking the source code of the CLI tool (https://github.com/achillean/shodan-python/blob/master/shodan/cli/settings.py), it seems that the OS does not matter and the configuration file is stored in ~/.shodan/config if the ~/.shodan/ directory exists. Otherwise, it is stored in ~/.config/shodan/.
I modified the importers to try reading from both locations, and changed the provisioner to use the ~/.shodan/ directory.

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(),
},
}
}
25 changes: 25 additions & 0 deletions plugins/shodan/shodan.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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().

),
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