|
| 1 | +/* |
| 2 | + * Copyright 2022 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.pubsublite.kafka.internal; |
| 18 | + |
| 19 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 20 | +import static java.util.Collections.singletonList; |
| 21 | + |
| 22 | +import com.google.auth.oauth2.AccessToken; |
| 23 | +import com.google.auth.oauth2.GoogleCredentials; |
| 24 | +import com.google.common.collect.ImmutableMap; |
| 25 | +import com.google.gson.Gson; |
| 26 | +import com.sun.net.httpserver.HttpServer; |
| 27 | +import java.io.IOException; |
| 28 | +import java.net.InetAddress; |
| 29 | +import java.net.InetSocketAddress; |
| 30 | +import java.time.Duration; |
| 31 | +import java.time.Instant; |
| 32 | +import java.util.Base64; |
| 33 | + |
| 34 | +public class AuthServer { |
| 35 | + public static int PORT = 14293; |
| 36 | + public static InetSocketAddress ADDRESS = |
| 37 | + new InetSocketAddress(InetAddress.getLoopbackAddress(), PORT); |
| 38 | + |
| 39 | + private static final String HEADER = |
| 40 | + new Gson().toJson(ImmutableMap.of("typ", "JWT", "alg", "GOOG_TOKEN")); |
| 41 | + |
| 42 | + static { |
| 43 | + spawnDaemon(); |
| 44 | + } |
| 45 | + |
| 46 | + private static String b64Encode(String data) { |
| 47 | + return Base64.getUrlEncoder().encodeToString(data.getBytes(UTF_8)); |
| 48 | + } |
| 49 | + |
| 50 | + private static String getJwt(AccessToken token) { |
| 51 | + return new Gson() |
| 52 | + .toJson( |
| 53 | + ImmutableMap.of( |
| 54 | + "exp", |
| 55 | + token.getExpirationTime().toInstant().getEpochSecond(), |
| 56 | + "iat", |
| 57 | + Instant.now().getEpochSecond(), |
| 58 | + "scope", |
| 59 | + "pubsub", |
| 60 | + "sub", |
| 61 | + "unused")); |
| 62 | + } |
| 63 | + |
| 64 | + private static String getKafkaAccessToken(AccessToken token) { |
| 65 | + return String.join( |
| 66 | + ".", b64Encode(HEADER), b64Encode(getJwt(token)), b64Encode(token.getTokenValue())); |
| 67 | + } |
| 68 | + |
| 69 | + private static String getResponse(GoogleCredentials creds) throws IOException { |
| 70 | + creds.refreshIfExpired(); |
| 71 | + AccessToken token = creds.getAccessToken(); |
| 72 | + long exipiresInSeconds = |
| 73 | + Duration.between(Instant.now(), token.getExpirationTime().toInstant()).getSeconds(); |
| 74 | + return new Gson() |
| 75 | + .toJson( |
| 76 | + ImmutableMap.of( |
| 77 | + "access_token", |
| 78 | + getKafkaAccessToken(token), |
| 79 | + "token_type", |
| 80 | + "bearer", |
| 81 | + "expires_in", |
| 82 | + Long.toString(exipiresInSeconds))); |
| 83 | + } |
| 84 | + |
| 85 | + private static void spawnDaemon() { |
| 86 | + // Run spawn() in a daemon thread so the created threads are themselves daemons. |
| 87 | + Thread thread = new Thread(AuthServer::spawn); |
| 88 | + thread.setDaemon(true); |
| 89 | + thread.start(); |
| 90 | + } |
| 91 | + |
| 92 | + private static void spawn() { |
| 93 | + try { |
| 94 | + GoogleCredentials creds = |
| 95 | + GoogleCredentials.getApplicationDefault() |
| 96 | + .createScoped("https://www.googleapis.com/auth/cloud-platform"); |
| 97 | + HttpServer server = HttpServer.create(ADDRESS, 0); |
| 98 | + server.createContext( |
| 99 | + "/", |
| 100 | + handler -> { |
| 101 | + try { |
| 102 | + byte[] response = getResponse(creds).getBytes(UTF_8); |
| 103 | + handler.getResponseHeaders().put("Content-type", singletonList("text/plain")); |
| 104 | + handler.sendResponseHeaders(200, response.length); |
| 105 | + handler.getResponseBody().write(response); |
| 106 | + handler.close(); |
| 107 | + } catch (Exception e) { |
| 108 | + e.printStackTrace(System.err); |
| 109 | + throw new RuntimeException(e); |
| 110 | + } |
| 111 | + }); |
| 112 | + server.start(); |
| 113 | + } catch (Exception e) { |
| 114 | + e.printStackTrace(System.err); |
| 115 | + throw new RuntimeException(e); |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments