aboutsummaryrefslogtreecommitdiffstats
path: root/security-utils/src/test/java/com/yahoo/security/AutoReloadingX509KeyManagerTest.java
blob: c335acc12bee1ae4a63f0696665ecea1e38bdf5a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.security;

import com.yahoo.security.AutoReloadingX509KeyManager;
import com.yahoo.security.KeyAlgorithm;
import com.yahoo.security.KeyUtils;
import com.yahoo.security.SignatureAlgorithm;
import com.yahoo.security.X509CertificateBuilder;
import com.yahoo.security.X509CertificateUtils;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;

import javax.security.auth.x500.X500Principal;

import java.io.File;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.KeyPair;
import java.security.Principal;
import java.security.cert.X509Certificate;
import java.time.Instant;
import java.util.concurrent.ScheduledExecutorService;

import static java.time.temporal.ChronoUnit.DAYS;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.verify;

/**
 * @author bjorncs
 */
public class AutoReloadingX509KeyManagerTest {
    private static final X500Principal SUBJECT = new X500Principal("CN=dummy");

    @TempDir
    public File tempDirectory;

    @Test
    void crypto_material_is_reloaded_when_scheduler_task_is_executed() throws IOException {
        KeyPair keyPair = KeyUtils.generateKeypair(KeyAlgorithm.EC);
        Path privateKeyFile = File.createTempFile("junit", null, tempDirectory).toPath();
        Files.write(privateKeyFile, KeyUtils.toPem(keyPair.getPrivate()).getBytes());

        Path certificateFile = File.createTempFile("junit", null, tempDirectory).toPath();
        BigInteger serialNumberInitialCertificate = BigInteger.ONE;
        X509Certificate initialCertificate = generateCertificate(keyPair, serialNumberInitialCertificate);
        Files.write(certificateFile, X509CertificateUtils.toPem(initialCertificate).getBytes());

        ScheduledExecutorService scheduler = Mockito.mock(ScheduledExecutorService.class);
        ArgumentCaptor<Runnable> updaterTaskCaptor = ArgumentCaptor.forClass(Runnable.class);

        AutoReloadingX509KeyManager keyManager = new AutoReloadingX509KeyManager(privateKeyFile, certificateFile, scheduler);
        verify(scheduler).scheduleAtFixedRate(updaterTaskCaptor.capture(), anyLong(), anyLong(), any());

        String[] initialAliases = keyManager.getClientAliases(keyPair.getPublic().getAlgorithm(), new Principal[]{SUBJECT});
        X509Certificate[] certChain = keyManager.getCertificateChain(initialAliases[0]);
        assertThat(certChain).hasSize(1);
        assertThat(certChain[0].getSerialNumber()).isEqualTo(serialNumberInitialCertificate);

        BigInteger serialNumberUpdatedCertificate = BigInteger.TEN;
        X509Certificate updatedCertificate = generateCertificate(keyPair, serialNumberUpdatedCertificate);
        Files.write(certificateFile, X509CertificateUtils.toPem(updatedCertificate).getBytes());

        updaterTaskCaptor.getValue().run(); // run update task in ReloadingX509KeyManager

        String[] updatedAliases = keyManager.getClientAliases(keyPair.getPublic().getAlgorithm(), new Principal[]{SUBJECT});
        X509Certificate[] updatedCertChain = keyManager.getCertificateChain(updatedAliases[0]);
        assertThat(updatedCertChain).hasSize(1);
        assertThat(updatedCertChain[0].getSerialNumber()).isEqualTo(serialNumberUpdatedCertificate);
    }

    private static X509Certificate generateCertificate(KeyPair keyPair, BigInteger serialNumber) {
        return X509CertificateBuilder.fromKeypair(keyPair,
                                                  SUBJECT,
                                                  Instant.EPOCH,
                                                  Instant.EPOCH.plus(1, DAYS),
                                                  SignatureAlgorithm.SHA256_WITH_ECDSA,
                                                  serialNumber)
                .build();
    }
}