|
| 1 | +/* |
| 2 | + * Copyright 2022 The gRPC Authors |
| 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 io.grpc.authz; |
| 18 | + |
| 19 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 20 | + |
| 21 | +import com.google.common.annotations.VisibleForTesting; |
| 22 | +import com.google.common.base.Strings; |
| 23 | +import com.google.common.util.concurrent.ThreadFactoryBuilder; |
| 24 | +import io.grpc.Metadata; |
| 25 | +import io.grpc.ServerCall; |
| 26 | +import io.grpc.ServerCallHandler; |
| 27 | +import io.grpc.ServerInterceptor; |
| 28 | +import java.io.Closeable; |
| 29 | +import java.io.File; |
| 30 | +import java.io.IOException; |
| 31 | +import java.nio.file.Files; |
| 32 | +import java.nio.file.attribute.FileTime; |
| 33 | +import java.util.concurrent.Executors; |
| 34 | +import java.util.concurrent.ScheduledExecutorService; |
| 35 | +import java.util.concurrent.ScheduledFuture; |
| 36 | +import java.util.concurrent.TimeUnit; |
| 37 | +import java.util.logging.Level; |
| 38 | +import java.util.logging.Logger; |
| 39 | + |
| 40 | +/** |
| 41 | + * Authorization server interceptor for policy from file with refresh capability. |
| 42 | + * The class will get <a href="https://github.com/grpc/proposal/blob/master/A43-grpc-authorization-api.md#user-facing-authorization-policy"> |
| 43 | + * gRPC Authorization policy</a> from a JSON file during initialization. |
| 44 | + */ |
| 45 | +public final class FileWatcherAuthorizationServerInterceptor implements ServerInterceptor { |
| 46 | + private static final Logger logger = |
| 47 | + Logger.getLogger(FileWatcherAuthorizationServerInterceptor.class.getName()); |
| 48 | + |
| 49 | + private volatile AuthorizationServerInterceptor internalAuthzServerInterceptor; |
| 50 | + private final ScheduledExecutorService scheduledExecutorService = |
| 51 | + Executors.newSingleThreadScheduledExecutor( |
| 52 | + new ThreadFactoryBuilder() |
| 53 | + .setNameFormat("filewatcher" + "-%d") |
| 54 | + .setDaemon(true) |
| 55 | + .build()); |
| 56 | + |
| 57 | + private final File policyFile; |
| 58 | + private FileTime lastModifiedTime; |
| 59 | + |
| 60 | + private Thread testCallback; |
| 61 | + |
| 62 | + private FileWatcherAuthorizationServerInterceptor(File policyFile) |
| 63 | + throws IllegalArgumentException, IOException { |
| 64 | + this.policyFile = policyFile; |
| 65 | + updateInternalInterceptor(); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall( |
| 70 | + ServerCall<ReqT, RespT> call, Metadata headers, |
| 71 | + ServerCallHandler<ReqT, RespT> next) { |
| 72 | + return internalAuthzServerInterceptor.interceptCall(call, headers, next); |
| 73 | + } |
| 74 | + |
| 75 | + void updateInternalInterceptor() throws IOException { |
| 76 | + FileTime currentTime = Files.getLastModifiedTime(policyFile.toPath()); |
| 77 | + if (currentTime.equals(lastModifiedTime)) { |
| 78 | + return; |
| 79 | + } |
| 80 | + String policyContents = new String(Files.readAllBytes(policyFile.toPath())); |
| 81 | + lastModifiedTime = currentTime; |
| 82 | + internalAuthzServerInterceptor = AuthorizationServerInterceptor.create(policyContents); |
| 83 | + if (testCallback != null) { |
| 84 | + testCallback.start(); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Policy is reloaded periodically as per the provided refresh interval. Unlike the |
| 90 | + * constructor, exception thrown during reload will be caught and logged and the |
| 91 | + * previous AuthorizationServerInterceptor will be used to make authorization |
| 92 | + * decisions. |
| 93 | + * |
| 94 | + * @param period the period between successive file load executions. |
| 95 | + * @param unit the time unit for period parameter |
| 96 | + * @return an object that caller should close when the file refreshes are not needed |
| 97 | + */ |
| 98 | + public Closeable scheduleRefreshes(long period, TimeUnit unit) |
| 99 | + throws IOException { |
| 100 | + if (period <= 0) { |
| 101 | + throw new IllegalArgumentException("Refresh interval must be greater than 0"); |
| 102 | + } |
| 103 | + final ScheduledFuture<?> future = |
| 104 | + scheduledExecutorService.scheduleWithFixedDelay(new Runnable() { |
| 105 | + @Override public void run() { |
| 106 | + try { |
| 107 | + updateInternalInterceptor(); |
| 108 | + } catch (Exception e) { |
| 109 | + logger.log(Level.WARNING, "Authorization Policy file reload failed: " + e); |
| 110 | + } |
| 111 | + } |
| 112 | + }, period, period, unit); |
| 113 | + return new Closeable() { |
| 114 | + @Override public void close() { |
| 115 | + future.cancel(false); |
| 116 | + } |
| 117 | + }; |
| 118 | + } |
| 119 | + |
| 120 | + @VisibleForTesting |
| 121 | + public void setCallbackForTesting(Thread callback) { |
| 122 | + testCallback = callback; |
| 123 | + } |
| 124 | + |
| 125 | + public static FileWatcherAuthorizationServerInterceptor create(File policyFile) |
| 126 | + throws IllegalArgumentException, IOException { |
| 127 | + checkNotNull(policyFile, "policyFile"); |
| 128 | + return new FileWatcherAuthorizationServerInterceptor(policyFile); |
| 129 | + } |
| 130 | +} |
0 commit comments