Skip to content

Commit

Permalink
Remove ngrok version check everytime a op plugin command is called
Browse files Browse the repository at this point in the history
  • Loading branch information
MOmarMiraj committed Jul 10, 2024
1 parent d36189b commit 7d1fd21
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 52 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: fileProvisioner{},
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, fileProvisioner{}, 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
20 changes: 14 additions & 6 deletions plugins/ngrok/provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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{})

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

0 comments on commit 7d1fd21

Please sign in to comment.