Remove Tabs, per Jococo
[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         String pwd = orig.getProperty(tag, def);
121         if(pwd==null) {
122             return;
123         } else if(pwd.startsWith("enc:")) {
124             pwd = orig.decrypt(pwd, true);
125         }
126         addEnc(tag,pwd);
127     }
128     
129     public void write() throws IOException {
130         if (props.size()==0) {
131             return;
132         }
133
134         if (file.exists()) {
135             System.out.println("Backing up " + file.getCanonicalPath());
136             File backup = File.createTempFile(file.getName()+'.', ".backup",dir);
137             file.renameTo(backup);
138         } else {
139             System.out.println("Creating new " + file.getCanonicalPath());
140         }
141         
142         // Append if not first
143         PrintWriter pw = new PrintWriter(new FileWriter(file));
144         try {
145             // Write a Header
146             for (int i=0;i<60;++i) {
147                 pw.print('#');
148             }
149             pw.println();
150             pw.println("# Properties Generated by AT&T Certificate Manager");
151             pw.print("#   by ");
152             pw.println(System.getProperty("user.name"));
153             pw.print("#   on ");
154             pw.println(Chrono.dateStamp());
155             pw.println("# @copyright 2019, AT&T");
156             for (int i=0;i<60;++i) {
157                 pw.print('#');
158             }
159             pw.println();
160             
161              for (Map.Entry<String,String> me : props.entrySet()) {
162                 String key = me.getKey();
163                     pw.print(key);
164                     pw.print('=');
165                     pw.println(me.getValue());
166             }
167         } finally {
168             pw.close();
169         }
170         Chmod.to644.chmod(file);
171     }
172     
173     public static void writeAll() throws IOException {
174         for(PropHolder ph : propHolders.values()) {
175             ph.write();
176         }
177     }
178     
179     @Override
180     public String toString() {
181         return file.getAbsolutePath() + ": " + props;
182     }
183 }