-
Notifications
You must be signed in to change notification settings - Fork 0
/
ca.go
45 lines (37 loc) · 1.07 KB
/
ca.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package ca
import (
_ "embed" // Needed for embedding the CA certificate and key
"os"
"crypto/tls"
)
// CACertificate Certificate Authority certificate used by the proxy.
//
//go:embed grafarg-e2e-ca.pem
var CACertificate []byte
// CAKey Certificate Authority private key used by the proxy.
//
//go:embed grafarg-e2e-ca.key.pem
var CAKey []byte
// Loads the CA key pair from the provided paths, and falls back to the default key pair if paths are not provided.
func GetCertificate(certPath, keyPath string) (tls.Certificate, error) {
if certPath == "" || keyPath == "" {
return tls.X509KeyPair(CACertificate, CAKey)
}
cert, key, err := LoadKeyPair(certPath, keyPath)
if err != nil {
return tls.Certificate{}, err
}
return tls.X509KeyPair(cert, key)
}
// Loads the CA key pair from the provided paths.
func LoadKeyPair(certPath, keyPath string) ([]byte, []byte, error) {
cert, err := os.ReadFile(certPath)
if err != nil {
return []byte{}, []byte{}, err
}
key, err := os.ReadFile(keyPath)
if err != nil {
return []byte{}, []byte{}, err
}
return cert, key, nil
}