aboutsummaryrefslogtreecommitdiffstats
path: root/configserver/src/test/java/com/yahoo/vespa/config/server/http/SecretStoreValidatorTest.java
blob: f07c822c4acf7ff52217dd81b1b0525ecd9a90e7 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server.http;

import com.github.tomakehurst.wiremock.junit.WireMockRule;
import com.yahoo.config.model.api.HostInfo;
import com.yahoo.config.model.api.Model;
import com.yahoo.config.model.api.ServiceInfo;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.SystemName;
import com.yahoo.config.provision.TenantName;
import com.yahoo.container.jdisc.secretstore.SecretStore;
import com.yahoo.slime.SlimeUtils;
import com.yahoo.vespa.config.server.application.Application;
import com.yahoo.vespa.config.server.tenant.SecretStoreExternalIdRetriever;
import org.junit.Rule;
import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
 * @author olaa
 */
public class SecretStoreValidatorTest {

    private final SecretStore secretStore = mock(SecretStore.class);
    private final SecretStoreValidator secretStoreValidator = new SecretStoreValidator(secretStore);

    @Rule
    public final WireMockRule wireMock = new WireMockRule(options().port(4080), true);

    @Test
    public void createsCorrectRequestData() throws IOException {
        var app = mockApplication();
        var requestBody = SlimeUtils.jsonToSlime("{\"awsId\":\"123\"," +
                "\"name\":\"store\"," +
                "\"role\":\"role\"," +
                "\"region\":\"some-region\"," +
                "\"parameterName\":\"some-parameter\"" +
                "}");
        var expectedSecretName = SecretStoreExternalIdRetriever.secretName(TenantName.defaultName(), SystemName.PublicCd, "store");
        when(secretStore.getSecret(expectedSecretName)).thenReturn("some-secret-value");
        stubFor(post(urlEqualTo("/validate-secret-store"))
                .withRequestBody(equalToJson("{\"awsId\":\"123\"," +
                        "\"name\":\"store\"," +
                        "\"role\":\"role\"," +
                        "\"region\":\"some-region\"," +
                        "\"parameterName\":\"some-parameter\"," +
                        "\"externalId\":\"some-secret-value\"" +
                        "}"))
                .willReturn(aResponse()
                        .withStatus(200)
                        .withBody("is ok")));
        var response = secretStoreValidator.validateSecretStore(app, SystemName.PublicCd, requestBody);
        var body = new ByteArrayOutputStream();
        response.render(body);
        assertEquals("is ok", body.toString());
    }

    private Application mockApplication() {
        var app = mock(Application.class);
        var model = mock(Model.class);
        var hostList = createHostList();
        when(app.getModel()).thenReturn(model);
        when(model.getHosts()).thenReturn(hostList);
        when(app.getId()).thenReturn(ApplicationId.defaultId());
        return app;
    }

    private List<HostInfo> createHostList() {
        return List.of(new HostInfo("localhost",
                List.of(new ServiceInfo("default", "container", null, null, "", "localhost"))
        ));
    }
}