fix shiro logs looping issue
[aaf/cadi.git] / shiro / src / main / java / org / onap / aaf / cadi / shiro / AAFRealm.java
index 3577c13..0fc962f 100644 (file)
 package org.onap.aaf.cadi.shiro;
 
 import java.io.IOException;
+import java.io.PrintStream;
 import java.security.Principal;
 import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.TreeMap;
 
+
+import org.apache.log4j.PropertyConfigurator;
 import org.apache.shiro.authc.AuthenticationException;
 import org.apache.shiro.authc.AuthenticationInfo;
 import org.apache.shiro.authc.AuthenticationToken;
@@ -45,8 +51,13 @@ import org.onap.aaf.cadi.config.Config;
 import org.onap.aaf.cadi.filter.MapBathConverter;
 import org.onap.aaf.cadi.util.CSV;
 import org.onap.aaf.misc.env.APIException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class AAFRealm extends AuthorizingRealm {
+       
+       final static  Logger logger =  LoggerFactory.getLogger(AAFRealm.class);
+       
        public static final String AAF_REALM = "AAFRealm";
        
        private PropAccess access;
@@ -55,6 +66,7 @@ public class AAFRealm extends AuthorizingRealm {
        private HashSet<Class<? extends AuthenticationToken>> supports;
        private AAFLurPerm authz;
        private MapBathConverter mbc;
+       private Map<String,String> idMap;
        
 
        /**
@@ -65,10 +77,11 @@ public class AAFRealm extends AuthorizingRealm {
        public AAFRealm () {
                access = new PropAccess(); // pick up cadi_prop_files from VM_Args
                mbc = null;
+               idMap = null;
                String cadi_prop_files = access.getProperty(Config.CADI_PROP_FILES);
                if(cadi_prop_files==null) {
                        String msg = Config.CADI_PROP_FILES + " in VM Args is required to initialize AAFRealm.";
-                       access.log(Level.INIT,msg);
+                       logger.info(msg);
                        throw new RuntimeException(msg);
                } else {
                        try {
@@ -80,14 +93,36 @@ public class AAFRealm extends AuthorizingRealm {
                                if(csv!=null) {
                                        try {
                                                mbc = new MapBathConverter(access, new CSV(csv));
-                                               access.printf(Level.INIT, "MapBathConversion enabled with file %s\n",csv);
+                                               logger.info("MapBathConversion enabled with file "+csv);
+                                               idMap = new TreeMap<String,String>();
+                                               // Load 
+                                               for(Entry<String, String> es : mbc.map().entrySet()) {
+                                                       String oldID = es.getKey();
+                                                       if(oldID.startsWith("Basic ")) {
+                                                               oldID = Symm.base64noSplit.decode(oldID.substring(6));
+                                                               int idx = oldID.indexOf(':');
+                                                               if(idx>=0) {
+                                                                       oldID = oldID.substring(0, idx);
+                                                               }
+                                                       }
+                                                       String newID = es.getValue();
+                                                       if(newID.startsWith("Basic ")) {
+                                                               newID = Symm.base64noSplit.decode(newID.substring(6));
+                                                               int idx = newID.indexOf(':');
+                                                               if(idx>=0) {
+                                                                       newID = newID.substring(0, idx);
+                                                               }
+                                                       }
+                                                       idMap.put(oldID,newID);
+                                                       
+                                               }
                                        } catch (IOException e) {
-                                               access.log(e);
+                                               logger.info(e.getMessage(), e);
                                        }
                                }
                        } catch (APIException | CadiException | LocatorException e) {
                                String msg = "Cannot initiate AAFRealm";
-                               access.log(Level.INIT,msg,e.getMessage());
+                               logger.info(msg + " "+ e.getMessage(), e);
                                throw new RuntimeException(msg,e);
                        }
                }
@@ -97,11 +132,12 @@ public class AAFRealm extends AuthorizingRealm {
 
        @Override
        protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
-               access.log(Level.DEBUG, "AAFRealm.doGetAuthenticationInfo",token);
-               
+               logger.info("AAFRealm.doGetAuthenticationInfo :"+token);
                final UsernamePasswordToken upt = (UsernamePasswordToken)token;
-               String user = upt.getUsername();
-               String password=new String(upt.getPassword());
+               final String user = upt.getUsername();
+               String authUser = user; 
+               final String password=new String(upt.getPassword());
+               String authPassword = password;
                if(mbc!=null) {
                        try {
                                final String oldBath = "Basic " + Symm.base64noSplit.encode(user+':'+password);
@@ -110,24 +146,26 @@ public class AAFRealm extends AuthorizingRealm {
                                        bath = Symm.base64noSplit.decode(bath.substring(6));
                                        int colon = bath.indexOf(':');
                                        if(colon>=0) {
-                                               user = bath.substring(0, colon);
-                                               password = bath.substring(colon+1);
+                                               authUser = bath.substring(0, colon);
+                                               authPassword = bath.substring(colon+1); 
                                        }
                                }
                        } catch (IOException e) {
-                               access.log(e);
+
+                               logger.info(e.getMessage(), e);
+
                        } 
                }
                String err;
                try {
-                       err = authn.validate(user,password);
+                       err = authn.validate(authUser,authPassword);
                } catch (IOException e) {
                        err = "Credential cannot be validated";
-                       access.log(e, err);
+                       logger.info(e.getMessage(), e);
                }
                
                if(err != null) {
-                       access.log(Level.DEBUG, err);
+                       logger.info(err);
                        throw new AuthenticationException(err);
                }
 
@@ -140,23 +178,40 @@ public class AAFRealm extends AuthorizingRealm {
 
        @Override
        protected void assertCredentialsMatch(AuthenticationToken atoken, AuthenticationInfo ai)throws AuthenticationException {
+               
                if(ai instanceof AAFAuthenticationInfo) {
                        if(!((AAFAuthenticationInfo)ai).matches(atoken)) {
                                throw new AuthenticationException("Credentials do not match");
+
                        }
+                       
                } else {
                        throw new AuthenticationException("AuthenticationInfo is not an AAFAuthenticationInfo");
+               
                }
        }
 
 
+
+
        @Override
        protected AAFAuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
-               access.log(Level.DEBUG, "AAFRealm.doGetAuthenthorizationInfo");
                Principal bait = (Principal)principals.getPrimaryPrincipal();
+               Principal newBait = bait;
+               if(idMap!=null) {
+                       final String newID = idMap.get(bait.getName());
+                       logger.info("Successful authentication attempt by " +bait.getName()); 
+                       if(newID!=null) {
+                               newBait = new Principal() {
+                                       @Override
+                                       public String getName() {
+                                               return newID;
+                                       }
+                               };
+                       }
+               }
                List<Permission> pond = new ArrayList<>();
-               authz.fishAll(bait,pond);
-               
+               authz.fishAll(newBait,pond);
                return new AAFAuthorizationInfo(access,bait,pond);
        
        }