Merge "AAFcli.java -Declare "value" on a separate line"
[aaf/authz.git] / auth / auth-certman / src / main / java / org / onap / aaf / auth / cm / mapper / Mapper1_0.java
index 53b9762..bf5665a 100644 (file)
@@ -3,13 +3,14 @@
  * org.onap.aaf
  * ===========================================================================
  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
+ *  Modifications Copyright (C) 2019 IBM.
  * ===========================================================================
  * 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.
@@ -24,6 +25,7 @@ package org.onap.aaf.auth.cm.mapper;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Set;
 
 import org.onap.aaf.auth.cm.data.CertDrop;
 import org.onap.aaf.auth.cm.data.CertRenew;
@@ -35,7 +37,6 @@ import org.onap.aaf.auth.dao.cass.ArtiDAO.Data;
 import org.onap.aaf.auth.dao.cass.CertDAO;
 import org.onap.aaf.auth.env.AuthzTrans;
 import org.onap.aaf.auth.layer.Result;
-import org.onap.aaf.cadi.util.FQI;
 import org.onap.aaf.cadi.util.Vars;
 
 import aaf.v2_0.Error;
@@ -49,7 +50,7 @@ import certman.v1_0.CertificateRequest;
 
 
 public class Mapper1_0 implements Mapper<BaseRequest,CertInfo,Artifacts,Error> {
-    
+
     @Override
     public Class<?> getClass(API api) {
         switch(api) {
@@ -81,13 +82,13 @@ public class Mapper1_0 implements Mapper<BaseRequest,CertInfo,Artifacts,Error> {
 
     //////////////  Mapping Functions /////////////
     @Override
-    public Error errorFromMessage(StringBuilder holder, String msgID, String text, String... var) {
+    public Error errorFromMessage(StringBuilder holder, String msgID, String text, Object ... var) {
         Error err = new Error();
         err.setMessageId(msgID);
         // AT&T Restful Error Format requires numbers "%" placements
         err.setText(Vars.convert(holder, text, var));
-        for(String s : var) {
-            err.getVariables().add(s);
+        for (Object s : var) {
+            err.getVariables().add(s.toString());
         }
         return err;
     }
@@ -97,7 +98,7 @@ public class Mapper1_0 implements Mapper<BaseRequest,CertInfo,Artifacts,Error> {
      */
     @Override
     public Result<CertInfo> toCert(AuthzTrans trans, Result<CertResp> in, boolean withTrustChain) throws IOException {
-        if(!in.isOK()) {
+        if (!in.isOK()) {
             return Result.err(in);
         }
 
@@ -105,15 +106,15 @@ public class Mapper1_0 implements Mapper<BaseRequest,CertInfo,Artifacts,Error> {
         CertInfo cout = newInstance(API.CERT);
         cout.setPrivatekey(cin.privateString());
         String value;
-        if((value=cin.challenge())!=null) {
+        if ((value=cin.challenge())!=null) {
             cout.setChallenge(value);
         }
         // In Version 1, Cert is always first
         cout.getCerts().add(cin.asCertString());
         // Follow with Trust Chain
-        if(cin.trustChain()!=null) {
-            for(String c : cin.trustChain()) {
-                if(c!=null) {
+        if (cin.trustChain()!=null) {
+            for (String c : cin.trustChain()) {
+                if (c!=null) {
                     cout.getCerts().add(c);
                 }
             }
@@ -121,20 +122,18 @@ public class Mapper1_0 implements Mapper<BaseRequest,CertInfo,Artifacts,Error> {
 
         // Adding all the Certs in one response is a mistake.  Makes it very hard for Agent to setup
         // Certs in keystore versus Truststore.  Separate in Version 2_0
-        if(cin.trustCAs()!=null) {
-            for(String c : cin.trustCAs()) {
-                if(c!=null) {
-                    if(!cout.getCerts().contains(c)) {
-                        cout.getCerts().add(c);
-                    }
-                }
+        if (cin.trustCAs()!=null) {
+            for (String c : cin.trustCAs()) {
+                if ((c!=null)&&(!cout.getCerts().contains(c))) {
+                     cout.getCerts().add(c);
+                 }
             }
         }
-        if(cin.notes()!=null) {
+        if (cin.notes()!=null) {
             boolean first = true;
             StringBuilder sb = new StringBuilder();
-            for(String n : cin.notes()) {
-                if(first) {
+            for (String n : cin.notes()) {
+                if (first) {
                     first = false;
                 } else {
                     sb.append('\n');
@@ -144,7 +143,7 @@ public class Mapper1_0 implements Mapper<BaseRequest,CertInfo,Artifacts,Error> {
             cout.setNotes(sb.toString());
         }
         List<String> caIssuerDNs = cout.getCaIssuerDNs();
-        for(String s : cin.caIssuerDNs()) {
+        for (String s : cin.caIssuerDNs()) {
             caIssuerDNs.add(s);
         }
         cout.setEnv(cin.env());
@@ -154,10 +153,10 @@ public class Mapper1_0 implements Mapper<BaseRequest,CertInfo,Artifacts,Error> {
 
     @Override
     public Result<CertInfo> toCert(AuthzTrans trans, Result<List<CertDAO.Data>> in) {
-        if(in.isOK()) {
+        if (in.isOK()) {
             CertInfo cout = newInstance(API.CERT);
             List<String> certs = cout.getCerts();
-            for(CertDAO.Data cdd : in.value) {
+            for (CertDAO.Data cdd : in.value) {
                 certs.add(cdd.x509);
             }
             return Result.ok(cout);
@@ -174,7 +173,7 @@ public class Mapper1_0 implements Mapper<BaseRequest,CertInfo,Artifacts,Error> {
         CertificateRequest in;
         try {
             in = (CertificateRequest)req;
-        } catch(ClassCastException e) {
+        } catch (ClassCastException e) {
             return Result.err(Result.ERR_BadData,"Request is not a CertificateRequest");
         }
 
@@ -184,7 +183,7 @@ public class Mapper1_0 implements Mapper<BaseRequest,CertInfo,Artifacts,Error> {
         out.fqdns=in.getFqdns();
         v.isNull("CertRequest", req).nullOrBlank("MechID", out.mechid);
         v.nullBlankMin("FQDNs", out.fqdns,1);
-        if(v.err()) {
+        if (v.err()) {
             return Result.err(Result.ERR_BadData, v.errs());
         }
         out.emails = in.getEmail();
@@ -217,33 +216,33 @@ public class Mapper1_0 implements Mapper<BaseRequest,CertInfo,Artifacts,Error> {
     @Override
     public List<ArtiDAO.Data> toArtifact(AuthzTrans trans, Artifacts artifacts) {
         List<ArtiDAO.Data> ladd = new ArrayList<>();
-        for(Artifact arti : artifacts.getArtifact()) {
+        for (Artifact arti : artifacts.getArtifact()) {
             ArtiDAO.Data data = new ArtiDAO.Data();
-            data.mechid = arti.getMechid();
-            data.machine = arti.getMachine();
-            data.type(true).addAll(arti.getType());
-            data.ca = arti.getCa();
-            data.dir = arti.getDir();
-            data.os_user = arti.getOsUser();
+            data.mechid = trim(arti.getMechid());
+            data.machine = trim(arti.getMachine());
+            if(arti.getType()!=null) {
+                Set<String> ss = data.type(true);
+                for(String t : arti.getType()) {
+                    ss.add(trim(t));
+                }
+            }
+            data.ca = trim(arti.getCa());
+            data.dir = trim(arti.getDir());
+            data.os_user = trim(arti.getOsUser());
             // Optional (on way in)
-            data.ns = arti.getNs();
+            data.ns = trim(arti.getNs());
             data.renewDays = arti.getRenewDays();
-            data.notify = arti.getNotification();
-            
+            data.notify = trim(arti.getNotification());
+
             // Ignored on way in for create/update
-            data.sponsor = arti.getSponsor();
-            data.expires = null;
-            
-            // Derive Optional Data from Machine (Domain) if exists
-            if(data.machine!=null) {
-                if(data.ca==null && data.machine.endsWith(".att.com")) {
-                        data.ca = "aaf"; // default
-                }
-                if(data.ns==null ) {
-                    data.ns=FQI.reverseDomain(data.machine);
+            data.sponsor = (arti.getSponsor());
+            if(arti.getSans()!=null) {
+                Set<String> ls = data.sans(true);
+                for(String t : arti.getSans()) {
+                    ls.add(trim(t));
                 }
             }
-            data.sans(true).addAll(arti.getSans());
+            data.expires = null;
             ladd.add(data);
         }
         return ladd;
@@ -254,21 +253,25 @@ public class Mapper1_0 implements Mapper<BaseRequest,CertInfo,Artifacts,Error> {
      */
     @Override
     public Result<Artifacts> fromArtifacts(Result<List<Data>> lArtiDAO) {
-        if(lArtiDAO.isOK()) {
+        if (lArtiDAO.isOK()) {
             Artifacts artis = new Artifacts();
-            for(ArtiDAO.Data arti : lArtiDAO.value) {
+            for (ArtiDAO.Data arti : lArtiDAO.value) {
                 Artifact a = new Artifact();
-                a.setMechid(arti.mechid);
-                a.setMachine(arti.machine);
-                a.setSponsor(arti.sponsor);
-                a.setNs(arti.ns);
-                a.setCa(arti.ca);
-                a.setDir(arti.dir);
-                a.getType().addAll(arti.type(false));
-                a.setOsUser(arti.os_user);
+                a.setMechid(trim(arti.mechid));
+                a.setMachine(trim(arti.machine));
+                a.setSponsor(trim(arti.sponsor));
+                a.setNs(trim(arti.ns));
+                a.setCa(trim(arti.ca));
+                a.setDir(trim(arti.dir));
+                for(String t : arti.type(false)) {
+                    a.getType().add(trim(t));
+                }
+                a.setOsUser(trim(arti.os_user));
                 a.setRenewDays(arti.renewDays);
-                a.setNotification(arti.notify);
-                a.getSans().addAll(arti.sans(false));
+                a.setNotification(trim(arti.notify));
+                for(String t : arti.sans(false)) {
+                    a.getSans().add(trim(t));
+                }
                 artis.getArtifact().add(a);
             }
             return Result.ok(artis);
@@ -276,7 +279,14 @@ public class Mapper1_0 implements Mapper<BaseRequest,CertInfo,Artifacts,Error> {
             return Result.err(lArtiDAO);
         }
     }
-    
-    
 
+
+
+    private String trim(String s) {
+        if(s==null) {
+            return s;
+        } else {
+            return s.trim();
+        }
+    }
 }
\ No newline at end of file