2018-12-17 05:27:07 -05:00
|
|
|
package context
|
|
|
|
|
|
|
|
import (
|
2022-02-25 08:37:07 -05:00
|
|
|
"os"
|
2018-12-17 05:27:07 -05:00
|
|
|
|
|
|
|
"github.com/docker/cli/cli/context/store"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
caKey = "ca.pem"
|
|
|
|
certKey = "cert.pem"
|
|
|
|
keyKey = "key.pem"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TLSData holds ca/cert/key raw data
|
|
|
|
type TLSData struct {
|
|
|
|
CA []byte
|
|
|
|
Key []byte
|
|
|
|
Cert []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToStoreTLSData converts TLSData to the store representation
|
|
|
|
func (data *TLSData) ToStoreTLSData() *store.EndpointTLSData {
|
|
|
|
if data == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
result := store.EndpointTLSData{
|
|
|
|
Files: make(map[string][]byte),
|
|
|
|
}
|
|
|
|
if data.CA != nil {
|
|
|
|
result.Files[caKey] = data.CA
|
|
|
|
}
|
|
|
|
if data.Cert != nil {
|
|
|
|
result.Files[certKey] = data.Cert
|
|
|
|
}
|
|
|
|
if data.Key != nil {
|
|
|
|
result.Files[keyKey] = data.Key
|
|
|
|
}
|
|
|
|
return &result
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadTLSData loads TLS data from the store
|
2019-04-15 06:03:03 -04:00
|
|
|
func LoadTLSData(s store.Reader, contextName, endpointName string) (*TLSData, error) {
|
2019-04-18 09:12:30 -04:00
|
|
|
tlsFiles, err := s.ListTLSFiles(contextName)
|
2018-12-17 05:27:07 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to retrieve context tls files for context %q", contextName)
|
|
|
|
}
|
|
|
|
if epTLSFiles, ok := tlsFiles[endpointName]; ok {
|
|
|
|
var tlsData TLSData
|
|
|
|
for _, f := range epTLSFiles {
|
2019-04-18 09:12:30 -04:00
|
|
|
data, err := s.GetTLSData(contextName, endpointName, f)
|
2018-12-17 05:27:07 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to retrieve context tls data for file %q of context %q", f, contextName)
|
|
|
|
}
|
|
|
|
switch f {
|
|
|
|
case caKey:
|
|
|
|
tlsData.CA = data
|
|
|
|
case certKey:
|
|
|
|
tlsData.Cert = data
|
|
|
|
case keyKey:
|
|
|
|
tlsData.Key = data
|
|
|
|
default:
|
|
|
|
logrus.Warnf("unknown file %s in context %s tls bundle", f, contextName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &tlsData, nil
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TLSDataFromFiles reads files into a TLSData struct (or returns nil if all paths are empty)
|
|
|
|
func TLSDataFromFiles(caPath, certPath, keyPath string) (*TLSData, error) {
|
|
|
|
var (
|
|
|
|
ca, cert, key []byte
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if caPath != "" {
|
2022-02-25 08:37:07 -05:00
|
|
|
if ca, err = os.ReadFile(caPath); err != nil {
|
2018-12-17 05:27:07 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if certPath != "" {
|
2022-02-25 08:37:07 -05:00
|
|
|
if cert, err = os.ReadFile(certPath); err != nil {
|
2018-12-17 05:27:07 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if keyPath != "" {
|
2022-02-25 08:37:07 -05:00
|
|
|
if key, err = os.ReadFile(keyPath); err != nil {
|
2018-12-17 05:27:07 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ca == nil && cert == nil && key == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return &TLSData{CA: ca, Cert: cert, Key: key}, nil
|
|
|
|
}
|