AT&T 2.0.19 Code drop, stage 3
[aaf/authz.git] / auth / auth-gui / src / main / java / org / onap / aaf / auth / gui / pages / CMArtiChangeForm.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.pages;
23
24 import static org.onap.aaf.misc.xgen.html.HTMLGen.TABLE;
25
26 import java.io.IOException;
27 import java.net.ConnectException;
28
29 import org.onap.aaf.auth.cmd.AAFcli;
30 import org.onap.aaf.auth.env.AuthzTrans;
31 import org.onap.aaf.auth.gui.AAF_GUI;
32 import org.onap.aaf.auth.gui.BreadCrumbs;
33 import org.onap.aaf.auth.gui.NamedCode;
34 import org.onap.aaf.auth.gui.Page;
35 import org.onap.aaf.auth.org.Organization;
36 import org.onap.aaf.auth.org.OrganizationException;
37 import org.onap.aaf.auth.org.OrganizationFactory;
38 import org.onap.aaf.auth.org.Organization.Identity;
39 import org.onap.aaf.cadi.CadiException;
40 import org.onap.aaf.cadi.LocatorException;
41 import org.onap.aaf.cadi.client.Future;
42 import org.onap.aaf.cadi.client.Rcli;
43 import org.onap.aaf.cadi.client.Retryable;
44 import org.onap.aaf.cadi.util.FQI;
45 import org.onap.aaf.misc.env.APIException;
46 import org.onap.aaf.misc.env.Slot;
47 import org.onap.aaf.misc.xgen.Cache;
48 import org.onap.aaf.misc.xgen.DynamicCode;
49 import org.onap.aaf.misc.xgen.Mark;
50 import org.onap.aaf.misc.xgen.html.HTMLGen;
51
52 import certman.v1_0.Artifacts;
53 import certman.v1_0.Artifacts.Artifact;
54
55 public class CMArtiChangeForm extends Page {
56         private static final String COPY_ARTIFACT = "copyArtifact";
57         private static final String DELETE_ARTIFACT = "deleteArtifact";
58         
59         // Package on purpose
60         static final String HREF = "/gui/artichange";
61         static final String NAME = "ArtifactChange";
62         static final String fields[] = {"id","machine","ns","directory","ca","osuser","renewal","notify","cmd","others","types[]","sans"};
63         
64         static final String types[] = {"jks","file","script"};
65         static final String UPDATE = "Update";
66         static final String CREATE = "Create";
67         static final String COPY = "Copy";
68         static final String DELETE = "Delete";
69         
70         public CMArtiChangeForm(final AAF_GUI gui, final Page ... breadcrumbs) throws APIException, IOException {
71                 super(gui.env,NAME,HREF, fields,
72                         new BreadCrumbs(breadcrumbs),
73                         new NamedCode(true,"content") {
74                         private final Slot sID = gui.env.slot(CMArtiChangeForm.NAME+'.'+CMArtiChangeForm.fields[0]);
75                         private final Slot sMach = gui.env.slot(CMArtiChangeForm.NAME+'.'+CMArtiChangeForm.fields[1]);
76                         private final Slot sNS = gui.env.slot(CMArtiChangeForm.NAME+'.'+CMArtiChangeForm.fields[2]);
77                         
78                         @Override
79                         public void code(final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
80                                 Mark js = new Mark();
81                                 Mark fn = new Mark();
82                                 hgen.js(js).function(fn,COPY_ARTIFACT)
83                                         .text("f=document.getElementById('"+fields[9]+"')")
84                                         .text("s=document.getElementById('theButton')")
85                                         .text("cmd=document.getElementById('"+fields[8]+"')")
86                                         .text("ins=document.getElementById('instruct')")
87                                         .text("c=document.getElementById('cbcopy')")
88                                         .text("trd=document.getElementById('trdelete')")
89                                         .li("if (c.checked==true) {" ,
90                                                         "f.style.display=ins.style.display='block'",
91                                                         "trd.style.display='none'",
92                                                         "s.orig=s.value;",
93                                                         "s.value='Copy'",
94                                                         "cmd.setAttribute('value',s.value)",
95                                                   "} else {",
96                                                         "f.style.display=ins.style.display='none';",
97                                                         "trd.style.display='block'",
98                                                         "s.value=s.orig",
99                                                         "cmd.setAttribute('value',s.orig)",
100                                                         "}"
101                                                         )
102                                         .end(fn)
103                                         .function(fn, DELETE_ARTIFACT)
104                                                 .text("d=document.getElementById('cbdelete')")
105                                                 .text("trc=document.getElementById('trcopy')")
106                                                 .text("s=document.getElementById('theButton')")
107                                                 .text("cmd=document.getElementById('"+fields[8]+"')")
108                                                 .li("if (d.checked==true) {",
109                                                           "s.orig=s.value;",
110                                                           "s.value='Delete';",
111                                                           "trc.style.display='none';",
112                                                           "cmd.setAttribute('value',s.value);",
113                                                         "} else {",
114                                                           "s.value=s.orig;",
115                                                           "trc.style.display='block';",
116                                                           "cmd.setAttribute('value',s.orig);",
117                                                         "}"
118                                                         )
119                                         .end(js);
120
121                                 hgen.leaf(HTMLGen.TITLE).text("Certificate Artifact Form").end();
122                                 Mark form = new Mark();
123                                 hgen.incr(form, "form","action="+HREF,"method=post");
124                                 
125                                 cache.dynamic(hgen, new DynamicCode<HTMLGen, AAF_GUI, AuthzTrans>() {
126                                         @Override
127                                         public void code(final AAF_GUI gui, final AuthzTrans trans,     final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
128
129                                                 final String incomingMach = trans.get(sMach,"");
130                                                 String incomingNS = trans.get(sNS,"");
131                                                 String id= trans.get(sID, "");
132                                         final String incomingID = id.indexOf('@')>=0?id:id+'@'+FQI.reverseDomain(incomingNS);
133
134                                                 String submitText=UPDATE;
135                                                 boolean delete=true;
136                                                 try {
137                                                         Artifact arti =gui.cmClientAsUser(trans.getUserPrincipal(), new Retryable<Artifact>() {
138                                                                 @Override
139                                                                 public Artifact code(Rcli<?> client) throws CadiException, ConnectException, APIException {
140                                                                         Future<Artifacts> fa = client.read("/cert/artifacts/"+incomingID+'/'+incomingMach, gui.artifactsDF);
141                                                                         if(fa.get(AAFcli.timeout())) {
142                                                                                 for(Artifact arti : fa.value.getArtifact()) {
143                                                                                         return arti; // just need the first one
144                                                                                 }
145                                                                         }
146                                                                         return null;
147                                                                 }
148                                                         });
149                                                         if(arti==null) {
150                                                                 Organization org = OrganizationFactory.get(trans);
151                                                                 Identity user = org.getIdentity(trans, incomingID);
152                                                                 if(user==null) {
153                                                                         hgen.p("The mechID you typed, \"" + incomingID + "\", is not a valid " + org.getName() + " ID");
154                                                                         return;
155                                                                 }
156                                                                 arti = new Artifact();
157                                                                 arti.setMechid(incomingID);
158                                                                 Identity managedBy = user.responsibleTo();
159                                                                 if(managedBy == null) {
160                                                                         arti.setSponsor("Unknown Sponsor");
161                                                                 } else {
162                                                                         arti.setSponsor(managedBy.fullID());
163                                                                 }
164                                                                 arti.setMachine(incomingMach);
165                                                                 arti.setNs(incomingNS);
166                                                                 arti.setDir("");
167                                                                 arti.setCa("aaf");
168                                                                 arti.setOsUser("");
169                                                                 arti.setRenewDays(30);
170                                                                 arti.setNotification("mailto:"+user.email());
171                                                                 arti.getType().add(types[0]);
172                                                                 arti.getType().add(types[2]);
173                                                                 submitText = CREATE;
174                                                                 delete = false;
175                                                         } else {
176                                                                 if(arti.getNotification()==null) {
177                                                                         Organization org = OrganizationFactory.get(trans);
178                                                                         Identity user = org.getIdentity(trans, incomingID);
179                                                                         arti.setNotification("mailto:"+user.email());
180                                                                 }
181                                                         }
182                                                         // CSO Approval no longer required for SAN use
183 //                                                      final String mechID = arti.getMechid();
184 //                                                      boolean maySans=gui.lur.fish(new Principal() {
185 //                                                              @Override
186 //                                                              public String getName() {
187 //                                                                      return mechID;
188 //                                                              }},getPerm(arti.getCa(),"san"));
189 //                                                      if(!maySans) {
190 //                                                              arti.getSans().clear();
191 //                                                      }
192                                                         Mark table = new Mark(TABLE);
193                                                         hgen.incr(table)
194                                                                 .input(fields[0],"MechID*",true,"value="+arti.getMechid())
195                                                                 .input("sponsor", "Sponsor",false,"value="+arti.getSponsor(),"readonly","style=border:none;background-color:white;")
196                                                                 .input(fields[1],"Machine*",true,"value="+arti.getMachine(),"style=width:130%;");
197 //                                                      if(maySans) {
198                                                                 hgen.incr(HTMLGen.TR).incr(HTMLGen.TD).end()
199                                                                         .incr(HTMLGen.TD,"class=subtext").text("Use full machine names, ");
200                                                                         if(!trans.fish(getPerm(arti.getCa(),"ip"))) {
201                                                                                 hgen.text("NO ");
202                                                                         }
203                                                                 StringBuilder sb = null;
204                                                                 for(String s: arti.getSans()) {
205                                                                         if(sb==null) {
206                                                                                 sb = new StringBuilder();
207                                                                         } else {
208                                                                                 sb.append(", ");
209                                                                         }
210                                                                         sb.append(s);
211                                                                 }
212                                                                 
213                                                                 hgen.text("IPs allowed, separated by commas.").end()
214                                                                         .input(fields[11], "SANs", false, "value="+(sb==null?"":sb.toString()),"style=width:180%;");
215 //                                                      }
216                                                         hgen.input(fields[2],"Namespace",true,"value="+arti.getNs(),"style=width:180%;")
217                                                                 .input(fields[3],"Directory", true, "value="+arti.getDir(),"style=width:180%;")
218                                                                 .input(fields[4],"Certificate Authority",true,"value="+arti.getCa(),"style=width:180%;")
219                                                                 .input(fields[5],"O/S User",true,"value="+arti.getOsUser())
220                                                                 .input(fields[6],"Renewal Days before Expiration", true, "value="+arti.getRenewDays(),"style=width:20%;")
221                                                                 .input(fields[7],"Notification",true,"value="+arti.getNotification())
222                                                                 .incr(HTMLGen.TR)
223                                                                 .incr(HTMLGen.TD).leaf("label","for=types","required").text("Artifact Types").end(2)
224                                                                 .incr(HTMLGen.TD);
225                                                         for(int i=0;i<types.length;++i) {
226                                                                 hgen.leaf("input","type=checkbox","name=types."+i,arti.getType().contains(types[i])?"checked":"").text(types[i]).end().br();
227                                                         }
228                                                         
229                                                         Mark tr = new Mark();
230                                                         hgen.incr(tr,HTMLGen.TR).incr(HTMLGen.TD,"id=trcopy")
231                                                                         .leaf("input","id=cbcopy","type=checkbox","onclick="+COPY_ARTIFACT+"()").text("Copy Artifact").end(2)
232                                                                 .incr(HTMLGen.TD,"id=tdcopy","style:display:none;")
233                                                                         .incr("label","id=instruct","style=font-style:italic;font-size:80%;display:none;")
234                                                                                 .text("Add full machine names, separated by commas.").end()
235                                                                         .tagOnly("input","id="+fields[9],"name="+fields[9],"style=display:none;width:150%;").end(2)
236                                                                 .end(tr);
237                                                         hgen.incr(tr,HTMLGen.TR,"id=trdelete").incr(HTMLGen.TD,"id=tddelete")
238                                                                 .leaf("input","id=cbdelete","type=checkbox","onclick="+DELETE_ARTIFACT+"()",delete?"style:display:none;":"").text("Delete Artifact").end(2)
239                                                                 .end(tr);
240                                                         hgen.end(table);
241                                                         
242                                                         hgen.tagOnly("input","id="+fields[8],"name="+fields[8],"value="+submitText,"style=display:none;");
243                                                         hgen.tagOnly("input","id=theButton","type=submit", "orig="+submitText,"value="+submitText);
244                                                         
245                                                 } catch(CadiException | LocatorException | OrganizationException e) {
246                                                         throw new APIException(e);
247                                                 }
248                                         }
249
250                                         });
251                                 hgen.end(form);
252                                 }
253                         });
254                 
255         }
256 }