2018-12-17 05:27:07 -05:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2022-09-28 16:18:51 -04:00
|
|
|
|
|
|
|
"github.com/docker/docker/errdefs"
|
2023-02-20 19:51:57 -05:00
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
2022-09-28 16:18:51 -04:00
|
|
|
"github.com/pkg/errors"
|
2018-12-17 05:27:07 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const tlsDir = "tls"
|
|
|
|
|
|
|
|
type tlsStore struct {
|
|
|
|
root string
|
|
|
|
}
|
|
|
|
|
2022-09-28 12:08:22 -04:00
|
|
|
func (s *tlsStore) contextDir(name string) string {
|
|
|
|
return filepath.Join(s.root, string(contextdirOf(name)))
|
2018-12-17 05:27:07 -05:00
|
|
|
}
|
|
|
|
|
2022-09-28 12:08:22 -04:00
|
|
|
func (s *tlsStore) endpointDir(name, endpointName string) string {
|
|
|
|
return filepath.Join(s.contextDir(name), endpointName)
|
2018-12-17 05:27:07 -05:00
|
|
|
}
|
|
|
|
|
2022-09-28 11:38:12 -04:00
|
|
|
func (s *tlsStore) createOrUpdate(name, endpointName, filename string, data []byte) error {
|
2018-12-17 05:27:07 -05:00
|
|
|
parentOfRoot := filepath.Dir(s.root)
|
2022-09-30 13:13:22 -04:00
|
|
|
if err := os.MkdirAll(parentOfRoot, 0o755); err != nil {
|
2018-12-17 05:27:07 -05:00
|
|
|
return err
|
|
|
|
}
|
2022-09-28 12:08:22 -04:00
|
|
|
endpointDir := s.endpointDir(name, endpointName)
|
2022-09-30 13:13:22 -04:00
|
|
|
if err := os.MkdirAll(endpointDir, 0o700); err != nil {
|
2018-12-17 05:27:07 -05:00
|
|
|
return err
|
|
|
|
}
|
2023-02-20 19:51:57 -05:00
|
|
|
return ioutils.AtomicWriteFile(filepath.Join(endpointDir, filename), data, 0o600)
|
2018-12-17 05:27:07 -05:00
|
|
|
}
|
|
|
|
|
2022-09-28 11:40:33 -04:00
|
|
|
func (s *tlsStore) getData(name, endpointName, filename string) ([]byte, error) {
|
2022-09-28 12:08:22 -04:00
|
|
|
data, err := os.ReadFile(filepath.Join(s.endpointDir(name, endpointName), filename))
|
2018-12-17 05:27:07 -05:00
|
|
|
if err != nil {
|
2022-09-28 16:18:51 -04:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil, errdefs.NotFound(errors.Errorf("TLS data for %s/%s/%s does not exist", name, endpointName, filename))
|
|
|
|
}
|
|
|
|
return nil, errors.Wrapf(err, "failed to read TLS data for endpoint %s", endpointName)
|
2018-12-17 05:27:07 -05:00
|
|
|
}
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
2022-09-29 08:55:29 -04:00
|
|
|
// remove deletes all TLS data for the given context.
|
|
|
|
func (s *tlsStore) remove(name string) error {
|
|
|
|
if err := os.RemoveAll(s.contextDir(name)); err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to remove TLS data")
|
2018-12-17 05:27:07 -05:00
|
|
|
}
|
2022-09-29 08:55:29 -04:00
|
|
|
return nil
|
2018-12-17 05:27:07 -05:00
|
|
|
}
|
|
|
|
|
2022-09-29 08:55:29 -04:00
|
|
|
func (s *tlsStore) removeEndpoint(name, endpointName string) error {
|
2022-09-28 16:18:51 -04:00
|
|
|
if err := os.RemoveAll(s.endpointDir(name, endpointName)); err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to remove TLS data for endpoint %s", endpointName)
|
|
|
|
}
|
|
|
|
return nil
|
2018-12-17 05:27:07 -05:00
|
|
|
}
|
|
|
|
|
2022-09-28 11:51:25 -04:00
|
|
|
func (s *tlsStore) listContextData(name string) (map[string]EndpointFiles, error) {
|
2022-09-28 12:08:22 -04:00
|
|
|
contextDir := s.contextDir(name)
|
|
|
|
epFSs, err := os.ReadDir(contextDir)
|
2018-12-17 05:27:07 -05:00
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return map[string]EndpointFiles{}, nil
|
|
|
|
}
|
2022-09-28 16:18:51 -04:00
|
|
|
return nil, errors.Wrapf(err, "failed to list TLS files for context %s", name)
|
2018-12-17 05:27:07 -05:00
|
|
|
}
|
|
|
|
r := make(map[string]EndpointFiles)
|
|
|
|
for _, epFS := range epFSs {
|
|
|
|
if epFS.IsDir() {
|
2022-09-28 12:08:22 -04:00
|
|
|
fss, err := os.ReadDir(filepath.Join(contextDir, epFS.Name()))
|
2022-09-28 16:21:31 -04:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
continue
|
|
|
|
}
|
2018-12-17 05:27:07 -05:00
|
|
|
if err != nil {
|
2022-09-28 16:18:51 -04:00
|
|
|
return nil, errors.Wrapf(err, "failed to list TLS files for endpoint %s", epFS.Name())
|
2018-12-17 05:27:07 -05:00
|
|
|
}
|
|
|
|
var files EndpointFiles
|
|
|
|
for _, fs := range fss {
|
|
|
|
if !fs.IsDir() {
|
|
|
|
files = append(files, fs.Name())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r[epFS.Name()] = files
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// EndpointFiles is a slice of strings representing file names
|
|
|
|
type EndpointFiles []string
|