summaryrefslogtreecommitdiffstats
path: root/auth/auth-gui/src/main/java/org/onap/aaf/auth/gui/pages/CMArtifactShow.java
blob: 6d4e167aed32928c4bdc825dc7b100e25a533a39 (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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/**
 * ============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.io.StringWriter;
import java.net.ConnectException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.GregorianCalendar;

import org.onap.aaf.auth.cmd.AAFcli;
import org.onap.aaf.auth.env.AuthzEnv;
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.Page;
import org.onap.aaf.auth.gui.SlotCode;
import org.onap.aaf.auth.gui.Table;
import org.onap.aaf.auth.gui.Table.Cells;
import org.onap.aaf.auth.gui.table.AbsCell;
import org.onap.aaf.auth.gui.table.TextCell;
import org.onap.aaf.cadi.CadiException;
import org.onap.aaf.cadi.client.Future;
import org.onap.aaf.cadi.client.Rcli;
import org.onap.aaf.cadi.client.Retryable;
import org.onap.aaf.cadi.configure.Factory;
import org.onap.aaf.cadi.util.FQI;
import org.onap.aaf.misc.env.APIException;
import org.onap.aaf.misc.env.Env;
import org.onap.aaf.misc.env.TimeTaken;
import org.onap.aaf.misc.env.util.Chrono;
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 certman.v1_0.Artifacts;
import certman.v1_0.Artifacts.Artifact;
import certman.v1_0.CertInfo;

public class CMArtifactShow extends Page {
	
	public static final String HREF = "/gui/cmarti";
	public static final String NAME = "ArtifactsShow";
	private static ArtiTable arti;
	public static SlotCode<AuthzTrans> slotCode;
	private enum Params{id,ns};


	public CMArtifactShow(final AAF_GUI gui, Page ... breadcrumbs) throws APIException, IOException {
		super(gui.env, NAME, HREF, Params.values() , 
				new BreadCrumbs(breadcrumbs),
				arti = new ArtiTable(gui.env)
				);
		// Setting so we can get access to HTMLGen clone and Slots
		arti.set(this,slotCode);
	}
	
	private static class ArtiTable extends Table<AAF_GUI, AuthzTrans> {
		private static Model model;
		private SlotCode<AuthzTrans> sc;
		enum Params {id,ns};
		public ArtiTable(AuthzEnv env) {
			super((String)null,env.newTransNoAvg(),model = new Model(),
					slotCode = new SlotCode<AuthzTrans>(false,env,NAME,Params.values()) {
						@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(AAF_GUI state, AuthzTrans trans, final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
								Mark js = new Mark();
								hgen.js(js).function("newArtifact")
								.text("machine=document.getElementById('machine');")
							    .text("window.open('"
							    		+CMArtiChangeForm.HREF+
										"?id="+get(trans, Params.id,"")+
										"&ns="+get(trans, Params.ns,"")+
										"&machine='+machine.value,'_self');"
							    	).end(js);
								hgen.leaf("input","id=machine","style=margin:1em 1em 1em 1em;width:30%").end();
								hgen.leaf(HTMLGen.A,"class=greenbutton","href=javascript:newArtifact()","style=color:white;").text("New FQDN").end();
							}
						});
						}
					},"class=std");
		}
		

		public void set(CMArtifactShow cmArtifactShow, SlotCode<AuthzTrans> sc) {
			this.sc = sc;
			model.set(cmArtifactShow,sc);
		}
		
		@Override
		protected String title(AuthzTrans trans) {
			StringBuilder sb = new StringBuilder("X509 Certificates");
			if(sc!=null) { // initialized
				sb.append(" for ");
				String id = sc.get(trans,Params.id,"");
				sb.append(id);
				if(id.indexOf('@')<0) {
					sb.append('@');
					sb.append(FQI.reverseDomain(sc.get(trans, Params.ns,"missingDomain")));
				}
			}
			return sb.toString();
		}
	}
	/**
	 * Implement the table content for Cred Detail
	 * 
	 * @author Jeremiah
	 *
	 */
	private static class Model implements Table.Data<AAF_GUI,AuthzTrans> {
		private CMArtifactShow cas;
		private SlotCode<AuthzTrans> sc;

		// Covering for Constructor Order
		private void set(CMArtifactShow cas, SlotCode<AuthzTrans> sc) {
			this.cas = cas;
			this.sc = sc;
		}

		private static final String[] headers = new String[]{"FQDN","Directory","CA","Renews","Expires",""};
		@Override
		public String[] headers() {
			return headers;
		}
		
		@Override
		public Cells get(final AuthzTrans trans, final AAF_GUI gui) {
			String str = sc.get(trans,Params.id, null);
			if(str==null) {
				return Cells.EMPTY;
			}
			final String id = str.indexOf('@')>=0?str:str + '@' + FQI.reverseDomain(sc.get(trans,Params.ns, ""));
			final ArrayList<AbsCell[]> rv = new ArrayList<>();
			final TimeTaken tt = trans.start("AAF X509 Details",Env.REMOTE);
			try {
				gui.cmClientAsUser(trans.getUserPrincipal(),new Retryable<Void>() {
					@Override
					public Void code(Rcli<?> client) throws CadiException, ConnectException, APIException {
						Future<CertInfo>  fuCI = client.read("/cert/id/"+id,gui.certInfoDF);
						Future<Artifacts> fuArt = client.read("/cert/artifacts?mechid="+id, gui.artifactsDF);
						
						X509Certificate[] lc;
						if(fuCI.get(AAFcli.timeout())) {
							TimeTaken tt1 = trans.start("x509Certificate", Env.SUB);
							try {
								Collection<? extends Certificate> xcs = Factory.toX509Certificate(fuCI.value.getCerts());
								 lc = new X509Certificate[xcs.size()];
								xcs.toArray(lc);
							} catch (CertificateException e) {
								trans.error().log(e,"Bad Certificate entry");
								throw new CadiException(e);
							} finally {
								tt1.done();
							}
						} else {
							lc = null;
							trans.error().log("Cannot retrieve Certificates for " + id);
						}
						if(fuArt.get(AAFcli.timeout())) {
							for(Artifact arti : fuArt.value.getArtifact()) {
								StringWriter sw = new StringWriter();
								HTMLGen hgen = cas.clone(sw);
								Mark mark = new Mark();
								hgen.leaf(HTMLGen.A,"class=button",
										"href="+CMArtiChangeForm.HREF+"?id="+arti.getMechid() +"&machine="+arti.getMachine()+"&ns="+arti.getNs())
										.text("Details")
									.end(mark);
								Date last = null;
								if(lc!=null) {
									for(X509Certificate xc : lc) {
										if(xc.getSubjectDN().getName().contains("CN="+arti.getMachine())) {
											if(last==null || last.before(xc.getNotAfter())) {
												last = xc.getNotAfter();
											}
										}
									}
								}
								GregorianCalendar renew;
								if(last!=null) {
									renew = new GregorianCalendar();
									renew.setTime(last);
									renew.add(GregorianCalendar.DAY_OF_MONTH,arti.getRenewDays()*-1);
								} else {
									renew = null;
								}

								rv.add(new AbsCell[] {
									new TextCell(arti.getMachine(),"style=width:20%;"), 
									new TextCell(arti.getDir(),"style=width:25%;"),
									new TextCell(arti.getCa(),"style=width:2%;text-align:center;"),
									new TextCell(renew==null?
											arti.getRenewDays().toString() + " days before Exp":
											Chrono.dateOnlyStamp(renew),"style=width:6%;text-align:center;"),
									new TextCell(last==null?"None Deployed":Chrono.dateOnlyStamp(last),"style=width:5%;text-align:center;"),
									new TextCell(sw.toString(),"style=width:10%;text-align:center;")
								});
							}
						} else {
							rv.add(new AbsCell[] {new TextCell("*** Data Unavailable ***")});
						}
						return null;
					}
				});
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				tt.done();
			}
			return new Cells(rv,null);
		}

		@Override
		public void prefix(AAF_GUI state, AuthzTrans trans, final Cache<HTMLGen> cache, final HTMLGen hgen) {
		}

		@Override
		public void postfix(AAF_GUI state, AuthzTrans trans, final Cache<HTMLGen> cache, final HTMLGen hgen) {
		}

	}
	
}