summaryrefslogtreecommitdiffstats
path: root/athenz-identity-provider-service/src/test/java/com/yahoo/vespa/hosted/athenz/instanceproviderservice/AthenzInstanceProviderServiceTest.java
blob: a15d681de3941f492bf8a7835fa328aae3330e3c (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.athenz.instanceproviderservice;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableSet;
import com.yahoo.component.Version;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.ApplicationName;
import com.yahoo.config.provision.ClusterMembership;
import com.yahoo.config.provision.Environment;
import com.yahoo.config.provision.InstanceName;
import com.yahoo.config.provision.NodeType;
import com.yahoo.config.provision.RegionName;
import com.yahoo.config.provision.SystemName;
import com.yahoo.config.provision.TenantName;
import com.yahoo.config.provision.Zone;
import com.yahoo.log.LogLevel;
import com.yahoo.vespa.hosted.athenz.instanceproviderservice.config.AthenzProviderServiceConfig;
import com.yahoo.vespa.hosted.athenz.instanceproviderservice.impl.CertificateClient;
import com.yahoo.vespa.hosted.athenz.instanceproviderservice.impl.IdentityDocumentGenerator;
import com.yahoo.vespa.hosted.athenz.instanceproviderservice.impl.InstanceValidator;
import com.yahoo.vespa.hosted.athenz.instanceproviderservice.impl.KeyProvider;
import com.yahoo.vespa.hosted.athenz.instanceproviderservice.impl.Utils;
import com.yahoo.vespa.hosted.athenz.instanceproviderservice.impl.model.IdentityDocument;
import com.yahoo.vespa.hosted.athenz.instanceproviderservice.impl.model.InstanceConfirmation;
import com.yahoo.vespa.hosted.athenz.instanceproviderservice.impl.model.ProviderUniqueId;
import com.yahoo.vespa.hosted.athenz.instanceproviderservice.impl.model.SignedIdentityDocument;
import com.yahoo.vespa.hosted.provision.Node;
import com.yahoo.vespa.hosted.provision.NodeRepository;
import com.yahoo.vespa.hosted.provision.node.Allocation;
import com.yahoo.vespa.hosted.provision.node.Generation;
import com.yahoo.vespa.hosted.provision.testutils.MockNodeFlavors;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.asn1.x500.X500Name;
import org.bouncycastle.asn1.x509.BasicConstraints;
import org.bouncycastle.cert.CertIOException;
import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter;
import org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.operator.ContentSigner;
import org.bouncycastle.operator.OperatorCreationException;
import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder;
import org.junit.Test;

import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.InvalidKeyException;
import java.security.KeyManagementException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.SignatureException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.time.Instant;
import java.time.temporal.TemporalAmount;
import java.util.Base64;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.Optional;
import java.util.logging.Logger;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
 * @author bjorncs
 */
public class AthenzInstanceProviderServiceTest {

    private static final Logger log = Logger.getLogger(AthenzInstanceProviderServiceTest.class.getName());
    private static final int PORT = 12345;
    private static final Zone ZONE = new Zone(SystemName.cd, Environment.dev, RegionName.from("us-north-1"));

    @Test
    public void provider_service_hosts_endpoint_secured_with_tls() throws Exception {
        String domain = "domain";
        String service = "service";
        AutoGeneratedKeyProvider keyProvider = new AutoGeneratedKeyProvider();
        PrivateKey privateKey = keyProvider.getPrivateKey(0);
        AthenzProviderServiceConfig config = getAthenzProviderConfig(domain, service);
        ScheduledExecutorServiceMock executor = new ScheduledExecutorServiceMock();

        AthenzInstanceProviderService athenzInstanceProviderService =
                new AthenzInstanceProviderService(config,
                                                  keyProvider,
                                                  executor,
                                                  mock(NodeRepository.class),
                                                  ZONE,
                                                  new SelfSignedCertificateClient(keyProvider.getKeyPair(), config));

        try (CloseableHttpClient client = createHttpClient(domain, service)) {
            Runnable certificateRefreshCommand = executor.getCommand()
                    .orElseThrow(() -> new AssertionError("Command not present"));
            assertFalse(getStatus(client));
            certificateRefreshCommand.run();
            assertTrue(getStatus(client));
            assertInstanceConfirmationSucceeds(client, privateKey);
            certificateRefreshCommand.run();
            assertTrue(getStatus(client));
            assertInstanceConfirmationSucceeds(client, privateKey);
        } finally {
            athenzInstanceProviderService.deconstruct();
        }
    }

    @Test
    public void generates_valid_identity_document() throws Exception {
        String hostname = "x.y.com";

        ApplicationId appid = ApplicationId.from(
                TenantName.from("tenant"), ApplicationName.from("application"), InstanceName.from("default"));
        Allocation allocation = new Allocation(appid,
                                               ClusterMembership.from("container/default/0/0", Version.fromString("1.2.3")),
                                               Generation.inital(),
                                               false);
        Node n = Node.create("ostkid",
                             ImmutableSet.of("127.0.0.1"),
                             new HashSet<>(),
                             hostname,
                             Optional.empty(),
                             new MockNodeFlavors().getFlavorOrThrow("default"),
                             NodeType.tenant)
                .with(allocation);

        NodeRepository nodeRepository = mock(NodeRepository.class);
        when(nodeRepository.getNode(eq(hostname))).thenReturn(Optional.of(n));
        AutoGeneratedKeyProvider keyProvider = new AutoGeneratedKeyProvider();

        IdentityDocumentGenerator identityDocumentGenerator = new IdentityDocumentGenerator(
                getAthenzProviderConfig("domain", "service"),
                nodeRepository,
                ZONE,
                keyProvider);
        String rawSignedIdentityDocument = identityDocumentGenerator.generateSignedIdentityDocument(hostname);


        SignedIdentityDocument signedIdentityDocument =
                Utils.getMapper().readValue(rawSignedIdentityDocument, SignedIdentityDocument.class);

        // Verify attributes
        assertEquals(hostname, signedIdentityDocument.identityDocument.instanceHostname);
        ProviderUniqueId expectedProviderUniqueId =
                new ProviderUniqueId("tenant", "application", "dev", "us-north-1", "default", "default", 0);
        assertEquals(expectedProviderUniqueId, signedIdentityDocument.identityDocument.providerUniqueId);

        // Validate signature
        assertTrue("Message", InstanceValidator.isSignatureValid(keyProvider.getPublicKey(0),
                                                                 signedIdentityDocument.rawIdentityDocument,
                                                                 signedIdentityDocument.signature));
    }

    private static AthenzProviderServiceConfig getAthenzProviderConfig(String domain, String service) {
        return new AthenzProviderServiceConfig(
                        new AthenzProviderServiceConfig.Builder()
                                .domain(domain)
                                .serviceName(service)
                                .port(PORT)
                                .keyPathPrefix("dummy-path")
                                .certDnsSuffix("dnsSuffix")
                                .ztsUrl("localhost/zts")
                                .athenzPrincipalHeaderName("Yahoo-Principal-Auth")
                                .apiPath("/"));

    }
    private static boolean getStatus(HttpClient client) {
        try {
            HttpResponse response = client.execute(new HttpGet("https://localhost:" + PORT + "/status.html"));
            return response.getStatusLine().getStatusCode() == HttpStatus.SC_OK;
        } catch (Exception e) {
            log.log(LogLevel.INFO, "Status.html failed: " + e);
            return false;
        }
    }

    private static void assertInstanceConfirmationSucceeds(HttpClient client, PrivateKey privateKey) throws IOException {
        HttpPost httpPost = new HttpPost("https://localhost:" + PORT + "/");
        httpPost.setEntity(createInstanceConfirmation(privateKey));
        HttpResponse response = client.execute(httpPost);
        assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
    }

    private static CloseableHttpClient createHttpClient(String domain, String service)
            throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException {
        SSLContext sslContext = new SSLContextBuilder()
                .loadTrustMaterial(null, (certificateChain, ignoredAuthType) ->
                        certificateChain[0].getSubjectX500Principal().getName().equals("CN=" + domain + "." + service))
                .build();

        return HttpClients.custom()
                .setSslcontext(sslContext)
                .setSSLHostnameVerifier(new NoopHostnameVerifier())
                .build();
    }

    private static HttpEntity createInstanceConfirmation(PrivateKey privateKey) {
        IdentityDocument identityDocument = new IdentityDocument(
                new ProviderUniqueId("tenant", "application", "environment", "region", "instance", "cluster-id", 0),
                "hostname",
                "instance-hostname",
                Instant.now());
        try {
            ObjectMapper mapper = Utils.getMapper();
            String encodedIdentityDocument =
                    Base64.getEncoder().encodeToString(mapper.writeValueAsString(identityDocument).getBytes());
            Signature sigGenerator = Signature.getInstance("SHA512withRSA");
            sigGenerator.initSign(privateKey);
            sigGenerator.update(encodedIdentityDocument.getBytes());

            InstanceConfirmation instanceConfirmation = new InstanceConfirmation(
                    "provider", "domain", "service",
                    new SignedIdentityDocument(encodedIdentityDocument,
                                               Base64.getEncoder().encodeToString(sigGenerator.sign()),
                                               0,
                                               identityDocument.providerUniqueId.asString(),
                                               "dnssuffix",
                                               "service",
                                               "localhost/zts",
                                               1));
            return new StringEntity(mapper.writeValueAsString(instanceConfirmation));
        } catch (JsonProcessingException
                | NoSuchAlgorithmException
                | UnsupportedEncodingException
                | SignatureException
                | InvalidKeyException e) {
            throw new RuntimeException(e);
        }
    }

    private static class AutoGeneratedKeyProvider implements KeyProvider {

        private final KeyPair keyPair;

        public AutoGeneratedKeyProvider() throws IOException, NoSuchAlgorithmException {
            KeyPairGenerator rsa = KeyPairGenerator.getInstance("RSA");
            rsa.initialize(2048);
            keyPair = rsa.genKeyPair();
        }

        @Override
        public PrivateKey getPrivateKey(int version) {
            return keyPair.getPrivate();
        }

        @Override
        public PublicKey getPublicKey(int version) {
            return keyPair.getPublic();
        }

        public KeyPair getKeyPair() {
            return keyPair;
        }
    }

    private static class SelfSignedCertificateClient implements CertificateClient {

        private final KeyPair keyPair;
        private final AthenzProviderServiceConfig config;

        private SelfSignedCertificateClient(KeyPair keyPair, AthenzProviderServiceConfig config) {
            this.keyPair = keyPair;
            this.config = config;
        }

        @Override
        public X509Certificate updateCertificate(PrivateKey privateKey, TemporalAmount expiryTime) {
            try {
                ContentSigner contentSigner = new JcaContentSignerBuilder("SHA512WithRSA").build(keyPair.getPrivate());
                X500Name dnName = new X500Name("CN=" + config.domain() + "." + config.serviceName());
                Calendar calendar = Calendar.getInstance();
                calendar.add(Calendar.HOUR, 1);
                Date endDate = calendar.getTime();
                JcaX509v3CertificateBuilder certBuilder = new JcaX509v3CertificateBuilder(
                        dnName, BigInteger.ONE, new Date(), endDate, dnName, keyPair.getPublic());
                certBuilder.addExtension(new ASN1ObjectIdentifier("2.5.29.19"), true, new BasicConstraints(true));

                return new JcaX509CertificateConverter()
                        .setProvider(new BouncyCastleProvider())
                        .getCertificate(certBuilder.build(contentSigner));
            } catch (CertificateException | CertIOException | OperatorCreationException e) {
                throw new RuntimeException(e);
            }
        }
    }
}