generated from origadmin/.github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tlsconfig.go
84 lines (63 loc) · 1.53 KB
/
tlsconfig.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
* Copyright (c) 2024 OrigAdmin. All rights reserved.
*/
// Package service implements the functions, types, and interfaces for the module.
package service
import (
"crypto/tls"
"crypto/x509"
"encoding/pem"
"os"
"github.com/goexts/generic/settings"
configv1 "github.com/origadmin/runtime/gen/go/config/v1"
)
type TLSOption = func(*tls.Config)
func SkipInsecure(b bool) TLSOption {
return func(c *tls.Config) {
c.InsecureSkipVerify = b
}
}
func NewServerTLSConfig(cfg *configv1.TLSConfig, opts ...TLSOption) (*tls.Config, error) {
if cfg == nil {
return nil, nil
}
tlsCfg := settings.Apply(&tls.Config{}, opts)
if cfg.GetFile() != nil {
} else if cfg.GetPem() != nil {
} else {
}
return tlsCfg, nil
}
func NewClientTLSConfig(cfg *configv1.TLSConfig, opts ...TLSOption) (*tls.Config, error) {
if cfg == nil {
return nil, nil
}
tlsCfg := settings.Apply(&tls.Config{}, opts)
if cfg.GetFile() != nil {
} else if cfg.GetPem() != nil {
} else {
}
return tlsCfg, nil
}
// newRootCertFile creates x509 certPool with provided CA file
func newRootCertFile(filepath string) (*x509.CertPool, error) {
rootPEM, err := os.ReadFile(filepath)
if err != nil {
return nil, err
}
return newRootCert(rootPEM)
}
func newRootCert(rootPEM []byte) (*x509.CertPool, error) {
certPool := x509.NewCertPool()
var err error
block, _ := pem.Decode(rootPEM)
if block == nil {
return certPool, nil
}
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return nil, err
}
certPool.AddCert(cert)
return certPool, nil
}