Skip to content

Commit 80f1f77

Browse files
committed
Various TLS & Sync related fixes
1 parent 6cac1c9 commit 80f1f77

File tree

5 files changed

+61
-58
lines changed

5 files changed

+61
-58
lines changed

pkg/apis/deployment/v1alpha/sync_external_access_spec.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import (
3535
type SyncExternalAccessSpec struct {
3636
ExternalAccessSpec
3737
MasterEndpoint []string `json:"masterEndpoint,omitempty"`
38-
AccessPackageSecretNames []string `json:accessPackageSecretNames,omitempty"`
38+
AccessPackageSecretNames []string `json:"accessPackageSecretNames,omitempty"`
3939
}
4040

4141
// GetMasterEndpoint returns the value of masterEndpoint.

pkg/deployment/context_impl.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ package deployment
2525
import (
2626
"context"
2727
"fmt"
28+
"net"
29+
"strconv"
2830

2931
"github.com/arangodb/arangosync/client"
3032
"github.com/arangodb/arangosync/tasks"
@@ -174,7 +176,11 @@ func (d *Deployment) GetSyncServerClient(ctx context.Context, group api.ServerGr
174176
dnsName := k8sutil.CreatePodDNSName(d.apiObject, group.AsRole(), id)
175177

176178
// Build client
177-
source := client.Endpoint{dnsName}
179+
port := k8sutil.ArangoSyncMasterPort
180+
if group == api.ServerGroupSyncWorkers {
181+
port = k8sutil.ArangoSyncWorkerPort
182+
}
183+
source := client.Endpoint{"https://" + net.JoinHostPort(dnsName, strconv.Itoa(port))}
178184
tlsAuth := tasks.TLSAuthentication{
179185
TLSClientAuthentication: tasks.TLSClientAuthentication{
180186
ClientToken: monitoringToken,

pkg/deployment/reconcile/action_wait_for_member_up.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ func (a *actionWaitForMemberUp) checkProgressCluster(ctx context.Context) (bool,
154154
// of a sync master / worker.
155155
func (a *actionWaitForMemberUp) checkProgressArangoSync(ctx context.Context) (bool, bool, error) {
156156
log := a.log
157-
c, err := a.actionCtx.GetSyncServerClient(ctx, a.action.Group, a.action.ID)
157+
c, err := a.actionCtx.GetSyncServerClient(ctx, a.action.Group, a.action.MemberID)
158158
if err != nil {
159159
log.Debug().Err(err).Msg("Failed to create arangosync client")
160160
return false, false, maskAny(err)

pkg/deployment/reconcile/plan_builder_tls.go

Lines changed: 47 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,14 @@ func createRotateTLSServerCertificatePlan(log zerolog.Logger, spec api.Deploymen
6262
Msg("Failed to get TLS secret")
6363
continue
6464
}
65-
renewalNeeded := tlsKeyfileNeedsRenewal(log, keyfile)
65+
tlsSpec := spec.TLS
66+
if group.IsArangosync() {
67+
tlsSpec = spec.Sync.TLS
68+
}
69+
renewalNeeded, reason := tlsKeyfileNeedsRenewal(log, keyfile, tlsSpec)
6670
if renewalNeeded {
6771
plan = append(append(plan,
68-
api.NewAction(api.ActionTypeRenewTLSCertificate, group, m.ID)),
72+
api.NewAction(api.ActionTypeRenewTLSCertificate, group, m.ID, reason)),
6973
createRotateMemberPlan(log, m, group, "TLS certificate renewal")...,
7074
)
7175
}
@@ -124,8 +128,32 @@ func createRotateTLSCAPlan(log zerolog.Logger, spec api.DeploymentSpec, status a
124128

125129
// tlsKeyfileNeedsRenewal decides if the certificate in the given keyfile
126130
// should be renewed.
127-
func tlsKeyfileNeedsRenewal(log zerolog.Logger, keyfile string) bool {
131+
func tlsKeyfileNeedsRenewal(log zerolog.Logger, keyfile string, spec api.TLSSpec) (bool, string) {
128132
raw := []byte(keyfile)
133+
// containsAll returns true when all elements in the expected list
134+
// are in the actual list.
135+
containsAll := func(actual []string, expected []string) bool {
136+
for _, x := range expected {
137+
found := false
138+
for _, y := range actual {
139+
if x == y {
140+
found = true
141+
break
142+
}
143+
}
144+
if !found {
145+
return false
146+
}
147+
}
148+
return true
149+
}
150+
ipsToStringSlice := func(list []net.IP) []string {
151+
result := make([]string, len(list))
152+
for i, x := range list {
153+
result[i] = x.String()
154+
}
155+
return result
156+
}
129157
for {
130158
var derBlock *pem.Block
131159
derBlock, raw = pem.Decode(raw)
@@ -137,7 +165,7 @@ func tlsKeyfileNeedsRenewal(log zerolog.Logger, keyfile string) bool {
137165
if err != nil {
138166
// We do not understand the certificate, let's renew it
139167
log.Warn().Err(err).Msg("Failed to parse x509 certificate. Renewing it")
140-
return true
168+
return true, "Cannot parse x509 certificate: " + err.Error()
141169
}
142170
if cert.IsCA {
143171
// Only look at the server certificate, not CA or intermediate
@@ -153,42 +181,31 @@ func tlsKeyfileNeedsRenewal(log zerolog.Logger, keyfile string) bool {
153181
Str("not-after", cert.NotAfter.String()).
154182
Str("expiration-date", expirationDate.String()).
155183
Msg("TLS certificate renewal needed")
156-
return true
184+
return true, "Server certificate about to expire"
185+
}
186+
// Check alternate names against spec
187+
dnsNames, ipAddresses, emailAddress, err := spec.GetParsedAltNames()
188+
if err == nil {
189+
if !containsAll(cert.DNSNames, dnsNames) {
190+
return true, "Some alternate DNS names are missing"
191+
}
192+
if !containsAll(ipsToStringSlice(cert.IPAddresses), ipAddresses) {
193+
return true, "Some alternate IP addresses are missing"
194+
}
195+
if !containsAll(cert.EmailAddresses, emailAddress) {
196+
return true, "Some alternate email addresses are missing"
197+
}
157198
}
158199
}
159200
}
160-
return false
201+
return false, ""
161202
}
162203

163204
// tlsCANeedsRenewal decides if the given CA certificate
164205
// should be renewed.
165206
// Returns: shouldRenew, reason
166207
func tlsCANeedsRenewal(log zerolog.Logger, cert string, spec api.TLSSpec) (bool, string) {
167208
raw := []byte(cert)
168-
// containsAll returns true when all elements in the expected list
169-
// are in the actual list.
170-
containsAll := func(actual []string, expected []string) bool {
171-
for _, x := range expected {
172-
found := false
173-
for _, y := range actual {
174-
if x == y {
175-
found = true
176-
break
177-
}
178-
}
179-
if !found {
180-
return false
181-
}
182-
}
183-
return true
184-
}
185-
ipsToStringSlice := func(list []net.IP) []string {
186-
result := make([]string, len(list))
187-
for i, x := range list {
188-
result[i] = x.String()
189-
}
190-
return result
191-
}
192209
for {
193210
var derBlock *pem.Block
194211
derBlock, raw = pem.Decode(raw)
@@ -218,19 +235,6 @@ func tlsCANeedsRenewal(log zerolog.Logger, cert string, spec api.TLSSpec) (bool,
218235
Msg("TLS CA certificate renewal needed")
219236
return true, "CA Certificate about to expire"
220237
}
221-
// Check alternate names against spec
222-
dnsNames, ipAddresses, emailAddress, err := spec.GetParsedAltNames()
223-
if err == nil {
224-
if !containsAll(cert.DNSNames, dnsNames) {
225-
return true, "Some alternate DNS names are missing"
226-
}
227-
if !containsAll(ipsToStringSlice(cert.IPAddresses), ipAddresses) {
228-
return true, "Some alternate IP addresses are missing"
229-
}
230-
if !containsAll(cert.EmailAddresses, emailAddress) {
231-
return true, "Some alternate email addresses are missing"
232-
}
233-
}
234238
}
235239
}
236240
return false, ""

pkg/deployment/resources/certificates_tls.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,13 @@ const (
4545
// specified in the given spec.
4646
func createTLSCACertificate(log zerolog.Logger, cli v1.CoreV1Interface, spec api.TLSSpec, deploymentName, namespace string, ownerRef *metav1.OwnerReference) error {
4747
log = log.With().Str("secret", spec.GetCASecretName()).Logger()
48-
dnsNames, ipAddresses, emailAddress, err := spec.GetParsedAltNames()
49-
if err != nil {
50-
log.Debug().Err(err).Msg("Failed to get alternate names")
51-
return maskAny(err)
52-
}
5348

5449
options := certificates.CreateCertificateOptions{
55-
CommonName: fmt.Sprintf("%s Root Certificate", deploymentName),
56-
Hosts: append(dnsNames, ipAddresses...),
57-
EmailAddresses: emailAddress,
58-
ValidFrom: time.Now(),
59-
ValidFor: caTTL,
60-
IsCA: true,
61-
ECDSACurve: tlsECDSACurve,
50+
CommonName: fmt.Sprintf("%s Root Certificate", deploymentName),
51+
ValidFrom: time.Now(),
52+
ValidFor: caTTL,
53+
IsCA: true,
54+
ECDSACurve: tlsECDSACurve,
6255
}
6356
cert, priv, err := certificates.CreateCertificate(options, nil)
6457
if err != nil {

0 commit comments

Comments
 (0)