Skip to content
Snippets Groups Projects

Move tls generation functions and data into own package

Merged Malte Bauch requested to merge tls-package into master
4 files
+ 77
70
Compare changes
  • Side-by-side
  • Inline
Files
4
+ 68
0
 
package kmstls
 
 
import (
 
"crypto/tls"
 
"crypto/x509"
 
"fmt"
 
"os"
 
 
"google.golang.org/grpc/credentials"
 
)
 
 
type TlsData struct {
 
TLS bool
 
CaFile string
 
CertFile string
 
KeyFile string
 
}
 
 
func GenerateGRPCServerTransportCredsWithTLS(caFilePath, certFile, keyFile string) (credentials.TransportCredentials, error) {
 
cp := x509.NewCertPool()
 
b, err := os.ReadFile(caFilePath)
 
if err != nil {
 
return nil, err
 
}
 
 
if !cp.AppendCertsFromPEM(b) {
 
return nil, fmt.Errorf("credentials: failed to append certificates")
 
}
 
 
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
 
if err != nil {
 
return nil, err
 
}
 
 
tlsConfig := &tls.Config{
 
MinVersion: tls.VersionTLS13,
 
ClientCAs: cp,
 
Certificates: []tls.Certificate{cert},
 
ClientAuth: tls.RequireAndVerifyClientCert,
 
}
 
 
return credentials.NewTLS(tlsConfig), nil
 
}
 
 
func GenerateGRPCClientTransportCredsWithTLS(caFilePath, certFile, keyFile string) (credentials.TransportCredentials, error) {
 
cp := x509.NewCertPool()
 
 
b, err := os.ReadFile(caFilePath)
 
if err != nil {
 
return nil, err
 
}
 
if !cp.AppendCertsFromPEM(b) {
 
return nil, fmt.Errorf("credentials: failed to append certificates")
 
}
 
 
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
 
if err != nil {
 
return nil, err
 
}
 
 
tlsConfig := &tls.Config{
 
MinVersion: tls.VersionTLS13,
 
RootCAs: cp,
 
Certificates: []tls.Certificate{cert},
 
}
 
 
return credentials.NewTLS(tlsConfig), nil
 
}
Loading