Skip to content

Commit df0d06d

Browse files
pkalsi97squakez
authored andcommitted
fix: simplify CA certificate handling and preserve base truststore password
1 parent d5f32b0 commit df0d06d

File tree

5 files changed

+28
-71
lines changed

5 files changed

+28
-71
lines changed

e2e/common/traits/jvm_test.go

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -91,44 +91,6 @@ func TestJVMTrait(t *testing.T) {
9191
g.Eventually(IntegrationLogs(t, ctx, ns, name), TestTimeoutShort).Should(ContainSubstring("Hello World!"))
9292
})
9393

94-
t.Run("JVM trait CA cert (deprecated fields)", func(t *testing.T) {
95-
certPem, err := generateSelfSignedCert()
96-
require.NoError(t, err)
97-
98-
caCertData := make(map[string]string)
99-
caCertData["ca.crt"] = string(certPem)
100-
101-
err = CreatePlainTextSecret(t, ctx, ns, "test-ca-cert", caCertData)
102-
require.NoError(t, err)
103-
104-
passwordData := make(map[string]string)
105-
passwordData["password"] = "test-password-123"
106-
err = CreatePlainTextSecret(t, ctx, ns, "test-ca-password", passwordData)
107-
require.NoError(t, err)
108-
109-
name := RandomizedSuffixName("cacert")
110-
g.Expect(KamelRun(t, ctx, ns,
111-
"./files/Java.java",
112-
"--name", name,
113-
"-t", "mount.configs=secret:test-ca-cert",
114-
"-t", "mount.configs=secret:test-ca-password",
115-
// Using deprecated fields for backward compatibility test
116-
"-t", "jvm.ca-cert=/etc/camel/conf.d/_secrets/test-ca-cert/ca.crt",
117-
"-t", "jvm.ca-cert-password=/etc/camel/conf.d/_secrets/test-ca-password/password",
118-
).Execute()).To(Succeed())
119-
120-
g.Eventually(IntegrationPodPhase(t, ctx, ns, name), TestTimeoutLong).Should(Equal(corev1.PodRunning))
121-
g.Eventually(IntegrationConditionStatus(t, ctx, ns, name, v1.IntegrationConditionReady), TestTimeoutShort).Should(Equal(corev1.ConditionTrue))
122-
123-
pod := IntegrationPod(t, ctx, ns, name)()
124-
g.Expect(pod).NotTo(BeNil())
125-
initContainerNames := make([]string, 0)
126-
for _, c := range pod.Spec.InitContainers {
127-
initContainerNames = append(initContainerNames, c.Name)
128-
}
129-
g.Expect(initContainerNames).To(ContainElement("generate-truststore"))
130-
})
131-
13294
t.Run("JVM trait multiple CA certs", func(t *testing.T) {
13395
// Test the new ca-certificates field with multiple certificates, each with its own password
13496
cert1Pem, err := generateSelfSignedCert()

pkg/trait/init_containers.go

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -95,28 +95,20 @@ func (t *initContainersTrait) Configure(e *Environment) (bool, *TraitCondition,
9595
if ok && jvm.hasCACerts() {
9696
var allCommands []string
9797

98+
var truststorePassPath string
9899
if jvm.hasBaseTruststore() {
99100
baseTruststore := jvm.getBaseTruststore()
101+
truststorePassPath = baseTruststore.PasswordPath
100102
copyCmd := fmt.Sprintf("cp %s %s", baseTruststore.TruststorePath, jvm.getTrustStorePath())
101103
allCommands = append(allCommands, copyCmd)
102-
103-
entries := jvm.getAllCACertEntries()
104-
if len(entries) > 0 {
105-
changePassCmd := fmt.Sprintf(
106-
"keytool -storepasswd -keystore %s -storepass:file %s -new $(cat %s)",
107-
jvm.getTrustStorePath(), baseTruststore.PasswordPath, entries[0].PasswordPath,
108-
)
109-
allCommands = append(allCommands, changePassCmd)
104+
} else {
105+
certEntries := jvm.getAllCACertEntries()
106+
if len(certEntries) > 0 {
107+
truststorePassPath = certEntries[0].PasswordPath
110108
}
111109
}
112110

113-
certEntries := jvm.getAllCACertEntries()
114-
// Use the first certificate's password for all imports since they share the same truststore
115-
truststorePassPath := ""
116-
if len(certEntries) > 0 {
117-
truststorePassPath = certEntries[0].PasswordPath
118-
}
119-
for i, entry := range certEntries {
111+
for i, entry := range jvm.getAllCACertEntries() {
120112
cmd := fmt.Sprintf(
121113
"keytool -importcert -noprompt -alias custom-ca-%d -storepass:file %s -keystore %s -file %s",
122114
i, truststorePassPath, jvm.getTrustStorePath(), entry.CertPath,

pkg/trait/init_containers_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -686,9 +686,9 @@ func TestApplyInitContainerWithBaseTruststore(t *testing.T) {
686686
assert.Contains(t, commandStr, "/bin/bash")
687687

688688
assert.Contains(t, commandStr, "cp /opt/java/openjdk/lib/security/cacerts")
689-
assert.Contains(t, commandStr, "keytool -storepasswd")
690-
assert.Contains(t, commandStr, "-storepass:file /etc/camel/conf.d/_secrets/base-truststore-pass/password")
689+
assert.NotContains(t, commandStr, "keytool -storepasswd")
691690
assert.Contains(t, commandStr, "keytool -importcert")
691+
assert.Contains(t, commandStr, "-storepass:file /etc/camel/conf.d/_secrets/base-truststore-pass/password")
692692
assert.Contains(t, commandStr, "/etc/camel/conf.d/_secrets/my-ca/ca.crt")
693693
assert.Contains(t, commandStr, "&&")
694694
}

pkg/trait/jvm.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -398,15 +398,22 @@ func (t *jvmTrait) configureCaCert() []string {
398398
return nil
399399
}
400400

401-
// Get the password path from the first certificate entry
402-
// All certificates use the same truststore, so we use the first entry's password
403-
entries := t.getAllCACertEntries()
404-
if len(entries) == 0 {
405-
return nil
401+
// Determine which password to use for the truststore:
402+
// If base truststore exists, use its password (we keep the original truststore password)
403+
// Otherwise, use the first certificate's password
404+
var truststorePassPath string
405+
if t.hasBaseTruststore() {
406+
truststorePassPath = t.getBaseTruststore().PasswordPath
407+
} else {
408+
entries := t.getAllCACertEntries()
409+
if len(entries) == 0 {
410+
return nil
411+
}
412+
truststorePassPath = entries[0].PasswordPath
406413
}
407414

408415
return []string{
409416
"-Djavax.net.ssl.trustStore=" + t.getTrustStorePath(),
410-
fmt.Sprintf("-Djavax.net.ssl.trustStorePassword=$(cat %s)", entries[0].PasswordPath),
417+
fmt.Sprintf("-Djavax.net.ssl.trustStorePassword=$(cat %s)", truststorePassPath),
411418
}
412419
}

pkg/trait/jvm_cacert.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,8 @@ func (t *jvmTrait) getAllCACertEntries() []CACertEntry {
104104
// validateCACertConfig validates the CA certificate configuration.
105105
func (t *jvmTrait) validateCACertConfig() error {
106106
for i, cert := range t.CACertificates {
107-
if cert.CertPath != "" && cert.PasswordPath == "" {
108-
return fmt.Errorf("CACertificates[%d]: password path is required when certificate path is specified", i)
109-
}
110-
if cert.CertPath == "" && cert.PasswordPath != "" {
111-
return fmt.Errorf("CACertificates[%d]: certificate path is required when password path is specified", i)
107+
if cert.CertPath == "" || cert.PasswordPath == "" {
108+
return fmt.Errorf("CACertificates[%d]: both cert-path and password-path are required", i)
112109
}
113110
}
114111

@@ -118,11 +115,10 @@ func (t *jvmTrait) validateCACertConfig() error {
118115
}
119116

120117
if t.BaseTruststore != nil {
121-
if t.BaseTruststore.TruststorePath != "" && t.BaseTruststore.PasswordPath == "" {
122-
return errors.New("base-truststore password path is required when truststore path is specified")
123-
}
124-
if t.BaseTruststore.TruststorePath == "" && t.BaseTruststore.PasswordPath != "" {
125-
return errors.New("base-truststore truststore path is required when password path is specified")
118+
hasTruststorePath := t.BaseTruststore.TruststorePath != ""
119+
hasPasswordPath := t.BaseTruststore.PasswordPath != ""
120+
if hasTruststorePath != hasPasswordPath {
121+
return errors.New("base-truststore: both truststore-path and password-path are required")
126122
}
127123
}
128124

0 commit comments

Comments
 (0)