* 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
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* 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.
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 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 boolean doExit;
private static AAFCon<?> aafcon;
-
+
private static List<String> CRED_TAGS = Arrays.asList(new String[] {
Config.CADI_KEYFILE,
Config.AAF_APPID, Config.AAF_APPPASS,
} else {
try {
AAFSSO aafsso=null;
- PropAccess access=null;
-
+ PropAccess access=null;
+
String hasEtc = null;
for(String a : args) {
if(a.startsWith(Config.CADI_PROP_FILES)) {
configNoPasswd=true;
}
}
-
+
if(access==null) {
if(args.length>1 && args[1].contains("@")) {
String domain = FQI.reverseDomain(args[1]);
}
}
}
- }
+ }
}
-
+
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) {
- // not enough credentials to use Props. Use AAFSSO
+ // not enough credentials to use Props. Use AAFSSO
access = null;
}
if (args.length>1) {
if (!args[0].equals("keypairgen")) {
props.put(Config.AAF_APPID, args[1]);
- }
+ }
}
return props;
}
access = aafsso.access();
}
}
-
+
if (aafsso!=null && aafsso.loginOnly()) {
aafsso.setLogDefault();
aafsso.writeFiles();
access.setProperty(p.substring(0,eq), p.substring(eq+1));
}
}
-
+
if (cmds.size()==0) {
if (aafsso!=null) {
aafsso.setLogDefault();
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 -?>");
-
+
if (doExit) {
System.exit(1);
}
}
-
+
TIMEOUT = Integer.parseInt(env.getProperty(Config.AAF_CONN_TIMEOUT, "5000"));
-
+
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());
-
+
Trans trans = env.newTrans();
String token;
if ((token=access.getProperty("oauth_token"))!=null) {
// show Std out again
aafsso.setLogDefault();
aafsso.setStdErrDefault();
-
+
/*urls=*/loadURLs(access);
aafsso.writeFiles();
}
-
-
-
+
+
+
String cmd = cmds.removeFirst();
switch(cmd) {
case "place":
lhost=Config.AAF_LOCATE_URL_TAG;
}
value = rph.replacements(AGENT_LOAD_URLS,
- proto + lhost + "/%CNS.%AAF_NS." + ("aaf".equals(u)?"service":u) + ':' + version,
+ proto + lhost + "/%CNS.%AAF_NS." + ("aaf".equals(u)?"service":u) + ':' + version,
null,dot_le);
if(append!=null) {
value+=append;
}
return alias!=null?alias:AAFSSO.cons.readLine("AppID: ");
}
- return cmds.removeFirst();
+ return cmds.removeFirst();
}
private static String machine(Deque<String> cmds) throws UnknownHostException {
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);
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);
}
return notification;
}
-
+
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"));
-
+
if (future.get(TIMEOUT)) {
boolean printed = false;
for (Artifact a : future.value.getArtifact()) {
- 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());
StringBuilder sb = new StringBuilder();
boolean first = true;
for (String t : a.getType()) {
sb.append(t);
}
AAFSSO.cons.printf("Types: %s\n",sb);
- AAFSSO.cons.printf("Namespace: %s\n",a.getNs());
+ AAFSSO.cons.printf("Namespace: %s\n",a.getNs());
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());
tt.done();
}
}
-
+
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);
-
+
if (future.get(TIMEOUT)) {
boolean printed = false;
for (Artifact a : future.value.getArtifact()) {
trans.error().printf("Call to AAF Certman failed, %s",
errMsg.toMsg(fup));
}
-
+
printed = true;
}
}
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);
-
+
AAFSSO.cons.printf("For %s on %s\n", a.getMechid(),a.getMachine());
arti.setMechid(a.getMechid());
arti.setMachine(a.getMachine());
else{sb.append(',');}
sb.append(t);
}
-
+
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())));
-
+
}
if (artifacts.getArtifact().size()==0) {
AAFSSO.cons.printf("Artifact for %s %s does not exist", mechID, machine);
tt.done();
}
}
-
+
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" );
-
+
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;
} else {
key = machine;
}
-
+
TimeTaken tt = trans.start("Place Artifact", Env.REMOTE);
try {
Future<Artifacts> acf = aafcon.client(CM_VER)
}
return rv;
}
-
+
private static void notifyPlaced(Artifact a, boolean rv) {
}
boolean allowed;
for (Artifact a : acf.value.getArtifact()) {
allowed = id!=null && (id.equals(a.getSponsor()) ||
- (id.equals(a.getMechid())
+ (id.equals(a.getMechid())
&& aafcon.securityInfo().defSS.getClass().isAssignableFrom(HBasicAuthSS.class)));
if (!allowed) {
- Future<String> pf = aafcon.client(CM_VER).read("/cert/may/" +
+ Future<String> pf = aafcon.client(CM_VER).read("/cert/may/" +
a.getNs()+"|certman|"+a.getCa()+"|showpass","*/*");
if (pf.get(TIMEOUT)) {
allowed = true;
} finally {
fis.close();
}
-
+
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:")) {
}
}
-
+
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)) {
return;
}
}
-
+
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());
}
-
+
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));
}
-
+
String keyfile = cred.getKeyPath();
if(keyfile!=null) {
File fkeyfile = new File(keyfile);
}
}
}
-
+
// 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(key, aaf_loc_prop.getValue().toString());
}
}
-
+
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 (origTruststore.exists()) {
+ if (origTruststore.exists()) {
break;
}
}
throw new CadiException(cts + " does not exist");
}
}
-
+
}
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 */);
-
+
String cpf = propAccess.getProperty(Config.CADI_PROP_FILES);
if (cpf!=null){
- String[] propFiles = Split.splitTrim(File.pathSeparatorChar, cpf);
+ String[] propFiles = Split.splitTrim(File.pathSeparatorChar, cpf);
for (int pfi = propFiles.length-1;pfi>=0;--pfi) {
String f = propFiles[pfi];
System.out.format("Reading %s\n",f);
- FileInputStream fis = new FileInputStream(f);
+ FileInputStream fis = new FileInputStream(f);
try {
Properties props = new Properties();
props.load(fis);
}
}
}
-
+
PropHolder.writeAll();
} finally {
tt.done();
}
}
- public static List<Props> aafProps(Trans trans, AAFCon<?> aafcon, String locator, String fqi) throws CadiException, APIException, URISyntaxException {
+ public static List<Props> aafProps(Trans trans, AAFCon<?> aafcon, String locator, String fqi) throws CadiException, APIException, LocatorException {
Future<Configuration> acf = aafcon.client(new SingleEndpointLocator(locator))
.read("/configure/"+fqi+"/aaf", configDF);
if (acf.get(TIMEOUT)) {
/**
* Check returns Error Codes, so that Scripts can know what to do
- *
+ *
* 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
- *
+ *
* @param trans
* @param aafcon
* @param cmds
int exitCode=1;
String mechID = fqi(cmds);
String machine = machine(cmds);
-
+
TimeTaken tt = trans.start("Check Certificate", Env.REMOTE);
try {
-
+
Future<Artifacts> acf = aafcon.client(CM_VER)
.read("/cert/artifacts/"+mechID+'/'+machine, artifactsDF);
if (acf.get(TIMEOUT)) {
} finally {
fis.close();
}
-
- String prop;
+
+ String prop;
File f;
-
+
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",
Symm symm = ArtifactDir.getSymm(f);
KeyStore ks = KeyStore.getInstance("JKS");
-
+
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)) {
- msg = String.format("X509Certificate for %s on %s has been checked on %s. It expires on %s; it will not be renewed until %s.\n",
+ msg = String.format("X509Certificate for %s on %s has been checked on %s. It expires on %s; it will not be renewed until %s.\n",
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)) {
- msg = String.format("X509Certificate for %s on %s has been renewed. Ensure services using are refreshed.\n",
+ msg = String.format("X509Certificate for %s on %s has been renewed. Ensure services using are refreshed.\n",
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
}
}
}
}
-
+
}
}
}
}
}
-
-
+
+