d55047a3c587546db194e53b231dec4c436c93e6
[aaf/authz.git] / auth / auth-gui / src / main / java / org / onap / aaf / auth / gui / AAF_GUI.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.gui;
23
24 import static org.onap.aaf.auth.rserv.HttpMethods.GET;
25 import static org.onap.aaf.auth.rserv.HttpMethods.POST;
26 import static org.onap.aaf.auth.rserv.HttpMethods.PUT;
27
28 import javax.servlet.Filter;
29 import javax.servlet.http.Cookie;
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpServletResponse;
32
33 import org.onap.aaf.auth.cmd.Cmd;
34 import org.onap.aaf.auth.cui.CUI;
35 import org.onap.aaf.auth.env.AuthzEnv;
36 import org.onap.aaf.auth.env.AuthzTrans;
37 import org.onap.aaf.auth.env.AuthzTransFilter;
38 import org.onap.aaf.auth.gui.pages.ApiDocs;
39 import org.onap.aaf.auth.gui.pages.ApiExample;
40 import org.onap.aaf.auth.gui.pages.ApprovalAction;
41 import org.onap.aaf.auth.gui.pages.ApprovalForm;
42 import org.onap.aaf.auth.gui.pages.CMArtiChangeAction;
43 import org.onap.aaf.auth.gui.pages.CMArtiChangeForm;
44 import org.onap.aaf.auth.gui.pages.CMArtifactShow;
45 import org.onap.aaf.auth.gui.pages.CredDetail;
46 import org.onap.aaf.auth.gui.pages.CredHistory;
47 import org.onap.aaf.auth.gui.pages.Home;
48 import org.onap.aaf.auth.gui.pages.LoginLanding;
49 import org.onap.aaf.auth.gui.pages.LoginLandingAction;
50 import org.onap.aaf.auth.gui.pages.NsDetail;
51 import org.onap.aaf.auth.gui.pages.NsHistory;
52 import org.onap.aaf.auth.gui.pages.NsInfoAction;
53 import org.onap.aaf.auth.gui.pages.NsInfoForm;
54 import org.onap.aaf.auth.gui.pages.NssShow;
55 import org.onap.aaf.auth.gui.pages.PassChangeAction;
56 import org.onap.aaf.auth.gui.pages.PassChangeForm;
57 import org.onap.aaf.auth.gui.pages.PassDeleteAction;
58 import org.onap.aaf.auth.gui.pages.PendingRequestsShow;
59 import org.onap.aaf.auth.gui.pages.PermDetail;
60 import org.onap.aaf.auth.gui.pages.PermGrantAction;
61 import org.onap.aaf.auth.gui.pages.PermGrantForm;
62 import org.onap.aaf.auth.gui.pages.PermHistory;
63 import org.onap.aaf.auth.gui.pages.PermsShow;
64 import org.onap.aaf.auth.gui.pages.RequestDetail;
65 import org.onap.aaf.auth.gui.pages.RoleDetail;
66 import org.onap.aaf.auth.gui.pages.RoleDetailAction;
67 import org.onap.aaf.auth.gui.pages.RoleHistory;
68 import org.onap.aaf.auth.gui.pages.RolesShow;
69 import org.onap.aaf.auth.gui.pages.UserRoleExtend;
70 import org.onap.aaf.auth.gui.pages.UserRoleRemove;
71 import org.onap.aaf.auth.gui.pages.WebCommand;
72 import org.onap.aaf.auth.rserv.CachingFileAccess;
73 import org.onap.aaf.auth.rserv.HttpCode;
74 import org.onap.aaf.auth.server.AbsService;
75 import org.onap.aaf.auth.server.JettyServiceStarter;
76 import org.onap.aaf.auth.server.Log4JLogIt;
77 import org.onap.aaf.cadi.CadiException;
78 import org.onap.aaf.cadi.LocatorException;
79 import org.onap.aaf.cadi.PropAccess;
80 import org.onap.aaf.cadi.aaf.v2_0.AAFConHttp;
81 import org.onap.aaf.cadi.aaf.v2_0.AAFLurPerm;
82 import org.onap.aaf.cadi.aaf.v2_0.AAFTrustChecker;
83 import org.onap.aaf.cadi.client.Future;
84 import org.onap.aaf.cadi.client.Retryable;
85 import org.onap.aaf.cadi.config.Config;
86 import org.onap.aaf.cadi.http.HTransferSS;
87 import org.onap.aaf.cadi.principal.TaggedPrincipal;
88 import org.onap.aaf.cadi.register.Registrant;
89 import org.onap.aaf.cadi.register.RemoteRegistrant;
90 import org.onap.aaf.misc.env.APIException;
91 import org.onap.aaf.misc.env.Env;
92 import org.onap.aaf.misc.env.Slot;
93 import org.onap.aaf.misc.env.StaticSlot;
94 import org.onap.aaf.misc.rosetta.env.RosettaDF;
95 import org.onap.aaf.misc.xgen.html.HTMLGen;
96 import org.onap.aaf.misc.xgen.html.State;
97
98 import certman.v1_0.Artifacts;
99 import certman.v1_0.CertInfo;
100
101 public class AAF_GUI extends AbsService<AuthzEnv, AuthzTrans> implements State<Env>{
102     public static final String AAF_GUI_THEME = "aaf_gui_theme";
103     public static final String AAF_GUI_COPYRIGHT = "aaf_gui_copyright";
104     public static final String HTTP_SERVLET_REQUEST = "HTTP_SERVLET_REQUEST";
105     public static final int TIMEOUT = 60000;
106     public static final String APP = "AAF GUI";
107
108     // AAF API
109
110     // Certificate manager API
111     public RosettaDF<Artifacts> artifactsDF;
112     public RosettaDF<CertInfo>  certInfoDF;
113
114     private final AAFConHttp cmCon;
115     public final AAFConHttp aafCon;
116     public final AAFLurPerm lur;
117
118     public final Slot slotHttpServletRequest;
119     protected final String deployedVersion;
120     private StaticSlot sThemeWebPath;
121     private StaticSlot sDefaultTheme;
122
123
124     public AAF_GUI(final AuthzEnv env) throws Exception {
125         super(env.access(), env);
126         sDefaultTheme = env.staticSlot(AAF_GUI_THEME);
127         String defTheme = env.getProperty(AAF_GUI_THEME,"onap");
128         env.put(sDefaultTheme, defTheme);
129
130         sThemeWebPath = env.staticSlot(CachingFileAccess.CFA_WEB_PATH);
131         if(env.get(sThemeWebPath)==null) {
132             env.put(sThemeWebPath,"theme");
133         }
134
135         slotHttpServletRequest = env.slot(HTTP_SERVLET_REQUEST);
136         deployedVersion = app_version;
137
138         // Certificate Manager
139         String aafUrlCm = env.getProperty(Config.AAF_URL_CM,Config.AAF_URL_CM_DEF);
140         cmCon =  new AAFConHttp(env.access(),aafUrlCm);
141         artifactsDF = env.newDataFactory(Artifacts.class);
142         certInfoDF  = env.newDataFactory(CertInfo.class);
143
144
145         /////////////////////////
146         // Screens
147         /////////////////////////
148         // Start Screen
149         final Page start = new Display(this, GET, new Home(this)).page();
150
151         // MyPerms Screens
152         final Page myPerms = new Display(this, GET, new PermsShow(this, start)).page();
153         Page permDetail = new Display(this, GET, new PermDetail(this, start, myPerms)).page();
154                             new Display(this, GET, new PermHistory(this,start,myPerms,permDetail));
155
156         // MyRoles Screens
157         final Page myRoles = new Display(this, GET, new RolesShow(this, start)).page();
158         Page roleDetail = new Display(this, GET, new RoleDetail(this, start, myRoles)).page();
159                           new Display(this, POST, new RoleDetailAction(this,start,myRoles,roleDetail));
160                           new Display(this, GET, new RoleHistory(this,start,myRoles,roleDetail));
161
162         // MyNameSpace
163         final Page myNamespaces = new Display(this, GET, new NssShow(this, start)).page();
164         Page nsDetail  = new Display(this, GET, new NsDetail(this, start, myNamespaces)).page();
165                          new Display(this, GET, new NsHistory(this, start,myNamespaces,nsDetail));
166         Page crdDetail = new Display(this, GET, new CredDetail(this, start, myNamespaces, nsDetail)).page();
167                          new Display(this, GET, new CredHistory(this,start,myNamespaces,nsDetail,crdDetail));
168         Page artiShow  = new Display(this, GET, new CMArtifactShow(this, start, myNamespaces, nsDetail, crdDetail)).page();
169         Page artiCForm = new Display(this, GET, new CMArtiChangeForm(this, start, myNamespaces, nsDetail, crdDetail,artiShow)).page();
170                          new Display(this, POST, new CMArtiChangeAction(this, start,artiShow,artiCForm));
171
172         // Password Change Screens
173         final Page pwc = new Display(this, GET, new PassChangeForm(this, start,crdDetail)).page();
174                          new Display(this, POST, new PassChangeAction(this, start, pwc));
175
176         // Password Delete Screen
177                          new Display(this, GET, new PassDeleteAction(this, start,crdDetail));
178
179         // Validation Change Screens
180         final Page validate = new Display(this, GET, new ApprovalForm(this, start)).page();
181                               new Display(this, POST, new ApprovalAction(this, start, validate));
182
183         // Onboard, Detailed Edit Screens
184         final Page onb = new Display(this, GET, new NsInfoForm(this, start)).page();
185                          new Display(this, POST, new NsInfoAction(this, start, onb));
186
187         // Web Command Screens
188         /* final Page webCommand =*/ new Display(this, GET, new WebCommand(this, start)).page();
189
190         // API Docs
191         final Page apidocs = new Display(this, GET, new ApiDocs(this, start)).page();
192                              new Display(this, GET, new ApiExample(this,start, apidocs)).page();
193
194         // Permission Grant Page
195         final Page permGrant =     new Display(this, GET, new PermGrantForm(this, start)).page();
196                                  new Display(this, POST, new PermGrantAction(this, start, permGrant)).page();
197
198         // Login Landing if no credentials detected
199         final Page loginLanding = new Display(this, GET, new LoginLanding(this, start)).page();
200                                   new Display(this, POST, new LoginLandingAction(this, start, loginLanding));
201
202         // User Role Request Extend and Remove
203         new Display(this, GET, new UserRoleExtend(this, start,myRoles)).page();
204         new Display(this, GET, new UserRoleRemove(this, start,myRoles)).page();
205
206         // See my Pending Requests
207         final Page requestsShow = new Display(this, GET, new PendingRequestsShow(this, start)).page();
208                                   new Display(this, GET, new RequestDetail(this, start, requestsShow));
209
210         // Command line Mechanism
211         route(env, PUT, "/gui/cui", new CUI(this),"text/plain;charset=utf-8","*/*");
212
213         route(env, GET, "/gui/clear", new HttpCode<AuthzTrans, Void>(null, "Clear"){
214             @Override
215             public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
216                 trans.clearCache();
217                 Cookie cookies[] = req.getCookies();
218                 if(cookies!=null) {
219                     for(Cookie c : cookies) {
220                         if(c.getName().startsWith("aaf.gui.")) {
221                             c.setMaxAge(0);
222                             resp.addCookie(c);
223                         }
224                     }
225                 }
226                 resp.sendRedirect("/gui/home");
227             }
228         }, "text/plain;charset=utf-8","*/*");
229
230         ///////////////////////
231         // WebContent Handler
232         ///////////////////////
233         CachingFileAccess<AuthzTrans> cfa = new CachingFileAccess<AuthzTrans>(env);
234         route(env,GET,"/theme/:key*", cfa);
235         ///////////////////////
236         aafCon = aafCon();
237         lur = aafCon.newLur();
238     }
239
240     public<T> RosettaDF<T> getDF(Class<T> cls) throws APIException {
241         return Cmd.getDF(env,cls);
242     }
243
244     public void writeError(AuthzTrans trans, Future<?> fp, HTMLGen hgen, int indent) {
245         if (hgen!=null) {
246             String msg = aafCon.readableErrMsg(fp);
247             hgen.incr(HTMLGen.P,"style=text-indent:"+indent*10+"px")
248                 .text("<font color=\"red\"><i>Error</i>:</font> ")
249                 .text(msg)
250                 .end();
251             trans.checkpoint(msg);
252         }
253     }
254
255     public<RET> RET cmClientAsUser(TaggedPrincipal p,Retryable<RET> retryable) throws APIException, LocatorException, CadiException  {
256             return cmCon.hman().best(new HTransferSS(p, APP, aafCon.securityInfo()), retryable);
257     }
258
259     @Override
260     public Filter[] _filters(Object ... additionalTafLurs) throws CadiException, LocatorException {
261         try {
262             return new Filter[] {
263                     new XFrameFilter(XFrameFilter.TYPE.none),
264                     new AuthzTransFilter(env,aafCon(),
265                             new AAFTrustChecker((Env)env),
266                             additionalTafLurs),
267                     new OrgLookupFilter()
268                 };
269         } catch (NumberFormatException e) {
270             throw new CadiException("Invalid Property information", e);
271         }
272     }
273
274     @SuppressWarnings("unchecked")
275     @Override
276     public Registrant<AuthzEnv>[] registrants(final int port) throws CadiException, LocatorException {
277         return new Registrant[] {
278             new RemoteRegistrant<AuthzEnv>(aafCon(),port)
279         };
280     }
281
282     public static void main(final String[] args) {
283         try {
284             Log4JLogIt logIt = new Log4JLogIt(args, "gui");
285             PropAccess propAccess = new PropAccess(logIt,args);
286
287             try {
288                 new JettyServiceStarter<AuthzEnv,AuthzTrans>(
289                     new AAF_GUI(new AuthzEnv(propAccess)),true)
290                         .start();
291             } catch (Exception e) {
292                 propAccess.log(e);
293             }
294         } catch (Exception e) {
295             e.printStackTrace();
296         }
297     }
298 }