-
Notifications
You must be signed in to change notification settings - Fork 289
/
Copy pathcredential.go
104 lines (93 loc) · 3.19 KB
/
credential.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright 2020 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package security
import (
"crypto/tls"
"crypto/x509"
"encoding/pem"
"os"
cerror "github.com/pingcap/ticdc/pkg/errors"
"github.com/pingcap/tidb-tools/pkg/utils"
pd "github.com/tikv/pd/client"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
// Credential holds necessary path parameter to build a tls.Config
type Credential struct {
CAPath string `toml:"ca-path" json:"ca-path"`
CertPath string `toml:"cert-path" json:"cert-path"`
KeyPath string `toml:"key-path" json:"key-path"`
CertAllowedCN []string `toml:"cert-allowed-cn" json:"cert-allowed-cn"`
}
// IsTLSEnabled checks whether TLS is enabled or not.
func (s *Credential) IsTLSEnabled() bool {
return len(s.CAPath) != 0
}
// PDSecurityOption creates a new pd SecurityOption from Security
func (s *Credential) PDSecurityOption() pd.SecurityOption {
return pd.SecurityOption{
CAPath: s.CAPath,
CertPath: s.CertPath,
KeyPath: s.KeyPath,
}
}
// ToGRPCDialOption constructs a gRPC dial option.
func (s *Credential) ToGRPCDialOption() (grpc.DialOption, error) {
tlsCfg, err := s.ToTLSConfig()
if err != nil || tlsCfg == nil {
return grpc.WithInsecure(), err
}
return grpc.WithTransportCredentials(credentials.NewTLS(tlsCfg)), nil
}
// ToTLSConfig generates tls's config from *Security
func (s *Credential) ToTLSConfig() (*tls.Config, error) {
cfg, err := utils.ToTLSConfig(s.CAPath, s.CertPath, s.KeyPath)
return cfg, cerror.WrapError(cerror.ErrToTLSConfigFailed, err)
}
// ToTLSConfigWithVerify generates tls's config from *Security and requires
// the remote common name to be verified.
func (s *Credential) ToTLSConfigWithVerify() (*tls.Config, error) {
cfg, err := utils.ToTLSConfigWithVerify(s.CAPath, s.CertPath, s.KeyPath, s.CertAllowedCN)
return cfg, cerror.WrapError(cerror.ErrToTLSConfigFailed, err)
}
func (s *Credential) getSelfCommonName() (string, error) {
if s.CertPath == "" {
return "", nil
}
data, err := os.ReadFile(s.CertPath)
if err != nil {
return "", cerror.WrapError(cerror.ErrToTLSConfigFailed, err)
}
block, _ := pem.Decode(data)
if block == nil || block.Type != "CERTIFICATE" {
return "", cerror.ErrToTLSConfigFailed.GenWithStack("failed to decode PEM block to certificate")
}
certificate, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return "", cerror.WrapError(cerror.ErrToTLSConfigFailed, err)
}
return certificate.Subject.CommonName, nil
}
// AddSelfCommonName add Common Name in certificate that specified by s.CertPath
// to s.CertAllowedCN
func (s *Credential) AddSelfCommonName() error {
cn, err := s.getSelfCommonName()
if err != nil {
return err
}
if cn == "" {
return nil
}
s.CertAllowedCN = append(s.CertAllowedCN, cn)
return nil
}