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.util.Chmod;
35 import org.onap.aaf.misc.env.util.Chrono;
37 import certman.v1_0.Artifacts.Artifact;
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 {
46 private Map<String,String> props;
48 private static boolean dirMessage = true;
49 protected final static Map<String,PropHolder> propHolders = new HashMap<>();
51 public static PropHolder get(Artifact arti, String suffix) throws IOException {
52 File dir = new File(arti.getDir());
55 System.out.println("Writing to " + dir.getCanonicalFile());
57 } else if (dir.mkdirs()) {
59 System.out.println("Created directory " + dir.getCanonicalFile());
62 throw new IOException("Unable to create or write to " + dir.getCanonicalPath());
65 File file = new File(dir,arti.getNs()+'.'+suffix);
67 PropHolder ph = propHolders.get(file.getAbsolutePath());
69 ph = new PropHolder(dir,file,new File(dir,arti.getNs()+".keyfile"));
70 propHolders.put(file.getAbsolutePath(), ph);
75 private PropHolder(File dir, File file, File keyfile) throws IOException {
78 this.keyfile = keyfile;
80 props = new TreeMap<>();
83 public String getPath() {
84 return file.getAbsolutePath();
87 public File getDir() {
91 public String getKeyPath() {
92 return keyfile.getAbsolutePath();
95 public String add(final String tag, final String value) {
96 final String rv = value==null?"":value;
101 public String add(final String tag, Access orig, final String def) {
102 return add(tag, orig.getProperty(tag, def));
105 public String addEnc(final String tag, final String value) throws IOException {
110 if(symm==null) { // Lazy Instantiations... on a few PropFiles have Security
111 symm = ArtifactDir.getSymm(keyfile);
113 rv = "enc:"+symm.enpass(value);
119 public void addEnc(final String tag, Access orig, final String def) throws IOException {
120 String pwd = orig.getProperty(tag, def);
123 } else if(pwd.startsWith("enc:")) {
124 pwd = orig.decrypt(pwd, true);
129 public void write() throws IOException {
130 if (props.size()==0) {
135 System.out.println("Backing up " + file.getCanonicalPath());
136 File backup = File.createTempFile(file.getName()+'.', ".backup",dir);
137 file.renameTo(backup);
139 System.out.println("Creating new " + file.getCanonicalPath());
142 // Append if not first
143 PrintWriter pw = new PrintWriter(new FileWriter(file));
146 for (int i=0;i<60;++i) {
150 pw.println("# Properties Generated by AT&T Certificate Manager");
152 pw.println(System.getProperty("user.name"));
154 pw.println(Chrono.dateStamp());
155 pw.println("# @copyright 2016, AT&T");
156 for (int i=0;i<60;++i) {
161 for (Map.Entry<String,String> me : props.entrySet()) {
162 String key = me.getKey();
163 //if ( key.startsWith("cm_")
164 // || key.startsWith(Config.HOSTNAME)
165 // || key.startsWith("aaf")
166 // || key.startsWith("cadi")
167 // || key.startsWith("Challenge")
171 pw.println(me.getValue());
177 Chmod.to644.chmod(file);
180 public static void writeAll() throws IOException {
181 for(PropHolder ph : propHolders.values()) {
187 public String toString() {
188 return file.getAbsolutePath() + ": " + props;