|
| 1 | +package com.authzed; |
| 2 | + |
| 3 | +import com.authzed.api.materialize.v0.Cursor; |
| 4 | +import com.authzed.api.materialize.v0.LookupPermissionSetsRequest; |
| 5 | +import com.authzed.api.materialize.v0.WatchPermissionSetsRequest; |
| 6 | +import com.authzed.api.materialize.v0.WatchPermissionSetsServiceGrpc; |
| 7 | +import com.authzed.api.v1.ZedToken; |
| 8 | +import com.authzed.grpcutil.BearerToken; |
| 9 | +import com.google.protobuf.TextFormat; |
| 10 | +import io.grpc.ManagedChannel; |
| 11 | +import io.grpc.netty.shaded.io.grpc.netty.GrpcSslContexts; |
| 12 | +import io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder; |
| 13 | +import io.grpc.netty.shaded.io.netty.handler.ssl.util.InsecureTrustManagerFactory; |
| 14 | + |
| 15 | +import javax.net.ssl.SSLException; |
| 16 | +import java.io.FileWriter; |
| 17 | +import java.io.IOException; |
| 18 | +import java.nio.file.Files; |
| 19 | +import java.nio.file.Paths; |
| 20 | +import java.util.concurrent.CountDownLatch; |
| 21 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 22 | +import java.util.concurrent.atomic.AtomicInteger; |
| 23 | + |
| 24 | +public class MaterializeClient { |
| 25 | + |
| 26 | + private static final String BEARER_TOKEN = "some token"; |
| 27 | + private static final String SERVER_HOST = "localhost"; |
| 28 | + private static final int SERVER_PORT = 50054; |
| 29 | + private static final String CURSOR_FILE = "cursor.txt"; |
| 30 | + private static final String ZED_TOKEN_FILE = "zed_token.txt"; |
| 31 | + private static final int PAGE_LIMIT = 10000; |
| 32 | + |
| 33 | + public static void main(String[] args) throws InterruptedException, SSLException { |
| 34 | + |
| 35 | + WatchPermissionSetsServiceGrpc.WatchPermissionSetsServiceStub stub = buildWatchPermissionSetsServiceStub(); |
| 36 | + |
| 37 | + System.out.println("Starting LookupPermissionSets..."); |
| 38 | + lookupPermissionSets(stub); |
| 39 | + |
| 40 | + System.out.println("Starting WatchPermissionSets..."); |
| 41 | + watchPermissionSets(stub); |
| 42 | + } |
| 43 | + |
| 44 | + private static void lookupPermissionSets(WatchPermissionSetsServiceGrpc.WatchPermissionSetsServiceStub stub) throws InterruptedException { |
| 45 | + final CountDownLatch[] pageLatch = {new CountDownLatch(1)}; |
| 46 | + AtomicBoolean lastPage = new AtomicBoolean(false); |
| 47 | + |
| 48 | + LookupPermissionSetsStreamObserver responseStreamObserver = new LookupPermissionSetsStreamObserver( |
| 49 | + pageLatch, lastPage, PAGE_LIMIT); |
| 50 | + |
| 51 | + LookupPermissionSetsRequest request = LookupPermissionSetsRequest.newBuilder().setLimit(PAGE_LIMIT) |
| 52 | + .build(); |
| 53 | + |
| 54 | + // Initial call to LPS, without any cursor. |
| 55 | + stub.lookupPermissionSets(request, responseStreamObserver); |
| 56 | + pageLatch[0].await(); |
| 57 | + |
| 58 | + // Page through results, using cursor to iterate through pages. |
| 59 | + while (!lastPage.get()) { |
| 60 | + pageLatch[0] = new CountDownLatch(1); |
| 61 | + request = LookupPermissionSetsRequest.newBuilder().setLimit(PAGE_LIMIT).setOptionalStartingAfterCursor(readCursorFromFile()) |
| 62 | + .build(); |
| 63 | + stub.lookupPermissionSets(request, responseStreamObserver); |
| 64 | + pageLatch[0].await(); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + private static void watchPermissionSets(WatchPermissionSetsServiceGrpc.WatchPermissionSetsServiceStub stub) { |
| 69 | + |
| 70 | + WatchPermissionSetsStreamObserver responseStreamObserver = new WatchPermissionSetsStreamObserver(); |
| 71 | + |
| 72 | + Cursor cursor = readCursorFromFile(); |
| 73 | + |
| 74 | + WatchPermissionSetsRequest.Builder requestBuilder = WatchPermissionSetsRequest.newBuilder(); |
| 75 | + if (cursor != null) { |
| 76 | + requestBuilder.setOptionalStartingAfter(cursor.getToken()); |
| 77 | + } |
| 78 | + WatchPermissionSetsRequest request = requestBuilder.build(); |
| 79 | + |
| 80 | + stub.watchPermissionSets(request, responseStreamObserver); |
| 81 | + |
| 82 | + // Keep the main thread alive to maintain the stream |
| 83 | + try { |
| 84 | + Thread.sleep(Long.MAX_VALUE); |
| 85 | + } catch (InterruptedException e) { |
| 86 | + System.out.println("Stream interrupted"); |
| 87 | + } |
| 88 | + |
| 89 | + } |
| 90 | + |
| 91 | + private static WatchPermissionSetsServiceGrpc.WatchPermissionSetsServiceStub buildWatchPermissionSetsServiceStub() throws SSLException { |
| 92 | + BearerToken bearerToken = new BearerToken(BEARER_TOKEN); |
| 93 | + ManagedChannel channel = NettyChannelBuilder.forAddress(SERVER_HOST, SERVER_PORT) |
| 94 | + .sslContext(GrpcSslContexts.forClient() |
| 95 | + .trustManager(InsecureTrustManagerFactory.INSTANCE) |
| 96 | + .build()) |
| 97 | + .build(); |
| 98 | + |
| 99 | + return WatchPermissionSetsServiceGrpc.newStub(channel) |
| 100 | + .withCallCredentials(bearerToken); |
| 101 | + } |
| 102 | + |
| 103 | + private static Cursor readCursorFromFile() { |
| 104 | + try { |
| 105 | + String content = Files.readString(Paths.get(CURSOR_FILE)); |
| 106 | + Cursor.Builder cursorBuilder = Cursor.newBuilder(); |
| 107 | + TextFormat.merge(content, cursorBuilder); |
| 108 | + return cursorBuilder.build(); |
| 109 | + } catch (Exception e) { |
| 110 | + System.out.println("Could not read or parse cursor.txt file: " + e.getMessage()); |
| 111 | + return null; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + static void writeCursorToFile(Cursor cursor) { |
| 116 | + try (FileWriter writer = new FileWriter(CURSOR_FILE, false)) { |
| 117 | + writer.write(cursor.toString()); |
| 118 | + } catch (IOException e) { |
| 119 | + System.err.println("Error writing cursor to file: " + e.getMessage()); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + static void writeZedTokenToFile(ZedToken zedToken) { |
| 124 | + try (FileWriter writer = new FileWriter(ZED_TOKEN_FILE, false)) { |
| 125 | + writer.write(zedToken.toString()); |
| 126 | + } catch (IOException e) { |
| 127 | + System.err.println("Error writing zed token to file: " + e.getMessage()); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + private static ZedToken readZedTokenFromFile() { |
| 132 | + try { |
| 133 | + String content = Files.readString(Paths.get(CURSOR_FILE)); |
| 134 | + ZedToken.Builder zedTokenBuilder = ZedToken.newBuilder(); |
| 135 | + TextFormat.merge(content, zedTokenBuilder); |
| 136 | + return zedTokenBuilder.build(); |
| 137 | + } catch (Exception e) { |
| 138 | + System.out.println("Could not read or parse zed_token.txt file: " + e.getMessage()); |
| 139 | + return null; |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + static void printCounts(AtomicInteger memberToSetRecordCount, AtomicInteger overallRecordCount, AtomicInteger recordCount, AtomicInteger setToSetRecordCount) { |
| 144 | + System.out.printf("RecordCount: %d. OverallRecordCount: %d. MemberToSetCount: %d. SetToSetCount: %d.\n", |
| 145 | + recordCount.get(), |
| 146 | + overallRecordCount.get(), |
| 147 | + memberToSetRecordCount.get(), |
| 148 | + setToSetRecordCount.get() |
| 149 | + ); |
| 150 | + } |
| 151 | +} |
0 commit comments