Update AAF Version 1.0.0
[aaf/cadi.git] / aaf / src / main / java / com / att / cadi / cm / ArtifactDir.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aaf\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
10  * * \r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * * \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
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 package com.att.cadi.cm;\r
24 \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
36 \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
43 \r
44 import certman.v1_0.Artifacts.Artifact;\r
45 import certman.v1_0.CertInfo;\r
46 \r
47 public abstract class ArtifactDir implements PlaceArtifact {\r
48 \r
49         protected static final String C_R = "\n";\r
50         protected File dir;\r
51         private List<String> encodeds = new ArrayList<String>();\r
52         \r
53         private Symm symm;\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
56 \r
57 \r
58         /**\r
59          * Note:  Derived Classes should ALWAYS call "super.place(cert,arti)" first, and \r
60          * then "placeProperties(arti)" just after they implement\r
61          */\r
62         @Override\r
63         public final boolean place(Trans trans, CertInfo certInfo, Artifact arti) throws CadiException {\r
64                 validate(arti);\r
65                 \r
66                 try {\r
67                         // Obtain/setup directory as required\r
68                         dir = new File(arti.getDir());\r
69                         if(processed.get("dir")==null) {\r
70                                 if(!dir.exists()) {\r
71                                         Chmod.to755.chmod(dir);\r
72                                         if(!dir.mkdirs()) {\r
73                                                 throw new CadiException("Could not create " + dir);\r
74                                         }\r
75                                 }\r
76                                 \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
80                                 //addProperty(Config.AAF_ENV,certInfo.getEnv());\r
81                                 // Obtain Issuers\r
82                                 boolean first = true;\r
83                                 StringBuilder issuers = new StringBuilder();\r
84 //                              for(String dn : certInfo.getCaIssuerDNs()) {\r
85 //                                      if(first) {\r
86 //                                              first=false;\r
87 //                                      } else {\r
88 //                                              issuers.append(':');\r
89 //                                      }\r
90 //                                      issuers.append(dn);\r
91 //                              }\r
92                                 addProperty(Config.CADI_X509_ISSUERS,issuers.toString());\r
93                         }\r
94                         symm = (Symm)processed.get("symm");\r
95                         if(symm==null) {\r
96                                 // CADI Key Gen\r
97                                 File f = new File(dir,arti.getAppName() + ".keyfile");\r
98                                 if(!f.exists()) {\r
99                                         write(f,Chmod.to400,Symm.baseCrypt().keygen());\r
100                                 }\r
101                                 symm = Symm.obtain(f); \r
102 \r
103                                 addEncProperty("ChallengePassword", certInfo.getChallenge());\r
104                                 \r
105                                 processed.put("symm",symm);\r
106                         }\r
107 \r
108                         _place(trans, certInfo,arti);\r
109                         \r
110                         placeProperties(arti);\r
111                         \r
112                         processed.put("dir",dir);\r
113 \r
114                 } catch (Exception e) {\r
115                         throw new CadiException(e);\r
116                 }\r
117                 return true;\r
118         }\r
119 \r
120         /**\r
121          * Derived Classes implement this instead, so Dir can process first, and write any Properties last\r
122          * @param cert\r
123          * @param arti\r
124          * @return\r
125          * @throws CadiException\r
126          */\r
127         protected abstract boolean _place(Trans trans, CertInfo certInfo, Artifact arti) throws CadiException;\r
128 \r
129         protected void addProperty(String tag, String value) throws IOException {\r
130                 StringBuilder sb = new StringBuilder();\r
131                 sb.append(tag);\r
132                 sb.append('=');\r
133                 sb.append(value);\r
134                 encodeds.add(sb.toString());\r
135         }\r
136 \r
137         protected void addEncProperty(String tag, String value) throws IOException {\r
138                 StringBuilder sb = new StringBuilder();\r
139                 sb.append(tag);\r
140                 sb.append('=');\r
141                 sb.append("enc:???");\r
142                 sb.append(symm.enpass(value));\r
143                 encodeds.add(sb.toString());\r
144         }\r
145 \r
146         protected void write(File f, Chmod c, String ... data) throws IOException {\r
147                 f.setWritable(true,true);\r
148                 \r
149                 FileOutputStream fos = new FileOutputStream(f);\r
150                 PrintStream ps = new PrintStream(fos);\r
151                 try {\r
152                         for(String s : data) {\r
153                                 ps.print(s);\r
154                         }\r
155                 } finally {\r
156                         ps.close();\r
157                         c.chmod(f);\r
158                 }\r
159         }\r
160 \r
161         protected void write(File f, Chmod c, byte[] bytes) throws IOException {\r
162                 f.setWritable(true,true);\r
163                 \r
164                 FileOutputStream fos = new FileOutputStream(f);\r
165                 try {\r
166                         fos.write(bytes);\r
167                 } finally {\r
168                         fos.close();\r
169                         c.chmod(f);\r
170                 }\r
171         }\r
172         \r
173         protected void write(File f, Chmod c, KeyStore ks, char[] pass ) throws IOException, CadiException {\r
174                 f.setWritable(true,true);\r
175                 \r
176                 FileOutputStream fos = new FileOutputStream(f);\r
177                 try {\r
178                         ks.store(fos, pass);\r
179                 } catch (Exception e) {\r
180                         throw new CadiException(e);\r
181                 } finally {\r
182                         fos.close();\r
183                         c.chmod(f);\r
184                 }\r
185         }\r
186 \r
187 \r
188         private void validate(Artifact a) throws CadiException {\r
189                 StringBuilder sb = new StringBuilder();\r
190                 if(a.getDir()==null) {\r
191                         sb.append("File Artifacts require a path");\r
192                 }\r
193 \r
194                 if(a.getAppName()==null) {\r
195                         if(sb.length()>0) {\r
196                                 sb.append('\n');\r
197                         }\r
198                         sb.append("File Artifacts require an AAF Namespace");\r
199                 }\r
200                 \r
201                 if(sb.length()>0) {\r
202                         throw new CadiException(sb.toString());\r
203                 }\r
204         }\r
205 \r
206         private boolean placeProperties(Artifact arti) throws CadiException {\r
207                 if(encodeds.size()==0) {\r
208                         return true;\r
209                 }\r
210                 boolean first=processed.get("dir")==null;\r
211                 try {\r
212                         File f = new File(dir,arti.getAppName()+".props");\r
213                         if(f.exists()) {\r
214                                 if(first) {\r
215                                         f.delete();\r
216                                 } else {\r
217                                         f.setWritable(true);\r
218                                 }\r
219                         }\r
220                         // Append if not first\r
221                         PrintWriter pw = new PrintWriter(new FileWriter(f,!first));\r
222                         \r
223                         // Write a Header\r
224                         if(first) {\r
225                                 for(int i=0;i<60;++i) {\r
226                                         pw.print('#');\r
227                                 }\r
228                                 pw.println();\r
229                                 pw.println("# Properties Generated by AT&T Certificate Manager");\r
230                                 pw.print("#   by ");\r
231                                 pw.println(System.getProperty("user.name"));\r
232                                 pw.print("#   on ");\r
233                                 pw.println(Chrono.dateStamp());\r
234                                 pw.println("# @copyright 2016, AT&T");\r
235                                 for(int i=0;i<60;++i) {\r
236                                         pw.print('#');\r
237                                 }\r
238                                 pw.println();\r
239                                 for(String prop : encodeds) {\r
240                                         if(    prop.startsWith("cm_") \r
241                                                 || prop.startsWith(Config.HOSTNAME)\r
242                                                 || prop.startsWith(Config.AAF_ENV)) {\r
243                                                 pw.println(prop);\r
244                                         }\r
245                                 }\r
246                         }\r
247                         \r
248                         try {\r
249                                 for(String prop : encodeds) {\r
250                                         if(prop.startsWith("cadi")) {\r
251                                                 pw.println(prop);\r
252                                         }\r
253                                 }\r
254                         } finally {\r
255                                 pw.close();\r
256                         }\r
257                         Chmod.to644.chmod(f);\r
258                         \r
259                         if(first) {\r
260                                 // Challenge\r
261                                 f = new File(dir,arti.getAppName()+".chal");\r
262                                 if(f.exists()) {\r
263                                         f.delete();\r
264                                 }\r
265                                 pw = new PrintWriter(new FileWriter(f));\r
266                                 try {\r
267                                         for(String prop : encodeds) {\r
268                                                 if(prop.startsWith("Challenge")) {\r
269                                                         pw.println(prop);\r
270                                                 }\r
271                                         }\r
272                                 } finally {\r
273                                         pw.close();\r
274                                 }\r
275                                 Chmod.to400.chmod(f);\r
276                         }\r
277                 } catch(Exception e) {\r
278                         throw new CadiException(e);\r
279                 }\r
280                 return true;\r
281         }\r
282         \r
283         public static void clear() {\r
284                 processed.clear();\r
285         }\r
286 \r
287 }\r