-
Notifications
You must be signed in to change notification settings - Fork 26
/
certificates.go
54 lines (47 loc) · 1.23 KB
/
certificates.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
46
47
48
49
50
51
52
53
54
package registry
import (
"crypto/x509"
"fmt"
"os"
"path"
"path/filepath"
)
const (
registryConfigPath = "/tmp/config/registry"
certFile = "ca.crt"
insecureFile = "insecure"
)
func GetRegistryInsecure(clusterName string) bool {
caFile := path.Join(registryConfigPath, clusterName+"_"+insecureFile)
if _, err := os.Stat(caFile); err != nil {
return false
}
return true
}
func GetClusterCertificateFileName(clusterName string) string {
caFile := path.Join(registryConfigPath, clusterName+"_"+certFile)
if _, err := os.Stat(caFile); err != nil {
return ""
}
return caFile
}
func GetClusterCertificate(clusterName string) (certificates *x509.CertPool, err error) {
caFile := GetClusterCertificateFileName(clusterName)
if caFile == "" {
return nil, nil
}
return GetCertificates(caFile)
}
// GetCertificates get X509 certificates.
func GetCertificates(certFile string) (certificates *x509.CertPool, err error) {
if len(certFile) < 1 {
return nil, nil
}
fileContents, err := os.ReadFile(filepath.Clean(certFile))
if err != nil {
return nil, fmt.Errorf("error reading certificate file <%s>: %v", certFile, err)
}
certPool := x509.NewCertPool()
certPool.AppendCertsFromPEM(fileContents)
return certPool, nil
}