Remove Tabs, per Jococo
[aaf/authz.git] / cadi / aaf / src / main / java / org / onap / aaf / cadi / configure / ArtifactDir.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  * ===========================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END====================================================
19  *
20  */
21
22 package org.onap.aaf.cadi.configure;
23
24 import java.io.File;
25 import java.io.FileOutputStream;
26 import java.io.IOException;
27 import java.io.PrintStream;
28 import java.security.KeyStore;
29 import java.util.HashMap;
30 import java.util.Map;
31
32 import org.onap.aaf.cadi.CadiException;
33 import org.onap.aaf.cadi.Symm;
34 import org.onap.aaf.cadi.config.Config;
35 import org.onap.aaf.cadi.util.Chmod;
36 import org.onap.aaf.misc.env.Trans;
37
38 import certman.v1_0.Artifacts.Artifact;
39 import certman.v1_0.CertInfo;
40
41 public abstract class ArtifactDir implements PlaceArtifact {
42
43     protected static final String C_R = "\n";
44     protected File dir;
45     
46     // This checks for multiple passes of Dir on the same objects.  Run clear after done.
47     protected final static Map<String,Object> processed = new HashMap<>();
48     private static final Map<String, Symm> symms = new HashMap<>();
49     
50     /**
51      * Note:  Derived Classes should ALWAYS call "super.place(cert,arti)" first, and 
52      * then "placeProperties(arti)" just after they implement
53      */
54     @Override
55     public final boolean place(Trans trans, CertInfo certInfo, Artifact arti, String machine) throws CadiException {
56         validate(arti);
57         
58         try {
59             PropHolder cred = PropHolder.get(arti,"cred.props");
60
61             // Obtain/setup directory as required
62             dir = new File(arti.getDir());
63             if (processed.get("dir")==null) {
64                 if (!dir.exists()) {
65                     Chmod.to755.chmod(dir);
66                     if (!dir.mkdirs()) {
67                         throw new CadiException("Could not create " + dir);
68                     }
69                 }
70                 
71                 // Obtain Issuers
72                 boolean first = true;
73                 StringBuilder issuers = new StringBuilder();
74                 for (String dn : certInfo.getCaIssuerDNs()) {
75                     if (first) {
76                         first=false;
77                     } else {
78                         issuers.append(':');
79                     }
80                     issuers.append(dn);
81                 }
82                 cred.add(Config.CADI_X509_ISSUERS,issuers.toString());
83
84                 cred.addEnc("Challenge", certInfo.getChallenge());
85             }
86                 
87             _place(trans, certInfo,arti);
88             
89             processed.put("dir",dir);
90
91         } catch (Exception e) {
92             throw new CadiException(e);
93         }
94         return true;
95     }
96
97     /**
98      * Derived Classes implement this instead, so Dir can process first, and write any Properties last
99      * @param cert
100      * @param arti
101      * @return
102      * @throws CadiException
103      */
104     protected abstract boolean _place(Trans trans, CertInfo certInfo, Artifact arti) throws CadiException;
105
106     public static void write(File f, Chmod c, String ... data) throws IOException {
107         System.out.println("Writing file " + f.getCanonicalPath());
108         f.setWritable(true,true);
109         
110         FileOutputStream fos = new FileOutputStream(f);
111         PrintStream ps = new PrintStream(fos);
112         try {
113             for (String s : data) {
114                 ps.print(s);
115             }
116         } finally {
117             ps.close();
118             c.chmod(f);
119         }
120     }
121
122     public static void write(File f, Chmod c, byte[] bytes) throws IOException {
123         System.out.println("Writing file " + f.getCanonicalPath());
124         f.setWritable(true,true);
125         
126         FileOutputStream fos = new FileOutputStream(f);
127         try {
128             fos.write(bytes);
129         } finally {
130             fos.close();
131             c.chmod(f);
132         }
133     }
134     
135     public static void write(File f, Chmod c, KeyStore ks, char[] pass ) throws IOException, CadiException {
136         System.out.println("Writing file " + f.getCanonicalPath());
137         f.setWritable(true,true);
138         
139         FileOutputStream fos = new FileOutputStream(f);
140         try {
141             ks.store(fos, pass);
142         } catch (Exception e) {
143             throw new CadiException(e);
144         } finally {
145             fos.close();
146             c.chmod(f);
147         }
148     }
149
150     // Get the Symm associated with specific File (there can be several active at once)
151     public synchronized static final Symm getSymm(File f) throws IOException {
152         Symm symm = symms.get(f.getCanonicalPath());
153         if(symm==null) {
154             if (!f.exists()) {
155                 write(f,Chmod.to400,Symm.keygen());
156 //            } else {
157 //                System.out.println("Encryptor using " + f.getCanonicalPath());
158             }
159             symm = Symm.obtain(f); 
160             symms.put(f.getCanonicalPath(),symm);
161         }
162         return symm;
163     }
164
165     private void validate(Artifact a) throws CadiException {
166         StringBuilder sb = new StringBuilder();
167         if (a.getDir()==null) {
168             sb.append("File Artifacts require a path");
169         }
170
171         if (a.getNs()==null) {
172             if (sb.length()>0) {
173                 sb.append('\n');
174             }
175             sb.append("File Artifacts require an AAF Namespace");
176         }
177         
178         if (sb.length()>0) {
179             throw new CadiException(sb.toString());
180         }
181     }
182
183     public static void clear() {
184         processed.clear();
185     }
186
187 }