ee55fdbcb72f90b2cac8d805661e098fa581bf60
[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[] = {"pkcs12","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[3]);
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                             Mark table = new Mark(TABLE);
183                             hgen.incr(table)
184                                 .input(fields[0],"AppID*",true,"value="+arti.getMechid())
185                                 .input("sponsor", "Sponsor",false,"value="+arti.getSponsor(),"readonly","style=border:none;background-color:white;")
186                                 .input(fields[1],"FQDN*",true,"value="+arti.getMachine(),"style=width:130%;");
187 //                            if (maySans) {
188                                 hgen.incr(HTMLGen.TR).incr(HTMLGen.TD).end()
189                                     .incr(HTMLGen.TD,"class=subtext").text("Use Fully Qualified Domain Names (that will be in DNS), ");
190                                     if (!trans.fish(getPerm(arti.getCa(),"ip"))) {
191                                         hgen.text("NO ");
192                                     }
193                                 StringBuilder sb = null;
194                                 for (String s: arti.getSans()) {
195                                     if (sb==null) {
196                                         sb = new StringBuilder();
197                                     } else {
198                                         sb.append(", ");
199                                     }
200                                     sb.append(s);
201                                 }
202                                 
203                                 hgen.text("IPs allowed, separated by commas.").end()
204                                     .input(fields[11], "SANs", false, "value="+(sb==null?"":sb.toString()),"style=width:130%;");
205 //                            }
206                             hgen.input(fields[2],"Namespace",true,"value="+arti.getNs(),"style=width:130%;")
207                                 .input(fields[3],"Directory", true, "value="+arti.getDir(),"style=width:130%;")
208                                 .input(fields[4],"Certificate Authority",true,"value="+arti.getCa(),"style=width:130%;")
209                                 .input(fields[5],"O/S User",true,"value="+arti.getOsUser())
210                                 .input(fields[6],"Renewal Days before Expiration", true, "value="+arti.getRenewDays(),"style=width:20%;")
211                                 .input(fields[7],"Notification",true,"value="+arti.getNotification())
212                                 .incr(HTMLGen.TR)
213                                 .incr(HTMLGen.TD).leaf("label","for=types","required").text("Artifact Types").end(2)
214                                 .incr(HTMLGen.TD);
215                             for (int i=0;i<types.length;++i) {
216                                 hgen.leaf("input","type=checkbox","name=types."+i,arti.getType().contains(types[i])?"checked":"").text(types[i]).end().br();
217                             }
218                             
219                             Mark tr = new Mark();
220                             hgen.incr(tr,HTMLGen.TR).incr(HTMLGen.TD,"id=trcopy")
221                                     .leaf("input","id=cbcopy","type=checkbox","onclick="+COPY_ARTIFACT+"()").text("Copy Artifact").end(2)
222                                 .incr(HTMLGen.TD,"id=tdcopy","style:display:none;")
223                                     .incr("label","id=instruct","style=font-style:italic;font-size:80%;display:none;")
224                                         .text("Add full machine names, separated by commas.").end()
225                                     .tagOnly("input","id="+fields[9],"name="+fields[9],"style=display:none;width:150%;").end(2)
226                                 .end(tr);
227                             hgen.incr(tr,HTMLGen.TR,"id=trdelete").incr(HTMLGen.TD,"id=tddelete")
228                                 .leaf("input","id=cbdelete","type=checkbox","onclick="+DELETE_ARTIFACT+"()",delete?"style:display:none;":"").text("Delete Artifact").end(2)
229                                 .end(tr);
230                             hgen.end(table);
231                             
232                             hgen.tagOnly("input","id="+fields[8],"name="+fields[8],"value="+submitText,"style=display:none;");
233                             hgen.tagOnly("input","id=theButton","type=submit", "orig="+submitText,"value="+submitText);
234                             
235                         } catch (CadiException | LocatorException | OrganizationException e) {
236                             throw new APIException(e);
237                         }
238                     }
239
240                     });
241                 hgen.end(form);
242                 }
243             });
244         
245     }
246 }