This repository has been archived by the owner on Mar 14, 2022. It is now read-only.
forked from istio/istio
/
nodeagent.go
137 lines (123 loc) · 4.15 KB
/
nodeagent.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright 2017 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 na
import (
"fmt"
"time"
"istio.io/istio/pkg/log"
"istio.io/istio/security/pkg/caclient"
"istio.io/istio/security/pkg/caclient/protocol"
pkiutil "istio.io/istio/security/pkg/pki/util"
"istio.io/istio/security/pkg/platform"
"istio.io/istio/security/pkg/util"
pb "istio.io/istio/security/proto"
)
// The real node agent implementation. This implements the "Start" function
// in the NodeAgent interface.
type nodeAgentInternal struct {
// Configuration specific to Node Agent
config *Config
pc platform.Client
caProtocol protocol.CAProtocol
identity string
certUtil util.CertUtil
}
// Start starts the node Agent.
func (na *nodeAgentInternal) Start() error {
if na.config == nil {
return fmt.Errorf("node Agent configuration is nil")
}
if !na.pc.IsProperPlatform() {
return fmt.Errorf("node Agent is not running on the right platform")
}
log.Infof("Node Agent starts successfully.")
retries := 0
retrialInterval := na.config.CAClientConfig.CSRInitialRetrialInterval
identity, err := na.pc.GetServiceIdentity()
if err != nil {
return err
}
na.identity = identity
var success bool
for {
privateKey, req, reqErr := na.createRequest()
if reqErr != nil {
return reqErr
}
log.Infof("Sending CSR (retrial #%d) ...", retries)
resp, err := na.caProtocol.SendCSR(req)
if err == nil && resp != nil && resp.IsApproved {
waitTime, ttlErr := na.certUtil.GetWaitTime(resp.SignedCert, time.Now())
if ttlErr != nil {
log.Errorf("Error getting TTL from approved cert: %v", ttlErr)
success = false
} else {
if err = caclient.SaveKeyCert(na.config.CAClientConfig.KeyFile,
na.config.CAClientConfig.CertChainFile,
privateKey, append(resp.SignedCert, resp.CertChain...)); err != nil {
return err
}
log.Infof("CSR is approved successfully. Will renew cert in %s", waitTime.String())
retries = 0
retrialInterval = na.config.CAClientConfig.CSRInitialRetrialInterval
timer := time.NewTimer(waitTime)
<-timer.C
success = true
}
} else {
success = false
}
if !success {
if retries >= na.config.CAClientConfig.CSRMaxRetries {
return fmt.Errorf(
"node agent can't get the CSR approved from Istio CA after max number of retries (%d)",
na.config.CAClientConfig.CSRMaxRetries)
}
if err != nil {
log.Errorf("CSR signing failed: %v. Will retry in %s", err, retrialInterval.String())
} else if resp == nil {
log.Errorf("CSR signing failed: response empty. Will retry in %s", retrialInterval.String())
} else if !resp.IsApproved {
log.Errorf("CSR signing failed: request not approved. Will retry in %s", retrialInterval.String())
} else {
log.Errorf("Certificate parsing error. Will retry in %s", retrialInterval.String())
}
retries++
timer := time.NewTimer(retrialInterval)
// Exponentially increase the backoff time.
retrialInterval = retrialInterval * 2
<-timer.C
}
}
}
func (na *nodeAgentInternal) createRequest() ([]byte, *pb.CsrRequest, error) {
csr, privKey, err := pkiutil.GenCSR(pkiutil.CertOptions{
Host: na.identity,
Org: na.config.CAClientConfig.Org,
RSAKeySize: na.config.CAClientConfig.RSAKeySize,
})
if err != nil {
return nil, nil, err
}
cred, err := na.pc.GetAgentCredential()
if err != nil {
return nil, nil, fmt.Errorf("request creation fails on getting agent credential (%v)", err)
}
return privKey, &pb.CsrRequest{
CsrPem: csr,
NodeAgentCredential: cred,
CredentialType: na.pc.GetCredentialType(),
RequestedTtlMinutes: int32(na.config.CAClientConfig.RequestedCertTTL.Minutes()),
}, nil
}