forked from istio/istio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
123 lines (109 loc) · 4.15 KB
/
client.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
119
120
121
122
123
// Copyright 2018 Istio Authors
//
// 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,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package caclient
import (
"fmt"
"io/ioutil"
"time"
"istio.io/istio/security/pkg/caclient/protocol"
pkiutil "istio.io/istio/security/pkg/pki/util"
"istio.io/istio/security/pkg/platform"
pb "istio.io/istio/security/proto"
"istio.io/pkg/log"
)
const (
// keyFilePermission is the permission bits for private key file.
keyFilePermission = 0600
// certFilePermission is the permission bits for certificate file.
certFilePermission = 0644
)
// CAClient is a client to provision key and certificate from the upstream CA via CSR protocol.
type CAClient struct {
platformClient platform.Client
maxRetries int
initialRetrialInterval time.Duration
caProtocol protocol.CAProtocol
}
// NewCAClient creates a new CAClient instance.
func NewCAClient(pltfmc platform.Client, protocolClient protocol.CAProtocol, maxRetries int, interval time.Duration) (*CAClient, error) {
if !pltfmc.IsProperPlatform() {
return nil, fmt.Errorf("CA client is not running on the right platform") // nolint
}
return &CAClient{
platformClient: pltfmc,
maxRetries: maxRetries,
initialRetrialInterval: interval,
caProtocol: protocolClient,
}, nil
}
// Retrieve sends the CSR to Istio CA with automatic retries. When successful, it returns the generated key
// and cert, otherwise, it returns error. This is a blocking function.
func (c *CAClient) Retrieve(options *pkiutil.CertOptions) (newCert []byte, certChain []byte, privateKey []byte, err error) {
retries := 0
retrialInterval := c.initialRetrialInterval
for {
privateKey, req, reqErr := c.createCSRRequest(options)
if reqErr != nil {
return nil, nil, nil, reqErr
}
log.Infof("Sending CSR (retrial #%d) ...", retries)
resp, err := c.caProtocol.SendCSR(req)
if err == nil && resp != nil && resp.IsApproved {
return resp.SignedCert, resp.CertChain, privateKey, nil
}
if retries >= c.maxRetries {
return nil, nil, nil, fmt.Errorf(
"CA client cannot get the CSR approved from Istio CA after max number of retries (%d)", c.maxRetries)
}
if err != nil {
log.Errorf("CSR signing failed: %v. Will retry in %v", err, retrialInterval)
} else if resp == nil {
log.Errorf("CSR signing failed: response empty. Will retry in %v", retrialInterval)
} else if !resp.IsApproved {
log.Errorf("CSR signing failed: request not approved. Will retry in %v", retrialInterval)
} else {
log.Errorf("Certificate parsing error. Will retry in %v", retrialInterval)
}
retries++
timer := time.NewTimer(retrialInterval)
// Exponentially increase the backoff time.
retrialInterval *= 2
<-timer.C
}
}
func (c *CAClient) createCSRRequest(opts *pkiutil.CertOptions) ([]byte, *pb.CsrRequest, error) {
csr, privKey, err := pkiutil.GenCSR(*opts)
if err != nil {
return nil, nil, err
}
cred, err := c.platformClient.GetAgentCredential()
if err != nil {
return nil, nil, fmt.Errorf("request creation fails on getting platform credential (%v)", err)
}
return privKey, &pb.CsrRequest{
CsrPem: csr,
NodeAgentCredential: cred,
CredentialType: c.platformClient.GetCredentialType(),
// TODO(inclfy): verify current value matches default value.
RequestedTtlMinutes: int32(opts.TTL.Minutes()),
}, nil
}
// SaveKeyCert stores the specified key/cert into file specified by the path.
// TODO(incfly): move this into CAClient struct's own method later.
func SaveKeyCert(keyFile, certFile string, privKey, cert []byte) error {
if err := ioutil.WriteFile(keyFile, privKey, keyFilePermission); err != nil {
return err
}
return ioutil.WriteFile(certFile, cert, certFilePermission)
}