diff --git a/plugins/ngrok/credentials.go b/plugins/ngrok/credentials.go index 4bb805d4f..9037f571a 100644 --- a/plugins/ngrok/credentials.go +++ b/plugins/ngrok/credentials.go @@ -45,7 +45,7 @@ func Credentials() schema.CredentialType { }, }, }, - DefaultProvisioner: ngrokEnvVarProvisioner{}, + DefaultProvisioner: ngrokProvisioner{}, Importer: importer.TryAll( importer.TryEnvVarPair(defaultEnvVarMapping), importer.MacOnly( diff --git a/plugins/ngrok/credentials_test.go b/plugins/ngrok/credentials_test.go index d45784eb4..4b09f33ff 100644 --- a/plugins/ngrok/credentials_test.go +++ b/plugins/ngrok/credentials_test.go @@ -10,7 +10,7 @@ import ( ) func TestCredentialsProvisioner(t *testing.T) { - plugintest.TestProvisioner(t, ngrokProvisioner(), map[string]plugintest.ProvisionCase{ + plugintest.TestProvisioner(t, ngrokProvisioner{}, map[string]plugintest.ProvisionCase{ "temp file": { ItemFields: map[sdk.FieldName]string{ fieldname.Authtoken: "cxG2Im21Yzkh8VnvFQaetlPHcQ9ZDUUk1IzzyHhcGcEXAMPLE", diff --git a/plugins/ngrok/env_var_provisioner.go b/plugins/ngrok/env_var_provisioner.go deleted file mode 100644 index 9517f16d3..000000000 --- a/plugins/ngrok/env_var_provisioner.go +++ /dev/null @@ -1,42 +0,0 @@ -package ngrok - -import ( - "context" - "fmt" - - "github.com/1Password/shell-plugins/sdk" - "github.com/1Password/shell-plugins/sdk/schema/fieldname" - "golang.org/x/mod/semver" -) - -type ngrokEnvVarProvisioner struct { -} - -func (p ngrokEnvVarProvisioner) Provision(ctx context.Context, in sdk.ProvisionInput, out *sdk.ProvisionOutput) { - currentVersion, err := getNgrokVersion() - if err != nil { - out.AddError(err) - return - } - - // If the current ngrok CLI version is 3.2.1 or higher, - // use environment variables to provision the Shell Plugin credentials - // - // semver.Compare resulting in 0 means 3.2.1 is in use - // semver.Compare resulting in +1 means >3.2.1 is in use - if semver.Compare(currentVersion, envVarAuthVersion) == -1 { - out.AddError(fmt.Errorf("ngrok version %s is not supported. Please upgrade to version %s or higher", currentVersion, envVarAuthVersion)) - return - } - - out.AddEnvVar("NGROK_AUTHTOKEN", in.ItemFields[fieldname.Authtoken]) - out.AddEnvVar("NGROK_API_KEY", in.ItemFields[fieldname.APIKey]) -} - -func (p ngrokEnvVarProvisioner) Deprovision(ctx context.Context, in sdk.DeprovisionInput, out *sdk.DeprovisionOutput) { - // Nothing to do here: environment variables get wiped automatically when the process exits. -} - -func (p ngrokEnvVarProvisioner) Description() string { - return "Provision ngrok credentials as environment variables NGROK_AUTH_TOKEN and NGROK_API_KEY" -} diff --git a/plugins/ngrok/ngrok.go b/plugins/ngrok/ngrok.go index acbd34fed..295fcd857 100644 --- a/plugins/ngrok/ngrok.go +++ b/plugins/ngrok/ngrok.go @@ -23,8 +23,7 @@ func ngrokCLI() schema.Executable { ), Uses: []schema.CredentialUsage{ { - Name: credname.Credentials, - Provisioner: ngrokProvisioner(), + Name: credname.Credentials, }, }, } diff --git a/plugins/ngrok/provisioner.go b/plugins/ngrok/provisioner.go index 68847359a..9d1aaf907 100644 --- a/plugins/ngrok/provisioner.go +++ b/plugins/ngrok/provisioner.go @@ -25,15 +25,15 @@ const ( envVarAuthVersion = "v3.2.1" ) -type fileProvisioner struct { +type ngrokProvisioner struct { } -func ngrokProvisioner() sdk.Provisioner { +func HasEnvVarSupport() bool { currentVersion, err := getNgrokVersion() if err != nil { // When ngrok version check fails for any reason, // use config file to provision as a fallback - return fileProvisioner{} + return false } // If the current ngrok CLI version is 3.2.1 or higher, @@ -42,14 +42,21 @@ func ngrokProvisioner() sdk.Provisioner { // semver.Compare resulting in 0 means 3.2.1 is in use // semver.Compare resulting in +1 means >3.2.1 is in use if semver.Compare(currentVersion, envVarAuthVersion) >= 0 { - return ngrokEnvVarProvisioner{} + return true } // Otherwise use config file to provision credentials - return fileProvisioner{} + return false } -func (f fileProvisioner) Provision(ctx context.Context, in sdk.ProvisionInput, out *sdk.ProvisionOutput) { +func (p ngrokProvisioner) Provision(ctx context.Context, in sdk.ProvisionInput, out *sdk.ProvisionOutput) { + + if HasEnvVarSupport() { + out.AddEnvVar("NGROK_AUTHTOKEN", in.ItemFields[fieldname.Authtoken]) + out.AddEnvVar("NGROK_API_KEY", in.ItemFields[fieldname.APIKey]) + return + } + provisionedConfigFilePath := filepath.Join(in.TempDir, "config.yml") config := make(map[string]interface{}) @@ -137,10 +144,10 @@ func getNgrokVersion() (string, error) { return currentVersion, nil } -func (f fileProvisioner) Deprovision(ctx context.Context, in sdk.DeprovisionInput, out *sdk.DeprovisionOutput) { - // nothing to do here: files get deleted automatically by 1Password CLI +func (p ngrokProvisioner) Deprovision(ctx context.Context, in sdk.DeprovisionInput, out *sdk.DeprovisionOutput) { + // nothing to do here: files get deleted automatically by 1Password CLI and environment variables get wiped when process exits } -func (f fileProvisioner) Description() string { - return "Config file aware provisioner. It will first check if an already existing config file is present." +func (p ngrokProvisioner) Description() string { + return "If ngrok version is 3.2.1 or higher than provision ngrok credentials as environment variables NGROK_AUTH_TOKEN and NGROK_API_KEY otherwise config file aware provisioner. It will first check if an already existing config file is present." }