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