7ddf52978d12fcd5f3a98897e5d6d66e8517252b
[aaf/cadi.git] / aaf / src / src / main / java / com / att / cadi / cm / ArtifactDir.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aai\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * Copyright © 2017 Amdocs\r
7  * * ===========================================================================\r
8  * * Licensed under the Apache License, Version 2.0 (the "License");\r
9  * * you may not use this file except in compliance with the License.\r
10  * * You may obtain a copy of the License at\r
11  * * \r
12  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
13  * * \r
14  *  * Unless required by applicable law or agreed to in writing, software\r
15  * * distributed under the License is distributed on an "AS IS" BASIS,\r
16  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
17  * * See the License for the specific language governing permissions and\r
18  * * limitations under the License.\r
19  * * ============LICENSE_END====================================================\r
20  * *\r
21  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
22  * *\r
23  ******************************************************************************/\r
24 package com.att.cadi.cm;\r
25 \r
26 import java.io.File;\r
27 import java.io.FileOutputStream;\r
28 import java.io.FileWriter;\r
29 import java.io.IOException;\r
30 import java.io.PrintStream;\r
31 import java.io.PrintWriter;\r
32 import java.security.KeyStore;\r
33 import java.util.ArrayList;\r
34 import java.util.HashMap;\r
35 import java.util.List;\r
36 import java.util.Map;\r
37 \r
38 import com.att.cadi.CadiException;\r
39 import com.att.cadi.Symm;\r
40 import com.att.cadi.config.Config;\r
41 import com.att.cadi.util.Chmod;\r
42 import com.att.inno.env.Trans;\r
43 import com.att.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                         }\r
82                         symm = (Symm)processed.get("symm");\r
83                         if(symm==null) {\r
84                                 // CADI Key Gen\r
85                                 File f = new File(dir,arti.getAppName() + ".keyfile");\r
86                                 if(!f.exists()) {\r
87                                         write(f,Chmod.to400,Symm.baseCrypt().keygen());\r
88                                 }\r
89                                 symm = Symm.obtain(f); \r
90 \r
91                                 addEncProperty("ChallengePassword", certInfo.getChallenge());\r
92                                 \r
93                                 processed.put("symm",symm);\r
94                         }\r
95 \r
96                         _place(trans, certInfo,arti);\r
97                         \r
98                         placeProperties(arti);\r
99                         \r
100                         processed.put("dir",dir);\r
101 \r
102                 } catch (Exception e) {\r
103                         throw new CadiException(e);\r
104                 }\r
105                 return true;\r
106         }\r
107 \r
108         /**\r
109          * Derived Classes implement this instead, so Dir can process first, and write any Properties last\r
110          * @param cert\r
111          * @param arti\r
112          * @return\r
113          * @throws CadiException\r
114          */\r
115         protected abstract boolean _place(Trans trans, CertInfo certInfo, Artifact arti) throws CadiException;\r
116 \r
117         protected void addProperty(String tag, String value) throws IOException {\r
118                 StringBuilder sb = new StringBuilder();\r
119                 sb.append(tag);\r
120                 sb.append('=');\r
121                 sb.append(value);\r
122                 encodeds.add(sb.toString());\r
123         }\r
124 \r
125         protected void addEncProperty(String tag, String value) throws IOException {\r
126                 StringBuilder sb = new StringBuilder();\r
127                 sb.append(tag);\r
128                 sb.append('=');\r
129                 sb.append("enc:???");\r
130                 sb.append(symm.enpass(value));\r
131                 encodeds.add(sb.toString());\r
132         }\r
133 \r
134         protected void write(File f, Chmod c, String ... data) throws IOException {\r
135                 f.setWritable(true,true);\r
136                 \r
137                 FileOutputStream fos = new FileOutputStream(f);\r
138                 PrintStream ps = new PrintStream(fos);\r
139                 try {\r
140                         for(String s : data) {\r
141                                 ps.print(s);\r
142                         }\r
143                 } finally {\r
144                         ps.close();\r
145                         c.chmod(f);\r
146                 }\r
147         }\r
148 \r
149         protected void write(File f, Chmod c, byte[] bytes) throws IOException {\r
150                 f.setWritable(true,true);\r
151                 \r
152                 FileOutputStream fos = new FileOutputStream(f);\r
153                 try {\r
154                         fos.write(bytes);\r
155                 } finally {\r
156                         fos.close();\r
157                         c.chmod(f);\r
158                 }\r
159         }\r
160         \r
161         protected void write(File f, Chmod c, KeyStore ks, char[] pass ) throws IOException, CadiException {\r
162                 f.setWritable(true,true);\r
163                 \r
164                 FileOutputStream fos = new FileOutputStream(f);\r
165                 try {\r
166                         ks.store(fos, pass);\r
167                 } catch (Exception e) {\r
168                         throw new CadiException(e);\r
169                 } finally {\r
170                         fos.close();\r
171                         c.chmod(f);\r
172                 }\r
173         }\r
174 \r
175 \r
176         private void validate(Artifact a) throws CadiException {\r
177                 StringBuilder sb = new StringBuilder();\r
178                 if(a.getDir()==null) {\r
179                         sb.append("File Artifacts require a path");\r
180                 }\r
181 \r
182                 if(a.getAppName()==null) {\r
183                         if(sb.length()>0) {\r
184                                 sb.append('\n');\r
185                         }\r
186                         sb.append("File Artifacts require an appName");\r
187                 }\r
188                 \r
189                 if(sb.length()>0) {\r
190                         throw new CadiException(sb.toString());\r
191                 }\r
192         }\r
193 \r
194         private boolean placeProperties(Artifact arti) throws CadiException {\r
195                 if(encodeds.size()==0) {\r
196                         return true;\r
197                 }\r
198                 boolean first=processed.get("dir")==null;\r
199                 try {\r
200                         File f = new File(dir,arti.getAppName()+".props");\r
201                         if(f.exists()) {\r
202                                 if(first) {\r
203                                         f.delete();\r
204                                 } else {\r
205                                         f.setWritable(true);\r
206                                 }\r
207                         }\r
208                         // Append if not first\r
209                         PrintWriter pw = new PrintWriter(new FileWriter(f,!first));\r
210                         \r
211                         // Write a Header\r
212                         if(first) {\r
213                                 for(int i=0;i<60;++i) {\r
214                                         pw.print('#');\r
215                                 }\r
216                                 pw.println();\r
217                                 pw.println("# Properties Generated by AT&T Certificate Manager");\r
218                                 pw.print("#   by ");\r
219                                 pw.println(System.getProperty("user.name"));\r
220                                 pw.print("#   on ");\r
221                                 pw.println(Chrono.dateStamp());\r
222                                 pw.println("# @copyright 2016, AT&T");\r
223                                 for(int i=0;i<60;++i) {\r
224                                         pw.print('#');\r
225                                 }\r
226                                 pw.println();\r
227                                 for(String prop : encodeds) {\r
228                                         if(prop.startsWith("cm_") || prop.startsWith(Config.HOSTNAME)) {\r
229                                                 pw.println(prop);\r
230                                         }\r
231                                 }\r
232                         }\r
233                         \r
234                         try {\r
235                                 for(String prop : encodeds) {\r
236                                         if(prop.startsWith("cadi")) {\r
237                                                 pw.println(prop);\r
238                                         }\r
239                                 }\r
240                         } finally {\r
241                                 pw.close();\r
242                         }\r
243                         Chmod.to400.chmod(f);\r
244                         \r
245                         if(first) {\r
246                                 // Challenge\r
247                                 f = new File(dir,arti.getAppName()+".chal");\r
248                                 if(f.exists()) {\r
249                                         f.delete();\r
250                                 }\r
251                                 pw = new PrintWriter(new FileWriter(f));\r
252                                 try {\r
253                                         for(String prop : encodeds) {\r
254                                                 if(prop.startsWith("Challenge")) {\r
255                                                         pw.println(prop);\r
256                                                 }\r
257                                         }\r
258                                 } finally {\r
259                                         pw.close();\r
260                                 }\r
261                                 Chmod.to400.chmod(f);\r
262                         }\r
263                 } catch(Exception e) {\r
264                         throw new CadiException(e);\r
265                 }\r
266                 return true;\r
267         }\r
268         \r
269         public static void clear() {\r
270                 processed.clear();\r
271         }\r
272 \r
273 }\r