aboutsummaryrefslogtreecommitdiffstats
path: root/vespa-athenz/src/test/java/com/yahoo/vespa/athenz/identityprovider/client/IdentityDocumentSignerTest.java
blob: f53518d9a07bf1f3b86f1aa80c6f0e88e34cffe3 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.athenz.identityprovider.client;

import com.yahoo.security.KeyAlgorithm;
import com.yahoo.security.KeyUtils;
import com.yahoo.vespa.athenz.api.AthenzService;
import com.yahoo.vespa.athenz.identityprovider.api.ClusterType;
import com.yahoo.vespa.athenz.identityprovider.api.IdentityType;
import com.yahoo.vespa.athenz.identityprovider.api.SignedIdentityDocument;
import com.yahoo.vespa.athenz.identityprovider.api.VespaUniqueInstanceId;
import org.junit.jupiter.api.Test;

import java.security.KeyPair;
import java.time.Instant;
import java.util.Arrays;
import java.util.HashSet;

import static com.yahoo.vespa.athenz.identityprovider.api.IdentityType.TENANT;
import static com.yahoo.vespa.athenz.identityprovider.api.SignedIdentityDocument.DEFAULT_DOCUMENT_VERSION;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
 * @author bjorncs
 */
public class IdentityDocumentSignerTest {
    public static final int KEY_VERSION = 0;

    private static final IdentityType identityType = TENANT;
    private static final VespaUniqueInstanceId id =
            new VespaUniqueInstanceId(1, "cluster-id", "instance", "application", "tenant", "region", "environment", identityType);
    private static final AthenzService providerService = new AthenzService("vespa", "service");
    private static final KeyPair keyPair = KeyUtils.generateKeypair(KeyAlgorithm.RSA);
    private static final String configserverHostname = "configserverhostname";
    private static final String instanceHostname = "instancehostname";
    private static final Instant createdAt = Instant.EPOCH;
    private static final HashSet<String> ipAddresses = new HashSet<>(Arrays.asList("1.2.3.4", "::1"));
    private static final ClusterType clusterType = ClusterType.CONTAINER;

    @Test
    void generates_and_validates_signature() {
        IdentityDocumentSigner signer = new IdentityDocumentSigner();
        String signature =
                signer.generateSignature(id, providerService, configserverHostname, instanceHostname, createdAt,
                                         ipAddresses, identityType, clusterType, keyPair.getPrivate());

        SignedIdentityDocument signedIdentityDocument = new SignedIdentityDocument(
                signature, KEY_VERSION, id, providerService, DEFAULT_DOCUMENT_VERSION, configserverHostname,
                instanceHostname, createdAt, ipAddresses, identityType, clusterType);

        assertTrue(signer.hasValidSignature(signedIdentityDocument, keyPair.getPublic()));
    }

    @Test
    void handles_missing_cluster_type() {
        IdentityDocumentSigner signer = new IdentityDocumentSigner();
        String signature =
                signer.generateSignature(id, providerService, configserverHostname, instanceHostname, createdAt,
                                         ipAddresses, identityType, /*clusterType*/null, keyPair.getPrivate());

        SignedIdentityDocument signedIdentityDocument = new SignedIdentityDocument(
                signature, KEY_VERSION, id, providerService, DEFAULT_DOCUMENT_VERSION, configserverHostname,
                instanceHostname, createdAt, ipAddresses, identityType, /*clusterType*/null);

        assertTrue(signer.hasValidSignature(signedIdentityDocument, keyPair.getPublic()));
    }

}