2018-12-17 05:27:07 -05:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
|
|
|
const tlsDir = "tls"
|
|
|
|
|
|
|
|
type tlsStore struct {
|
|
|
|
root string
|
|
|
|
}
|
|
|
|
|
2018-11-09 09:10:41 -05:00
|
|
|
func (s *tlsStore) contextDir(id contextdir) string {
|
|
|
|
return filepath.Join(s.root, string(id))
|
2018-12-17 05:27:07 -05:00
|
|
|
}
|
|
|
|
|
2018-11-09 09:10:41 -05:00
|
|
|
func (s *tlsStore) endpointDir(contextID contextdir, name string) string {
|
|
|
|
return filepath.Join(s.root, string(contextID), name)
|
2018-12-17 05:27:07 -05:00
|
|
|
}
|
|
|
|
|
2018-11-09 09:10:41 -05:00
|
|
|
func (s *tlsStore) filePath(contextID contextdir, endpointName, filename string) string {
|
|
|
|
return filepath.Join(s.root, string(contextID), endpointName, filename)
|
2018-12-17 05:27:07 -05:00
|
|
|
}
|
|
|
|
|
2018-11-09 09:10:41 -05:00
|
|
|
func (s *tlsStore) createOrUpdate(contextID contextdir, endpointName, filename string, data []byte) error {
|
|
|
|
epdir := s.endpointDir(contextID, endpointName)
|
2018-12-17 05:27:07 -05:00
|
|
|
parentOfRoot := filepath.Dir(s.root)
|
|
|
|
if err := os.MkdirAll(parentOfRoot, 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := os.MkdirAll(epdir, 0700); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-02-25 08:37:07 -05:00
|
|
|
return os.WriteFile(s.filePath(contextID, endpointName, filename), data, 0600)
|
2018-12-17 05:27:07 -05:00
|
|
|
}
|
|
|
|
|
2018-11-09 09:10:41 -05:00
|
|
|
func (s *tlsStore) getData(contextID contextdir, endpointName, filename string) ([]byte, error) {
|
2022-02-25 08:37:07 -05:00
|
|
|
data, err := os.ReadFile(s.filePath(contextID, endpointName, filename))
|
2018-12-17 05:27:07 -05:00
|
|
|
if err != nil {
|
2018-11-09 09:10:41 -05:00
|
|
|
return nil, convertTLSDataDoesNotExist(endpointName, filename, err)
|
2018-12-17 05:27:07 -05:00
|
|
|
}
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
2022-09-02 16:45:05 -04:00
|
|
|
func (s *tlsStore) remove(contextID contextdir, endpointName, filename string) error {
|
2018-11-09 09:10:41 -05:00
|
|
|
err := os.Remove(s.filePath(contextID, endpointName, filename))
|
2018-12-17 05:27:07 -05:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-11-09 09:10:41 -05:00
|
|
|
func (s *tlsStore) removeAllEndpointData(contextID contextdir, endpointName string) error {
|
|
|
|
return os.RemoveAll(s.endpointDir(contextID, endpointName))
|
2018-12-17 05:27:07 -05:00
|
|
|
}
|
|
|
|
|
2018-11-09 09:10:41 -05:00
|
|
|
func (s *tlsStore) removeAllContextData(contextID contextdir) error {
|
|
|
|
return os.RemoveAll(s.contextDir(contextID))
|
2018-12-17 05:27:07 -05:00
|
|
|
}
|
|
|
|
|
2018-11-09 09:10:41 -05:00
|
|
|
func (s *tlsStore) listContextData(contextID contextdir) (map[string]EndpointFiles, error) {
|
2022-02-25 08:37:07 -05:00
|
|
|
epFSs, err := os.ReadDir(s.contextDir(contextID))
|
2018-12-17 05:27:07 -05:00
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return map[string]EndpointFiles{}, nil
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
r := make(map[string]EndpointFiles)
|
|
|
|
for _, epFS := range epFSs {
|
|
|
|
if epFS.IsDir() {
|
2018-11-09 09:10:41 -05:00
|
|
|
epDir := s.endpointDir(contextID, epFS.Name())
|
2022-02-25 08:37:07 -05:00
|
|
|
fss, err := os.ReadDir(epDir)
|
2018-12-17 05:27:07 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
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
|
|
|
|
|
2018-11-09 09:10:41 -05:00
|
|
|
func convertTLSDataDoesNotExist(endpoint, file string, err error) error {
|
2018-12-17 05:27:07 -05:00
|
|
|
if os.IsNotExist(err) {
|
2018-11-09 09:10:41 -05:00
|
|
|
return &tlsDataDoesNotExistError{endpoint: endpoint, file: file}
|
2018-12-17 05:27:07 -05:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|