aboutsummaryrefslogtreecommitdiffstats
path: root/aaf/src/main/java/org/onap/aaf/cadi/cm/ArtifactDir.java
blob: af50682ed564a0353f1e9f8bce7eb08b623f5553 (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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*******************************************************************************
 * ============LICENSE_START====================================================
 * * org.onap.aaf
 * * ===========================================================================
 * * Copyright © 2017 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====================================================
 * *
 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 * *
 ******************************************************************************/
package org.onap.aaf.cadi.cm;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.security.KeyStore;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.onap.aaf.cadi.CadiException;
import org.onap.aaf.cadi.Symm;
import org.onap.aaf.cadi.config.Config;
import org.onap.aaf.cadi.util.Chmod;

import org.onap.aaf.inno.env.Trans;
import org.onap.aaf.inno.env.util.Chrono;

import certman.v1_0.Artifacts.Artifact;
import certman.v1_0.CertInfo;

public abstract class ArtifactDir implements PlaceArtifact {

	protected static final String C_R = "\n";
	protected File dir;
	private List<String> encodeds = new ArrayList<String>();
	
	private Symm symm;
	// This checks for multiple passes of Dir on the same objects.  Run clear after done.
	protected static Map<String,Object> processed = new HashMap<String,Object>();


	/**
	 * Note:  Derived Classes should ALWAYS call "super.place(cert,arti)" first, and 
	 * then "placeProperties(arti)" just after they implement
	 */
	@Override
	public final boolean place(Trans trans, CertInfo certInfo, Artifact arti) throws CadiException {
		validate(arti);
		
		try {
			// Obtain/setup directory as required
			dir = new File(arti.getDir());
			if(processed.get("dir")==null) {
				if(!dir.exists()) {
					Chmod.to755.chmod(dir);
					if(!dir.mkdirs()) {
						throw new CadiException("Could not create " + dir);
					}
				}
				
				// Also place cm_url and Host Name
				addProperty(Config.CM_URL,trans.getProperty(Config.CM_URL));
				addProperty(Config.HOSTNAME,arti.getMachine());
				//addProperty(Config.AAF_ENV,certInfo.getEnv());
				// Obtain Issuers
				boolean first = true;
				StringBuilder issuers = new StringBuilder();
//				for(String dn : certInfo.getCaIssuerDNs()) {
//					if(first) {
//						first=false;
//					} else {
//						issuers.append(':');
//					}
//					issuers.append(dn);
//				}
				addProperty(Config.CADI_X509_ISSUERS,issuers.toString());
			}
			symm = (Symm)processed.get("symm");
			if(symm==null) {
				// CADI Key Gen
				File f = new File(dir,arti.getAppName() + ".keyfile");
				if(!f.exists()) {
					write(f,Chmod.to400,Symm.baseCrypt().keygen());
				}
				symm = Symm.obtain(f); 

				addEncProperty("ChallengePassword", certInfo.getChallenge());
				
				processed.put("symm",symm);
			}

			_place(trans, certInfo,arti);
			
			placeProperties(arti);
			
			processed.put("dir",dir);

		} catch (Exception e) {
			throw new CadiException(e);
		}
		return true;
	}

	/**
	 * Derived Classes implement this instead, so Dir can process first, and write any Properties last
	 * @param cert
	 * @param arti
	 * @return
	 * @throws CadiException
	 */
	protected abstract boolean _place(Trans trans, CertInfo certInfo, Artifact arti) throws CadiException;

	protected void addProperty(String tag, String value) throws IOException {
		StringBuilder sb = new StringBuilder();
		sb.append(tag);
		sb.append('=');
		sb.append(value);
		encodeds.add(sb.toString());
	}

	protected void addEncProperty(String tag, String value) throws IOException {
		StringBuilder sb = new StringBuilder();
		sb.append(tag);
		sb.append('=');
		sb.append("enc:???");
		sb.append(symm.enpass(value));
		encodeds.add(sb.toString());
	}

	protected void write(File f, Chmod c, String ... data) throws IOException {
		f.setWritable(true,true);
		
		FileOutputStream fos = new FileOutputStream(f);
		PrintStream ps = new PrintStream(fos);
		try {
			for(String s : data) {
				ps.print(s);
			}
		} finally {
			ps.close();
			c.chmod(f);
		}
	}

	protected void write(File f, Chmod c, byte[] bytes) throws IOException {
		f.setWritable(true,true);
		
		FileOutputStream fos = new FileOutputStream(f);
		try {
			fos.write(bytes);
		} finally {
			fos.close();
			c.chmod(f);
		}
	}
	
	protected void write(File f, Chmod c, KeyStore ks, char[] pass ) throws IOException, CadiException {
		f.setWritable(true,true);
		
		FileOutputStream fos = new FileOutputStream(f);
		try {
			ks.store(fos, pass);
		} catch (Exception e) {
			throw new CadiException(e);
		} finally {
			fos.close();
			c.chmod(f);
		}
	}


	private void validate(Artifact a) throws CadiException {
		StringBuilder sb = new StringBuilder();
		if(a.getDir()==null) {
			sb.append("File Artifacts require a path");
		}

		if(a.getAppName()==null) {
			if(sb.length()>0) {
				sb.append('\n');
			}
			sb.append("File Artifacts require an AAF Namespace");
		}
		
		if(sb.length()>0) {
			throw new CadiException(sb.toString());
		}
	}

	private boolean placeProperties(Artifact arti) throws CadiException {
		if(encodeds.size()==0) {
			return true;
		}
		boolean first=processed.get("dir")==null;
		try {
			File f = new File(dir,arti.getAppName()+".props");
			if(f.exists()) {
				if(first) {
					f.delete();
				} else {
					f.setWritable(true);
				}
			}
			// Append if not first
			PrintWriter pw = new PrintWriter(new FileWriter(f,!first));
			
			// Write a Header
			if(first) {
				for(int i=0;i<60;++i) {
					pw.print('#');
				}
				pw.println();
				pw.println("# Properties Generated by AT&T Certificate Manager");
				pw.print("#   by ");
				pw.println(System.getProperty("user.name"));
				pw.print("#   on ");
				pw.println(Chrono.dateStamp());
				pw.println("# @copyright 2016, AT&T");
				for(int i=0;i<60;++i) {
					pw.print('#');
				}
				pw.println();
				for(String prop : encodeds) {
					if(    prop.startsWith("cm_") 
						|| prop.startsWith(Config.HOSTNAME)
						|| prop.startsWith(Config.AAF_ENV)) {
						pw.println(prop);
					}
				}
			}
			
			try {
				for(String prop : encodeds) {
					if(prop.startsWith("cadi")) {
						pw.println(prop);
					}
				}
			} finally {
				pw.close();
			}
			Chmod.to644.chmod(f);
			
			if(first) {
				// Challenge
				f = new File(dir,arti.getAppName()+".chal");
				if(f.exists()) {
					f.delete();
				}
				pw = new PrintWriter(new FileWriter(f));
				try {
					for(String prop : encodeds) {
						if(prop.startsWith("Challenge")) {
							pw.println(prop);
						}
					}
				} finally {
					pw.close();
				}
				Chmod.to400.chmod(f);
			}
		} catch(Exception e) {
			throw new CadiException(e);
		}
		return true;
	}
	
	public static void clear() {
		processed.clear();
	}

}