2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
22 package org.onap.aaf.auth.gui.pages;
24 import static org.onap.aaf.misc.xgen.html.HTMLGen.TABLE;
26 import java.io.IOException;
27 import java.net.ConnectException;
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;
52 import certman.v1_0.Artifacts;
53 import certman.v1_0.Artifacts.Artifact;
55 public class CMArtiChangeForm extends Page {
56 private static final String COPY_ARTIFACT = "copyArtifact";
57 private static final String DELETE_ARTIFACT = "deleteArtifact";
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"};
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";
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]);
79 public void code(final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
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'",
94 "cmd.setAttribute('value',s.value)",
96 "f.style.display=ins.style.display='none';",
97 "trd.style.display='block'",
99 "cmd.setAttribute('value',s.orig)",
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) {",
111 "trc.style.display='none';",
112 "cmd.setAttribute('value',s.value);",
115 "trc.style.display='block';",
116 "cmd.setAttribute('value',s.orig);",
121 hgen.leaf(HTMLGen.TITLE).text("Certificate Artifact Form").end();
122 Mark form = new Mark();
123 hgen.incr(form, "form","action="+HREF,"method=post");
125 cache.dynamic(hgen, new DynamicCode<HTMLGen, AAF_GUI, AuthzTrans>() {
127 public void code(final AAF_GUI gui, final AuthzTrans trans, final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
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);
134 String submitText=UPDATE;
137 Artifact arti =gui.cmClientAsUser(trans.getUserPrincipal(), new Retryable<Artifact>() {
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
150 Organization org = OrganizationFactory.get(trans);
151 Identity user = org.getIdentity(trans, incomingID);
153 hgen.p("The mechID you typed, \"" + incomingID + "\", is not a valid " + org.getName() + " ID");
156 arti = new Artifact();
157 arti.setMechid(incomingID);
158 Identity managedBy = user.responsibleTo();
159 if (managedBy == null) {
160 arti.setSponsor("Unknown Sponsor");
162 arti.setSponsor(managedBy.fullID());
164 arti.setMachine(incomingMach);
165 arti.setNs(incomingNS);
169 arti.setRenewDays(30);
170 arti.setNotification("mailto:"+user.email());
171 arti.getType().add(types[0]);
172 arti.getType().add(types[3]);
176 if (arti.getNotification()==null) {
177 Organization org = OrganizationFactory.get(trans);
178 Identity user = org.getIdentity(trans, incomingID);
179 arti.setNotification("mailto:"+user.email());
182 Mark table = new Mark(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%;");
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"))) {
193 StringBuilder sb = null;
194 for (String s: arti.getSans()) {
196 sb = new StringBuilder();
203 hgen.text("IPs allowed, separated by commas.").end()
204 .input(fields[11], "SANs", false, "value="+(sb==null?"":sb.toString()),"style=width:130%;");
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())
213 .incr(HTMLGen.TD).leaf("label","for=types","required").text("Artifact Types").end(2)
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();
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)
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)
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);
235 } catch (CadiException | LocatorException | OrganizationException e) {
236 throw new APIException(e);