-
Notifications
You must be signed in to change notification settings - Fork 160
/
credentials.go
91 lines (83 loc) · 2.44 KB
/
credentials.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package ngrok
import (
"context"
"github.com/1Password/shell-plugins/sdk"
"github.com/1Password/shell-plugins/sdk/importer"
"github.com/1Password/shell-plugins/sdk/schema"
"github.com/1Password/shell-plugins/sdk/schema/credname"
"github.com/1Password/shell-plugins/sdk/schema/fieldname"
)
func Credentials() schema.CredentialType {
return schema.CredentialType{
Name: credname.Credentials,
DocsURL: sdk.URL("https://ngrok.com/docs/ngrok-agent/config"),
Fields: []schema.CredentialField{
{
Name: fieldname.Authtoken,
AlternativeNames: []string{"Auth Token"},
MarkdownDescription: "Authtoken used to authenticate to ngrok.",
Optional: false,
Secret: true,
Composition: &schema.ValueComposition{
Length: 49,
Charset: schema.Charset{
Uppercase: true,
Lowercase: true,
Digits: true,
},
},
},
{
Name: fieldname.APIKey,
MarkdownDescription: "API Key used to authenticate to ngrok API.",
Optional: true,
Secret: true,
Composition: &schema.ValueComposition{
Length: 49,
Charset: schema.Charset{
Uppercase: true,
Lowercase: true,
Digits: true,
},
},
},
},
DefaultProvisioner: ngrokEnvVarProvisioner{},
Importer: importer.TryAll(
importer.TryEnvVarPair(defaultEnvVarMapping),
importer.MacOnly(
TryngrokConfigFile("~/Library/Application Support/ngrok/ngrok.yml"),
),
importer.LinuxOnly(
TryngrokConfigFile("~/.config/ngrok/ngrok.yml"),
),
)}
}
var defaultEnvVarMapping = map[string]sdk.FieldName{
"NGROK_AUTHTOKEN": fieldname.Authtoken,
"NGROK_API_KEY": fieldname.APIKey,
}
func TryngrokConfigFile(path string) sdk.Importer {
return importer.TryFile(path, func(ctx context.Context, contents importer.FileContents, in sdk.ImportInput, out *sdk.ImportAttempt) {
var config Config
if err := contents.ToYAML(&config); err != nil {
out.AddError(err)
return
}
if config.AuthToken == "" || config.APIKey == "" || config.Version == "" {
return
}
out.AddCandidate(sdk.ImportCandidate{
Fields: map[sdk.FieldName]string{
fieldname.Authtoken: config.AuthToken,
fieldname.APIKey: config.APIKey,
},
})
})
}
// Config struct is exhaustive, covering all documented configurations.
type Config struct {
AuthToken string `yaml:"authtoken"`
APIKey string `yaml:"api_key"`
Version string `yaml:"version"`
}