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 tunnelto with config file importer and file-based provisioning #171

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

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,
ManagementURL: sdk.URL("https://dashboard.tunnelto.dev/"),
Fields: []schema.CredentialField{
Copy link
Member

Choose a reason for hiding this comment

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

Is provisioning of host, port, scheme and subdomain also available via envvars? If so, I'd propose we add these as well as optional fields.

Copy link
Contributor

@AndyTitu AndyTitu Feb 28, 2023

Choose a reason for hiding this comment

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

{
Name: fieldname.APIKey,
MarkdownDescription: "API Key used to authenticate to tunnelto.dev.",
Secret: true,
Composition: &schema.ValueComposition{
Length: 22,
Charset: schema.Charset{
Uppercase: true,
Lowercase: true,
Digits: true,
},
},
},
{
Name: fieldname.Host,
MarkdownDescription: "Host address to forward incoming traffic to.",
Secret: false,
Optional: true,
Composition: &schema.ValueComposition{
Charset: schema.Charset{
Uppercase: true,
Lowercase: true,
Digits: true,
},
},
},
{
Name: fieldname.Port,
MarkdownDescription: "Port on the host address to forward incoming traffic to.",
Secret: false,
Optional: true,
Composition: &schema.ValueComposition{
Charset: schema.Charset{
Digits: true,
},
},
},
{
Name: fieldname.Scheme,
MarkdownDescription: "Protocol (http or https) to use for the local host.",
Secret: false,
Optional: true,
Composition: &schema.ValueComposition{
Charset: schema.Charset{
Lowercase: true,
},
},
},
{
Name: fieldname.Subdomain,
MarkdownDescription: "Subdomain to use for the tunnel.",
Secret: false,
Optional: true,
Composition: &schema.ValueComposition{
Charset: schema.Charset{
Uppercase: true,
Lowercase: true,
Digits: true,
},
},
},
},
DefaultProvisioner: provision.Arguments(argumentsMapping),
Importer: importer.TryAll(
TrytunneltodevConfigFile(),
)}
}

var argumentsMapping = map[string]sdk.FieldName{
"--key": fieldname.APIKey,
"--host": fieldname.Host,
"--port": fieldname.Port,
"--scheme": fieldname.Scheme,
"--subdomain": fieldname.Subdomain,
}

func TrytunneltodevConfigFile() sdk.Importer {
return importer.TryFile("~/.tunnelto/key.token", func(ctx context.Context, contents importer.FileContents, in sdk.ImportInput, out *sdk.ImportAttempt) {
if contents.ToString() == "" {
return
}

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

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{
"temp file": {
ItemFields: map[sdk.FieldName]string{
fieldname.APIKey: "XddpK7jZiQ0CpE3EXAMPLE",
},
CommandLine: []string{"tunnelto"},
ExpectedOutput: sdk.ProvisionOutput{
CommandLine: []string{"tunnelto"},
Files: map[string]sdk.OutputFile{
"~/.tunnelto/key.token": {
Contents: []byte(plugintest.LoadFixture(t, "key.token")),
},
},
},
},
})
}

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

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

func New() schema.Plugin {
return schema.Plugin{
Name: "tunnelto",
Platform: schema.PlatformInfo{
Name: "tunnelto.dev",
Homepage: sdk.URL("https://tunnelto.dev"),
},
Credentials: []schema.CredentialType{
APIKey(),
},
Executables: []schema.Executable{
tunneltodevCLI(),
},
}
}
1 change: 1 addition & 0 deletions plugins/tunnelto/test-fixtures/key.token
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
XddpK7jZiQ0CpE3EXAMPLE
27 changes: 27 additions & 0 deletions plugins/tunnelto/tunnelto.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package tunnelto

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 tunneltodevCLI() schema.Executable {
return schema.Executable{
Name: "tunnelto.dev CLI",
Runs: []string{"tunnelto"},
DocsURL: sdk.URL("https://github.com/agrinman/tunnelto"),
NeedsAuth: needsauth.IfAll(
needsauth.NotForHelpOrVersion(),
needsauth.NotWhenContainsArgs("-k"),
needsauth.NotWhenContainsArgs("--key"),
needsauth.NotWhenContainsArgs("set-auth"),
),
Uses: []schema.CredentialUsage{
{
Name: credname.APIKey,
},
},
}
}
38 changes: 38 additions & 0 deletions sdk/provision/arguments_provisioner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package provision

import (
"context"

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

// ArgumentsProvisioner provisions secrets as command line arguments
type ArgumentsProvisioner struct {
sdk.Provisioner

Schema map[string]sdk.FieldName
}

// Arguments creates an ArgumentsProvisioner that provisions secrets as command line arguments, based
// on the specified schema of argument flag/name and its value, which is a FieldName.
func Arguments(schema map[string]sdk.FieldName) sdk.Provisioner {
return ArgumentsProvisioner{
Schema: schema,
}
}

func (p ArgumentsProvisioner) Provision(ctx context.Context, in sdk.ProvisionInput, out *sdk.ProvisionOutput) {
for argName, fieldName := range p.Schema {
if value, ok := in.ItemFields[fieldName]; ok {
out.AddArgs(argName, value)
}
}
}

func (p ArgumentsProvisioner) Deprovision(ctx context.Context, in sdk.DeprovisionInput, out *sdk.DeprovisionOutput) {
// Nothing to do here: passed argument values get wiped automatically when the process exits.
}

func (p ArgumentsProvisioner) Description() string {
return "Provision secrets as command line arguments"
}
3 changes: 3 additions & 0 deletions sdk/schema/fieldname/names.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const (
Port = sdk.FieldName("Port")
PrivateKey = sdk.FieldName("Private Key")
Region = sdk.FieldName("Region")
Scheme = sdk.FieldName("Scheme")
Secret = sdk.FieldName("Secret")
SecretAccessKey = sdk.FieldName("Secret Access Key")
Subdomain = sdk.FieldName("Subdomain")
Expand Down Expand Up @@ -91,8 +92,10 @@ func ListAll() []sdk.FieldName {
Port,
PrivateKey,
Region,
Scheme,
Secret,
SecretAccessKey,
Subdomain,
Token,
URL,
User,
Expand Down