aboutsummaryrefslogtreecommitdiffstats
path: root/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/restapi/dataplanetoken/DataplaneTokenServiceTest.java
blob: acfba03a7002ed4b0af9e1afd8191e58b4bbe139 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.restapi.dataplanetoken;

import com.yahoo.config.provision.SystemName;
import com.yahoo.config.provision.TenantName;
import com.yahoo.vespa.hosted.controller.ControllerTester;
import com.yahoo.vespa.hosted.controller.api.integration.dataplanetoken.DataplaneToken;
import com.yahoo.vespa.hosted.controller.api.integration.dataplanetoken.DataplaneTokenVersions;
import com.yahoo.vespa.hosted.controller.api.integration.dataplanetoken.FingerPrint;
import com.yahoo.vespa.hosted.controller.api.integration.dataplanetoken.TokenId;
import com.yahoo.vespa.hosted.controller.api.role.SimplePrincipal;
import org.junit.jupiter.api.Test;

import java.security.Principal;
import java.time.Duration;
import java.util.Collection;
import java.util.List;
import java.util.Set;

import static java.util.stream.Collectors.toSet;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class DataplaneTokenServiceTest {
    private final ControllerTester tester = new ControllerTester(SystemName.Public);
    private final DataplaneTokenService dataplaneTokenService = new DataplaneTokenService(tester.controller());
    private final TenantName tenantName = TenantName.from("tenant");
    Principal principal = new SimplePrincipal("user");
    private final TokenId tokenId = TokenId.of("myTokenId");

    @Test
    void generates_and_persists_token() {
        DataplaneToken dataplaneToken = dataplaneTokenService.generateToken(tenantName, tokenId, tester.clock().instant().plus(Duration.ofDays(100)), principal);
        List<DataplaneTokenVersions> dataplaneTokenVersions = dataplaneTokenService.listTokens(tenantName);
        assertEquals(dataplaneToken.fingerPrint(), dataplaneTokenVersions.get(0).tokenVersions().get(0).fingerPrint());
        assertEquals(dataplaneToken.expiration(), dataplaneTokenVersions.get(0).tokenVersions().get(0).expiration());
    }

    @Test
    void generating_new_token_appends() {
        DataplaneToken dataplaneToken1 = dataplaneTokenService.generateToken(tenantName, tokenId, tester.clock().instant().plus(Duration.ofDays(1)), principal);
        DataplaneToken dataplaneToken2 = dataplaneTokenService.generateToken(tenantName, tokenId, null, principal);
        assertNotEquals(dataplaneToken1.fingerPrint(), dataplaneToken2.fingerPrint());

        List<DataplaneTokenVersions> dataplaneTokenVersions = dataplaneTokenService.listTokens(tenantName);
        Set<FingerPrint> tokenFingerprints = dataplaneTokenVersions.stream()
                .filter(token -> token.tokenId().equals(tokenId))
                .map(DataplaneTokenVersions::tokenVersions)
                .flatMap(Collection::stream)
                .map(DataplaneTokenVersions.Version::fingerPrint)
                .collect(toSet());
        assertEquals(tokenFingerprints, Set.of(dataplaneToken1.fingerPrint(), dataplaneToken2.fingerPrint()));
    }

    @Test
    void delete_last_fingerprint_deletes_token() {
        DataplaneToken dataplaneToken1 = dataplaneTokenService.generateToken(tenantName, tokenId, null, principal);
        DataplaneToken dataplaneToken2 = dataplaneTokenService.generateToken(tenantName, tokenId, null, principal);
        dataplaneTokenService.deleteToken(tenantName, tokenId, dataplaneToken1.fingerPrint());
        dataplaneTokenService.deleteToken(tenantName, tokenId, dataplaneToken2.fingerPrint());
        assertEquals(List.of(), dataplaneTokenService.listTokens(tenantName));
    }

    @Test
    void deleting_nonexistent_fingerprint_throws() {
        DataplaneToken dataplaneToken = dataplaneTokenService.generateToken(tenantName, tokenId, null, principal);
        DataplaneToken dataplaneToken2 = dataplaneTokenService.generateToken(tenantName, tokenId, null, principal);
        dataplaneTokenService.deleteToken(tenantName, tokenId, dataplaneToken.fingerPrint());

        // Token currently contains value of "dataplaneToken2"
        IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> dataplaneTokenService.deleteToken(tenantName, tokenId, dataplaneToken.fingerPrint()));
        assertEquals("Fingerprint does not exist: " + dataplaneToken.fingerPrint(), exception.getMessage());
    }

    @Test
    void deleting_nonexistent_token_throws() {
        DataplaneToken dataplaneToken = dataplaneTokenService.generateToken(tenantName, tokenId, null, principal);
        dataplaneTokenService.deleteToken(tenantName, tokenId, dataplaneToken.fingerPrint());

        // Token is created and deleted above, no longer exists
        IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> dataplaneTokenService.deleteToken(tenantName, tokenId, dataplaneToken.fingerPrint()));
        assertEquals("Token does not exist: " + tokenId, exception.getMessage());
    }
}