X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=auth%2Fauth-locate%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Faaf%2Fauth%2Flocate%2Fapi%2FAPI_Proxy.java;h=8d56dc961062681981441cdedef24f3bcf83f000;hb=a06268eb4fec6c245815f33335f807474e024ab7;hp=d2e4583cca23fd0442bfe0e5c5b78a11cef422a1;hpb=ceda6e8bc270202bcb24340b86617110289c902e;p=aaf%2Fauthz.git diff --git a/auth/auth-locate/src/main/java/org/onap/aaf/auth/locate/api/API_Proxy.java b/auth/auth-locate/src/main/java/org/onap/aaf/auth/locate/api/API_Proxy.java index d2e4583c..8d56dc96 100644 --- a/auth/auth-locate/src/main/java/org/onap/aaf/auth/locate/api/API_Proxy.java +++ b/auth/auth-locate/src/main/java/org/onap/aaf/auth/locate/api/API_Proxy.java @@ -7,9 +7,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -27,6 +27,7 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.eclipse.jetty.http.HttpStatus; +import org.onap.aaf.auth.dao.hl.Question; import org.onap.aaf.auth.env.AuthzTrans; import org.onap.aaf.auth.locate.AAF_Locate; import org.onap.aaf.auth.locate.BasicAuthCode; @@ -35,6 +36,7 @@ import org.onap.aaf.auth.locate.facade.LocateFacade; import org.onap.aaf.auth.locate.mapper.Mapper.API; import org.onap.aaf.auth.rserv.HttpMethods; import org.onap.aaf.cadi.CadiException; +import org.onap.aaf.cadi.Symm; import org.onap.aaf.cadi.client.Future; import org.onap.aaf.cadi.client.Rcli; import org.onap.aaf.cadi.client.Retryable; @@ -42,122 +44,148 @@ import org.onap.aaf.cadi.config.Config; import org.onap.aaf.cadi.oauth.OAuth2Principal; import org.onap.aaf.misc.env.APIException; import org.onap.aaf.misc.env.Env; +import org.onap.aaf.misc.env.LogTarget; import org.onap.aaf.misc.env.TimeTaken; /** * API Apis.. using Redirect for mechanism - * + * * @author Jonathan * */ public class API_Proxy { - /** - * Normal Init level APIs + /** + * Normal Init level APIs + * + * @param gwAPI + * @param facade + * @throws Exception + */ + public static void init(final AAF_Locate gwAPI, LocateFacade facade, final Question question) { + + String aafurl = gwAPI.access.getProperty(Config.AAF_URL,null); + if (aafurl!=null) { + //////// + // Transferring APIs + // But DO NOT transfer BasicAuth case... wastes resources. + /////// + final BasicAuthCode bac = new BasicAuthCode(gwAPI.aafAuthn,facade); + + gwAPI.routeAll(HttpMethods.GET,"/proxy/:path*",API.VOID,new LocateCode(facade,"Proxy GET", true) { + @Override + public void handle(final AuthzTrans trans, final HttpServletRequest req, final HttpServletResponse resp) throws Exception { + populateCredentialTag(trans, req, question); + if ("/proxy/authn/basicAuth".equals(req.getPathInfo()) && !(req.getUserPrincipal() instanceof OAuth2Principal)) { + bac.handle(trans, req, resp); + } else { + TimeTaken tt = trans.start("Forward to AAF Service", Env.REMOTE); + try { + gwAPI.clientAsUser(trans.getUserPrincipal(), new Retryable() { + @Override + public Void code(Rcli client) throws CadiException, ConnectException, APIException { + Future ft = client.transfer(req,resp,pathParam(req, ":path"),HttpStatus.OK_200); + ft.get(10000); // Covers return codes and err messages + return null; + } + }); + + } catch (CadiException | APIException e) { + trans.error().log(e); + } finally { + tt.done(); + } + } + } + }); + + gwAPI.routeAll(HttpMethods.POST,"/proxy/:path*",API.VOID,new LocateCode(facade,"Proxy POST", true) { + @Override + public void handle(final AuthzTrans trans, final HttpServletRequest req, final HttpServletResponse resp) throws Exception { + TimeTaken tt = trans.start("Forward to AAF Service", Env.REMOTE); + try { + gwAPI.clientAsUser(trans.getUserPrincipal(), new Retryable() { + @Override + public Void code(Rcli client) throws CadiException, ConnectException, APIException { + Future ft = client.transfer(req,resp,pathParam(req, ":path"),HttpStatus.CREATED_201); + ft.get(10000); // Covers return codes and err messages + return null; + } + }); + } catch (CadiException | APIException e) { + trans.error().log(e); + } finally { + tt.done(); + } + } + }); + + gwAPI.routeAll(HttpMethods.PUT,"/proxy/:path*",API.VOID,new LocateCode(facade,"Proxy PUT", true) { + @Override + public void handle(final AuthzTrans trans, final HttpServletRequest req, final HttpServletResponse resp) throws Exception { + TimeTaken tt = trans.start("Forward to AAF Service", Env.REMOTE); + try { + gwAPI.clientAsUser(trans.getUserPrincipal(), new Retryable() { + @Override + public Void code(Rcli client) throws CadiException, ConnectException, APIException { + Future ft = client.transfer(req,resp,pathParam(req, ":path"),HttpStatus.OK_200); + ft.get(10000); // Covers return codes and err messages + return null; + } + }); + } catch (CadiException | APIException e) { + trans.error().log(e); + } finally { + tt.done(); + } + } + }); + + gwAPI.routeAll(HttpMethods.DELETE,"/proxy/:path*",API.VOID,new LocateCode(facade,"Proxy DELETE", true) { + @Override + public void handle(final AuthzTrans trans, final HttpServletRequest req, final HttpServletResponse resp) throws Exception { + TimeTaken tt = trans.start("Forward to AAF Service", Env.REMOTE); + try { + gwAPI.clientAsUser(trans.getUserPrincipal(), new Retryable() { + @Override + public Void code(Rcli client) throws CadiException, ConnectException, APIException { + Future ft = client.transfer(req,resp,pathParam(req, ":path"),HttpStatus.OK_200); + ft.get(10000); // Covers return codes and err messages + return null; + } + }); + } catch (CadiException | APIException e) { + trans.error().log(e); + } finally { + tt.done(); + } + } + }); + } + } + + /** + * Populates TAG value for the user from DB * - * @param gwAPI - * @param facade - * @throws Exception + * @param trans + * @param req + * @param question */ - public static void init(final AAF_Locate gwAPI, LocateFacade facade) throws Exception { - - String aafurl = gwAPI.access.getProperty(Config.AAF_URL,null); - if(aafurl==null) { - } else { - //////// - // Transferring APIs - // But DO NOT transfer BasicAuth case... wastes resources. - /////// - final BasicAuthCode bac = new BasicAuthCode(gwAPI.aafAuthn,facade); - - gwAPI.routeAll(HttpMethods.GET,"/proxy/:path*",API.VOID,new LocateCode(facade,"Proxy GET", true) { - @Override - public void handle(final AuthzTrans trans, final HttpServletRequest req, final HttpServletResponse resp) throws Exception { - if("/proxy/authn/basicAuth".equals(req.getPathInfo()) && !(req.getUserPrincipal() instanceof OAuth2Principal)) { - bac.handle(trans, req, resp); - } else { - TimeTaken tt = trans.start("Forward to AAF Service", Env.REMOTE); - try { - gwAPI.clientAsUser(trans.getUserPrincipal(), new Retryable() { - @Override - public Void code(Rcli client) throws CadiException, ConnectException, APIException { - Future ft = client.transfer(req,resp,pathParam(req, ":path"),HttpStatus.OK_200); - ft.get(10000); // Covers return codes and err messages - return null; - } - }); - - } catch (CadiException | APIException e) { - trans.error().log(e); - } finally { - tt.done(); - } - } - } - }); - - gwAPI.routeAll(HttpMethods.POST,"/proxy/:path*",API.VOID,new LocateCode(facade,"Proxy POST", true) { - @Override - public void handle(final AuthzTrans trans, final HttpServletRequest req, final HttpServletResponse resp) throws Exception { - TimeTaken tt = trans.start("Forward to AAF Service", Env.REMOTE); - try { - gwAPI.clientAsUser(trans.getUserPrincipal(), new Retryable() { - @Override - public Void code(Rcli client) throws CadiException, ConnectException, APIException { - Future ft = client.transfer(req,resp,pathParam(req, ":path"),HttpStatus.CREATED_201); - ft.get(10000); // Covers return codes and err messages - return null; - } - }); - } catch (CadiException | APIException e) { - trans.error().log(e); - } finally { - tt.done(); - } - } - }); - - gwAPI.routeAll(HttpMethods.PUT,"/proxy/:path*",API.VOID,new LocateCode(facade,"Proxy PUT", true) { - @Override - public void handle(final AuthzTrans trans, final HttpServletRequest req, final HttpServletResponse resp) throws Exception { - TimeTaken tt = trans.start("Forward to AAF Service", Env.REMOTE); - try { - gwAPI.clientAsUser(trans.getUserPrincipal(), new Retryable() { - @Override - public Void code(Rcli client) throws CadiException, ConnectException, APIException { - Future ft = client.transfer(req,resp,pathParam(req, ":path"),HttpStatus.OK_200); - ft.get(10000); // Covers return codes and err messages - return null; - } - }); - } catch (CadiException | APIException e) { - trans.error().log(e); - } finally { - tt.done(); - } - } - }); - - gwAPI.routeAll(HttpMethods.DELETE,"/proxy/:path*",API.VOID,new LocateCode(facade,"Proxy DELETE", true) { - @Override - public void handle(final AuthzTrans trans, final HttpServletRequest req, final HttpServletResponse resp) throws Exception { - TimeTaken tt = trans.start("Forward to AAF Service", Env.REMOTE); - try { - gwAPI.clientAsUser(trans.getUserPrincipal(), new Retryable() { - @Override - public Void code(Rcli client) throws CadiException, ConnectException, APIException { - Future ft = client.transfer(req,resp,pathParam(req, ":path"),HttpStatus.OK_200); - ft.get(10000); // Covers return codes and err messages - return null; - } - }); - } catch (CadiException | APIException e) { - trans.error().log(e); - } finally { - tt.done(); - } - } - }); + private static void populateCredentialTag(AuthzTrans trans, HttpServletRequest req, Question question) { + + try { + String authz = req.getHeader("Authorization"); + String decoded = Symm.base64noSplit.decode(authz.substring(6)); + int colon = decoded.indexOf(':'); + // Update transaction object with TAG information from DB + question.doesUserCredMatch(trans, decoded.substring(0, colon), decoded.substring(colon + 1).getBytes()); + String tag = trans.getTag(); + if (null != tag) { + req.setAttribute("CRED_TAG", tag); + } + } catch (Exception e) { + LogTarget lt = trans.error(); + lt.log("Exception occured while fetching TAG details from DB :" + e.getMessage()); } } }