-
Notifications
You must be signed in to change notification settings - Fork 303
/
Copy pathAPIGatewayVerticle.java
299 lines (263 loc) · 10.2 KB
/
APIGatewayVerticle.java
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
package io.vertx.blueprint.microservice.gateway;
import io.vertx.blueprint.microservice.account.Account;
import io.vertx.blueprint.microservice.account.AccountService;
import io.vertx.blueprint.microservice.common.RestAPIVerticle;
import io.vertx.core.Future;
import io.vertx.core.http.HttpClient;
import io.vertx.core.http.HttpClientRequest;
import io.vertx.core.http.HttpServerOptions;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.core.json.JsonObject;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;
import io.vertx.core.net.JksOptions;
import io.vertx.ext.auth.oauth2.KeycloakHelper;
import io.vertx.ext.auth.oauth2.OAuth2Auth;
import io.vertx.ext.auth.oauth2.OAuth2FlowType;
import io.vertx.ext.auth.oauth2.providers.KeycloakAuth;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.handler.BodyHandler;
import io.vertx.ext.web.handler.StaticHandler;
import io.vertx.ext.web.handler.UserSessionHandler;
import io.vertx.servicediscovery.Record;
import io.vertx.servicediscovery.ServiceDiscovery;
import io.vertx.servicediscovery.types.EventBusService;
import io.vertx.servicediscovery.types.HttpEndpoint;
import java.util.List;
import java.util.Optional;
/**
* A verticle for global API gateway.
* This API gateway uses HTTP-HTTP pattern. It's also responsible for
* load balance and failure handling.
*
* @author Eric Zhao
*/
public class APIGatewayVerticle extends RestAPIVerticle {
private static final int DEFAULT_PORT = 8787;
private static final Logger logger = LoggerFactory.getLogger(APIGatewayVerticle.class);
private OAuth2Auth oauth2;
@Override
public void start(Future<Void> future) throws Exception {
super.start();
// get HTTP host and port from configuration, or use default value
String host = config().getString("api.gateway.http.address", "localhost");
int port = config().getInteger("api.gateway.http.port", DEFAULT_PORT);
Router router = Router.router(vertx);
// cookie and session handler
enableLocalSession(router);
// body handler
router.route().handler(BodyHandler.create());
// version handler
router.get("/api/v").handler(this::apiVersion);
// create OAuth 2 instance for Keycloak
oauth2 = KeycloakAuth.create(vertx, OAuth2FlowType.AUTH_CODE, config());
router.route().handler(UserSessionHandler.create(oauth2));
String hostURI = buildHostURI();
// set auth callback handler
router.route("/callback").handler(context -> authCallback(oauth2, hostURI, context));
router.get("/uaa").handler(this::authUaaHandler);
router.get("/login").handler(this::loginEntryHandler);
router.post("/logout").handler(this::logoutHandler);
// api dispatcher
router.route("/api/*").handler(this::dispatchRequests);
// static content
router.route("/*").handler(StaticHandler.create());
// enable HTTPS
HttpServerOptions httpServerOptions = new HttpServerOptions()
.setSsl(true)
.setKeyStoreOptions(new JksOptions().setPath("server.jks").setPassword("123456"));
// create http server
vertx.createHttpServer(httpServerOptions)
.requestHandler(router::accept)
.listen(port, host, ar -> {
if (ar.succeeded()) {
publishApiGateway(host, port);
future.complete();
logger.info("API Gateway is running on port " + port);
// publish log
publishGatewayLog("api_gateway_init_success:" + port);
} else {
future.fail(ar.cause());
}
});
}
private void dispatchRequests(RoutingContext context) {
int initialOffset = 5; // length of `/api/`
// run with circuit breaker in order to deal with failure
circuitBreaker.execute(future -> {
getAllEndpoints().setHandler(ar -> {
if (ar.succeeded()) {
List<Record> recordList = ar.result();
// get relative path and retrieve prefix to dispatch client
String path = context.request().uri();
if (path.length() <= initialOffset) {
notFound(context);
future.complete();
return;
}
String prefix = (path.substring(initialOffset)
.split("/"))[0];
// generate new relative path
String newPath = path.substring(initialOffset + prefix.length());
// get one relevant HTTP client, may not exist
Optional<Record> client = recordList.stream()
.filter(record -> record.getMetadata().getString("api.name") != null)
.filter(record -> record.getMetadata().getString("api.name").equals(prefix))
.findAny(); // simple load balance
if (client.isPresent()) {
doDispatch(context, newPath, discovery.getReference(client.get()).get(), future);
} else {
notFound(context);
future.complete();
}
} else {
future.fail(ar.cause());
}
});
}).setHandler(ar -> {
if (ar.failed()) {
badGateway(ar.cause(), context);
}
});
}
/**
* Dispatch the request to the downstream REST layers.
*
* @param context routing context instance
* @param path relative path
* @param client relevant HTTP client
*/
private void doDispatch(RoutingContext context, String path, HttpClient client, Future<Object> cbFuture) {
HttpClientRequest toReq = client
.request(context.request().method(), path, response -> {
response.bodyHandler(body -> {
if (response.statusCode() >= 500) { // api endpoint server error, circuit breaker should fail
cbFuture.fail(response.statusCode() + ": " + body.toString());
} else {
HttpServerResponse toRsp = context.response()
.setStatusCode(response.statusCode());
response.headers().forEach(header -> {
toRsp.putHeader(header.getKey(), header.getValue());
});
// send response
toRsp.end(body);
cbFuture.complete();
}
ServiceDiscovery.releaseServiceObject(discovery, client);
});
});
// set headers
context.request().headers().forEach(header -> {
toReq.putHeader(header.getKey(), header.getValue());
});
if (context.user() != null) {
toReq.putHeader("user-principal", context.user().principal().encode());
}
// send request
if (context.getBody() == null) {
toReq.end();
} else {
toReq.end(context.getBody());
}
}
private void apiVersion(RoutingContext context) {
context.response()
.end(new JsonObject().put("version", "v1").encodePrettily());
}
/**
* Get all REST endpoints from the service discovery infrastructure.
*
* @return async result
*/
private Future<List<Record>> getAllEndpoints() {
Future<List<Record>> future = Future.future();
discovery.getRecords(record -> record.getType().equals(HttpEndpoint.TYPE),
future.completer());
return future;
}
// log methods
private void publishGatewayLog(String info) {
JsonObject message = new JsonObject()
.put("info", info)
.put("time", System.currentTimeMillis());
publishLogEvent("gateway", message);
}
private void publishGatewayLog(JsonObject msg) {
JsonObject message = msg.copy()
.put("time", System.currentTimeMillis());
publishLogEvent("gateway", message);
}
// auth
private void authCallback(OAuth2Auth oauth2, String hostURL, RoutingContext context) {
final String code = context.request().getParam("code");
// code is a require value
if (code == null) {
context.fail(400);
return;
}
final String redirectTo = context.request().getParam("redirect_uri");
final String redirectURI = hostURL + context.currentRoute().getPath() + "?redirect_uri=" + redirectTo;
oauth2.getToken(new JsonObject().put("code", code).put("redirect_uri", redirectURI), ar -> {
if (ar.failed()) {
logger.warn("Auth fail");
context.fail(ar.cause());
} else {
logger.info("Auth success");
context.setUser(ar.result());
context.response()
.putHeader("Location", redirectTo)
.setStatusCode(302)
.end();
}
});
}
private void authUaaHandler(RoutingContext context) {
if (context.user() != null) {
JsonObject principal = context.user().principal();
String username = null; // TODO: Only for demo. Complete this in next version.
// String username = KeycloakHelper.preferredUsername(principal);
if (username == null) {
context.response()
.putHeader("content-type", "application/json")
.end(new Account().setId("TEST666").setUsername("Eric").toString()); // TODO: no username should be an error
} else {
Future<AccountService> future = Future.future();
EventBusService.getProxy(discovery, AccountService.class, future.completer());
future.compose(accountService -> {
Future<Account> accountFuture = Future.future();
accountService.retrieveByUsername(username, accountFuture.completer());
return accountFuture.map(a -> {
ServiceDiscovery.releaseServiceObject(discovery, accountService);
return a;
});
})
.setHandler(resultHandlerNonEmpty(context)); // if user does not exist, should return 404
}
} else {
context.fail(401);
}
}
private void loginEntryHandler(RoutingContext context) {
context.response()
.putHeader("Location", generateAuthRedirectURI(buildHostURI()))
.setStatusCode(302)
.end();
}
private void logoutHandler(RoutingContext context) {
context.clearUser();
context.session().destroy();
context.response().setStatusCode(204).end();
}
private String generateAuthRedirectURI(String from) {
return oauth2.authorizeURL(new JsonObject()
.put("redirect_uri", from + "/callback?redirect_uri=" + from)
.put("scope", "")
.put("state", ""));
}
private String buildHostURI() {
int port = config().getInteger("api.gateway.http.port", DEFAULT_PORT);
final String host = config().getString("api.gateway.http.address.external", "localhost");
return String.format("https://%s:%d", host, port);
}
}