0f9666caf0358205a45787be9b89cf42bf423ad8
[aaf/authz.git] / cadi / aaf / src / main / java / org / onap / aaf / cadi / configure / PropHolder.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.FileWriter;
26 import java.io.IOException;
27 import java.io.PrintWriter;
28 import java.util.HashMap;
29 import java.util.Map;
30 import java.util.TreeMap;
31
32 import org.onap.aaf.cadi.Access;
33 import org.onap.aaf.cadi.Symm;
34 import org.onap.aaf.cadi.util.Chmod;
35 import org.onap.aaf.misc.env.util.Chrono;
36
37 import certman.v1_0.Artifacts.Artifact;
38
39 // Doing this because there are lots of lists spread out in this model...
40     // we want these to be unique.
41 public class PropHolder {
42         private File dir;
43         private File file;
44         private File keyfile;
45         private Symm symm;
46         private Map<String,String> props;
47
48         private static boolean dirMessage = true;
49     protected final static Map<String,PropHolder> propHolders = new HashMap<>();
50
51     public static PropHolder get(Artifact arti, String suffix) throws IOException {
52         File dir = new File(arti.getDir());
53         if (dir.exists()) {
54                 if(dirMessage) {
55                         System.out.println("Writing to " + dir.getCanonicalFile());
56                 }
57         } else if (dir.mkdirs()) {
58                 if(dirMessage) {
59                         System.out.println("Created directory " + dir.getCanonicalFile());
60                 }
61         } else {
62             throw new IOException("Unable to create or write to " + dir.getCanonicalPath());
63         }
64         dirMessage = false;
65         File file = new File(dir,arti.getNs()+'.'+suffix);
66
67         PropHolder ph = propHolders.get(file.getAbsolutePath());
68         if(ph == null) {
69                 ph = new PropHolder(dir,file,new File(dir,arti.getNs()+".keyfile"));
70                 propHolders.put(file.getAbsolutePath(), ph);
71         } 
72         return ph;
73     }
74         
75         private PropHolder(File dir, File file, File keyfile) throws IOException {
76                 this.dir = dir;
77                 this.file = file;
78                 this.keyfile = keyfile;
79                 symm = null;
80                 props = new TreeMap<>();
81         }
82         
83         public String getPath() {
84                 return file.getAbsolutePath();
85         }
86         
87         public File getDir() {
88                 return dir;
89         }
90
91         public String getKeyPath() {
92                 return keyfile.getAbsolutePath();
93         }
94
95         public String add(final String tag, final String value) {
96                 final String rv = value==null?"":value;
97                 props.put(tag, rv);
98                 return rv;
99         }
100
101         public String add(final String tag, Access orig, final String def) {
102                 return add(tag, orig.getProperty(tag, def));
103         }
104
105         public String addEnc(final String tag, final String value) throws IOException {
106                 String rv;
107                 if(value==null) {
108                         rv = "";
109                 } else {
110                         if(symm==null) { // Lazy Instantiations... on a few PropFiles have Security
111                                 symm = ArtifactDir.getSymm(keyfile);
112                         }
113                         rv = "enc:"+symm.enpass(value);
114                 }
115                 props.put(tag, rv);
116                 return rv;
117         }
118
119         public void addEnc(final String tag, Access orig, final String def) throws IOException {
120                 addEnc(tag,orig.getProperty(tag, def));
121         }
122         
123         public void write() throws IOException {
124         if (props.size()==0) {
125             return;
126         }
127
128         if (file.exists()) {
129                 System.out.println("Backing up " + file.getCanonicalPath());
130             File backup = File.createTempFile(file.getName()+'.', ".backup",dir);
131             file.renameTo(backup);
132         } else {
133                 System.out.println("Creating new " + file.getCanonicalPath());
134         }
135         
136         // Append if not first
137         PrintWriter pw = new PrintWriter(new FileWriter(file));
138         try {
139             // Write a Header
140             for (int i=0;i<60;++i) {
141                 pw.print('#');
142             }
143             pw.println();
144             pw.println("# Properties Generated by AT&T Certificate Manager");
145             pw.print("#   by ");
146             pw.println(System.getProperty("user.name"));
147             pw.print("#   on ");
148             pw.println(Chrono.dateStamp());
149             pw.println("# @copyright 2016, AT&T");
150             for (int i=0;i<60;++i) {
151                 pw.print('#');
152             }
153             pw.println();
154             
155              for (Map.Entry<String,String> me : props.entrySet()) {
156                 String key = me.getKey();
157                 //if (    key.startsWith("cm_") 
158 //                    || key.startsWith(Config.HOSTNAME)
159 //                    || key.startsWith("aaf")
160 //                    || key.startsWith("cadi")
161 //                    || key.startsWith("Challenge")
162 //                    ) {
163                     pw.print(key);
164                     pw.print('=');
165                     pw.println(me.getValue());
166 //                }
167             }
168         } finally {
169             pw.close();
170         }
171         Chmod.to644.chmod(file);
172         }
173         
174         public static void writeAll() throws IOException {
175                 for(PropHolder ph : propHolders.values()) {
176                         ph.write();
177                 }
178         }
179         
180         @Override
181         public String toString() {
182                 return file.getAbsolutePath() + ": " + props;
183         }
184 }