summaryrefslogtreecommitdiffstats
path: root/athenz-identity-provider-service/src/main/java/com/yahoo/vespa/hosted/athenz/instanceproviderservice/identitydocument/IdentityDocumentResource.java
blob: 24efde665aa900098492fbbbc7dfb62cbc352883 (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
// Copyright 2018 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.identitydocument;

import com.google.inject.Inject;
import com.yahoo.container.jaxrs.annotation.Component;
import com.yahoo.jdisc.http.servlet.ServletRequest;
import com.yahoo.log.LogLevel;
import com.yahoo.vespa.athenz.identityprovider.api.bindings.IdentityDocumentApi;
import com.yahoo.vespa.athenz.identityprovider.api.bindings.SignedIdentityDocument;
import com.yahoo.vespa.hosted.provision.restapi.v2.filter.NodePrincipal;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.BadRequestException;
import javax.ws.rs.ForbiddenException;
import javax.ws.rs.GET;
import javax.ws.rs.InternalServerErrorException;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.logging.Logger;

/**
 * @author bjorncs
 */
@Path("/identity-document")
public class IdentityDocumentResource implements IdentityDocumentApi {

    private static final Logger log = Logger.getLogger(IdentityDocumentResource.class.getName());

    private final IdentityDocumentGenerator identityDocumentGenerator;
    private final HttpServletRequest request;

    @Inject
    public IdentityDocumentResource(@Component IdentityDocumentGenerator identityDocumentGenerator,
                                    @Context HttpServletRequest request) {
        this.identityDocumentGenerator = identityDocumentGenerator;
        this.request = request;
    }

    /**
     * @deprecated Use {@link #getNodeIdentityDocument(String)} and {@link #getTenantIdentityDocument(String)} instead.
     */
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Deprecated
    @Override
    // TODO Make this method private when the rest api is not longer in use
    public SignedIdentityDocument getIdentityDocument(@QueryParam("hostname") String hostname) {
        if (hostname == null) {
            throw new BadRequestException("The 'hostname' query parameter is missing");
        }
        NodePrincipal principal = (NodePrincipal) request.getAttribute(ServletRequest.JDISC_REQUEST_PRINCIPAL);
        String remoteHost;
        if (principal == null) {
            // TODO Remove once self-signed certs are gone
            log.warning("Client is not authenticated - fallback to remote ip");
            remoteHost = request.getRemoteAddr();
        } else {
            remoteHost = principal.getHostIdentityName();
        }
        // TODO Move this check to AuthorizationFilter in node-repository
        if (!identityDocumentGenerator.validateAccess(hostname, remoteHost)) {
            throw new ForbiddenException();
        }
        try {
            return identityDocumentGenerator.generateSignedIdentityDocument(hostname);
        } catch (Exception e) {
            String message = String.format("Unable to generate identity doument for '%s': %s", hostname, e.getMessage());
            log.log(LogLevel.ERROR, message, e);
            throw new InternalServerErrorException(message, e);
        }
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/node/{host}")
    @Override
    public SignedIdentityDocument getNodeIdentityDocument(@PathParam("host") String host) {
        return getIdentityDocument(host);
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/tenant/{host}")
    @Override
    public SignedIdentityDocument getTenantIdentityDocument(@PathParam("host") String host) {
        return getIdentityDocument(host);
    }

}