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.FileOutputStream;
26 import java.io.IOException;
27 import java.io.PrintStream;
28 import java.security.KeyStore;
29 import java.util.HashMap;
32 import org.onap.aaf.cadi.CadiException;
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.Trans;
38 import certman.v1_0.Artifacts.Artifact;
39 import certman.v1_0.CertInfo;
41 public abstract class ArtifactDir implements PlaceArtifact {
43 protected static final String C_R = "\n";
46 // This checks for multiple passes of Dir on the same objects. Run clear after done.
47 protected final static Map<String,Object> processed = new HashMap<>();
48 private static final Map<String, Symm> symms = new HashMap<>();
51 * Note: Derived Classes should ALWAYS call "super.place(cert,arti)" first, and
52 * then "placeProperties(arti)" just after they implement
55 public final boolean place(Trans trans, CertInfo certInfo, Artifact arti, String machine) throws CadiException {
59 PropHolder cred = PropHolder.get(arti,"cred.props");
61 // Obtain/setup directory as required
62 dir = new File(arti.getDir());
63 if (processed.get("dir")==null) {
65 Chmod.to755.chmod(dir);
67 throw new CadiException("Could not create " + dir);
73 StringBuilder issuers = new StringBuilder();
74 for (String dn : certInfo.getCaIssuerDNs()) {
82 cred.add(Config.CADI_X509_ISSUERS,issuers.toString());
84 cred.addEnc("Challenge", certInfo.getChallenge());
87 _place(trans, certInfo,arti);
89 processed.put("dir",dir);
91 } catch (Exception e) {
92 throw new CadiException(e);
98 * Derived Classes implement this instead, so Dir can process first, and write any Properties last
102 * @throws CadiException
104 protected abstract boolean _place(Trans trans, CertInfo certInfo, Artifact arti) throws CadiException;
106 public static void write(File f, Chmod c, String ... data) throws IOException {
107 System.out.println("Writing file " + f.getCanonicalPath());
108 f.setWritable(true,true);
110 FileOutputStream fos = new FileOutputStream(f);
111 PrintStream ps = new PrintStream(fos);
113 for (String s : data) {
122 public static void write(File f, Chmod c, byte[] bytes) throws IOException {
123 System.out.println("Writing file " + f.getCanonicalPath());
124 f.setWritable(true,true);
126 FileOutputStream fos = new FileOutputStream(f);
135 public static void write(File f, Chmod c, KeyStore ks, char[] pass ) throws IOException, CadiException {
136 System.out.println("Writing file " + f.getCanonicalPath());
137 f.setWritable(true,true);
139 FileOutputStream fos = new FileOutputStream(f);
142 } catch (Exception e) {
143 throw new CadiException(e);
150 // Get the Symm associated with specific File (there can be several active at once)
151 public synchronized static final Symm getSymm(File f) throws IOException {
152 Symm symm = symms.get(f.getCanonicalPath());
155 write(f,Chmod.to400,Symm.keygen());
157 // System.out.println("Encryptor using " + f.getCanonicalPath());
159 symm = Symm.obtain(f);
160 symms.put(f.getCanonicalPath(),symm);
165 private void validate(Artifact a) throws CadiException {
166 StringBuilder sb = new StringBuilder();
167 if (a.getDir()==null) {
168 sb.append("File Artifacts require a path");
171 if (a.getNs()==null) {
175 sb.append("File Artifacts require an AAF Namespace");
179 throw new CadiException(sb.toString());
183 public static void clear() {