1 /*******************************************************************************
\r
2 * ============LICENSE_START====================================================
\r
4 * * ===========================================================================
\r
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * * ===========================================================================
\r
7 * * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * * you may not use this file except in compliance with the License.
\r
9 * * You may obtain a copy of the License at
\r
11 * * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * * Unless required by applicable law or agreed to in writing, software
\r
14 * * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * * See the License for the specific language governing permissions and
\r
17 * * limitations under the License.
\r
18 * * ============LICENSE_END====================================================
\r
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
23 package com.att.cadi.cm;
\r
25 import java.io.File;
\r
26 import java.io.FileOutputStream;
\r
27 import java.io.FileWriter;
\r
28 import java.io.IOException;
\r
29 import java.io.PrintStream;
\r
30 import java.io.PrintWriter;
\r
31 import java.security.KeyStore;
\r
32 import java.util.ArrayList;
\r
33 import java.util.HashMap;
\r
34 import java.util.List;
\r
35 import java.util.Map;
\r
37 import com.att.cadi.CadiException;
\r
38 import com.att.cadi.Symm;
\r
39 import com.att.cadi.config.Config;
\r
40 import com.att.cadi.util.Chmod;
\r
41 import com.att.inno.env.Trans;
\r
42 import com.att.inno.env.util.Chrono;
\r
44 import certman.v1_0.Artifacts.Artifact;
\r
45 import certman.v1_0.CertInfo;
\r
47 public abstract class ArtifactDir implements PlaceArtifact {
\r
49 protected static final String C_R = "\n";
\r
51 private List<String> encodeds = new ArrayList<String>();
\r
54 // This checks for multiple passes of Dir on the same objects. Run clear after done.
\r
55 protected static Map<String,Object> processed = new HashMap<String,Object>();
\r
59 * Note: Derived Classes should ALWAYS call "super.place(cert,arti)" first, and
\r
60 * then "placeProperties(arti)" just after they implement
\r
63 public final boolean place(Trans trans, CertInfo certInfo, Artifact arti) throws CadiException {
\r
67 // Obtain/setup directory as required
\r
68 dir = new File(arti.getDir());
\r
69 if(processed.get("dir")==null) {
\r
71 Chmod.to755.chmod(dir);
\r
73 throw new CadiException("Could not create " + dir);
\r
77 // Also place cm_url and Host Name
\r
78 addProperty(Config.CM_URL,trans.getProperty(Config.CM_URL));
\r
79 addProperty(Config.HOSTNAME,arti.getMachine());
\r
81 symm = (Symm)processed.get("symm");
\r
84 File f = new File(dir,arti.getAppName() + ".keyfile");
\r
86 write(f,Chmod.to400,Symm.baseCrypt().keygen());
\r
88 symm = Symm.obtain(f);
\r
90 addEncProperty("ChallengePassword", certInfo.getChallenge());
\r
92 processed.put("symm",symm);
\r
95 _place(trans, certInfo,arti);
\r
97 placeProperties(arti);
\r
99 processed.put("dir",dir);
\r
101 } catch (Exception e) {
\r
102 throw new CadiException(e);
\r
108 * Derived Classes implement this instead, so Dir can process first, and write any Properties last
\r
112 * @throws CadiException
\r
114 protected abstract boolean _place(Trans trans, CertInfo certInfo, Artifact arti) throws CadiException;
\r
116 protected void addProperty(String tag, String value) throws IOException {
\r
117 StringBuilder sb = new StringBuilder();
\r
121 encodeds.add(sb.toString());
\r
124 protected void addEncProperty(String tag, String value) throws IOException {
\r
125 StringBuilder sb = new StringBuilder();
\r
128 sb.append("enc:???");
\r
129 sb.append(symm.enpass(value));
\r
130 encodeds.add(sb.toString());
\r
133 protected void write(File f, Chmod c, String ... data) throws IOException {
\r
134 f.setWritable(true,true);
\r
136 FileOutputStream fos = new FileOutputStream(f);
\r
137 PrintStream ps = new PrintStream(fos);
\r
139 for(String s : data) {
\r
148 protected void write(File f, Chmod c, byte[] bytes) throws IOException {
\r
149 f.setWritable(true,true);
\r
151 FileOutputStream fos = new FileOutputStream(f);
\r
160 protected void write(File f, Chmod c, KeyStore ks, char[] pass ) throws IOException, CadiException {
\r
161 f.setWritable(true,true);
\r
163 FileOutputStream fos = new FileOutputStream(f);
\r
165 ks.store(fos, pass);
\r
166 } catch (Exception e) {
\r
167 throw new CadiException(e);
\r
175 private void validate(Artifact a) throws CadiException {
\r
176 StringBuilder sb = new StringBuilder();
\r
177 if(a.getDir()==null) {
\r
178 sb.append("File Artifacts require a path");
\r
181 if(a.getAppName()==null) {
\r
182 if(sb.length()>0) {
\r
185 sb.append("File Artifacts require an appName");
\r
188 if(sb.length()>0) {
\r
189 throw new CadiException(sb.toString());
\r
193 private boolean placeProperties(Artifact arti) throws CadiException {
\r
194 if(encodeds.size()==0) {
\r
197 boolean first=processed.get("dir")==null;
\r
199 File f = new File(dir,arti.getAppName()+".props");
\r
204 f.setWritable(true);
\r
207 // Append if not first
\r
208 PrintWriter pw = new PrintWriter(new FileWriter(f,!first));
\r
212 for(int i=0;i<60;++i) {
\r
216 pw.println("# Properties Generated by AT&T Certificate Manager");
\r
218 pw.println(System.getProperty("user.name"));
\r
220 pw.println(Chrono.dateStamp());
\r
221 pw.println("# @copyright 2016, AT&T");
\r
222 for(int i=0;i<60;++i) {
\r
226 for(String prop : encodeds) {
\r
227 if(prop.startsWith("cm_") || prop.startsWith(Config.HOSTNAME)) {
\r
234 for(String prop : encodeds) {
\r
235 if(prop.startsWith("cadi")) {
\r
242 Chmod.to400.chmod(f);
\r
246 f = new File(dir,arti.getAppName()+".chal");
\r
250 pw = new PrintWriter(new FileWriter(f));
\r
252 for(String prop : encodeds) {
\r
253 if(prop.startsWith("Challenge")) {
\r
260 Chmod.to400.chmod(f);
\r
262 } catch(Exception e) {
\r
263 throw new CadiException(e);
\r
268 public static void clear() {
\r