-
Notifications
You must be signed in to change notification settings - Fork 94
feat(secretserver): use official secretserver library #930
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,126 +2,85 @@ package secretserver | |
|
|
||
| import ( | ||
| "crypto/tls" | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "net/http" | ||
| "os" | ||
| "strconv" | ||
| "strings" | ||
|
|
||
| tssSdk "github.com/DelineaXPM/tss-sdk-go/v3/server" | ||
|
|
||
| "github.com/helmfile/vals/pkg/api" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and a blank line.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| ) | ||
|
|
||
| type secretServerSecret struct { | ||
| Items []secretServerSecretItem `json:"items"` | ||
| } | ||
|
|
||
| type secretServerSecretItem struct { | ||
| Slug string `json:"slug"` | ||
| ItemValue string `json:"itemValue"` | ||
| } | ||
|
|
||
| type provider struct { | ||
| APIVersion string | ||
| SSLVerify bool | ||
| tss tssSdk.Server | ||
| } | ||
|
|
||
| func New(cfg api.StaticConfig) *provider { | ||
| p := &provider{} | ||
| v := cfg.String("ssl_verify") | ||
| p.SSLVerify = v != "false" | ||
|
|
||
| if a := cfg.String("api_version"); a == "" { | ||
| p.APIVersion = "v1" | ||
| } else { | ||
| p.APIVersion = a | ||
| func New(cfg api.StaticConfig) (*provider, error) { | ||
| tss, err := tssSdk.New(tssSdk.Configuration{ | ||
| Credentials: tssSdk.UserCredential{ | ||
| Domain: os.Getenv("TSS_DOMAIN"), | ||
| Username: os.Getenv("TSS_USERNAME"), | ||
| Password: os.Getenv("TSS_PASSWORD"), | ||
| Token: os.Getenv("TSS_TOKEN"), | ||
| }, | ||
| ServerURL: os.Getenv("TSS_SERVER_URL"), | ||
| TLD: os.Getenv("TSS_TLD"), | ||
| Tenant: os.Getenv("TSS_TENANT"), | ||
| TLSClientConfig: &tls.Config{InsecureSkipVerify: cfg.String("ssl_verify") == "false"}, | ||
| }) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return p | ||
| return &provider{tss: *tss}, nil | ||
| } | ||
|
|
||
| func (p *provider) GetString(key string) (string, error) { | ||
| splits := strings.Split(key, "/") | ||
| if len(splits) != 2 { | ||
| return "", fmt.Errorf("malformed key") | ||
| return "", fmt.Errorf("malformed key '%s'", key) | ||
| } | ||
| secretID := splits[0] | ||
| fieldName := splits[1] | ||
|
|
||
| g, err := p.getSecret(secretID) | ||
| secret, err := p.getSecret(secretID) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| for _, item := range g.Items { | ||
| if item.Slug == fieldName { | ||
| return item.ItemValue, nil | ||
| } | ||
| if field, ok := secret.Field(fieldName); ok { | ||
| return field, nil | ||
| } else { | ||
| return "", fmt.Errorf("cannot find field %s in secret %s", fieldName, secretID) | ||
| } | ||
|
Comment on lines
+51
to
55
|
||
|
|
||
| return "", fmt.Errorf("cannot find field %s in secret", fieldName) | ||
| } | ||
|
|
||
| func (p *provider) GetStringMap(key string) (map[string]interface{}, error) { | ||
| secretMap := map[string]interface{}{} | ||
|
|
||
| secret, err := p.getSecret(key) | ||
| if err != nil { | ||
| return secretMap, err | ||
| return nil, err | ||
| } | ||
|
|
||
| for _, item := range secret.Items { | ||
| secretMap := map[string]interface{}{} | ||
| for _, item := range secret.Fields { | ||
| secretMap[item.FieldName] = item.ItemValue | ||
| secretMap[item.Slug] = item.ItemValue | ||
| } | ||
|
|
||
| return secretMap, nil | ||
| } | ||
|
|
||
| func (p *provider) getSecret(secretID string) (secretServerSecret, error) { | ||
| var secret secretServerSecret | ||
| accessToken, ok := os.LookupEnv("SECRETSERVER_TOKEN") | ||
| if !ok { | ||
| return secret, errors.New("missing SECRETSERVER_TOKEN environment variable") | ||
| } | ||
| baseUrl, ok := os.LookupEnv("SECRETSERVER_URL") | ||
| if !ok { | ||
| return secret, errors.New("missing SECRETSERVER_URL environment variable") | ||
| } | ||
|
|
||
| url := fmt.Sprintf("%s/api/%s/secrets/%s", | ||
| baseUrl, | ||
| p.APIVersion, | ||
| secretID) | ||
|
|
||
| tr := &http.Transport{ | ||
| TLSClientConfig: &tls.Config{InsecureSkipVerify: !p.SSLVerify}, | ||
| } | ||
| client := &http.Client{Transport: tr} | ||
| req, err := http.NewRequest(http.MethodGet, url, nil) | ||
| if err != nil { | ||
| return secret, err | ||
| } | ||
| req.Header = http.Header{ | ||
| "Content-Type": {"application/json"}, | ||
| "Authorization": {fmt.Sprintf("Bearer %s", accessToken)}, | ||
| } | ||
|
|
||
| res, err := client.Do(req) | ||
| if err != nil { | ||
| return secret, err | ||
| } | ||
|
|
||
| defer func() { | ||
| _ = res.Body.Close() | ||
| }() | ||
|
|
||
| if res.StatusCode != http.StatusOK { | ||
| return secret, fmt.Errorf("SecretServer request %s failed: %s", req.URL, res.Status) | ||
| } | ||
|
|
||
| err = json.NewDecoder(res.Body).Decode(&secret) | ||
| if err != nil { | ||
| return secret, fmt.Errorf("cannot decode JSON: %v", err) | ||
| func (p *provider) getSecret(key string) (*tssSdk.Secret, error) { | ||
| if i, err := strconv.Atoi(key); err == nil { | ||
| return p.tss.Secret(i) | ||
| } else { | ||
| secrets, err := p.tss.Secrets(key, "Name") | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if len(secrets) != 1 { | ||
| return nil, fmt.Errorf("expected exactly one secret with name '%s' but got %d", key, len(secrets)) | ||
| } | ||
| return &secrets[0], nil | ||
| } | ||
| return secret, nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -87,8 +87,8 @@ func New(l *log.Logger, provider api.StaticConfig, awsLogLevel string) (api.Lazy | |
| return httpjson.New(l, provider), nil | ||
| case "scaleway": | ||
| return scaleway.New(l, provider), nil | ||
| case "secretserver": | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @pd-gov why change secretserver to tss? this is a beaking change?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried to align the naming scheme with that used by the vendor. I though this was Okay, since the Provider was introduced just a few days ago.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @yxxhero I can change it back if you prefere
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @pd-gov maybe we can add alias. like : case "tss" "secretserver"
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @pd-gov Or add some notes?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @yxxhero What do you mean by adding notes? In the readme? My assumption was, that no one was actually using the
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @pd-gov you are right. so we can use new name. I will add the changes in the release notes.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @yxxhero The current version contains your proposed aliases. Should I keep that in for backwards compatibility?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @pd-gov Using new name is ok.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @yxxhero Okay, I removed the alias again. |
||
| return secretserver.New(provider), nil | ||
| case "tss": | ||
| return secretserver.New(provider) | ||
| case "infisical": | ||
| return infisical.New(l, provider), nil | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.