Merge pull request #1745 from tonistiigi/readonly-config

authprovider: allow readonly config dir
v0.8
Akihiro Suda 2020-10-22 18:23:30 +09:00 committed by GitHub
commit 212a0b1439
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 26 additions and 15 deletions

View File

@ -7,6 +7,7 @@ import (
"os"
"path/filepath"
"sync"
"syscall"
"github.com/gofrs/flock"
"github.com/pkg/errors"
@ -15,6 +16,7 @@ import (
type tokenSeeds struct {
mu sync.Mutex
dir string
m map[string]seed
}
type seed struct {
@ -29,40 +31,49 @@ func (ts *tokenSeeds) getSeed(host string) ([]byte, error) {
return nil, err
}
if ts.m == nil {
ts.m = map[string]seed{}
}
l := flock.New(filepath.Join(ts.dir, ".token_seed.lock"))
if err := l.Lock(); err != nil {
return nil, err
}
defer l.Unlock()
// we include client side randomness to avoid chosen plaintext attack from the daemon side
fp := filepath.Join(ts.dir, ".token_seed")
dt, err := ioutil.ReadFile(fp)
m := map[string]seed{}
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
if !errors.Is(err, syscall.EROFS) && errors.Is(err, syscall.EPERM) {
return nil, err
}
} else {
if err := json.Unmarshal(dt, &m); err != nil {
defer l.Unlock()
}
fp := filepath.Join(ts.dir, ".token_seed")
// we include client side randomness to avoid chosen plaintext attack from the daemon side
dt, err := ioutil.ReadFile(fp)
if err != nil {
if !errors.Is(err, os.ErrNotExist) && !errors.Is(err, syscall.ENOTDIR) {
return nil, err
}
} else {
if err := json.Unmarshal(dt, &ts.m); err != nil {
return nil, errors.Wrapf(err, "failed to parse %s", fp)
}
}
v, ok := m[host]
v, ok := ts.m[host]
if !ok {
v = seed{Seed: newSeed()}
}
m[host] = v
ts.m[host] = v
dt, err = json.MarshalIndent(m, "", " ")
dt, err = json.MarshalIndent(ts.m, "", " ")
if err != nil {
return nil, err
}
if err := ioutil.WriteFile(fp, dt, 0600); err != nil {
if !errors.Is(err, syscall.EROFS) && !errors.Is(err, syscall.EPERM) {
return nil, err
}
}
return v.Seed, nil
}