Skip to content

Commit 7911a75

Browse files
authored
Merge pull request #222 from arunsathiya/ngrok/switch-to-env-var-provisioners
update(ngrok): switch to envvars provisioner for ngrok 3.2.1 and higher
2 parents 70073da + 6e84778 commit 7911a75

7 files changed

Lines changed: 92 additions & 4 deletions

File tree

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ require (
5151
github.com/oklog/run v1.0.0 // indirect
5252
github.com/pmezard/go-difflib v1.0.0 // indirect
5353
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 // indirect
54+
golang.org/x/mod v0.9.0
5455
golang.org/x/net v0.8.0 // indirect
5556
golang.org/x/sys v0.6.0 // indirect
5657
golang.org/x/term v0.6.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL
136136
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
137137
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
138138
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
139+
golang.org/x/mod v0.9.0 h1:KENHtAZL2y3NLMYZeHY9DW8HW8V+kQyJsY/V9JlKvCs=
140+
golang.org/x/mod v0.9.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
139141
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
140142
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
141143
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=

plugins/ngrok/credentials.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func Credentials() schema.CredentialType {
4545
},
4646
},
4747
},
48-
DefaultProvisioner: newNgrokProvisioner(),
48+
DefaultProvisioner: ngrokEnvVarProvisioner{},
4949
Importer: importer.TryAll(
5050
importer.TryEnvVarPair(defaultEnvVarMapping),
5151
importer.MacOnly(

plugins/ngrok/credentials_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212
func TestCredentialsProvisioner(t *testing.T) {
13-
plugintest.TestProvisioner(t, Credentials().DefaultProvisioner, map[string]plugintest.ProvisionCase{
13+
plugintest.TestProvisioner(t, ngrokProvisioner(), map[string]plugintest.ProvisionCase{
1414
"temp file": {
1515
ItemFields: map[sdk.FieldName]string{
1616
fieldname.Authtoken: "uSuQ7LUOJLs4xRbIySZ15F4v5KxfTnMknMdFEXAMPLE",
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package ngrok
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/1Password/shell-plugins/sdk"
8+
"github.com/1Password/shell-plugins/sdk/schema/fieldname"
9+
"golang.org/x/mod/semver"
10+
)
11+
12+
type ngrokEnvVarProvisioner struct {
13+
}
14+
15+
func (p ngrokEnvVarProvisioner) Provision(ctx context.Context, in sdk.ProvisionInput, out *sdk.ProvisionOutput) {
16+
currentVersion, err := getNgrokVersion()
17+
if err != nil {
18+
out.AddError(err)
19+
return
20+
}
21+
22+
// If the current ngrok CLI version is 3.2.1 or higher,
23+
// use environment variables to provision the Shell Plugin credentials
24+
//
25+
// semver.Compare resulting in 0 means 3.2.1 is in use
26+
// semver.Compare resulting in +1 means >3.2.1 is in use
27+
if semver.Compare(currentVersion, envVarAuthVersion) == -1 {
28+
out.AddError(fmt.Errorf("ngrok version %s is not supported. Please upgrade to version %s or higher", currentVersion, envVarAuthVersion))
29+
return
30+
}
31+
32+
out.AddEnvVar("NGROK_AUTHTOKEN", in.ItemFields[fieldname.Authtoken])
33+
out.AddEnvVar("NGROK_API_KEY", in.ItemFields[fieldname.APIKey])
34+
}
35+
36+
func (p ngrokEnvVarProvisioner) Deprovision(ctx context.Context, in sdk.DeprovisionInput, out *sdk.DeprovisionOutput) {
37+
// Nothing to do here: environment variables get wiped automatically when the process exits.
38+
}
39+
40+
func (p ngrokEnvVarProvisioner) Description() string {
41+
return "Provision ngrok credentials as environment variables NGROK_AUTH_TOKEN and NGROK_API_KEY"
42+
}

plugins/ngrok/ngrok.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ func ngrokCLI() schema.Executable {
2323
),
2424
Uses: []schema.CredentialUsage{
2525
{
26-
Name: credname.Credentials,
26+
Name: credname.Credentials,
27+
Provisioner: ngrokProvisioner(),
2728
},
2829
},
2930
}

plugins/ngrok/provisioner.go

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@ package ngrok
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"os"
8+
"os/exec"
79
"path/filepath"
810
"runtime"
911
"strings"
1012

1113
"github.com/1Password/shell-plugins/sdk"
1214
"github.com/1Password/shell-plugins/sdk/importer"
1315
"github.com/1Password/shell-plugins/sdk/schema/fieldname"
16+
"golang.org/x/mod/semver"
1417
"gopkg.in/yaml.v3"
1518
)
1619

@@ -19,12 +22,30 @@ const (
1922
apiKeyYamlName = "api_key"
2023
authTokenYamlName = "authtoken"
2124
versionYamlName = "version"
25+
envVarAuthVersion = "v3.2.1"
2226
)
2327

2428
type fileProvisioner struct {
2529
}
2630

27-
func newNgrokProvisioner() sdk.Provisioner {
31+
func ngrokProvisioner() sdk.Provisioner {
32+
currentVersion, err := getNgrokVersion()
33+
if err != nil {
34+
// When ngrok version check fails for any reason,
35+
// use config file to provision as a fallback
36+
return fileProvisioner{}
37+
}
38+
39+
// If the current ngrok CLI version is 3.2.1 or higher,
40+
// use environment variables to provision credentials
41+
//
42+
// semver.Compare resulting in 0 means 3.2.1 is in use
43+
// semver.Compare resulting in +1 means >3.2.1 is in use
44+
if semver.Compare(currentVersion, envVarAuthVersion) >= 0 {
45+
return ngrokEnvVarProvisioner{}
46+
}
47+
48+
// Otherwise use config file to provision credentials
2849
return fileProvisioner{}
2950
}
3051

@@ -95,6 +116,27 @@ func getConfigValueAndNewArgs(args []string, newFilePath string) (string, []stri
95116
return "", append(args, fmt.Sprintf("--config=%s", newFilePath))
96117
}
97118

119+
// Get installed ngrok version and required version
120+
func getNgrokVersion() (string, error) {
121+
cmd := exec.Command("ngrok", "--version")
122+
ngrokVersion, err := cmd.Output()
123+
if err != nil {
124+
return "", errors.New("ngrok not found")
125+
}
126+
127+
// Example: "ngrok version 3.1.1\n" to "3.1.1\n"
128+
currentVersion := strings.TrimPrefix(string(ngrokVersion), "ngrok version ")
129+
130+
// Example: "3.1.1\n" to "3.1.1"
131+
currentVersion = strings.Trim(currentVersion, "\n")
132+
133+
// Example: "3.1.1" to "v3.1.1" as that's the format
134+
// the package semver expects
135+
currentVersion = "v" + currentVersion
136+
137+
return currentVersion, nil
138+
}
139+
98140
func (f fileProvisioner) Deprovision(ctx context.Context, in sdk.DeprovisionInput, out *sdk.DeprovisionOutput) {
99141
// nothing to do here: files get deleted automatically by 1Password CLI
100142
}

0 commit comments

Comments
 (0)