Skip to content

Commit

Permalink
Merge pull request #474 from 1Password/omar/fix-ngrok-version-check
Browse files Browse the repository at this point in the history
[BUG FIX] Remove ngrok version check everytime a op plugin command is called
  • Loading branch information
MOmarMiraj authored Sep 25, 2024
2 parents d36189b + 67fff70 commit 36e4bf4
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 56 deletions.
2 changes: 1 addition & 1 deletion plugins/ngrok/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func Credentials() schema.CredentialType {
},
},
},
DefaultProvisioner: ngrokEnvVarProvisioner{},
DefaultProvisioner: ngrokProvisioner{},
Importer: importer.TryAll(
importer.TryEnvVarPair(defaultEnvVarMapping),
importer.MacOnly(
Expand Down
2 changes: 1 addition & 1 deletion plugins/ngrok/credentials_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
42 changes: 0 additions & 42 deletions plugins/ngrok/env_var_provisioner.go

This file was deleted.

3 changes: 1 addition & 2 deletions plugins/ngrok/ngrok.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ func ngrokCLI() schema.Executable {
),
Uses: []schema.CredentialUsage{
{
Name: credname.Credentials,
Provisioner: ngrokProvisioner(),
Name: credname.Credentials,
},
},
}
Expand Down
27 changes: 17 additions & 10 deletions plugins/ngrok/provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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{})

Expand Down Expand Up @@ -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."
}

0 comments on commit 36e4bf4

Please sign in to comment.