Skip to content

Commit 0cfd3bf

Browse files
committed
add os version check and set configuration param properly
in addition, use dir mount as default Signed-off-by: josedev-union <[email protected]>
1 parent 8fd5b17 commit 0cfd3bf

File tree

3 files changed

+202
-159
lines changed

3 files changed

+202
-159
lines changed

opensearch-operator/pkg/reconcilers/tls.go

Lines changed: 81 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -127,18 +127,34 @@ func (r *TLSReconciler) handleAdminCertificate() (*ctrl.Result, error) {
127127
return res, nil
128128
}
129129

130-
func (r *TLSReconciler) securityChangeVersion() bool {
131-
newVersionConstraint, err := semver.NewConstraint(">=2.0.0")
130+
func (r *TLSReconciler) checkVersionConstraint(constraint string, defaultOnError bool, errMsg string) bool {
131+
versionConstraint, err := semver.NewConstraint(constraint)
132132
if err != nil {
133133
panic(err)
134134
}
135135

136136
version, err := semver.NewVersion(r.instance.Spec.General.Version)
137137
if err != nil {
138-
r.logger.Error(err, "unable to parse version, assuming >= 2.0.0")
139-
return true
138+
r.logger.Error(err, errMsg)
139+
return defaultOnError
140140
}
141-
return newVersionConstraint.Check(version)
141+
return versionConstraint.Check(version)
142+
}
143+
144+
func (r *TLSReconciler) securityChangeVersion() bool {
145+
return r.checkVersionConstraint(
146+
">=2.0.0",
147+
true,
148+
"unable to parse version, assuming >= 2.0.0",
149+
)
150+
}
151+
152+
func (r *TLSReconciler) supportsHotReload() bool {
153+
return r.checkVersionConstraint(
154+
">=2.19.1",
155+
false,
156+
"unable to parse version for hot reload check, assuming not supported",
157+
)
142158
}
143159

144160
func (r *TLSReconciler) adminCAName() string {
@@ -448,29 +464,40 @@ func (r *TLSReconciler) handleTransportExistingCerts() error {
448464
// r.recorder.Event(r.instance, "Warning", "Security", "Notice - Not all secrets for transport provided")
449465
return err
450466
}
467+
468+
// Implement new mounting logic based on CaSecret.Name configuration
451469
if tlsConfig.CaSecret.Name == "" {
470+
// If CaSecret.Name is empty, mount Secret.Name as a directory
471+
mountFolder("transport", "certs", tlsConfig.Secret.Name, r.reconcilerContext)
472+
} else if tlsConfig.CaSecret.Name == tlsConfig.Secret.Name {
473+
// If CaSecret.Name is same as Secret.Name, mount only Secret.Name as a directory
452474
mountFolder("transport", "certs", tlsConfig.Secret.Name, r.reconcilerContext)
453475
} else {
454-
enableHotReload := tlsConfig.EnableHotReload
455-
mountWithHotReload("transport", "ca", CaCertKey, tlsConfig.CaSecret.Name, r.reconcilerContext, enableHotReload)
456-
mountWithHotReload("transport", "key", corev1.TLSPrivateKeyKey, tlsConfig.Secret.Name, r.reconcilerContext, enableHotReload)
457-
mountWithHotReload("transport", "cert", corev1.TLSCertKey, tlsConfig.Secret.Name, r.reconcilerContext, enableHotReload)
476+
// If CaSecret.Name is different from Secret.Name, mount both secrets as directories
477+
// Mount Secret.Name as tls-transport/
478+
mountFolder("transport", "certs", tlsConfig.Secret.Name, r.reconcilerContext)
479+
// Mount CaSecret.Name as tls-transport-ca/
480+
mountFolder("transport", "ca", tlsConfig.CaSecret.Name, r.reconcilerContext)
458481
}
459-
// Extend opensearch.yml with appropriate file paths based on hot reload setting
460-
if tlsConfig.EnableHotReload && tlsConfig.CaSecret.Name != "" {
461-
r.reconcilerContext.AddConfig("plugins.security.ssl.transport.pemcert_filepath", fmt.Sprintf("tls-transport-cert/%s", corev1.TLSCertKey))
462-
r.reconcilerContext.AddConfig("plugins.security.ssl.transport.pemkey_filepath", fmt.Sprintf("tls-transport-key/%s", corev1.TLSPrivateKeyKey))
482+
483+
// Extend opensearch.yml with appropriate file paths based on mounting logic
484+
if tlsConfig.CaSecret.Name == "" || tlsConfig.CaSecret.Name == tlsConfig.Secret.Name {
485+
// Single secret mounted as directory
486+
r.reconcilerContext.AddConfig("plugins.security.ssl.transport.pemcert_filepath", fmt.Sprintf("tls-transport/%s", corev1.TLSCertKey))
487+
r.reconcilerContext.AddConfig("plugins.security.ssl.transport.pemkey_filepath", fmt.Sprintf("tls-transport/%s", corev1.TLSPrivateKeyKey))
488+
r.reconcilerContext.AddConfig("plugins.security.ssl.transport.pemtrustedcas_filepath", fmt.Sprintf("tls-transport/%s", CaCertKey))
463489
} else {
490+
// Separate secrets mounted as directories
464491
r.reconcilerContext.AddConfig("plugins.security.ssl.transport.pemcert_filepath", fmt.Sprintf("tls-transport/%s", corev1.TLSCertKey))
465492
r.reconcilerContext.AddConfig("plugins.security.ssl.transport.pemkey_filepath", fmt.Sprintf("tls-transport/%s", corev1.TLSPrivateKeyKey))
493+
r.reconcilerContext.AddConfig("plugins.security.ssl.transport.pemtrustedcas_filepath", fmt.Sprintf("tls-transport-ca/%s", CaCertKey))
466494
}
467495
r.reconcilerContext.AddConfig("plugins.security.ssl.transport.enforce_hostname_verification", "false")
468-
}
469-
// Set CA certificate path based on hot reload setting
470-
if tlsConfig.EnableHotReload && tlsConfig.CaSecret.Name != "" {
471-
r.reconcilerContext.AddConfig("plugins.security.ssl.transport.pemtrustedcas_filepath", fmt.Sprintf("tls-transport-ca/%s", CaCertKey))
472-
} else {
473-
r.reconcilerContext.AddConfig("plugins.security.ssl.transport.pemtrustedcas_filepath", fmt.Sprintf("tls-transport/%s", CaCertKey))
496+
497+
// Enable hot reload if configured and version supports it
498+
if tlsConfig.EnableHotReload && r.supportsHotReload() {
499+
r.reconcilerContext.AddConfig("plugins.security.ssl.certificates_hot_reload.enabled", "true")
500+
}
474501
}
475502
dnList := strings.Join(tlsConfig.NodesDn, "\",\"")
476503
r.reconcilerContext.AddConfig("plugins.security.nodes_dn", fmt.Sprintf("[\"%s\"]", dnList))
@@ -539,25 +566,41 @@ func (r *TLSReconciler) handleHttp() error {
539566
// r.recorder.Event(r.instance, "Warning", "Security", "Notice - Not all secrets for http provided")
540567
return err
541568
}
569+
570+
// Implement new mounting logic based on CaSecret.Name configuration
542571
if tlsConfig.CaSecret.Name == "" {
572+
// If CaSecret.Name is empty, mount Secret.Name as a directory
573+
mountFolder("http", "certs", tlsConfig.Secret.Name, r.reconcilerContext)
574+
} else if tlsConfig.CaSecret.Name == tlsConfig.Secret.Name {
575+
// If CaSecret.Name is same as Secret.Name, mount only Secret.Name as a directory
543576
mountFolder("http", "certs", tlsConfig.Secret.Name, r.reconcilerContext)
544577
} else {
545-
enableHotReload := tlsConfig.EnableHotReload
546-
mountWithHotReload("http", "ca", CaCertKey, tlsConfig.CaSecret.Name, r.reconcilerContext, enableHotReload)
547-
mountWithHotReload("http", "key", corev1.TLSPrivateKeyKey, tlsConfig.Secret.Name, r.reconcilerContext, enableHotReload)
548-
mountWithHotReload("http", "cert", corev1.TLSCertKey, tlsConfig.Secret.Name, r.reconcilerContext, enableHotReload)
578+
// If CaSecret.Name is different from Secret.Name, mount both secrets as directories
579+
// Mount Secret.Name as tls-http/
580+
mountFolder("http", "certs", tlsConfig.Secret.Name, r.reconcilerContext)
581+
// Mount CaSecret.Name as tls-http-ca/
582+
mountFolder("http", "ca", tlsConfig.CaSecret.Name, r.reconcilerContext)
549583
}
550584
}
551-
// Extend opensearch.yml with appropriate file paths based on hot reload setting
585+
// Extend opensearch.yml with appropriate file paths based on mounting logic
552586
r.reconcilerContext.AddConfig("plugins.security.ssl.http.enabled", "true")
553-
if tlsConfig.EnableHotReload && tlsConfig.CaSecret.Name != "" {
554-
r.reconcilerContext.AddConfig("plugins.security.ssl.http.pemcert_filepath", fmt.Sprintf("tls-http-cert/%s", corev1.TLSCertKey))
555-
r.reconcilerContext.AddConfig("plugins.security.ssl.http.pemkey_filepath", fmt.Sprintf("tls-http-key/%s", corev1.TLSPrivateKeyKey))
556-
r.reconcilerContext.AddConfig("plugins.security.ssl.http.pemtrustedcas_filepath", fmt.Sprintf("tls-http-ca/%s", CaCertKey))
557-
} else {
587+
588+
// Set certificate file paths based on mounting configuration
589+
if tlsConfig.CaSecret.Name == "" || tlsConfig.CaSecret.Name == tlsConfig.Secret.Name {
590+
// Single secret mounted as directory
558591
r.reconcilerContext.AddConfig("plugins.security.ssl.http.pemcert_filepath", fmt.Sprintf("tls-http/%s", corev1.TLSCertKey))
559592
r.reconcilerContext.AddConfig("plugins.security.ssl.http.pemkey_filepath", fmt.Sprintf("tls-http/%s", corev1.TLSPrivateKeyKey))
560593
r.reconcilerContext.AddConfig("plugins.security.ssl.http.pemtrustedcas_filepath", fmt.Sprintf("tls-http/%s", CaCertKey))
594+
} else {
595+
// Separate secrets mounted as directories
596+
r.reconcilerContext.AddConfig("plugins.security.ssl.http.pemcert_filepath", fmt.Sprintf("tls-http/%s", corev1.TLSCertKey))
597+
r.reconcilerContext.AddConfig("plugins.security.ssl.http.pemkey_filepath", fmt.Sprintf("tls-http/%s", corev1.TLSPrivateKeyKey))
598+
r.reconcilerContext.AddConfig("plugins.security.ssl.http.pemtrustedcas_filepath", fmt.Sprintf("tls-http-ca/%s", CaCertKey))
599+
}
600+
601+
// Enable hot reload if configured and version supports it
602+
if tlsConfig.EnableHotReload && r.supportsHotReload() {
603+
r.reconcilerContext.AddConfig("plugins.security.ssl.certificates_hot_reload.enabled", "true")
561604
}
562605
return nil
563606
}
@@ -604,7 +647,15 @@ func mountWithHotReload(interfaceName string, name string, filename string, secr
604647
func mountFolder(interfaceName string, name string, secretName string, reconcilerContext *ReconcilerContext) {
605648
volume := corev1.Volume{Name: interfaceName + "-" + name, VolumeSource: corev1.VolumeSource{Secret: &corev1.SecretVolumeSource{SecretName: secretName}}}
606649
reconcilerContext.Volumes = append(reconcilerContext.Volumes, volume)
607-
mount := corev1.VolumeMount{Name: interfaceName + "-" + name, MountPath: fmt.Sprintf("/usr/share/opensearch/config/tls-%s", interfaceName)}
650+
651+
var mountPath string
652+
if name == "ca" {
653+
mountPath = fmt.Sprintf("/usr/share/opensearch/config/tls-%s-ca", interfaceName)
654+
} else {
655+
mountPath = fmt.Sprintf("/usr/share/opensearch/config/tls-%s", interfaceName)
656+
}
657+
658+
mount := corev1.VolumeMount{Name: interfaceName + "-" + name, MountPath: mountPath}
608659
reconcilerContext.VolumeMounts = append(reconcilerContext.VolumeMounts, mount)
609660
}
610661

opensearch-operator/pkg/reconcilers/tls_hotreload_test.go

Lines changed: 0 additions & 123 deletions
This file was deleted.

0 commit comments

Comments
 (0)