feat(filestore): add function to save mTLS certificates (#8206)

pull/8207/head
andres-portainer 2022-12-15 16:16:29 -03:00 committed by GitHub
parent 68975620c5
commit a8ccd2b153
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 36 additions and 0 deletions

View File

@ -62,6 +62,10 @@ const (
SSLKeyFilename = "key.pem" SSLKeyFilename = "key.pem"
// SSLCACertFilename represents the CA ssl certificate file name for mTLS // SSLCACertFilename represents the CA ssl certificate file name for mTLS
SSLCACertFilename = "ca-cert.pem" SSLCACertFilename = "ca-cert.pem"
MTLSCertFilename = "mtls-cert.pem"
MTLSCACertFilename = "mtls-ca-cert.pem"
MTLSKeyFilename = "mtls-key.pem"
) )
// ErrUndefinedTLSFileType represents an error returned on undefined TLS file type // ErrUndefinedTLSFileType represents an error returned on undefined TLS file type
@ -663,6 +667,14 @@ func (service *Service) GetDefaultSSLCertsPath() (string, string) {
return service.wrapFileStore(certPath), service.wrapFileStore(keyPath) return service.wrapFileStore(certPath), service.wrapFileStore(keyPath)
} }
func defaultMTLSCertPathUnderFileStore() (string, string, string) {
certPath := JoinPaths(SSLCertPath, MTLSCertFilename)
caCertPath := JoinPaths(SSLCertPath, MTLSCACertFilename)
keyPath := JoinPaths(SSLCertPath, MTLSKeyFilename)
return certPath, caCertPath, keyPath
}
// StoreSSLCertPair stores a ssl certificate pair // StoreSSLCertPair stores a ssl certificate pair
func (service *Service) StoreSSLCertPair(cert, key []byte) (string, string, error) { func (service *Service) StoreSSLCertPair(cert, key []byte) (string, string, error) {
certPath, keyPath := defaultCertPathUnderFileStore() certPath, keyPath := defaultCertPathUnderFileStore()
@ -767,3 +779,27 @@ func CreateFile(path string, r io.Reader) error {
_, err = io.Copy(out, r) _, err = io.Copy(out, r)
return err return err
} }
func (service *Service) StoreMTLSCertificates(cert, caCert, key []byte) (string, string, string, error) {
certPath, caCertPath, keyPath := defaultMTLSCertPathUnderFileStore()
r := bytes.NewReader(cert)
err := service.createFileInStore(certPath, r)
if err != nil {
return "", "", "", err
}
r = bytes.NewReader(caCert)
err = service.createFileInStore(caCertPath, r)
if err != nil {
return "", "", "", err
}
r = bytes.NewReader(key)
err = service.createFileInStore(keyPath, r)
if err != nil {
return "", "", "", err
}
return service.wrapFileStore(certPath), service.wrapFileStore(caCertPath), service.wrapFileStore(keyPath), nil
}