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 Fastly with envvar provisioning and config file imports #169

Merged
merged 2 commits into from Feb 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
77 changes: 77 additions & 0 deletions plugins/fastly/api_token.go
@@ -0,0 +1,77 @@
package fastly

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://docs.fastly.com/en/guides/using-api-tokens"),
ManagementURL: sdk.URL("https://manage.fastly.com/account/personal/tokens"),
Fields: []schema.CredentialField{
{
Name: fieldname.Token,
MarkdownDescription: "API Token used to authenticate to Fastly.",
Secret: true,
Composition: &schema.ValueComposition{
Length: 32,
Charset: schema.Charset{
Uppercase: true,
Lowercase: true,
Digits: true,
},
},
},
},
DefaultProvisioner: provision.EnvVars(defaultEnvVarMapping),
Importer: importer.TryAll(
importer.TryAllEnvVars(fieldname.Token, "FASTLY_API_TOKEN"),
importer.MacOnly(
TryFastlyConfigFile("~/Library/Application Support/fastly/config.toml"),
AndyTitu marked this conversation as resolved.
Show resolved Hide resolved
),
importer.LinuxOnly(
TryFastlyConfigFile("~/.config/fastly/config.toml"),
),
),
}
}

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

func TryFastlyConfigFile(path string) sdk.Importer {
return importer.TryFile(path, func(ctx context.Context, contents importer.FileContents, in sdk.ImportInput, out *sdk.ImportAttempt) {
var config Config
if err := contents.ToTOML(&config); err != nil {
out.AddError(err)
return
}

for profileName, configProfile := range config.Profile {
out.AddCandidate(sdk.ImportCandidate{
Fields: map[sdk.FieldName]string{
fieldname.Token: configProfile.Token,
},
NameHint: importer.SanitizeNameHint(profileName),
})
}
})
}

type ConfigProfile struct {
Email string `toml:"email"`
Token string `toml:"token"`
}

type Config struct {
Profile map[string]ConfigProfile
}
93 changes: 93 additions & 0 deletions plugins/fastly/api_token_test.go
@@ -0,0 +1,93 @@
package fastly

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: "4Oncq0V723ZO8HIqUgOTB77dsEXAMPLE",
},
ExpectedOutput: sdk.ProvisionOutput{
Environment: map[string]string{
"FASTLY_API_TOKEN": "4Oncq0V723ZO8HIqUgOTB77dsEXAMPLE",
},
},
},
})
}

func TestAPITokenImporter(t *testing.T) {
plugintest.TestImporter(t, APIToken().Importer, map[string]plugintest.ImportCase{
"environment": {
Environment: map[string]string{
"FASTLY_API_TOKEN": "4Oncq0V723ZO8HIqUgOTB77dsEXAMPLE",
},
ExpectedCandidates: []sdk.ImportCandidate{
{
Fields: map[sdk.FieldName]string{
fieldname.Token: "4Oncq0V723ZO8HIqUgOTB77dsEXAMPLE",
},
},
},
},
"config file on macOS": {
OS: "darwin",
Files: map[string]string{
"~/Library/Application Support/fastly/config.toml": plugintest.LoadFixture(t, "config.toml"),
},
ExpectedCandidates: []sdk.ImportCandidate{
{
Fields: map[sdk.FieldName]string{
fieldname.Token: "4Oncq0V723ZO8HIqUgOTB77dsEXAMPLE",
},
NameHint: "first",
},
{
Fields: map[sdk.FieldName]string{
fieldname.Token: "NyK5NwkqXpuf74Le0omvFVUtZEXAMPLE",
},
NameHint: "second",
},
{
Fields: map[sdk.FieldName]string{
fieldname.Token: "L2IofeJGtvwy1fDSKNj5dEIRgEXAMPLE",
},
NameHint: "third",
},
},
},
"config file on Linux": {
OS: "linux",
Files: map[string]string{
"~/.config/fastly/config.toml": plugintest.LoadFixture(t, "config.toml"),
},
ExpectedCandidates: []sdk.ImportCandidate{
{
Fields: map[sdk.FieldName]string{
fieldname.Token: "4Oncq0V723ZO8HIqUgOTB77dsEXAMPLE",
},
NameHint: "first",
},
{
Fields: map[sdk.FieldName]string{
fieldname.Token: "NyK5NwkqXpuf74Le0omvFVUtZEXAMPLE",
},
NameHint: "second",
},
{
Fields: map[sdk.FieldName]string{
fieldname.Token: "L2IofeJGtvwy1fDSKNj5dEIRgEXAMPLE",
},
NameHint: "third",
},
},
},
})
}
29 changes: 29 additions & 0 deletions plugins/fastly/fastly.go
@@ -0,0 +1,29 @@
package fastly

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 FastlyCLI() schema.Executable {
return schema.Executable{
Name: "Fastly CLI",
Runs: []string{"fastly"},
DocsURL: sdk.URL("https://developer.fastly.com/reference/cli/"),
NeedsAuth: needsauth.IfAll(
needsauth.NotForHelpOrVersion(),
needsauth.NotWithoutArgs(),
needsauth.NotWhenContainsArgs("-t"),
needsauth.NotWhenContainsArgs("--token"),
needsauth.NotWhenContainsArgs("profile"),
needsauth.NotWhenContainsArgs("config"),
),
Uses: []schema.CredentialUsage{
{
Name: credname.APIToken,
},
},
}
}
22 changes: 22 additions & 0 deletions plugins/fastly/plugin.go
@@ -0,0 +1,22 @@
package fastly

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

func New() schema.Plugin {
return schema.Plugin{
Name: "fastly",
Platform: schema.PlatformInfo{
Name: "Fastly",
Homepage: sdk.URL("https://fastly.com"),
},
Credentials: []schema.CredentialType{
APIToken(),
},
Executables: []schema.Executable{
FastlyCLI(),
},
}
}
16 changes: 16 additions & 0 deletions plugins/fastly/test-fixtures/config.toml
@@ -0,0 +1,16 @@
[profile]

[profile.first]
default = true
email = "user@example.com"
token = "4Oncq0V723ZO8HIqUgOTB77dsEXAMPLE"

[profile.second]
default = false
email = "user-2@example.com"
token = "NyK5NwkqXpuf74Le0omvFVUtZEXAMPLE"

[profile.third]
default = false
email = "user-3@example.com"
token = "L2IofeJGtvwy1fDSKNj5dEIRgEXAMPLE"