* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.file.Files;
import java.security.KeyPair;
import java.net.UnknownHostException;
import java.nio.file.Files;
import java.security.KeyPair;
public static final String PKCS12 = "pkcs12";
public static final String JKS = "jks";
private static final String SCRIPT="script";
public static final String PKCS12 = "pkcs12";
public static final String JKS = "jks";
private static final String SCRIPT="script";
private static final String CM_VER = "1.0";
public static final int PASS_SIZE = 24;
private static int TIMEOUT;
private static final String CM_VER = "1.0";
public static final int PASS_SIZE = 24;
private static int TIMEOUT;
private static RosettaDF<CertificateRequest> reqDF;
private static RosettaDF<CertInfo> certDF;
private static RosettaDF<Artifacts> artifactsDF;
private static RosettaDF<CertificateRequest> reqDF;
private static RosettaDF<CertInfo> certDF;
private static RosettaDF<Artifacts> artifactsDF;
private static ErrMessage errMsg;
private static Map<String,PlaceArtifact> placeArtifact;
private static RosettaEnv env;
private static ErrMessage errMsg;
private static Map<String,PlaceArtifact> placeArtifact;
private static RosettaEnv env;
private static List<String> CRED_TAGS = Arrays.asList(new String[] {
Config.CADI_KEYFILE,
Config.AAF_APPID, Config.AAF_APPPASS,
private static List<String> CRED_TAGS = Arrays.asList(new String[] {
Config.CADI_KEYFILE,
Config.AAF_APPID, Config.AAF_APPPASS,
if(access==null) {
for(Entry<Object, Object> es : System.getProperties().entrySet()) {
if(Config.CADI_PROP_FILES.equals(es.getKey())) {
if(access==null) {
for(Entry<Object, Object> es : System.getProperties().entrySet()) {
if(Config.CADI_PROP_FILES.equals(es.getKey())) {
// When using Config file, check if Cred Exists, and if not, work with Deployer.
if(access!=null && !"config".equals(args[0]) && access.getProperty(Config.AAF_APPPASS)==null && access.getProperty(Config.CADI_ALIAS)==null) {
// When using Config file, check if Cred Exists, and if not, work with Deployer.
if(access!=null && !"config".equals(args[0]) && access.getProperty(Config.AAF_APPPASS)==null && access.getProperty(Config.CADI_ALIAS)==null) {
if (args.length>1) {
if (!args[0].equals("keypairgen")) {
props.put(Config.AAF_APPID, args[1]);
if (args.length>1) {
if (!args[0].equals("keypairgen")) {
props.put(Config.AAF_APPID, args[1]);
System.out.println(" update <FQI> [<machine>]");
System.out.println(" delete <FQI> [<machine>]");
System.out.println(" copy <FQI> <machine> <newmachine>[,<newmachine>]*");
System.out.println(" update <FQI> [<machine>]");
System.out.println(" delete <FQI> [<machine>]");
System.out.println(" copy <FQI> <machine> <newmachine>[,<newmachine>]*");
System.out.println(" showpass <FQI> [<machine>]");
System.out.println(" check <FQI> [<machine>]");
System.out.println(" keypairgen <FQI>");
System.out.println(" showpass <FQI> [<machine>]");
System.out.println(" check <FQI> [<machine>]");
System.out.println(" keypairgen <FQI>");
System.out.println(" ** Type with no params for Tool Help");
System.out.println(" ** If using with Agent, preface with \"cadi\"");
System.out.println(" cadi <cadi tool params, see -?>");
System.out.println(" ** Type with no params for Tool Help");
System.out.println(" ** If using with Agent, preface with \"cadi\"");
System.out.println(" cadi <cadi tool params, see -?>");
reqDF = env.newDataFactory(CertificateRequest.class);
artifactsDF = env.newDataFactory(Artifacts.class);
certDF = env.newDataFactory(CertInfo.class);
configDF = env.newDataFactory(Configuration.class);
permDF = env.newDataFactory(Perms.class);
errMsg = new ErrMessage(env);
reqDF = env.newDataFactory(CertificateRequest.class);
artifactsDF = env.newDataFactory(Artifacts.class);
certDF = env.newDataFactory(CertInfo.class);
configDF = env.newDataFactory(Configuration.class);
permDF = env.newDataFactory(Perms.class);
errMsg = new ErrMessage(env);
placeArtifact = new HashMap<>();
placeArtifact.put(JKS, new PlaceArtifactInKeystore(JKS));
placeArtifact.put(PKCS12, new PlaceArtifactInKeystore(PKCS12));
placeArtifact.put(FILE, new PlaceArtifactInFiles());
placeArtifact.put(PRINT, new PlaceArtifactOnStream(System.out));
placeArtifact.put(SCRIPT, new PlaceArtifactScripts());
placeArtifact = new HashMap<>();
placeArtifact.put(JKS, new PlaceArtifactInKeystore(JKS));
placeArtifact.put(PKCS12, new PlaceArtifactInKeystore(PKCS12));
placeArtifact.put(FILE, new PlaceArtifactInFiles());
placeArtifact.put(PRINT, new PlaceArtifactOnStream(System.out));
placeArtifact.put(SCRIPT, new PlaceArtifactScripts());
lhost=Config.AAF_LOCATE_URL_TAG;
}
value = rph.replacements(AGENT_LOAD_URLS,
lhost=Config.AAF_LOCATE_URL_TAG;
}
value = rph.replacements(AGENT_LOAD_URLS,
arti.setMechid(mechID!=null?mechID:AAFSSO.cons.readLine("AppID: "));
arti.setMachine(machine!=null?machine:AAFSSO.cons.readLine("Machine (%s): ",InetAddress.getLocalHost().getHostName()));
arti.setCa(AAFSSO.cons.readLine("CA: (%s): ","aaf"));
arti.setMechid(mechID!=null?mechID:AAFSSO.cons.readLine("AppID: "));
arti.setMachine(machine!=null?machine:AAFSSO.cons.readLine("Machine (%s): ",InetAddress.getLocalHost().getHostName()));
arti.setCa(AAFSSO.cons.readLine("CA: (%s): ","aaf"));
String resp = AAFSSO.cons.readLine("Types [file,pkcs12,jks,script] (%s): ", PKCS12);
for (String s : Split.splitTrim(',', resp)) {
arti.getType().add(s);
String resp = AAFSSO.cons.readLine("Types [file,pkcs12,jks,script] (%s): ", PKCS12);
for (String s : Split.splitTrim(',', resp)) {
arti.getType().add(s);
arti.setOsUser(AAFSSO.cons.readLine("OS User (%s): ", System.getProperty("user.name")));
arti.setRenewDays(Integer.parseInt(AAFSSO.cons.readLine("Renewal Days (%s):", "30")));
arti.setNotification(toNotification(AAFSSO.cons.readLine("Notification (mailto owner):", "")));
arti.setOsUser(AAFSSO.cons.readLine("OS User (%s): ", System.getProperty("user.name")));
arti.setRenewDays(Integer.parseInt(AAFSSO.cons.readLine("Renewal Days (%s):", "30")));
arti.setNotification(toNotification(AAFSSO.cons.readLine("Notification (mailto owner):", "")));
TimeTaken tt = trans.start("Create Artifact", Env.REMOTE);
try {
Future<Artifacts> future = aafcon.client(CM_VER).create("/cert/artifacts", artifactsDF, artifacts);
TimeTaken tt = trans.start("Create Artifact", Env.REMOTE);
try {
Future<Artifacts> future = aafcon.client(CM_VER).create("/cert/artifacts", artifactsDF, artifacts);
private static void readArtifact(Trans trans, AAFCon<?> aafcon, Deque<String> cmds) throws Exception {
String mechID = fqi(cmds);
private static void readArtifact(Trans trans, AAFCon<?> aafcon, Deque<String> cmds) throws Exception {
String mechID = fqi(cmds);
try {
Future<Artifacts> future = aafcon.client(CM_VER)
.read("/cert/artifacts/"+mechID+'/'+machine, artifactsDF,"Authorization","Bearer " + trans.getProperty("oauth_token"));
try {
Future<Artifacts> future = aafcon.client(CM_VER)
.read("/cert/artifacts/"+mechID+'/'+machine, artifactsDF,"Authorization","Bearer " + trans.getProperty("oauth_token"));
- AAFSSO.cons.printf("AppID: %s\n",a.getMechid());
- AAFSSO.cons.printf(" Sponsor: %s\n",a.getSponsor());
- AAFSSO.cons.printf("Machine: %s\n",a.getMachine());
- AAFSSO.cons.printf("CA: %s\n",a.getCa());
+ AAFSSO.cons.printf("AppID: %s\n",a.getMechid());
+ AAFSSO.cons.printf(" Sponsor: %s\n",a.getSponsor());
+ AAFSSO.cons.printf("Machine: %s\n",a.getMachine());
+ AAFSSO.cons.printf("CA: %s\n",a.getCa());
AAFSSO.cons.printf("Directory: %s\n",a.getDir());
AAFSSO.cons.printf("O/S User: %s\n",a.getOsUser());
AAFSSO.cons.printf("Renew Days: %d\n",a.getRenewDays());
AAFSSO.cons.printf("Directory: %s\n",a.getDir());
AAFSSO.cons.printf("O/S User: %s\n",a.getOsUser());
AAFSSO.cons.printf("Renew Days: %d\n",a.getRenewDays());
private static void copyArtifact(Trans trans, AAFCon<?> aafcon, Deque<String> cmds) throws Exception {
String mechID = fqi(cmds);
String machine = machine(cmds);
private static void copyArtifact(Trans trans, AAFCon<?> aafcon, Deque<String> cmds) throws Exception {
String mechID = fqi(cmds);
String machine = machine(cmds);
try {
Future<Artifacts> future = aafcon.client(CM_VER)
.read("/cert/artifacts/"+mechID+'/'+machine, artifactsDF);
try {
Future<Artifacts> future = aafcon.client(CM_VER)
.read("/cert/artifacts/"+mechID+'/'+machine, artifactsDF);
try {
Future<Artifacts> fread = aafcon.client(CM_VER)
.read("/cert/artifacts/"+mechID+'/'+machine, artifactsDF);
try {
Future<Artifacts> fread = aafcon.client(CM_VER)
.read("/cert/artifacts/"+mechID+'/'+machine, artifactsDF);
if (fread.get(TIMEOUT)) {
Artifacts artifacts = new Artifacts();
for (Artifact a : fread.value.getArtifact()) {
Artifact arti = new Artifact();
artifacts.getArtifact().add(arti);
if (fread.get(TIMEOUT)) {
Artifacts artifacts = new Artifacts();
for (Artifact a : fread.value.getArtifact()) {
Artifact arti = new Artifact();
artifacts.getArtifact().add(arti);
AAFSSO.cons.printf("For %s on %s\n", a.getMechid(),a.getMachine());
arti.setMechid(a.getMechid());
arti.setMachine(a.getMachine());
AAFSSO.cons.printf("For %s on %s\n", a.getMechid(),a.getMachine());
arti.setMechid(a.getMechid());
arti.setMachine(a.getMachine());
String resp = AAFSSO.cons.readLine("Types [file,jks,pkcs12] (%s): ", sb);
for (String s : Split.splitTrim(',', resp)) {
arti.getType().add(s);
String resp = AAFSSO.cons.readLine("Types [file,jks,pkcs12] (%s): ", sb);
for (String s : Split.splitTrim(',', resp)) {
arti.getType().add(s);
arti.setOsUser(AAFSSO.cons.readLine("OS User (%s): ", a.getOsUser()));
arti.setRenewDays(Integer.parseInt(AAFSSO.cons.readLine("Renew Days (%s):", a.getRenewDays())));
arti.setNotification(toNotification(AAFSSO.cons.readLine("Notification (%s):", a.getNotification())));
arti.setOsUser(AAFSSO.cons.readLine("OS User (%s): ", a.getOsUser()));
arti.setRenewDays(Integer.parseInt(AAFSSO.cons.readLine("Renew Days (%s):", a.getRenewDays())));
arti.setNotification(toNotification(AAFSSO.cons.readLine("Notification (%s):", a.getNotification())));
}
if (artifacts.getArtifact().size()==0) {
AAFSSO.cons.printf("Artifact for %s %s does not exist", mechID, machine);
}
if (artifacts.getArtifact().size()==0) {
AAFSSO.cons.printf("Artifact for %s %s does not exist", mechID, machine);
private static void deleteArtifact(Trans trans, AAFCon<?> aafcon, Deque<String> cmds) throws Exception {
String mechid = fqi(cmds);
String machine = machine(cmds);
private static void deleteArtifact(Trans trans, AAFCon<?> aafcon, Deque<String> cmds) throws Exception {
String mechid = fqi(cmds);
String machine = machine(cmds);
TimeTaken tt = trans.start("Delete Artifact", Env.REMOTE);
try {
Future<Void> future = aafcon.client(CM_VER)
.delete("/cert/artifacts/"+mechid+"/"+machine,"application/json" );
TimeTaken tt = trans.start("Delete Artifact", Env.REMOTE);
try {
Future<Void> future = aafcon.client(CM_VER)
.delete("/cert/artifacts/"+mechid+"/"+machine,"application/json" );
if (future.get(TIMEOUT)) {
trans.info().printf("Call to AAF Certman successful %s, %s",mechid,machine);
} else {
if (future.get(TIMEOUT)) {
trans.info().printf("Call to AAF Certman successful %s, %s",mechid,machine);
} else {
private static boolean placeCerts(Trans trans, AAFCon<?> aafcon, Deque<String> cmds) throws Exception {
boolean rv = false;
private static boolean placeCerts(Trans trans, AAFCon<?> aafcon, Deque<String> cmds) throws Exception {
boolean rv = false;
TimeTaken tt = trans.start("Place Artifact", Env.REMOTE);
try {
Future<Artifacts> acf = aafcon.client(CM_VER)
TimeTaken tt = trans.start("Place Artifact", Env.REMOTE);
try {
Future<Artifacts> acf = aafcon.client(CM_VER)
CertificateRequest cr = new CertificateRequest();
cr.setMechid(a.getMechid());
cr.setSponsor(a.getSponsor());
CertificateRequest cr = new CertificateRequest();
cr.setMechid(a.getMechid());
cr.setSponsor(a.getSponsor());
}
Future<String> f = aafcon.client(CM_VER)
.updateRespondString("/cert/" + a.getCa()+"?withTrust",reqDF, cr);
}
Future<String> f = aafcon.client(CM_VER)
.updateRespondString("/cert/" + a.getCa()+"?withTrust",reqDF, cr);
File f = new File(dir,a.getNs()+".keyfile");
if (f.exists()) {
Symm symm = ArtifactDir.getSymm(f);
File f = new File(dir,a.getNs()+".keyfile");
if (f.exists()) {
Symm symm = ArtifactDir.getSymm(f);
for (Iterator<Entry<Object,Object>> iter = props.entrySet().iterator(); iter.hasNext();) {
Entry<Object,Object> en = iter.next();
if (en.getValue().toString().startsWith("enc:")) {
for (Iterator<Entry<Object,Object>> iter = props.entrySet().iterator(); iter.hasNext();) {
Entry<Object,Object> en = iter.next();
if (en.getValue().toString().startsWith("enc:")) {
private static void keypairGen(final Trans trans, final PropAccess access, final Deque<String> cmds) throws IOException {
final String fqi = fqi(cmds);
final String ns = FQI.reverseDomain(fqi);
File dir = new File(access.getProperty(Config.CADI_ETCDIR,".")); // default to current Directory
File f = new File(dir,ns+".key");
private static void keypairGen(final Trans trans, final PropAccess access, final Deque<String> cmds) throws IOException {
final String fqi = fqi(cmds);
final String ns = FQI.reverseDomain(fqi);
File dir = new File(access.getProperty(Config.CADI_ETCDIR,".")); // default to current Directory
File f = new File(dir,ns+".key");
if (f.exists()) {
String line = AAFSSO.cons.readLine("%s exists. Overwrite? (y/n): ", f.getCanonicalPath());
if (!"Y".equalsIgnoreCase(line)) {
if (f.exists()) {
String line = AAFSSO.cons.readLine("%s exists. Overwrite? (y/n): ", f.getCanonicalPath());
if (!"Y".equalsIgnoreCase(line)) {
KeyPair kp = Factory.generateKeyPair(trans);
ArtifactDir.write(f, Chmod.to400, Factory.toString(trans, kp.getPrivate()));
System.out.printf("Wrote %s\n", f.getCanonicalFile());
KeyPair kp = Factory.generateKeyPair(trans);
ArtifactDir.write(f, Chmod.to400, Factory.toString(trans, kp.getPrivate()));
System.out.printf("Wrote %s\n", f.getCanonicalFile());
ArtifactDir.write(f, Chmod.to644, Factory.toString(trans, kp.getPublic()));
System.out.printf("Wrote %s\n", f.getCanonicalFile());
}
ArtifactDir.write(f, Chmod.to644, Factory.toString(trans, kp.getPublic()));
System.out.printf("Wrote %s\n", f.getCanonicalFile());
}
private static void config(Trans trans, PropAccess propAccess, String[] args, Deque<String> cmds) throws Exception {
TimeTaken tt = trans.start("Get Configuration", Env.REMOTE);
try {
private static void config(Trans trans, PropAccess propAccess, String[] args, Deque<String> cmds) throws Exception {
TimeTaken tt = trans.start("Get Configuration", Env.REMOTE);
try {
for (String tag : LOC_TAGS) {
loc.add(tag, getProperty(propAccess, trans, false, tag, "%s: ",tag));
}
for (String tag : LOC_TAGS) {
loc.add(tag, getProperty(propAccess, trans, false, tag, "%s: ",tag));
}
// load all properties that are already setup.
Map<String, String> aaf_urls = loadURLs(propAccess);
for(Entry<String, String> es : aaf_urls.entrySet()) {
app.add(es.getKey(), es.getValue());
}
// load all properties that are already setup.
Map<String, String> aaf_urls = loadURLs(propAccess);
for(Entry<String, String> es : aaf_urls.entrySet()) {
app.add(es.getKey(), es.getValue());
}
app.add(Config.AAF_LOCATE_URL, Config.getAAFLocateUrl(propAccess));
app.add(Config.AAF_ENV,propAccess, "DEV");
String release = propAccess.getProperty(Config.AAF_DEPLOYED_VERSION);
app.add(Config.AAF_LOCATE_URL, Config.getAAFLocateUrl(propAccess));
app.add(Config.AAF_ENV,propAccess, "DEV");
String release = propAccess.getProperty(Config.AAF_DEPLOYED_VERSION);
app.add(Config.AAF_APPID, fqi);
String cts = propAccess.getProperty(Config.CADI_TRUSTSTORE);
app.add(Config.AAF_APPID, fqi);
String cts = propAccess.getProperty(Config.CADI_TRUSTSTORE);
if (fcpf.exists()) {
int lastSep = cts.lastIndexOf(File.pathSeparator);
origTruststore = new File(fcpf.getParentFile(),lastSep>=0?cts.substring(lastSep):cts);
if (fcpf.exists()) {
int lastSep = cts.lastIndexOf(File.pathSeparator);
origTruststore = new File(fcpf.getParentFile(),lastSep>=0?cts.substring(lastSep):cts);
}
if (!newTruststore.exists() && origTruststore.exists()) {
Files.copy(origTruststore.toPath(), newTruststore.toPath());
}
if (!newTruststore.exists() && origTruststore.exists()) {
Files.copy(origTruststore.toPath(), newTruststore.toPath());
System.out.println("New Truststore is " + newTruststore);
cred.add(Config.CADI_TRUSTSTORE, newTruststore.getCanonicalPath());
cred.add(Config.CADI_TRUSTSTORE_PASSWORD, "changeit" /* Java default */);
System.out.println("New Truststore is " + newTruststore);
cred.add(Config.CADI_TRUSTSTORE, newTruststore.getCanonicalPath());
cred.add(Config.CADI_TRUSTSTORE_PASSWORD, "changeit" /* Java default */);
for (int pfi = propFiles.length-1;pfi>=0;--pfi) {
String f = propFiles[pfi];
System.out.format("Reading %s\n",f);
for (int pfi = propFiles.length-1;pfi>=0;--pfi) {
String f = propFiles[pfi];
System.out.format("Reading %s\n",f);
* 0 - Check Complete, nothing to do
* 1 - General Error
* 2 - Error for specific Artifact - read check.msg
* 10 - Certificate Updated - check.msg is email content
* 0 - Check Complete, nothing to do
* 1 - General Error
* 2 - Error for specific Artifact - read check.msg
* 10 - Certificate Updated - check.msg is email content
Future<Artifacts> acf = aafcon.client(CM_VER)
.read("/cert/artifacts/"+mechID+'/'+machine, artifactsDF);
if (acf.get(TIMEOUT)) {
Future<Artifacts> acf = aafcon.client(CM_VER)
.read("/cert/artifacts/"+mechID+'/'+machine, artifactsDF);
if (acf.get(TIMEOUT)) {
if ((prop=trans.getProperty(Config.CADI_KEYFILE))==null ||
!(f=new File(prop)).exists()) {
trans.error().printf("Keyfile must exist to check Certificates for %s on %s",
if ((prop=trans.getProperty(Config.CADI_KEYFILE))==null ||
!(f=new File(prop)).exists()) {
trans.error().printf("Keyfile must exist to check Certificates for %s on %s",
fis = new FileInputStream(ksf);
try {
ks.load(fis,symm.depass(ksps).toCharArray());
fis = new FileInputStream(ksf);
try {
ks.load(fis,symm.depass(ksps).toCharArray());
renew.setTime(cert.getNotAfter());
renew.add(GregorianCalendar.DAY_OF_MONTH,-1*a.getRenewDays());
if (renew.after(now)) {
renew.setTime(cert.getNotAfter());
renew.add(GregorianCalendar.DAY_OF_MONTH,-1*a.getRenewDays());
if (renew.after(now)) {
a.getMechid(), a.getMachine(),Chrono.dateOnlyStamp(now),cert.getNotAfter(),Chrono.dateOnlyStamp(renew));
trans.info().log(msg);
exitCode = 0; // OK
} else {
a.getMechid(), a.getMachine(),Chrono.dateOnlyStamp(now),cert.getNotAfter(),Chrono.dateOnlyStamp(renew));
trans.info().log(msg);
exitCode = 0; // OK
} else {
- trans.info().printf("X509Certificate for %s on %s expiration, %s, needs Renewal.\n",
+ trans.info().printf("X509Certificate for %s on %s expiration, %s, needs Renewal.\n",
a.getMechid(), a.getMachine(),cert.getNotAfter());
cmds.offerLast(mechID);
cmds.offerLast(machine);
if (placeCerts(trans,aafcon,cmds)) {
a.getMechid(), a.getMachine(),cert.getNotAfter());
cmds.offerLast(mechID);
cmds.offerLast(machine);
if (placeCerts(trans,aafcon,cmds)) {
a.getMechid(), a.getMachine());
exitCode = 10; // Refreshed
} else {
a.getMechid(), a.getMachine());
exitCode = 10; // Refreshed
} else {
- msg = String.format("X509Certificate for %s on %s attempted renewal, but failed. Immediate Investigation is required!\n",
+ msg = String.format("X509Certificate for %s on %s attempted renewal, but failed. Immediate Investigation is required!\n",
a.getMechid(), a.getMachine());
exitCode = 1; // Error Renewing
}
a.getMechid(), a.getMachine());
exitCode = 1; // Error Renewing
}