2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
22 package org.onap.aaf.cadi.configure;
25 import java.io.FileWriter;
26 import java.io.IOException;
27 import java.io.PrintWriter;
28 import java.util.HashMap;
30 import java.util.TreeMap;
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;
38 import certman.v1_0.Artifacts.Artifact;
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 {
47 private Map<String,String> props;
49 private static boolean dirMessage = true;
50 protected final static Map<String,PropHolder> propHolders = new HashMap<>();
52 public static PropHolder get(Artifact arti, String suffix) throws IOException {
53 File dir = new File(arti.getDir());
56 System.out.println("Writing to " + dir.getCanonicalFile());
58 } else if (dir.mkdirs()) {
60 System.out.println("Created directory " + dir.getCanonicalFile());
63 throw new IOException("Unable to create or write to " + dir.getCanonicalPath());
66 File file = new File(dir,arti.getNs()+'.'+suffix);
68 PropHolder ph = propHolders.get(file.getAbsolutePath());
70 ph = new PropHolder(dir,file,new File(dir,arti.getNs()+".keyfile"));
71 propHolders.put(file.getAbsolutePath(), ph);
76 private PropHolder(File dir, File file, File keyfile) throws IOException {
79 this.keyfile = keyfile;
81 props = new TreeMap<>();
84 public String getPath() {
85 return file.getAbsolutePath();
88 public File getDir() {
92 public String getKeyPath() {
93 return keyfile.getAbsolutePath();
96 public void add(final String tag, final String value) {
100 props.put(tag, value);
104 public void add(final String tag, Access orig, final String def) {
105 add(tag, orig.getProperty(tag, def));
108 public void addEnc(final String tag, final String value) throws IOException {
112 if(symm==null) { // Lazy Instantiations... on a few PropFiles have Security
113 symm = ArtifactDir.getSymm(keyfile);
115 props.put(tag, "enc:"+symm.enpass(value));
119 public void addEnc(final String tag, Access orig, final String def) throws IOException {
120 addEnc(tag,orig.getProperty(tag, def));
123 public void write() throws IOException {
124 if (props.size()==0) {
129 System.out.println("Backing up " + file.getCanonicalPath());
130 File backup = File.createTempFile(file.getName()+'.', ".backup",dir);
131 file.renameTo(backup);
133 System.out.println("Creating new " + file.getCanonicalPath());
136 // Append if not first
137 PrintWriter pw = new PrintWriter(new FileWriter(file));
140 for (int i=0;i<60;++i) {
144 pw.println("# Properties Generated by AT&T Certificate Manager");
146 pw.println(System.getProperty("user.name"));
148 pw.println(Chrono.dateStamp());
149 pw.println("# @copyright 2016, AT&T");
150 for (int i=0;i<60;++i) {
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")
165 pw.println(me.getValue());
171 Chmod.to644.chmod(file);
174 public static void writeAll() throws IOException {
175 for(PropHolder ph : propHolders.values()) {
181 public String toString() {
182 return file.getAbsolutePath() + ": " + props;