|
| 1 | +package org.pytorch.serve.http; |
| 2 | + |
| 3 | +import io.netty.channel.ChannelHandlerContext; |
| 4 | +import io.netty.handler.codec.http.FullHttpRequest; |
| 5 | +import io.netty.handler.codec.http.QueryStringDecoder; |
| 6 | +import java.lang.reflect.*; |
| 7 | +import org.pytorch.serve.archive.DownloadArchiveException; |
| 8 | +import org.pytorch.serve.archive.model.InvalidKeyException; |
| 9 | +import org.pytorch.serve.archive.model.ModelException; |
| 10 | +import org.pytorch.serve.archive.workflow.WorkflowException; |
| 11 | +import org.pytorch.serve.util.ConfigManager; |
| 12 | +import org.pytorch.serve.util.TokenType; |
| 13 | +import org.pytorch.serve.wlm.WorkerInitializationException; |
| 14 | +import org.slf4j.Logger; |
| 15 | +import org.slf4j.LoggerFactory; |
| 16 | + |
| 17 | +/** |
| 18 | + * A class handling token check for all inbound HTTP requests |
| 19 | + * |
| 20 | + * <p>This class // |
| 21 | + */ |
| 22 | +public class TokenAuthorizationHandler extends HttpRequestHandlerChain { |
| 23 | + |
| 24 | + private static final Logger logger = LoggerFactory.getLogger(TokenAuthorizationHandler.class); |
| 25 | + private static TokenType tokenType; |
| 26 | + private static Boolean tokenEnabled = false; |
| 27 | + private static Class<?> tokenClass; |
| 28 | + private static Object tokenObject; |
| 29 | + private static Double timeToExpirationMinutes = 60.0; |
| 30 | + |
| 31 | + /** Creates a new {@code InferenceRequestHandler} instance. */ |
| 32 | + public TokenAuthorizationHandler(TokenType type) { |
| 33 | + tokenType = type; |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public void handleRequest( |
| 38 | + ChannelHandlerContext ctx, |
| 39 | + FullHttpRequest req, |
| 40 | + QueryStringDecoder decoder, |
| 41 | + String[] segments) |
| 42 | + throws ModelException, DownloadArchiveException, WorkflowException, |
| 43 | + WorkerInitializationException { |
| 44 | + if (tokenEnabled) { |
| 45 | + if (tokenType == TokenType.MANAGEMENT) { |
| 46 | + if (req.toString().contains("/token")) { |
| 47 | + checkTokenAuthorization(req, "token"); |
| 48 | + } else { |
| 49 | + checkTokenAuthorization(req, "management"); |
| 50 | + } |
| 51 | + } else if (tokenType == TokenType.INFERENCE) { |
| 52 | + checkTokenAuthorization(req, "inference"); |
| 53 | + } |
| 54 | + } |
| 55 | + chain.handleRequest(ctx, req, decoder, segments); |
| 56 | + } |
| 57 | + |
| 58 | + public static void setupTokenClass() { |
| 59 | + try { |
| 60 | + tokenClass = Class.forName("org.pytorch.serve.plugins.endpoint.Token"); |
| 61 | + tokenObject = tokenClass.getDeclaredConstructor().newInstance(); |
| 62 | + Method method = tokenClass.getMethod("setTime", Double.class); |
| 63 | + Double time = ConfigManager.getInstance().getTimeToExpiration(); |
| 64 | + if (time != 0.0) { |
| 65 | + timeToExpirationMinutes = time; |
| 66 | + } |
| 67 | + method.invoke(tokenObject, timeToExpirationMinutes); |
| 68 | + method = tokenClass.getMethod("generateKeyFile", String.class); |
| 69 | + if ((boolean) method.invoke(tokenObject, "token")) { |
| 70 | + logger.info("TOKEN CLASS IMPORTED SUCCESSFULLY"); |
| 71 | + } |
| 72 | + } catch (NoSuchMethodException |
| 73 | + | IllegalAccessException |
| 74 | + | InstantiationException |
| 75 | + | InvocationTargetException |
| 76 | + | ClassNotFoundException e) { |
| 77 | + e.printStackTrace(); |
| 78 | + logger.error("TOKEN CLASS IMPORTED UNSUCCESSFULLY"); |
| 79 | + throw new IllegalStateException("Unable to import token class", e); |
| 80 | + } |
| 81 | + tokenEnabled = true; |
| 82 | + } |
| 83 | + |
| 84 | + private void checkTokenAuthorization(FullHttpRequest req, String type) throws ModelException { |
| 85 | + |
| 86 | + try { |
| 87 | + Method method = |
| 88 | + tokenClass.getMethod( |
| 89 | + "checkTokenAuthorization", |
| 90 | + io.netty.handler.codec.http.FullHttpRequest.class, |
| 91 | + String.class); |
| 92 | + boolean result = (boolean) (method.invoke(tokenObject, req, type)); |
| 93 | + if (!result) { |
| 94 | + throw new InvalidKeyException( |
| 95 | + "Token Authentication failed. Token either incorrect, expired, or not provided correctly"); |
| 96 | + } |
| 97 | + } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { |
| 98 | + e.printStackTrace(); |
| 99 | + throw new InvalidKeyException( |
| 100 | + "Token Authentication failed. Token either incorrect, expired, or not provided correctly"); |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments