8d56dc961062681981441cdedef24f3bcf83f000
[aaf/authz.git] / auth / auth-locate / src / main / java / org / onap / aaf / auth / locate / api / API_Proxy.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  * ===========================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END====================================================
19  *
20  */
21
22 package org.onap.aaf.auth.locate.api;
23
24 import java.net.ConnectException;
25
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
28
29 import org.eclipse.jetty.http.HttpStatus;
30 import org.onap.aaf.auth.dao.hl.Question;
31 import org.onap.aaf.auth.env.AuthzTrans;
32 import org.onap.aaf.auth.locate.AAF_Locate;
33 import org.onap.aaf.auth.locate.BasicAuthCode;
34 import org.onap.aaf.auth.locate.LocateCode;
35 import org.onap.aaf.auth.locate.facade.LocateFacade;
36 import org.onap.aaf.auth.locate.mapper.Mapper.API;
37 import org.onap.aaf.auth.rserv.HttpMethods;
38 import org.onap.aaf.cadi.CadiException;
39 import org.onap.aaf.cadi.Symm;
40 import org.onap.aaf.cadi.client.Future;
41 import org.onap.aaf.cadi.client.Rcli;
42 import org.onap.aaf.cadi.client.Retryable;
43 import org.onap.aaf.cadi.config.Config;
44 import org.onap.aaf.cadi.oauth.OAuth2Principal;
45 import org.onap.aaf.misc.env.APIException;
46 import org.onap.aaf.misc.env.Env;
47 import org.onap.aaf.misc.env.LogTarget;
48 import org.onap.aaf.misc.env.TimeTaken;
49
50 /**
51  * API Apis.. using Redirect for mechanism
52  *
53  * @author Jonathan
54  *
55  */
56 public class API_Proxy {
57
58     /**
59      * Normal Init level APIs
60      *
61      * @param gwAPI
62      * @param facade
63      * @throws Exception
64      */
65     public static void init(final AAF_Locate gwAPI, LocateFacade facade, final Question question) {
66
67         String aafurl = gwAPI.access.getProperty(Config.AAF_URL,null);
68         if (aafurl!=null) {
69             ////////
70             // Transferring APIs
71             // But DO NOT transfer BasicAuth case... wastes resources.
72             ///////
73             final BasicAuthCode bac = new BasicAuthCode(gwAPI.aafAuthn,facade);
74
75             gwAPI.routeAll(HttpMethods.GET,"/proxy/:path*",API.VOID,new LocateCode(facade,"Proxy GET", true) {
76                 @Override
77                 public void handle(final AuthzTrans trans, final HttpServletRequest req, final HttpServletResponse resp) throws Exception {
78                         populateCredentialTag(trans, req, question);
79                     if ("/proxy/authn/basicAuth".equals(req.getPathInfo()) && !(req.getUserPrincipal() instanceof OAuth2Principal)) {
80                         bac.handle(trans, req, resp);
81                     } else {
82                         TimeTaken tt = trans.start("Forward to AAF Service", Env.REMOTE);
83                         try {
84                             gwAPI.clientAsUser(trans.getUserPrincipal(), new Retryable<Void>() {
85                                 @Override
86                                 public Void code(Rcli<?> client) throws CadiException, ConnectException, APIException {
87                                     Future<Void> ft = client.transfer(req,resp,pathParam(req, ":path"),HttpStatus.OK_200);
88                                     ft.get(10000); // Covers return codes and err messages
89                                     return null;
90                                 }
91                             });
92
93                         } catch (CadiException | APIException e) {
94                             trans.error().log(e);
95                         } finally {
96                             tt.done();
97                         }
98                     }
99                 }
100             });
101
102             gwAPI.routeAll(HttpMethods.POST,"/proxy/:path*",API.VOID,new LocateCode(facade,"Proxy POST", true) {
103                 @Override
104                 public void handle(final AuthzTrans trans, final HttpServletRequest req, final HttpServletResponse resp) throws Exception {
105                     TimeTaken tt = trans.start("Forward to AAF Service", Env.REMOTE);
106                     try {
107                         gwAPI.clientAsUser(trans.getUserPrincipal(), new Retryable<Void>() {
108                             @Override
109                             public Void code(Rcli<?> client) throws CadiException, ConnectException, APIException {
110                                 Future<Void> ft = client.transfer(req,resp,pathParam(req, ":path"),HttpStatus.CREATED_201);
111                                 ft.get(10000); // Covers return codes and err messages
112                                 return null;
113                             }
114                         });
115                     } catch (CadiException | APIException e) {
116                         trans.error().log(e);
117                     } finally {
118                         tt.done();
119                     }
120                 }
121             });
122
123             gwAPI.routeAll(HttpMethods.PUT,"/proxy/:path*",API.VOID,new LocateCode(facade,"Proxy PUT", true) {
124                 @Override
125                 public void handle(final AuthzTrans trans, final HttpServletRequest req, final HttpServletResponse resp) throws Exception {
126                     TimeTaken tt = trans.start("Forward to AAF Service", Env.REMOTE);
127                     try {
128                         gwAPI.clientAsUser(trans.getUserPrincipal(), new Retryable<Void>() {
129                             @Override
130                             public Void code(Rcli<?> client) throws CadiException, ConnectException, APIException {
131                                 Future<Void> ft = client.transfer(req,resp,pathParam(req, ":path"),HttpStatus.OK_200);
132                                 ft.get(10000); // Covers return codes and err messages
133                                 return null;
134                             }
135                         });
136                     } catch (CadiException | APIException e) {
137                         trans.error().log(e);
138                     } finally {
139                         tt.done();
140                     }
141                 }
142             });
143
144             gwAPI.routeAll(HttpMethods.DELETE,"/proxy/:path*",API.VOID,new LocateCode(facade,"Proxy DELETE", true) {
145                 @Override
146                 public void handle(final AuthzTrans trans, final HttpServletRequest req, final HttpServletResponse resp) throws Exception {
147                     TimeTaken tt = trans.start("Forward to AAF Service", Env.REMOTE);
148                     try {
149                         gwAPI.clientAsUser(trans.getUserPrincipal(), new Retryable<Void>() {
150                             @Override
151                             public Void code(Rcli<?> client) throws CadiException, ConnectException, APIException {
152                                 Future<Void> ft = client.transfer(req,resp,pathParam(req, ":path"),HttpStatus.OK_200);
153                                 ft.get(10000); // Covers return codes and err messages
154                                 return null;
155                             }
156                         });
157                     } catch (CadiException | APIException e) {
158                         trans.error().log(e);
159                     } finally {
160                         tt.done();
161                     }
162                 }
163             });
164         }
165     }
166     
167     /**
168          * Populates TAG value for the user from DB
169          * 
170          * @param trans
171          * @param req
172          * @param question
173          */
174         private static void populateCredentialTag(AuthzTrans trans, HttpServletRequest req, Question question) {
175
176                 try {
177                         String authz = req.getHeader("Authorization");
178                         String decoded = Symm.base64noSplit.decode(authz.substring(6));
179                         int colon = decoded.indexOf(':');
180                         // Update transaction object with TAG information from DB
181                         question.doesUserCredMatch(trans, decoded.substring(0, colon), decoded.substring(colon + 1).getBytes());
182                         String tag = trans.getTag();
183                         if (null != tag) {
184                                 req.setAttribute("CRED_TAG", tag);
185                         }
186                 } catch (Exception e) {
187                         LogTarget lt = trans.error();
188                         lt.log("Exception occured while fetching TAG details from DB :" + e.getMessage());
189                 }
190         }
191 }