From 7d1fd21100302a0ec64d4cf43b4a6c3d20fd1851 Mon Sep 17 00:00:00 2001 From: Omar Miraj Date: Wed, 10 Jul 2024 11:40:44 -0400 Subject: [PATCH 1/4] Remove ngrok version check everytime a op plugin command is called --- plugins/ngrok/credentials.go | 2 +- plugins/ngrok/credentials_test.go | 2 +- plugins/ngrok/env_var_provisioner.go | 42 ---------------------------- plugins/ngrok/ngrok.go | 3 +- plugins/ngrok/provisioner.go | 20 +++++++++---- 5 files changed, 17 insertions(+), 52 deletions(-) delete mode 100644 plugins/ngrok/env_var_provisioner.go diff --git a/plugins/ngrok/credentials.go b/plugins/ngrok/credentials.go index 4bb805d4f..57f10cd6a 100644 --- a/plugins/ngrok/credentials.go +++ b/plugins/ngrok/credentials.go @@ -45,7 +45,7 @@ func Credentials() schema.CredentialType { }, }, }, - DefaultProvisioner: ngrokEnvVarProvisioner{}, + DefaultProvisioner: fileProvisioner{}, Importer: importer.TryAll( importer.TryEnvVarPair(defaultEnvVarMapping), importer.MacOnly( diff --git a/plugins/ngrok/credentials_test.go b/plugins/ngrok/credentials_test.go index d45784eb4..51ac03f21 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, fileProvisioner{}, 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..5a6095052 100644 --- a/plugins/ngrok/provisioner.go +++ b/plugins/ngrok/provisioner.go @@ -28,12 +28,12 @@ const ( type fileProvisioner struct { } -func ngrokProvisioner() sdk.Provisioner { +func IsNgrokAPIKeySupported() 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,22 @@ 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) { + ngrokProvisioner := IsNgrokAPIKeySupported() + + if ngrokProvisioner { + 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{}) @@ -138,9 +146,9 @@ func getNgrokVersion() (string, error) { } func (f fileProvisioner) Deprovision(ctx context.Context, in sdk.DeprovisionInput, out *sdk.DeprovisionOutput) { - // nothing to do here: files get deleted automatically by 1Password CLI + // 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." + 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." } From aeb2307849bd932c059b7cc947e2f56bb744dcab Mon Sep 17 00:00:00 2001 From: Mohammed Omar Miraj <103403052+MOmarMiraj@users.noreply.github.com> Date: Fri, 12 Jul 2024 09:12:14 -0400 Subject: [PATCH 2/4] Update plugins/ngrok/provisioner.go Co-authored-by: Andi Titu <45081667+AndyTitu@users.noreply.github.com> --- plugins/ngrok/provisioner.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/ngrok/provisioner.go b/plugins/ngrok/provisioner.go index 5a6095052..ab308672d 100644 --- a/plugins/ngrok/provisioner.go +++ b/plugins/ngrok/provisioner.go @@ -150,5 +150,5 @@ func (f fileProvisioner) Deprovision(ctx context.Context, in sdk.DeprovisionInpu } func (f fileProvisioner) 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." + return "If ngrok version is 3.2.1 or higher then provision ngrok credentials as the environment variables NGROK_AUTH_TOKEN and NGROK_API_KEY, otherwise use config file aware provisioner. It will first check if an already existing config file is present." } From 52971335f5d2b9054f57e0e8c0a8e89fbd6386ef Mon Sep 17 00:00:00 2001 From: Omar Miraj Date: Fri, 12 Jul 2024 09:37:46 -0400 Subject: [PATCH 3/4] Change naming of function and struct and clean up code --- plugins/ngrok/credentials.go | 2 +- plugins/ngrok/credentials_test.go | 2 +- plugins/ngrok/provisioner.go | 13 ++++++------- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/plugins/ngrok/credentials.go b/plugins/ngrok/credentials.go index 57f10cd6a..9037f571a 100644 --- a/plugins/ngrok/credentials.go +++ b/plugins/ngrok/credentials.go @@ -45,7 +45,7 @@ func Credentials() schema.CredentialType { }, }, }, - DefaultProvisioner: fileProvisioner{}, + 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 51ac03f21..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, fileProvisioner{}, 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/provisioner.go b/plugins/ngrok/provisioner.go index 5a6095052..ab8e08dce 100644 --- a/plugins/ngrok/provisioner.go +++ b/plugins/ngrok/provisioner.go @@ -25,10 +25,10 @@ const ( envVarAuthVersion = "v3.2.1" ) -type fileProvisioner struct { +type ngrokProvisioner struct { } -func IsNgrokAPIKeySupported() bool { +func HasEnvVarSupport() bool { currentVersion, err := getNgrokVersion() if err != nil { // When ngrok version check fails for any reason, @@ -49,10 +49,9 @@ func IsNgrokAPIKeySupported() bool { return false } -func (f fileProvisioner) Provision(ctx context.Context, in sdk.ProvisionInput, out *sdk.ProvisionOutput) { - ngrokProvisioner := IsNgrokAPIKeySupported() +func (p ngrokProvisioner) Provision(ctx context.Context, in sdk.ProvisionInput, out *sdk.ProvisionOutput) { - if ngrokProvisioner { + if ngrokProvisioner := HasEnvVarSupport(); ngrokProvisioner { out.AddEnvVar("NGROK_AUTHTOKEN", in.ItemFields[fieldname.Authtoken]) out.AddEnvVar("NGROK_API_KEY", in.ItemFields[fieldname.APIKey]) return @@ -145,10 +144,10 @@ func getNgrokVersion() (string, error) { return currentVersion, nil } -func (f fileProvisioner) Deprovision(ctx context.Context, in sdk.DeprovisionInput, out *sdk.DeprovisionOutput) { +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 { +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." } From 13629e5c65ed5bac297a8e122f663047dd171e63 Mon Sep 17 00:00:00 2001 From: Omar Miraj Date: Fri, 12 Jul 2024 09:37:46 -0400 Subject: [PATCH 4/4] Change naming of function and struct and clean up code Signed-off-by: Omar Miraj --- plugins/ngrok/credentials.go | 2 +- plugins/ngrok/credentials_test.go | 2 +- plugins/ngrok/provisioner.go | 13 ++++++------- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/plugins/ngrok/credentials.go b/plugins/ngrok/credentials.go index 57f10cd6a..9037f571a 100644 --- a/plugins/ngrok/credentials.go +++ b/plugins/ngrok/credentials.go @@ -45,7 +45,7 @@ func Credentials() schema.CredentialType { }, }, }, - DefaultProvisioner: fileProvisioner{}, + 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 51ac03f21..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, fileProvisioner{}, 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/provisioner.go b/plugins/ngrok/provisioner.go index 5a6095052..ab8e08dce 100644 --- a/plugins/ngrok/provisioner.go +++ b/plugins/ngrok/provisioner.go @@ -25,10 +25,10 @@ const ( envVarAuthVersion = "v3.2.1" ) -type fileProvisioner struct { +type ngrokProvisioner struct { } -func IsNgrokAPIKeySupported() bool { +func HasEnvVarSupport() bool { currentVersion, err := getNgrokVersion() if err != nil { // When ngrok version check fails for any reason, @@ -49,10 +49,9 @@ func IsNgrokAPIKeySupported() bool { return false } -func (f fileProvisioner) Provision(ctx context.Context, in sdk.ProvisionInput, out *sdk.ProvisionOutput) { - ngrokProvisioner := IsNgrokAPIKeySupported() +func (p ngrokProvisioner) Provision(ctx context.Context, in sdk.ProvisionInput, out *sdk.ProvisionOutput) { - if ngrokProvisioner { + if ngrokProvisioner := HasEnvVarSupport(); ngrokProvisioner { out.AddEnvVar("NGROK_AUTHTOKEN", in.ItemFields[fieldname.Authtoken]) out.AddEnvVar("NGROK_API_KEY", in.ItemFields[fieldname.APIKey]) return @@ -145,10 +144,10 @@ func getNgrokVersion() (string, error) { return currentVersion, nil } -func (f fileProvisioner) Deprovision(ctx context.Context, in sdk.DeprovisionInput, out *sdk.DeprovisionOutput) { +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 { +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." }