-
Notifications
You must be signed in to change notification settings - Fork 308
/
tls.go
118 lines (107 loc) · 3.27 KB
/
tls.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// 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 task
import (
"encoding/pem"
"fmt"
"net"
"path/filepath"
"github.com/pingcap/errors"
"github.com/pingcap/tiup/pkg/cluster/spec"
"github.com/pingcap/tiup/pkg/crypto"
"github.com/pingcap/tiup/pkg/file"
"github.com/pingcap/tiup/pkg/meta"
)
// TLSCert generates a certificate for instance
type TLSCert struct {
inst spec.Instance
ca *crypto.CertificateAuthority
paths meta.DirPaths
}
// Execute implements the Task interface
func (c *TLSCert) Execute(ctx *Context) error {
privKey, err := crypto.NewKeyPair(crypto.KeyTypeRSA, crypto.KeySchemeRSASSAPSSSHA256)
if err != nil {
return err
}
hosts := []string{c.inst.GetHost()}
ips := []string{}
if net.ParseIP(c.inst.GetHost()) != nil {
hosts, ips = ips, hosts
}
csr, err := privKey.CSR(c.inst.Role(), c.inst.ComponentName(), hosts, ips)
if err != nil {
return err
}
cert, err := c.ca.Sign(csr)
if err != nil {
return err
}
// save cert to cache dir
keyFileName := fmt.Sprintf("%s-%s-%d.pem", c.inst.Role(), c.inst.GetHost(), c.inst.GetMainPort())
certFileName := fmt.Sprintf("%s-%s-%d.crt", c.inst.Role(), c.inst.GetHost(), c.inst.GetMainPort())
keyFile := filepath.Join(
c.paths.Cache,
keyFileName,
)
certFile := filepath.Join(
c.paths.Cache,
certFileName,
)
caFile := filepath.Join(c.paths.Cache, spec.TLSCACert)
if err := file.SaveFileWithBackup(keyFile, privKey.Pem(), ""); err != nil {
return err
}
if err := file.SaveFileWithBackup(certFile, pem.EncodeToMemory(&pem.Block{
Type: "CERTIFICATE",
Bytes: cert,
}), ""); err != nil {
return err
}
if err := file.SaveFileWithBackup(caFile, pem.EncodeToMemory(&pem.Block{
Type: "CERTIFICATE",
Bytes: c.ca.Cert.Raw,
}), ""); err != nil {
return err
}
// transfer file to remote
e, ok := ctx.GetExecutor(c.inst.GetHost())
if !ok {
return ErrNoExecutor
}
if err := e.Transfer(caFile,
filepath.Join(c.paths.Deploy, "tls", spec.TLSCACert),
false /* download */); err != nil {
return errors.Annotate(err, "failed to transfer CA cert to server")
}
if err := e.Transfer(keyFile,
filepath.Join(c.paths.Deploy, "tls", fmt.Sprintf("%s.pem", c.inst.Role())),
false /* download */); err != nil {
return errors.Annotate(err, "failed to transfer TLS private key to server")
}
if err := e.Transfer(certFile,
filepath.Join(c.paths.Deploy, "tls", fmt.Sprintf("%s.crt", c.inst.Role())),
false /* download */); err != nil {
return errors.Annotate(err, "failed to transfer TLS cert to server")
}
return nil
}
// Rollback implements the Task interface
func (c *TLSCert) Rollback(ctx *Context) error {
return ErrUnsupportedRollback
}
// String implements the fmt.Stringer interface
func (c *TLSCert) String() string {
return fmt.Sprintf("TLSCert: host=%s role=%s cn=%s",
c.inst.GetHost(), c.inst.Role(), c.inst.ComponentName())
}