Update shiro logging and sl4j init
[aaf/cadi.git] / shiro / src / main / java / org / onap / aaf / cadi / shiro / AAFRealm.java
index 05b4d78..4b24c70 100644 (file)
@@ -21,6 +21,7 @@
 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;
@@ -29,6 +30,7 @@ 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;
@@ -48,8 +50,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;
@@ -73,19 +80,28 @@ public class AAFRealm extends AuthorizingRealm {
                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);
+                       access.log(Level.DEBUG,msg);
                        throw new RuntimeException(msg);
                } else {
+                       try {
+                               String log4jConfigFile = "./etc/org.ops4j.pax.logging.cfg";
+                               
+                       PropertyConfigurator.configure(log4jConfigFile);
+                       System.setOut(createLoggingProxy(System.out));
+                       System.setErr(createLoggingProxy(System.err));
+                       } catch(Exception e) {
+                               e.printStackTrace();
+                       }
+                       //System.out.println("Configuration done");
                        try {
                                acon = AAFCon.newInstance(access);
                                authn = acon.newAuthn();
                                authz = acon.newLur(authn);
-                               
                                final String csv = access.getProperty(Config.CADI_BATH_CONVERT);
                                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()) {
@@ -108,7 +124,7 @@ public class AAFRealm extends AuthorizingRealm {
                                                        idMap.put(oldID,newID);
                                                }
                                        } catch (IOException e) {
-                                               access.log(e);
+//                                             access.log(e);
                                        }
                                }
                        } catch (APIException | CadiException | LocatorException e) {
@@ -120,18 +136,25 @@ public class AAFRealm extends AuthorizingRealm {
                supports = new HashSet<Class<? extends AuthenticationToken>>();
                supports.add(UsernamePasswordToken.class);
        }
+       public static PrintStream createLoggingProxy(final PrintStream realPrintStream) {
+        return new PrintStream(realPrintStream) {
+            public void print(final String string) {
+                realPrintStream.print(string);
+                logger.info(string);
+            }
+        };
+    }
 
        @Override
        protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
-               access.log(Level.DEBUG, "AAFRealm.doGetAuthenticationInfo",token);
-               
                final UsernamePasswordToken upt = (UsernamePasswordToken)token;
                final String user = upt.getUsername();
                String authUser = user; 
                final String password=new String(upt.getPassword());
                String authPassword = password;
-               if(mbc!=null) {
+               if(mbc!=null) { 
                        try {
+
                                final String oldBath = "Basic " + Symm.base64noSplit.encode(user+':'+password);
                                String bath = mbc.convert(access, oldBath);
                                if(bath!=oldBath) {
@@ -139,7 +162,9 @@ public class AAFRealm extends AuthorizingRealm {
                                        int colon = bath.indexOf(':');
                                        if(colon>=0) {
                                                authUser = bath.substring(0, colon);
-                                               authPassword = bath.substring(colon+1);
+                                               authPassword = bath.substring(colon+1); 
+                                               access.log(Level.DEBUG, authUser,"user authenticated");
+                                               access.log(Level.DEBUG, authn.validate(authUser,authPassword));
                                        }
                                }
                        } catch (IOException e) {
@@ -151,11 +176,11 @@ public class AAFRealm extends AuthorizingRealm {
                        err = authn.validate(authUser,authPassword);
                } catch (IOException e) {
                        err = "Credential cannot be validated";
-                       access.log(e, err);
+                       access.log(Level.DEBUG, e, err);
                }
                
                if(err != null) {
-                       access.log(Level.DEBUG, err);
+                       access.log(Level.DEBUG, err, " - Credential cannot be validated");
                        throw new AuthenticationException(err);
                }
 
@@ -163,7 +188,9 @@ public class AAFRealm extends AuthorizingRealm {
                        access,
                        user,
                        password
+                       
            );
+           
        }
 
        @Override
@@ -180,7 +207,6 @@ public class AAFRealm extends AuthorizingRealm {
 
        @Override
        protected AAFAuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
-               access.log(Level.DEBUG, "AAFRealm.doGetAuthenthorizationInfo");
                Principal bait = (Principal)principals.getPrimaryPrincipal();
                Principal newBait = bait;
                if(idMap!=null) {
@@ -196,7 +222,6 @@ public class AAFRealm extends AuthorizingRealm {
                }
                List<Permission> pond = new ArrayList<>();
                authz.fishAll(newBait,pond);
-               
                return new AAFAuthorizationInfo(access,bait,pond);
        
        }