secrets: remove file specifics from variable names

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
v0.8
Tonis Tiigi 2020-06-17 12:33:15 -07:00
parent 64e64e424d
commit 0ab180019d
2 changed files with 14 additions and 11 deletions

View File

@ -11,7 +11,7 @@ import (
// ParseSecret parses --secret // ParseSecret parses --secret
func ParseSecret(sl []string) (session.Attachable, error) { func ParseSecret(sl []string) (session.Attachable, error) {
fs := make([]secretsprovider.FileSource, 0, len(sl)) fs := make([]secretsprovider.Source, 0, len(sl))
for _, v := range sl { for _, v := range sl {
s, err := parseSecret(v) s, err := parseSecret(v)
if err != nil { if err != nil {
@ -19,21 +19,21 @@ func ParseSecret(sl []string) (session.Attachable, error) {
} }
fs = append(fs, *s) fs = append(fs, *s)
} }
store, err := secretsprovider.NewFileStore(fs) store, err := secretsprovider.NewStore(fs)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return secretsprovider.NewSecretProvider(store), nil return secretsprovider.NewSecretProvider(store), nil
} }
func parseSecret(value string) (*secretsprovider.FileSource, error) { func parseSecret(value string) (*secretsprovider.Source, error) {
csvReader := csv.NewReader(strings.NewReader(value)) csvReader := csv.NewReader(strings.NewReader(value))
fields, err := csvReader.Read() fields, err := csvReader.Read()
if err != nil { if err != nil {
return nil, errors.Wrap(err, "failed to parse csv secret") return nil, errors.Wrap(err, "failed to parse csv secret")
} }
fs := secretsprovider.FileSource{} fs := secretsprovider.Source{}
var typ string var typ string
for _, field := range fields { for _, field := range fields {

View File

@ -11,14 +11,14 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
) )
type FileSource struct { type Source struct {
ID string ID string
FilePath string FilePath string
Env string Env string
} }
func NewFileStore(files []FileSource) (secrets.SecretStore, error) { func NewStore(files []Source) (secrets.SecretStore, error) {
m := map[string]FileSource{} m := map[string]Source{}
for _, f := range files { for _, f := range files {
if f.ID == "" { if f.ID == "" {
return nil, errors.Errorf("secret missing ID") return nil, errors.Errorf("secret missing ID")
@ -47,7 +47,7 @@ func NewFileStore(files []FileSource) (secrets.SecretStore, error) {
} }
type fileStore struct { type fileStore struct {
m map[string]FileSource m map[string]Source
} }
func (fs *fileStore) GetSecret(ctx context.Context, id string) ([]byte, error) { func (fs *fileStore) GetSecret(ctx context.Context, id string) ([]byte, error) {
@ -67,14 +67,17 @@ func (fs *fileStore) GetSecret(ctx context.Context, id string) ([]byte, error) {
func hasEnv(name string) bool { func hasEnv(name string) bool {
for _, entry := range os.Environ() { for _, entry := range os.Environ() {
parts := strings.SplitN(entry, "=", 2) idx := strings.IndexRune(entry, '=')
if idx == -1 {
continue
}
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
// Environment variable are case-insensitive on Windows. PaTh, path and PATH are equivalent. // Environment variable are case-insensitive on Windows. PaTh, path and PATH are equivalent.
if strings.EqualFold(parts[0], name) { if strings.EqualFold(entry[:idx], name) {
return true return true
} }
} }
if parts[0] == name { if entry[:idx] == name {
return true return true
} }
} }