|
| 1 | +package ngrok |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/1Password/shell-plugins/sdk" |
| 7 | + "github.com/1Password/shell-plugins/sdk/importer" |
| 8 | + "github.com/1Password/shell-plugins/sdk/provision" |
| 9 | + "github.com/1Password/shell-plugins/sdk/schema" |
| 10 | + "github.com/1Password/shell-plugins/sdk/schema/credname" |
| 11 | + "github.com/1Password/shell-plugins/sdk/schema/fieldname" |
| 12 | + "gopkg.in/yaml.v3" |
| 13 | +) |
| 14 | + |
| 15 | +func Credentials() schema.CredentialType { |
| 16 | + return schema.CredentialType{ |
| 17 | + Name: credname.Credentials, |
| 18 | + DocsURL: sdk.URL("https://ngrok.com/docs/ngrok-agent/config"), |
| 19 | + Fields: []schema.CredentialField{ |
| 20 | + { |
| 21 | + Name: fieldname.AuthToken, |
| 22 | + MarkdownDescription: "Auth Token used to authenticate to ngrok.", |
| 23 | + Optional: false, |
| 24 | + Secret: true, |
| 25 | + Composition: &schema.ValueComposition{ |
| 26 | + Length: 43, |
| 27 | + Charset: schema.Charset{ |
| 28 | + Uppercase: true, |
| 29 | + Lowercase: true, |
| 30 | + Digits: true, |
| 31 | + }, |
| 32 | + }, |
| 33 | + }, |
| 34 | + { |
| 35 | + Name: fieldname.APIKey, |
| 36 | + MarkdownDescription: "API Key used to authenticate to ngrok API.", |
| 37 | + Optional: true, |
| 38 | + Secret: true, |
| 39 | + Composition: &schema.ValueComposition{ |
| 40 | + Length: 48, |
| 41 | + Charset: schema.Charset{ |
| 42 | + Uppercase: true, |
| 43 | + Lowercase: true, |
| 44 | + Digits: true, |
| 45 | + }, |
| 46 | + }, |
| 47 | + }, |
| 48 | + }, |
| 49 | + DefaultProvisioner: provision.TempFile(ngrokConfig, provision.Filename("config.yml"), provision.AddArgs("--config", "{{ .Path }}")), |
| 50 | + Importer: importer.TryAll( |
| 51 | + importer.TryEnvVarPair(defaultEnvVarMapping), |
| 52 | + importer.MacOnly( |
| 53 | + TryngrokConfigFile("~/Library/Application Support/ngrok/ngrok.yml"), |
| 54 | + ), |
| 55 | + importer.LinuxOnly( |
| 56 | + TryngrokConfigFile("~/.config/ngrok/ngrok.yml"), |
| 57 | + ), |
| 58 | + )} |
| 59 | +} |
| 60 | + |
| 61 | +func ngrokConfig(in sdk.ProvisionInput) ([]byte, error) { |
| 62 | + config := Config{ |
| 63 | + AuthToken: in.ItemFields[fieldname.AuthToken], |
| 64 | + APIKey: in.ItemFields[fieldname.APIKey], |
| 65 | + Version: "2", // required field for ngrok CLI to work when file-based configuration is used; automatically configured by the CLI program and is not configurable by the user |
| 66 | + } |
| 67 | + contents, err := yaml.Marshal(&config) |
| 68 | + if err != nil { |
| 69 | + return nil, err |
| 70 | + } |
| 71 | + return []byte(contents), nil |
| 72 | +} |
| 73 | + |
| 74 | +var defaultEnvVarMapping = map[string]sdk.FieldName{ |
| 75 | + "NGROK_AUTHTOKEN": fieldname.AuthToken, |
| 76 | + "NGROK_API_KEY": fieldname.APIKey, |
| 77 | +} |
| 78 | + |
| 79 | +func TryngrokConfigFile(path string) sdk.Importer { |
| 80 | + return importer.TryFile(path, func(ctx context.Context, contents importer.FileContents, in sdk.ImportInput, out *sdk.ImportAttempt) { |
| 81 | + var config Config |
| 82 | + if err := contents.ToYAML(&config); err != nil { |
| 83 | + out.AddError(err) |
| 84 | + return |
| 85 | + } |
| 86 | + |
| 87 | + if config.AuthToken == "" || config.APIKey == "" || config.Version == "" { |
| 88 | + return |
| 89 | + } |
| 90 | + |
| 91 | + out.AddCandidate(sdk.ImportCandidate{ |
| 92 | + Fields: map[sdk.FieldName]string{ |
| 93 | + fieldname.AuthToken: config.AuthToken, |
| 94 | + fieldname.APIKey: config.APIKey, |
| 95 | + }, |
| 96 | + }) |
| 97 | + }) |
| 98 | +} |
| 99 | + |
| 100 | +type Config struct { |
| 101 | + AuthToken string `yaml:"authtoken"` |
| 102 | + APIKey string `yaml:"api_key"` |
| 103 | + Version string |
| 104 | +} |
0 commit comments