-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
Signed-off-by: Dmitry K. Anisimov <[email protected]>
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package keychain | ||
|
||
import ( | ||
"errors" | ||
"strings" | ||
|
||
"gopkg.in/yaml.v3" | ||
|
||
"github.com/helmfile/vals/pkg/api" | ||
"github.com/keybase/go-keychain" | ||
) | ||
|
||
const keychainKind = "vals-secret" | ||
|
||
type provider struct { | ||
} | ||
|
||
func New(cfg api.StaticConfig) *provider { | ||
p := &provider{} | ||
return p | ||
} | ||
|
||
func getKeychainSecret(key string) ([]byte, error) { | ||
query := keychain.NewItem() | ||
Check failure on line 24 in pkg/providers/keychain/keychain.go GitHub Actions / unit-test
Check failure on line 24 in pkg/providers/keychain/keychain.go GitHub Actions / Lint
Check failure on line 24 in pkg/providers/keychain/keychain.go GitHub Actions / Lint
|
||
query.SetSecClass(keychain.SecClassGenericPassword) | ||
Check failure on line 25 in pkg/providers/keychain/keychain.go GitHub Actions / unit-test
Check failure on line 25 in pkg/providers/keychain/keychain.go GitHub Actions / Lint
Check failure on line 25 in pkg/providers/keychain/keychain.go GitHub Actions / Lint
|
||
query.SetLabel(key) | ||
query.SetDescription(keychainKind) | ||
query.SetMatchLimit(keychain.MatchLimitOne) | ||
Check failure on line 28 in pkg/providers/keychain/keychain.go GitHub Actions / unit-test
Check failure on line 28 in pkg/providers/keychain/keychain.go GitHub Actions / Lint
Check failure on line 28 in pkg/providers/keychain/keychain.go GitHub Actions / Lint
|
||
query.SetReturnData(true) | ||
|
||
results, err := keychain.QueryItem(query) | ||
Check failure on line 31 in pkg/providers/keychain/keychain.go GitHub Actions / unit-test
Check failure on line 31 in pkg/providers/keychain/keychain.go GitHub Actions / Lint
Check failure on line 31 in pkg/providers/keychain/keychain.go GitHub Actions / Lint
|
||
if err != nil { | ||
return nil, err | ||
} else if len(results) == 0 { | ||
return nil, errors.New("not found") | ||
} | ||
|
||
return results[0].Data, nil | ||
} | ||
|
||
func (p *provider) GetString(key string) (string, error) { | ||
key = strings.TrimSuffix(key, "/") | ||
key = strings.TrimSpace(key) | ||
|
||
secret, err := getKeychainSecret(key) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return string(secret), err | ||
} | ||
|
||
func (p *provider) GetStringMap(key string) (map[string]interface{}, error) { | ||
key = strings.TrimSuffix(key, "/") | ||
key = strings.TrimSpace(key) | ||
|
||
secret, err := getKeychainSecret(key) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
m := map[string]interface{}{} | ||
if err := yaml.Unmarshal(secret, &m); err != nil { | ||
return nil, err | ||
} | ||
return m, nil | ||
} |