blob: 2ff0c1ab05ce9545b55279099214a144b2d1a1fa (
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
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.restapi.athenz;
import com.yahoo.component.annotation.Inject;
import com.yahoo.config.provision.SystemName;
import com.yahoo.container.jdisc.HttpResponse;
import com.yahoo.restapi.ResourceResponse;
import com.yahoo.restapi.RestApi;
import com.yahoo.restapi.RestApiRequestHandler;
import com.yahoo.slime.Cursor;
import com.yahoo.slime.Slime;
import com.yahoo.vespa.athenz.api.AthenzDomain;
import com.yahoo.vespa.athenz.api.AthenzPrincipal;
import com.yahoo.vespa.athenz.api.AthenzUser;
import com.yahoo.vespa.hosted.controller.Controller;
import com.yahoo.vespa.hosted.controller.api.identifiers.Property;
import com.yahoo.vespa.hosted.controller.api.identifiers.PropertyId;
import com.yahoo.vespa.hosted.controller.api.integration.entity.EntityService;
import com.yahoo.vespa.hosted.controller.athenz.impl.AthenzFacade;
import java.util.Map;
import java.util.logging.Logger;
import static com.yahoo.restapi.RestApi.route;
/**
* This API proxies requests to an Athenz server.
*
* @author jonmv
*/
@SuppressWarnings("unused") // Handler
public class AthenzApiHandler extends RestApiRequestHandler<AthenzApiHandler> {
private final static Logger log = Logger.getLogger(AthenzApiHandler.class.getName());
private final AthenzFacade athenz;
private final AthenzDomain sandboxDomain;
private final EntityService properties;
@Inject
public AthenzApiHandler(Context parentCtx, AthenzFacade athenz, Controller controller) {
super(parentCtx, AthenzApiHandler::createRestApi);
this.athenz = athenz;
this.sandboxDomain = new AthenzDomain(sandboxDomainIn(controller.system()));
this.properties = controller.serviceRegistry().entityService();
}
private static RestApi createRestApi(AthenzApiHandler self) {
return RestApi.builder()
.addRoute(route("/athenz/v1")
.get(self::root))
.addRoute(route("/athenz/v1/domains")
.get(self::domainList))
.addRoute(route("/athenz/v1/properties")
.get(self::properties))
.addRoute(route("/athenz/v1/user")
.post(self::signup))
.build();
}
private HttpResponse root(RestApi.RequestContext ctx) {
return new ResourceResponse(ctx.request(), "domains", "properties");
}
private Slime properties(RestApi.RequestContext ctx) {
Slime slime = new Slime();
Cursor response = slime.setObject();
Cursor array = response.setArray("properties");
for (Map.Entry<PropertyId, Property> entry : properties.listProperties().entrySet()) {
Cursor propertyObject = array.addObject();
propertyObject.setString("propertyid", entry.getKey().id());
propertyObject.setString("property", entry.getValue().id());
}
return slime;
}
private Slime domainList(RestApi.RequestContext ctx) {
Slime slime = new Slime();
Cursor array = slime.setObject().setArray("data");
for (AthenzDomain athenzDomain : athenz.getDomainList(ctx.queryParameters().getString("prefix").orElse(null)))
array.addString(athenzDomain.getName());
return slime;
}
private String signup(RestApi.RequestContext ctx) {
AthenzUser user = athenzUser(ctx);
athenz.addTenantAdmin(sandboxDomain, user);
return "User '" + user.getName() + "' added to admin role of '" + sandboxDomain.getName() + "'";
}
private static AthenzUser athenzUser(RestApi.RequestContext ctx) {
return ctx.userPrincipal()
.filter(AthenzPrincipal.class::isInstance)
.map(AthenzPrincipal.class::cast)
.map(AthenzPrincipal::getIdentity)
.filter(AthenzUser.class::isInstance)
.map(AthenzUser.class::cast)
.orElseThrow(() -> new IllegalArgumentException("No Athenz user principal on request"));
}
static String sandboxDomainIn(SystemName system) {
switch (system) {
case main: return "vespa.vespa.tenants.sandbox";
case cd: return "vespa.vespa.cd.tenants.sandbox";
default: throw new IllegalArgumentException("No sandbox domain in system '" + system + "'");
}
}
}
|