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