@@ -24,7 +24,7 @@ func NewManager(mrcClient MRCClient, serviceCertValidityDuration time.Duration,
24
24
return nil , err
25
25
}
26
26
27
- c := & issuer {Issuer : client , ID : clientID , CertificateAuthority : ca }
27
+ c := & issuer {Issuer : client , ID : clientID , CertificateAuthority : ca , TrustDomain : mrcs [ 0 ]. Spec . TrustDomain }
28
28
29
29
m := & Manager {
30
30
// The signingIssuer is responsible for signing all newly issued certificates
@@ -59,15 +59,23 @@ func (m *Manager) Start(checkInterval time.Duration, stop <-chan struct{}) {
59
59
// GetTrustDomain returns the trust domain from the configured signingkey issuer.
60
60
// Note that the CRD uses a default, so this value will always be set.
61
61
func (m * Manager ) GetTrustDomain () string {
62
- // TODO(4754): implement
63
- return ""
62
+ m .mu .Lock ()
63
+ defer m .mu .Unlock ()
64
+ return m .signingIssuer .TrustDomain
64
65
}
65
66
66
67
func (m * Manager ) checkAndRotate () {
67
68
// NOTE: checkAndRotate can reintroduce a certificate that has been released, thereby creating an unbounded cache.
68
69
// A certificate can also have been rotated already, leaving the list of issued certs stale, and we re-rotate.
69
70
// the latter is not a bug, but a source of inefficiency.
70
- for _ , cert := range m .ListIssuedCertificates () {
71
+
72
+ certs := map [string ]* Certificate {}
73
+ m .cache .Range (func (keyIface interface {}, certInterface interface {}) bool {
74
+ key := keyIface .(string )
75
+ certs [key ] = certInterface .(* Certificate )
76
+ return true // continue the iteration
77
+ })
78
+ for key , cert := range certs {
71
79
shouldRotate := cert .ShouldRotate ()
72
80
73
81
word := map [bool ]string {true : "will" , false : "will not" }[shouldRotate ]
@@ -78,7 +86,14 @@ func (m *Manager) checkAndRotate() {
78
86
RenewBeforeCertExpires )
79
87
80
88
if shouldRotate {
81
- newCert , err := m .IssueCertificate (cert .GetCommonName (), m .serviceCertValidityDuration )
89
+ opts := []IssueOption {WithValidityPeriod (m .serviceCertValidityDuration )}
90
+ // if the key is equal to the common name, then it was issued with FullCNProvided(). This will prevent
91
+ // an additional trust domain from being appended. We don't do this in every case, in case the trust domain
92
+ // has changed since the last issue.
93
+ if key == cert .CommonName .String () {
94
+ opts = append (opts , FullCNProvided ())
95
+ }
96
+ newCert , err := m .IssueCertificate (key , opts ... )
82
97
if err != nil {
83
98
// TODO(#3962): metric might not be scraped before process restart resulting from this error
84
99
log .Error ().Err (err ).Str (errcode .Kind , errcode .GetErrCodeWithMetric (errcode .ErrRotatingCert )).
@@ -97,8 +112,8 @@ func (m *Manager) checkAndRotate() {
97
112
}
98
113
}
99
114
100
- func (m * Manager ) getFromCache (cn CommonName ) * Certificate {
101
- certInterface , exists := m .cache .Load (cn )
115
+ func (m * Manager ) getFromCache (key string ) * Certificate {
116
+ certInterface , exists := m .cache .Load (key )
102
117
if ! exists {
103
118
return nil
104
119
}
@@ -112,18 +127,24 @@ func (m *Manager) getFromCache(cn CommonName) *Certificate {
112
127
}
113
128
114
129
// IssueCertificate implements Manager and returns a newly issued certificate from the given client.
115
- func (m * Manager ) IssueCertificate (cn CommonName , validityPeriod time. Duration ) (* Certificate , error ) {
130
+ func (m * Manager ) IssueCertificate (prefix string , opts ... IssueOption ) (* Certificate , error ) {
116
131
var err error
117
- cert := m .getFromCache (cn ) // Don't call this while holding the lock
132
+ cert := m .getFromCache (prefix ) // Don't call this while holding the lock
133
+
134
+ options := defaultOptions (m .serviceCertValidityDuration )
135
+
136
+ for _ , o := range opts {
137
+ o (options )
138
+ }
118
139
119
- m .mu .RLock ()
140
+ m .mu .Lock ()
120
141
validatingIssuer := m .validatingIssuer
121
142
signingIssuer := m .signingIssuer
122
- m .mu .RUnlock ()
143
+ m .mu .Unlock ()
123
144
124
145
start := time .Now ()
125
146
if cert == nil || cert .signingIssuerID != signingIssuer .ID || cert .validatingIssuerID != validatingIssuer .ID {
126
- cert , err = signingIssuer .IssueCertificate (cn , validityPeriod )
147
+ cert , err = signingIssuer .IssueCertificate (options . formatCN ( prefix , signingIssuer . TrustDomain ), options . validityPeriod )
127
148
if err != nil {
128
149
return nil , err
129
150
}
@@ -138,17 +159,17 @@ func (m *Manager) IssueCertificate(cn CommonName, validityPeriod time.Duration)
138
159
cert .validatingIssuerID = validatingIssuer .ID
139
160
}
140
161
141
- m .cache .Store (cn , cert )
162
+ m .cache .Store (prefix , cert )
142
163
143
164
log .Trace ().Msgf ("It took %s to issue certificate with SerialNumber=%s" , time .Since (start ), cert .GetSerialNumber ())
144
165
145
166
return cert , nil
146
167
}
147
168
148
169
// ReleaseCertificate is called when a cert will no longer be needed and should be removed from the system.
149
- func (m * Manager ) ReleaseCertificate (cn CommonName ) {
150
- log .Trace ().Msgf ("Releasing certificate %s" , cn )
151
- m .cache .Delete (cn )
170
+ func (m * Manager ) ReleaseCertificate (key string ) {
171
+ log .Trace ().Msgf ("Releasing certificate %s" , key )
172
+ m .cache .Delete (key )
152
173
}
153
174
154
175
// ListIssuedCertificates implements CertificateDebugger interface and returns the list of issued certificates.
0 commit comments