2018-07-20 06:30:00 +00:00
|
|
|
package secretsprovider
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/moby/buildkit/session/secrets"
|
|
|
|
"github.com/pkg/errors"
|
2020-06-18 16:19:23 +00:00
|
|
|
"github.com/tonistiigi/units"
|
2018-07-20 06:30:00 +00:00
|
|
|
)
|
|
|
|
|
2020-06-17 19:33:15 +00:00
|
|
|
type Source struct {
|
2018-07-20 06:30:00 +00:00
|
|
|
ID string
|
|
|
|
FilePath string
|
2020-06-16 23:59:28 +00:00
|
|
|
Env string
|
2018-07-20 06:30:00 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 19:33:15 +00:00
|
|
|
func NewStore(files []Source) (secrets.SecretStore, error) {
|
|
|
|
m := map[string]Source{}
|
2018-07-20 06:30:00 +00:00
|
|
|
for _, f := range files {
|
|
|
|
if f.ID == "" {
|
|
|
|
return nil, errors.Errorf("secret missing ID")
|
|
|
|
}
|
2020-06-16 23:59:28 +00:00
|
|
|
if f.Env == "" && f.FilePath == "" {
|
2020-08-14 15:27:41 +00:00
|
|
|
if _, ok := os.LookupEnv(f.ID); ok {
|
2020-06-16 23:59:28 +00:00
|
|
|
f.Env = f.ID
|
|
|
|
} else {
|
|
|
|
f.FilePath = f.ID
|
|
|
|
}
|
2018-07-20 06:30:00 +00:00
|
|
|
}
|
2020-06-16 23:59:28 +00:00
|
|
|
if f.FilePath != "" {
|
|
|
|
fi, err := os.Stat(f.FilePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to stat %s", f.FilePath)
|
|
|
|
}
|
|
|
|
if fi.Size() > MaxSecretSize {
|
2020-06-18 16:19:23 +00:00
|
|
|
return nil, errors.Errorf("secret %s too big. max size %#.f", f.ID, MaxSecretSize*units.B)
|
2020-06-16 23:59:28 +00:00
|
|
|
}
|
2018-07-20 06:30:00 +00:00
|
|
|
}
|
|
|
|
m[f.ID] = f
|
|
|
|
}
|
|
|
|
return &fileStore{
|
|
|
|
m: m,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type fileStore struct {
|
2020-06-17 19:33:15 +00:00
|
|
|
m map[string]Source
|
2018-07-20 06:30:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *fileStore) GetSecret(ctx context.Context, id string) ([]byte, error) {
|
|
|
|
v, ok := fs.m[id]
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.WithStack(secrets.ErrNotFound)
|
|
|
|
}
|
2020-06-16 23:59:28 +00:00
|
|
|
if v.Env != "" {
|
|
|
|
return []byte(os.Getenv(v.Env)), nil
|
|
|
|
}
|
2018-07-20 06:30:00 +00:00
|
|
|
dt, err := ioutil.ReadFile(v.FilePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return dt, nil
|
|
|
|
}
|