summaryrefslogtreecommitdiffstats
path: root/auth/auth-gui/src/main/java/org/onap/aaf/auth/gui/pages/CMArtiChangeAction.java
blob: c19820d3459d45da057124e6d247fd61f0b77ba0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/**
 * ============LICENSE_START====================================================
 * org.onap.aaf
 * ===========================================================================
 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
 * ===========================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END====================================================
 *
 */

package org.onap.aaf.auth.gui.pages;

import java.io.IOException;
import java.net.ConnectException;

import org.onap.aaf.auth.cmd.AAFcli;
import org.onap.aaf.auth.env.AuthzTrans;
import org.onap.aaf.auth.gui.AAF_GUI;
import org.onap.aaf.auth.gui.BreadCrumbs;
import org.onap.aaf.auth.gui.NamedCode;
import org.onap.aaf.auth.gui.Page;
import org.onap.aaf.cadi.CadiException;
import org.onap.aaf.cadi.client.Future;
import org.onap.aaf.cadi.client.Holder;
import org.onap.aaf.cadi.client.Rcli;
import org.onap.aaf.cadi.client.Retryable;
import org.onap.aaf.cadi.util.Vars;
import org.onap.aaf.misc.env.APIException;
import org.onap.aaf.misc.env.Data.TYPE;
import org.onap.aaf.misc.env.Slot;
import org.onap.aaf.misc.env.util.IPValidator;
import org.onap.aaf.misc.env.util.Split;
import org.onap.aaf.misc.xgen.Cache;
import org.onap.aaf.misc.xgen.DynamicCode;
import org.onap.aaf.misc.xgen.Mark;
import org.onap.aaf.misc.xgen.html.HTMLGen;

import aaf.v2_0.Error;
import certman.v1_0.Artifacts;
import certman.v1_0.Artifacts.Artifact;

public class CMArtiChangeAction extends Page {
    public CMArtiChangeAction(final AAF_GUI gui, final Page ... breadcrumbs) throws APIException, IOException {
        super(gui.env,CMArtiChangeForm.NAME,CMArtiChangeForm.HREF, CMArtiChangeForm.fields,
            new BreadCrumbs(breadcrumbs),
            new NamedCode(true,"content") {
                final Slot sID = gui.env.slot(CMArtiChangeForm.NAME+'.'+CMArtiChangeForm.fields[0]);
                final Slot sMachine = gui.env.slot(CMArtiChangeForm.NAME+'.'+CMArtiChangeForm.fields[1]);
                final Slot sNS = gui.env.slot(CMArtiChangeForm.NAME+'.'+CMArtiChangeForm.fields[2]);
                final Slot sDirectory = gui.env.slot(CMArtiChangeForm.NAME+'.'+CMArtiChangeForm.fields[3]);
                final Slot sCA = gui.env.slot(CMArtiChangeForm.NAME+'.'+CMArtiChangeForm.fields[4]);
                final Slot sOSUser = gui.env.slot(CMArtiChangeForm.NAME+'.'+CMArtiChangeForm.fields[5]);
                final Slot sRenewal = gui.env.slot(CMArtiChangeForm.NAME+'.'+CMArtiChangeForm.fields[6]);
                final Slot sNotify = gui.env.slot(CMArtiChangeForm.NAME+'.'+CMArtiChangeForm.fields[7]);
                final Slot sCmd = gui.env.slot(CMArtiChangeForm.NAME+'.'+CMArtiChangeForm.fields[8]);
                final Slot sOther = gui.env.slot(CMArtiChangeForm.NAME+'.'+CMArtiChangeForm.fields[9]);
                final Slot sType = gui.env.slot(CMArtiChangeForm.NAME+'.'+CMArtiChangeForm.fields[10]);
                final Slot sSans = gui.env.slot(CMArtiChangeForm.NAME+'.'+CMArtiChangeForm.fields[11]);

                @Override
                public void code(final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
                    cache.dynamic(hgen, new DynamicCode<HTMLGen,AAF_GUI, AuthzTrans>() {
                        @Override
                        public void code(final AAF_GUI gui, final AuthzTrans trans,final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
                            trans.info().log("Step 1");
                            final Artifact arti = new Artifact();
                            final String machine = trans.get(sMachine,null);
                            final String ca = trans.get(sCA, null);
                            final String sans = ((String)trans.get(sSans,null));
                            if (sans!=null) {
                                for (String s: Split.splitTrim(',', sans)) {
                                    arti.getSans().add(s);
                                }
                            }

                            // These checks to not apply to deletions
                            if(!CMArtiChangeForm.DELETE.equals(trans.get(sCmd, ""))) {
                                // Disallow IP entries, except by special Permission
                                if (!trans.fish(getPerm(ca,"ip"))) {
                                    boolean ok=true;
                                    if (IPValidator.ip(machine)) {
                                        ok=false;
                                    }
                                    if (ok) {
                                        for (String s: arti.getSans()) {
                                            if (IPValidator.ip(s)) {
                                                ok=false;
                                                break;
                                            }
                                        }
                                    }
                                    if (!ok) {
                                        hgen.p("Policy Failure: IPs in certificates are only allowed by Exception.");
                                        return;
                                    }
                                }

                            }

                            arti.setMechid((String)trans.get(sID,null));
                            arti.setMachine(machine);
                            arti.setNs((String)trans.get(sNS,null));
                            arti.setDir((String)trans.get(sDirectory,null));
                            arti.setCa(ca);
                            arti.setOsUser((String)trans.get(sOSUser, null));
                            arti.setRenewDays(Integer.parseInt((String)trans.get(sRenewal, null)));
                            arti.setNotification((String)trans.get(sNotify, null));
                            String[] checkbox = trans.get(sType,null);
                            for (int i=0;i<CMArtiChangeForm.types.length;++i) {
                                if ("on".equals(checkbox[i])) {
                                    arti.getType().add(CMArtiChangeForm.types[i]);
                                }
                            }

                            // Run Validations
                            if (arti.getMechid()==null || arti.getMechid().indexOf('@')<=0) {
                                hgen.p("Data Entry Failure: Please enter a valid ID, including domain.");
                            // VALIDATE OTHERS?
                            } else { // everything else is checked by Server

                                try {
                                    final Artifacts artifacts = new Artifacts();
                                    artifacts.getArtifact().add(arti);
                                    final Holder<Boolean> ok = new Holder<Boolean>(false);
                                    final Holder<Boolean> deleted = new Holder<Boolean>(false);
                                    Future<?> f = gui.cmClientAsUser(trans.getUserPrincipal(), new Retryable<Future<?>>() {
                                        @Override
                                        public Future<?> code(Rcli<?> client)throws CadiException, ConnectException, APIException {
                                            Future<?> rv = null;
                                            switch((String)trans.get(sCmd, "")) {
                                                case CMArtiChangeForm.CREATE:
                                                    Future<Artifacts> fc;
                                                    rv = fc = client.create("/cert/artifacts", gui.artifactsDF, artifacts);
                                                    if (fc.get(AAFcli.timeout())) {
                                                        hgen.p("Created Artifact " + arti.getMechid() + " on " + arti.getMachine());
                                                        ok.set(true);
                                                    }
                                                    break;
                                                case CMArtiChangeForm.UPDATE:
                                                    Future<Artifacts> fu = client.update("/cert/artifacts", gui.artifactsDF, artifacts);
                                                    rv=fu;
                                                    if(rv.get(AAFcli.timeout())) {
                                                        hgen.p("Artifact " + arti.getMechid() + " on " + arti.getMachine() + " is updated");
                                                        ok.set(true);
                                                    }
                                                    break;
                                                case CMArtiChangeForm.COPY:
                                                    Future<Artifacts> future = client.read("/cert/artifacts/"+arti.getMechid()+'/'+arti.getMachine(), gui.artifactsDF);
                                                    rv = future;
                                                    if (future.get(AAFcli.timeout())) {
                                                        for (Artifact a : future.value.getArtifact()) { // only one, because these two are key
                                                            for (String newMachine :Split.split(',', trans.get(sOther, ""))) {
                                                                a.setMachine(newMachine);
                                                                Future<Artifacts> fup = client.update("/cert/artifacts", gui.artifactsDF, future.value);
                                                                if (fup.get(AAFcli.timeout())) {
                                                                    hgen.p("Copied to " + newMachine);
                                                                    ok.set(true);
                                                                }
                                                            }
                                                        }
                                                    }
                                                    break;
                                                case CMArtiChangeForm.DELETE:
                                                    Future<Void> fv;
                                                    rv = fv = client.delete("/cert/artifacts/"+arti.getMechid()+"/"+arti.getMachine(),"application/json");
                                                    if (fv.get(AAFcli.timeout())) {
                                                        hgen.p("Deleted " + arti.getMechid() + " on " + arti.getMachine());
                                                        ok.set(true);
                                                        deleted.set(true);
                                                    }
                                                    break;
                                            }
                                            return rv;
                                        }
                                    });
                                    if (!ok.get()) {
                                        if (f==null) {
                                            hgen.p("Unknown Command");
                                        } else {
                                            if (f.code() > 201) {
                                                Error err = gui.getDF(Error.class).newData().in(TYPE.JSON).load(f.body()).asObject();
                                                if(f.body().contains("%") ) {
                                                    hgen.p(Vars.convert(err.getText(),err.getVariables()));
                                                } else {
                                                    int colon = err.getText().indexOf(':');
                                                    if(colon>0) {
                                                        hgen.p(err.getMessageId() + ": " + err.getText().substring(0, colon));
                                                        Mark bq = new Mark();
                                                        hgen.incr(bq,"blockquote");
                                                        for(String em : Split.splitTrim('\n', err.getText().substring(colon+1))) {
                                                            hgen.p(em);
                                                        }
                                                        hgen.end(bq);
                                                    } else {
                                                        hgen.p(err.getMessageId() + ": " + err.getText());
                                                    }
                                                }
                                            } else {
                                                hgen.p(arti.getMechid() + " on " + arti.getMachine() + ": " + f.body());
                                            }
                                        }
                                    }
                                    hgen.br().leaf(HTMLGen.A,"class=greenbutton","href="+(deleted.get()?CMArtifactShow.HREF:CMArtiChangeForm.HREF)+
                                            "?id="+arti.getMechid()+
                                            "&amp;machine="+arti.getMachine() +
                                            "&amp;ns="+arti.getNs())
                                    .text("Back")
                                    .end();

                            } catch (Exception e) {
                                hgen.p("Unknown Error");
                                e.printStackTrace();
                            }

                        }
                        hgen.br();
                    }
                });
            }
        });
    }
}