Skip to content

Commit 94a8a41

Browse files
Adding e2e test for cert renewal with given private key pem file (#931)
Signed-off-by: Pravin Pushkar <[email protected]>
1 parent a134860 commit 94a8a41

File tree

4 files changed

+80
-1
lines changed

4 files changed

+80
-1
lines changed

pkg/kubernetes/renew_certificate.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"crypto/rand"
1919
"crypto/x509"
2020
"encoding/pem"
21+
"errors"
2122
"fmt"
2223
"io/ioutil"
2324
"os"
@@ -154,7 +155,11 @@ func GenerateNewCertificates(validUntil time.Duration, privateKeyFile string) ([
154155
if err != nil {
155156
return nil, nil, nil, err
156157
}
157-
rootKey, err = x509.ParseECPrivateKey(privateKeyBytes)
158+
privateKeyPemBlock, _ := pem.Decode(privateKeyBytes)
159+
if privateKeyPemBlock == nil {
160+
return nil, nil, nil, errors.New("provided private key file is not pem encoded")
161+
}
162+
rootKey, err = x509.ParseECPrivateKey(privateKeyPemBlock.Bytes)
158163
if err != nil {
159164
return nil, nil, nil, err
160165
}

tests/e2e/common/common.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,34 @@ func GenerateNewCertAndRenew(details VersionDetails, opts TestOptions) func(t *t
424424
}
425425
}
426426

427+
func UseProvidedPrivateKeyAndRenewCerts(details VersionDetails, opts TestOptions) func(t *testing.T) {
428+
return func(t *testing.T) {
429+
daprPath := getDaprPath()
430+
args := []string{
431+
"mtls", "renew-certificate", "-k",
432+
"--private-key", "../testdata/example-root.key",
433+
"--valid-until", "20",
434+
}
435+
output, err := spawn.Command(daprPath, args...)
436+
t.Log(output)
437+
require.NoError(t, err, "expected no error on certificate renewal")
438+
439+
done := make(chan struct{})
440+
podsRunning := make(chan struct{})
441+
442+
go waitAllPodsRunning(t, DaprTestNamespace, opts.HAEnabled, done, podsRunning)
443+
select {
444+
case <-podsRunning:
445+
t.Logf("verified all pods running in namespace %s are running after certficate change", DaprTestNamespace)
446+
case <-time.After(2 * time.Minute):
447+
done <- struct{}{}
448+
t.Logf("timeout verifying all pods running in namespace %s", DaprTestNamespace)
449+
t.FailNow()
450+
}
451+
assert.Contains(t, output, "Certificate rotation is successful!")
452+
}
453+
}
454+
427455
func UseProvidedNewCertAndRenew(details VersionDetails, opts TestOptions) func(t *testing.T) {
428456
return func(t *testing.T) {
429457
daprPath := getDaprPath()

tests/e2e/kubernetes/kubernetes_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,3 +265,44 @@ func TestRenewCertificateMTLSDisabled(t *testing.T) {
265265
t.Run(tc.Name, tc.Callable)
266266
}
267267
}
268+
269+
func TestRenewCertWithPrivateKey(t *testing.T) {
270+
common.EnsureUninstall(true)
271+
272+
tests := []common.TestCase{}
273+
var installOpts = common.TestOptions{
274+
HAEnabled: false,
275+
MTLSEnabled: true,
276+
ApplyComponentChanges: true,
277+
CheckResourceExists: map[common.Resource]bool{
278+
common.CustomResourceDefs: true,
279+
common.ClusterRoles: true,
280+
common.ClusterRoleBindings: true,
281+
},
282+
}
283+
284+
tests = append(tests, common.GetTestsOnInstall(currentVersionDetails, installOpts)...)
285+
286+
// tests for certifcate renewal with newly generated certificates when pem encoded private root.key file is provided
287+
tests = append(tests, []common.TestCase{
288+
{"Renew certificate which expires in less than 30 days", common.UseProvidedPrivateKeyAndRenewCerts(currentVersionDetails, installOpts)},
289+
}...)
290+
291+
tests = append(tests, common.GetTestsPostCertificateRenewal(currentVersionDetails, installOpts)...)
292+
tests = append(tests, []common.TestCase{
293+
{"Cert Expiry warning message check " + currentVersionDetails.RuntimeVersion, common.CheckMTLSStatus(currentVersionDetails, installOpts, true)},
294+
}...)
295+
296+
// teardown everything
297+
tests = append(tests, common.GetTestsOnUninstall(currentVersionDetails, common.TestOptions{
298+
CheckResourceExists: map[common.Resource]bool{
299+
common.CustomResourceDefs: true,
300+
common.ClusterRoles: false,
301+
common.ClusterRoleBindings: false,
302+
},
303+
})...)
304+
305+
for _, tc := range tests {
306+
t.Run(tc.Name, tc.Callable)
307+
}
308+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-----BEGIN EC PRIVATE KEY-----
2+
MHcCAQEEIHAXJW1CiSkXb4T1+Hx9Kefsd+CF+s4KzJl+P95Y4FnaoAoGCCqGSM49
3+
AwEHoUQDQgAE4c8Fq5Ol7n4FkDnaEp9VjklE2fBNybcq5vwZOdFTjCsNE9HivnPd
4+
qXIeSTyYAZ87E3BP5tvlcwCmxiM/p8FIpQ==
5+
-----END EC PRIVATE KEY-----

0 commit comments

Comments
 (0)