Refactor Client Config
[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.config.Config;
35 import org.onap.aaf.cadi.util.Chmod;
36 import org.onap.aaf.misc.env.util.Chrono;
37
38 import certman.v1_0.Artifacts.Artifact;
39
40 // Doing this because there are lots of lists spread out in this model...
41     // we want these to be unique.
42 public class PropHolder {
43         private File dir;
44         private File file;
45         private File keyfile;
46         private Symm symm;
47         private Map<String,String> props;
48
49         private static boolean dirMessage = true;
50     protected final static Map<String,PropHolder> propHolders = new HashMap<>();
51
52     public static PropHolder get(Artifact arti, String suffix) throws IOException {
53         File dir = new File(arti.getDir());
54         if (dir.exists()) {
55                 if(dirMessage) {
56                         System.out.println("Writing to " + dir.getCanonicalFile());
57                 }
58         } else if (dir.mkdirs()) {
59                 if(dirMessage) {
60                         System.out.println("Created directory " + dir.getCanonicalFile());
61                 }
62         } else {
63             throw new IOException("Unable to create or write to " + dir.getCanonicalPath());
64         }
65         dirMessage = false;
66         File file = new File(dir,arti.getNs()+'.'+suffix);
67
68         PropHolder ph = propHolders.get(file.getAbsolutePath());
69         if(ph == null) {
70                 ph = new PropHolder(dir,file,new File(dir,arti.getNs()+".keyfile"));
71                 propHolders.put(file.getAbsolutePath(), ph);
72         } 
73         return ph;
74     }
75         
76         private PropHolder(File dir, File file, File keyfile) throws IOException {
77                 this.dir = dir;
78                 this.file = file;
79                 this.keyfile = keyfile;
80                 symm = null;
81                 props = new TreeMap<>();
82         }
83         
84         public String getPath() {
85                 return file.getAbsolutePath();
86         }
87         
88         public File getDir() {
89                 return dir;
90         }
91
92         public String getKeyPath() {
93                 return keyfile.getAbsolutePath();
94         }
95
96         public void add(final String tag, final String value) {
97                 if(value==null) {
98                         props.put(tag,"");
99                 } else {
100                         props.put(tag, value);
101                 }
102         }
103
104         public void add(final String tag, Access orig, final String def) {
105                 add(tag, orig.getProperty(tag, def));
106         }
107
108         public void addEnc(final String tag, final String value) throws IOException {
109                 if(value==null) {
110                         props.put(tag,"");
111                 } else {
112                         if(symm==null) { // Lazy Instantiations... on a few PropFiles have Security
113                                 symm = ArtifactDir.getSymm(keyfile);
114                         }
115                         props.put(tag, "enc:"+symm.enpass(value));
116                 }
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 }