Fixes/Refinements from Testing
[aaf/authz.git] / auth / auth-gui / src / main / java / org / onap / aaf / auth / gui / pages / CMArtiChangeAction.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 java.io.IOException;
25 import java.net.ConnectException;
26
27 import org.onap.aaf.auth.cmd.AAFcli;
28 import org.onap.aaf.auth.env.AuthzTrans;
29 import org.onap.aaf.auth.gui.AAF_GUI;
30 import org.onap.aaf.auth.gui.BreadCrumbs;
31 import org.onap.aaf.auth.gui.NamedCode;
32 import org.onap.aaf.auth.gui.Page;
33 import org.onap.aaf.cadi.CadiException;
34 import org.onap.aaf.cadi.client.Future;
35 import org.onap.aaf.cadi.client.Holder;
36 import org.onap.aaf.cadi.client.Rcli;
37 import org.onap.aaf.cadi.client.Retryable;
38 import org.onap.aaf.cadi.util.Vars;
39 import org.onap.aaf.misc.env.APIException;
40 import org.onap.aaf.misc.env.Slot;
41 import org.onap.aaf.misc.env.Data.TYPE;
42 import org.onap.aaf.misc.env.util.IPValidator;
43 import org.onap.aaf.misc.env.util.Split;
44 import org.onap.aaf.misc.xgen.Cache;
45 import org.onap.aaf.misc.xgen.DynamicCode;
46 import org.onap.aaf.misc.xgen.html.HTMLGen;
47
48 import aaf.v2_0.Error;
49 import certman.v1_0.Artifacts;
50 import certman.v1_0.Artifacts.Artifact;
51
52 public class CMArtiChangeAction extends Page {
53     public CMArtiChangeAction(final AAF_GUI gui, final Page ... breadcrumbs) throws APIException, IOException {
54         super(gui.env,CMArtiChangeForm.NAME,CMArtiChangeForm.HREF, CMArtiChangeForm.fields,
55             new BreadCrumbs(breadcrumbs),
56             new NamedCode(true,"content") {
57                 final Slot sID = gui.env.slot(CMArtiChangeForm.NAME+'.'+CMArtiChangeForm.fields[0]);
58                 final Slot sMachine = gui.env.slot(CMArtiChangeForm.NAME+'.'+CMArtiChangeForm.fields[1]);
59                 final Slot sNS = gui.env.slot(CMArtiChangeForm.NAME+'.'+CMArtiChangeForm.fields[2]);
60                 final Slot sDirectory = gui.env.slot(CMArtiChangeForm.NAME+'.'+CMArtiChangeForm.fields[3]);
61                 final Slot sCA = gui.env.slot(CMArtiChangeForm.NAME+'.'+CMArtiChangeForm.fields[4]);
62                 final Slot sOSUser = gui.env.slot(CMArtiChangeForm.NAME+'.'+CMArtiChangeForm.fields[5]);
63                 final Slot sRenewal = gui.env.slot(CMArtiChangeForm.NAME+'.'+CMArtiChangeForm.fields[6]);
64                 final Slot sNotify = gui.env.slot(CMArtiChangeForm.NAME+'.'+CMArtiChangeForm.fields[7]);
65                 final Slot sCmd = gui.env.slot(CMArtiChangeForm.NAME+'.'+CMArtiChangeForm.fields[8]);
66                 final Slot sOther = gui.env.slot(CMArtiChangeForm.NAME+'.'+CMArtiChangeForm.fields[9]);
67                 final Slot sType = gui.env.slot(CMArtiChangeForm.NAME+'.'+CMArtiChangeForm.fields[10]);
68                 final Slot sSans = gui.env.slot(CMArtiChangeForm.NAME+'.'+CMArtiChangeForm.fields[11]);
69                 
70                 @Override
71                 public void code(final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
72                     cache.dynamic(hgen, new DynamicCode<HTMLGen,AAF_GUI, AuthzTrans>() {
73                         @Override
74                         public void code(final AAF_GUI gui, final AuthzTrans trans,final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
75 trans.info().log("Step 1");
76                             final Artifact arti = new Artifact();
77                             final String machine = trans.get(sMachine,null);
78                             final String ca = trans.get(sCA, null);
79                             final String sans = ((String)trans.get(sSans,null));
80                             if (sans!=null) {
81                                 for (String s: Split.splitTrim(',', sans)) {
82                                     arti.getSans().add(s);
83                                 }
84                             }
85                             
86                             // These checks to not apply to deletions
87                             if(!CMArtiChangeForm.DELETE.equals(trans.get(sCmd, ""))) {
88                                     // Disallow IP entries, except by special Permission
89                                     if (!trans.fish(getPerm(ca,"ip"))) {
90                                         boolean ok=true;
91                                         if (IPValidator.ip(machine)) {
92                                             ok=false;
93                                         }
94                                         if (ok) {
95                                             for (String s: arti.getSans()) {
96                                                 if (IPValidator.ip(s)) {
97                                                     ok=false;
98                                                     break;
99                                                 }
100                                             }
101                                         }
102                                         if (!ok) {
103                                             hgen.p("Policy Failure: IPs in certificates are only allowed by Exception.");
104                                             return;
105                                         }
106                                     }
107                                     
108                                     // Disallow Domain based Definitions without exception
109                                     if (machine.startsWith("*")) { // Domain set
110                                         if (!trans.fish(getPerm(ca, "domain"))) {
111                                             hgen.p("Policy Failure: Domain Artifact Declarations are only allowed by Exception.");
112                                             return;
113                                         }
114                                     }
115                             }
116                             
117                             arti.setMechid((String)trans.get(sID,null));
118                             arti.setMachine(machine);
119                             arti.setNs((String)trans.get(sNS,null));
120                             arti.setDir((String)trans.get(sDirectory,null));
121                             arti.setCa(ca);
122                             arti.setOsUser((String)trans.get(sOSUser, null));
123                             arti.setRenewDays(Integer.parseInt((String)trans.get(sRenewal, null)));
124                             arti.setNotification((String)trans.get(sNotify, null));
125                             String[] checkbox = trans.get(sType,null);
126                             for (int i=0;i<CMArtiChangeForm.types.length;++i) {
127                                 if ("on".equals(checkbox[i])) {
128                                     arti.getType().add(CMArtiChangeForm.types[i]);
129                                 }
130                             }
131
132                             // Run Validations
133                             if (arti.getMechid()==null || arti.getMechid().indexOf('@')<=0) {
134                                 hgen.p("Data Entry Failure: Please enter a valid ID, including domain.");
135                             // VALIDATE OTHERS?
136                             } else { // everything else is checked by Server
137                                 
138                                 try {
139                                     final Artifacts artifacts = new Artifacts();
140                                     artifacts.getArtifact().add(arti);
141                                     final Holder<Boolean> ok = new Holder<Boolean>(false); 
142                                     final Holder<Boolean> deleted = new Holder<Boolean>(false);
143                                     Future<?> f = gui.cmClientAsUser(trans.getUserPrincipal(), new Retryable<Future<?>>() {
144                                         @Override
145                                         public Future<?> code(Rcli<?> client)throws CadiException, ConnectException, APIException {
146                                             Future<?> rv = null;
147                                             switch((String)trans.get(sCmd, "")) {
148                                                 case CMArtiChangeForm.CREATE:
149                                                     Future<Artifacts> fc;
150                                                     rv = fc = client.create("/cert/artifacts", gui.artifactsDF, artifacts);
151                                                     if (fc.get(AAFcli.timeout())) {
152                                                         hgen.p("Created Artifact " + arti.getMechid() + " on " + arti.getMachine());
153                                                         ok.set(true);
154                                                     }
155                                                     break;
156                                                 case CMArtiChangeForm.UPDATE:
157                                                     Future<Artifacts> fu = client.update("/cert/artifacts", gui.artifactsDF, artifacts);
158                                                     if ((rv=fu).get(AAFcli.timeout())) {
159                                                         hgen.p("Artifact " + arti.getMechid() + " on " + arti.getMachine() + " is updated");
160                                                         ok.set(true);
161                                                     }
162                                                     break;
163                                                 case CMArtiChangeForm.COPY:
164                                                     Future<Artifacts> future = client.read("/cert/artifacts/"+arti.getMechid()+'/'+arti.getMachine(), gui.artifactsDF);
165                                                     rv = future;
166                                                     if (future.get(AAFcli.timeout())) {
167                                                         for (Artifact a : future.value.getArtifact()) { // only one, because these two are key
168                                                             for (String newMachine :Split.split(',', trans.get(sOther, ""))) {
169                                                                 a.setMachine(newMachine);
170                                                                 Future<Artifacts> fup = client.update("/cert/artifacts", gui.artifactsDF, future.value);
171                                                                 if (fup.get(AAFcli.timeout())) {
172                                                                     hgen.p("Copied to " + newMachine);
173                                                                     ok.set(true);
174                                                                 }
175                                                             }
176                                                         }
177                                                     }
178                                                     break;
179                                                 case CMArtiChangeForm.DELETE:
180                                                     Future<Void> fv;
181                                                     rv = fv = client.delete("/cert/artifacts/"+arti.getMechid()+"/"+arti.getMachine(),"application/json");
182                                                     if (fv.get(AAFcli.timeout())) {
183                                                         hgen.p("Deleted " + arti.getMechid() + " on " + arti.getMachine());
184                                                         ok.set(true);
185                                                         deleted.set(true);
186                                                     }
187                                                     break;
188                                             }
189                                             return rv;
190                                         }
191                                     });
192                                     if (!ok.get()) {
193                                         if (f==null) {
194                                             hgen.p("Unknown Command");
195                                         } else {
196                                             if (f.body().contains("%")) {
197                                                 Error err = gui.getDF(Error.class).newData().in(TYPE.JSON).load(f.body()).asObject();
198                                                 hgen.p(Vars.convert(err.getText(),err.getVariables()));
199                                             } else {
200                                                 hgen.p(arti.getMechid() + " on " + arti.getMachine() + ": " + f.body());
201                                             }
202                                         }
203                                     }
204                                     hgen.br().leaf(HTMLGen.A,"class=greenbutton","href="+(deleted.get()?CMArtifactShow.HREF:CMArtiChangeForm.HREF)+
205                                             "?id="+arti.getMechid()+
206                                             "&amp;machine="+arti.getMachine() +
207                                             "&amp;ns="+arti.getNs())
208                                     .text("Back")
209                                     .end();
210
211                             } catch (Exception e) {
212                                 hgen.p("Unknown Error");
213                                 e.printStackTrace();
214                             }
215                                 
216                         }
217                         hgen.br();
218                     }
219                 });
220             }
221         });
222     }
223 }