diff --git a/api/filesystem/filesystem.go b/api/filesystem/filesystem.go index df49a2706..43e38b4e7 100644 --- a/api/filesystem/filesystem.go +++ b/api/filesystem/filesystem.go @@ -841,11 +841,11 @@ func (service *Service) GetDefaultSSLCertsPath() (string, string) { } func defaultMTLSCertPathUnderFileStore() (string, string, string) { - certPath := JoinPaths(SSLCertPath, MTLSCertFilename) caCertPath := JoinPaths(SSLCertPath, MTLSCACertFilename) + certPath := JoinPaths(SSLCertPath, MTLSCertFilename) keyPath := JoinPaths(SSLCertPath, MTLSKeyFilename) - return certPath, caCertPath, keyPath + return caCertPath, certPath, keyPath } // GetDefaultChiselPrivateKeyPath returns the chisle private key path @@ -1014,26 +1014,45 @@ func CreateFile(path string, r io.Reader) error { return err } -func (service *Service) StoreMTLSCertificates(cert, caCert, key []byte) (string, string, string, error) { - certPath, caCertPath, keyPath := defaultMTLSCertPathUnderFileStore() +func (service *Service) StoreMTLSCertificates(caCert, cert, key []byte) (string, string, string, error) { + caCertPath, certPath, keyPath := defaultMTLSCertPathUnderFileStore() - r := bytes.NewReader(cert) - err := service.createFileInStore(certPath, r) - if err != nil { + r := bytes.NewReader(caCert) + if err := service.createFileInStore(caCertPath, r); err != nil { return "", "", "", err } - r = bytes.NewReader(caCert) - err = service.createFileInStore(caCertPath, r) - if err != nil { + r = bytes.NewReader(cert) + if err := service.createFileInStore(certPath, r); err != nil { return "", "", "", err } r = bytes.NewReader(key) - err = service.createFileInStore(keyPath, r) - if err != nil { + if err := service.createFileInStore(keyPath, r); err != nil { return "", "", "", err } - return service.wrapFileStore(certPath), service.wrapFileStore(caCertPath), service.wrapFileStore(keyPath), nil + return service.wrapFileStore(caCertPath), service.wrapFileStore(certPath), service.wrapFileStore(keyPath), nil +} + +func (service *Service) GetMTLSCertificates() (string, string, string, error) { + caCertPath, certPath, keyPath := defaultMTLSCertPathUnderFileStore() + + caCertPath = service.wrapFileStore(caCertPath) + certPath = service.wrapFileStore(certPath) + keyPath = service.wrapFileStore(keyPath) + + paths := [...]string{caCertPath, certPath, keyPath} + for _, path := range paths { + exists, err := service.FileExists(path) + if err != nil { + return "", "", "", err + } + + if !exists { + return "", "", "", fmt.Errorf("file %s does not exist", path) + } + } + + return caCertPath, certPath, keyPath, nil } diff --git a/api/portainer.go b/api/portainer.go index dd48936ed..2f9af995f 100644 --- a/api/portainer.go +++ b/api/portainer.go @@ -1491,7 +1491,8 @@ type ( StoreSSLCertPair(cert, key []byte) (string, string, error) CopySSLCertPair(certPath, keyPath string) (string, string, error) CopySSLCACert(caCertPath string) (string, error) - StoreMTLSCertificates(cert, caCert, key []byte) (string, string, string, error) + StoreMTLSCertificates(caCert, cert, key []byte) (string, string, string, error) + GetMTLSCertificates() (string, string, string, error) GetDefaultChiselPrivateKeyPath() string StoreChiselPrivateKey(privateKey []byte) error }