From ac01cdd6ce69d73a61d89149756f5c5f84410cab Mon Sep 17 00:00:00 2001
From: Brian Major <major.brian@gmail.com>
Date: Wed, 18 Jan 2017 15:38:11 -0800
Subject: [PATCH] authenticator impl allows cert-only subjects

---
 cadc-access-control-identity/build.gradle     |   4 +-
 .../ca/nrc/cadc/auth/ACIdentityManager.java   |  69 ++++++++++
 .../ca/nrc/cadc/auth/AuthenticatorImpl.java   |  86 ++++++++++--
 .../nrc/cadc/auth/AuthenticatorImplTest.java  | 128 ++++++++++++++++++
 4 files changed, 276 insertions(+), 11 deletions(-)
 create mode 100644 cadc-access-control-identity/src/test/java/ca/nrc/cadc/auth/AuthenticatorImplTest.java

diff --git a/cadc-access-control-identity/build.gradle b/cadc-access-control-identity/build.gradle
index c35de040..a204a007 100644
--- a/cadc-access-control-identity/build.gradle
+++ b/cadc-access-control-identity/build.gradle
@@ -13,7 +13,7 @@ repositories {
 sourceCompatibility = 1.7
 group = 'org.opencadc'
 
-version = '1.0.2'
+version = '1.0.3'
 
 dependencies {
     compile 'log4j:log4j:1.2.+'
@@ -22,4 +22,6 @@ dependencies {
     compile 'org.opencadc:cadc-registry:1.+'
     compile 'org.opencadc:cadc-vosi:1.+'
     compile 'org.opencadc:cadc-access-control:1.+'
+    
+    testCompile 'junit:junit:4.+'
 }
diff --git a/cadc-access-control-identity/src/main/java/ca/nrc/cadc/auth/ACIdentityManager.java b/cadc-access-control-identity/src/main/java/ca/nrc/cadc/auth/ACIdentityManager.java
index 8e68463b..1dc9932c 100644
--- a/cadc-access-control-identity/src/main/java/ca/nrc/cadc/auth/ACIdentityManager.java
+++ b/cadc-access-control-identity/src/main/java/ca/nrc/cadc/auth/ACIdentityManager.java
@@ -1,3 +1,72 @@
+/*
+************************************************************************
+*******************  CANADIAN ASTRONOMY DATA CENTRE  *******************
+**************  CENTRE CANADIEN DE DONNÉES ASTRONOMIQUES  **************
+*
+*  (c) 2017.                            (c) 2017.
+*  Government of Canada                 Gouvernement du Canada
+*  National Research Council            Conseil national de recherches
+*  Ottawa, Canada, K1A 0R6              Ottawa, Canada, K1A 0R6
+*  All rights reserved                  Tous droits réservés
+*
+*  NRC disclaims any warranties,        Le CNRC dénie toute garantie
+*  expressed, implied, or               énoncée, implicite ou légale,
+*  statutory, of any kind with          de quelque nature que ce
+*  respect to the software,             soit, concernant le logiciel,
+*  including without limitation         y compris sans restriction
+*  any warranty of merchantability      toute garantie de valeur
+*  or fitness for a particular          marchande ou de pertinence
+*  purpose. NRC shall not be            pour un usage particulier.
+*  liable in any event for any          Le CNRC ne pourra en aucun cas
+*  damages, whether direct or           être tenu responsable de tout
+*  indirect, special or general,        dommage, direct ou indirect,
+*  consequential or incidental,         particulier ou général,
+*  arising from the use of the          accessoire ou fortuit, résultant
+*  software.  Neither the name          de l'utilisation du logiciel. Ni
+*  of the National Research             le nom du Conseil National de
+*  Council of Canada nor the            Recherches du Canada ni les noms
+*  names of its contributors may        de ses  participants ne peuvent
+*  be used to endorse or promote        être utilisés pour approuver ou
+*  products derived from this           promouvoir les produits dérivés
+*  software without specific prior      de ce logiciel sans autorisation
+*  written permission.                  préalable et particulière
+*                                       par écrit.
+*
+*  This file is part of the             Ce fichier fait partie du projet
+*  OpenCADC project.                    OpenCADC.
+*
+*  OpenCADC is free software:           OpenCADC est un logiciel libre ;
+*  you can redistribute it and/or       vous pouvez le redistribuer ou le
+*  modify it under the terms of         modifier suivant les termes de
+*  the GNU Affero General Public        la “GNU Affero General Public
+*  License as published by the          License” telle que publiée
+*  Free Software Foundation,            par la Free Software Foundation
+*  either version 3 of the              : soit la version 3 de cette
+*  License, or (at your option)         licence, soit (à votre gré)
+*  any later version.                   toute version ultérieure.
+*
+*  OpenCADC is distributed in the       OpenCADC est distribué
+*  hope that it will be useful,         dans l’espoir qu’il vous
+*  but WITHOUT ANY WARRANTY;            sera utile, mais SANS AUCUNE
+*  without even the implied             GARANTIE : sans même la garantie
+*  warranty of MERCHANTABILITY          implicite de COMMERCIALISABILITÉ
+*  or FITNESS FOR A PARTICULAR          ni d’ADÉQUATION À UN OBJECTIF
+*  PURPOSE.  See the GNU Affero         PARTICULIER. Consultez la Licence
+*  General Public License for           Générale Publique GNU Affero
+*  more details.                        pour plus de détails.
+*
+*  You should have received             Vous devriez avoir reçu une
+*  a copy of the GNU Affero             copie de la Licence Générale
+*  General Public License along         Publique GNU Affero avec
+*  with OpenCADC.  If not, see          OpenCADC ; si ce n’est
+*  <http://www.gnu.org/licenses/>.      pas le cas, consultez :
+*                                       <http://www.gnu.org/licenses/>.
+*
+*  $Revision: 4 $
+*
+************************************************************************
+*/
+
 package ca.nrc.cadc.auth;
 
 import java.io.File;
diff --git a/cadc-access-control-identity/src/main/java/ca/nrc/cadc/auth/AuthenticatorImpl.java b/cadc-access-control-identity/src/main/java/ca/nrc/cadc/auth/AuthenticatorImpl.java
index a679af59..4eff5c8d 100644
--- a/cadc-access-control-identity/src/main/java/ca/nrc/cadc/auth/AuthenticatorImpl.java
+++ b/cadc-access-control-identity/src/main/java/ca/nrc/cadc/auth/AuthenticatorImpl.java
@@ -1,3 +1,72 @@
+/*
+************************************************************************
+*******************  CANADIAN ASTRONOMY DATA CENTRE  *******************
+**************  CENTRE CANADIEN DE DONNÉES ASTRONOMIQUES  **************
+*
+*  (c) 2017.                            (c) 2017.
+*  Government of Canada                 Gouvernement du Canada
+*  National Research Council            Conseil national de recherches
+*  Ottawa, Canada, K1A 0R6              Ottawa, Canada, K1A 0R6
+*  All rights reserved                  Tous droits réservés
+*
+*  NRC disclaims any warranties,        Le CNRC dénie toute garantie
+*  expressed, implied, or               énoncée, implicite ou légale,
+*  statutory, of any kind with          de quelque nature que ce
+*  respect to the software,             soit, concernant le logiciel,
+*  including without limitation         y compris sans restriction
+*  any warranty of merchantability      toute garantie de valeur
+*  or fitness for a particular          marchande ou de pertinence
+*  purpose. NRC shall not be            pour un usage particulier.
+*  liable in any event for any          Le CNRC ne pourra en aucun cas
+*  damages, whether direct or           être tenu responsable de tout
+*  indirect, special or general,        dommage, direct ou indirect,
+*  consequential or incidental,         particulier ou général,
+*  arising from the use of the          accessoire ou fortuit, résultant
+*  software.  Neither the name          de l'utilisation du logiciel. Ni
+*  of the National Research             le nom du Conseil National de
+*  Council of Canada nor the            Recherches du Canada ni les noms
+*  names of its contributors may        de ses  participants ne peuvent
+*  be used to endorse or promote        être utilisés pour approuver ou
+*  products derived from this           promouvoir les produits dérivés
+*  software without specific prior      de ce logiciel sans autorisation
+*  written permission.                  préalable et particulière
+*                                       par écrit.
+*
+*  This file is part of the             Ce fichier fait partie du projet
+*  OpenCADC project.                    OpenCADC.
+*
+*  OpenCADC is free software:           OpenCADC est un logiciel libre ;
+*  you can redistribute it and/or       vous pouvez le redistribuer ou le
+*  modify it under the terms of         modifier suivant les termes de
+*  the GNU Affero General Public        la “GNU Affero General Public
+*  License as published by the          License” telle que publiée
+*  Free Software Foundation,            par la Free Software Foundation
+*  either version 3 of the              : soit la version 3 de cette
+*  License, or (at your option)         licence, soit (à votre gré)
+*  any later version.                   toute version ultérieure.
+*
+*  OpenCADC is distributed in the       OpenCADC est distribué
+*  hope that it will be useful,         dans l’espoir qu’il vous
+*  but WITHOUT ANY WARRANTY;            sera utile, mais SANS AUCUNE
+*  without even the implied             GARANTIE : sans même la garantie
+*  warranty of MERCHANTABILITY          implicite de COMMERCIALISABILITÉ
+*  or FITNESS FOR A PARTICULAR          ni d’ADÉQUATION À UN OBJECTIF
+*  PURPOSE.  See the GNU Affero         PARTICULIER. Consultez la Licence
+*  General Public License for           Générale Publique GNU Affero
+*  more details.                        pour plus de détails.
+*
+*  You should have received             Vous devriez avoir reçu une
+*  a copy of the GNU Affero             copie de la Licence Générale
+*  General Public License along         Publique GNU Affero avec
+*  with OpenCADC.  If not, see          OpenCADC ; si ce n’est
+*  <http://www.gnu.org/licenses/>.      pas le cas, consultez :
+*                                       <http://www.gnu.org/licenses/>.
+*
+*  $Revision: 4 $
+*
+************************************************************************
+*/
+
 package ca.nrc.cadc.auth;
 
 import java.net.URI;
@@ -45,22 +114,19 @@ public class AuthenticatorImpl implements Authenticator
         if (subject != null && subject.getPrincipals().size() > 0)
         {
             Profiler prof = new Profiler(AuthenticatorImpl.class);
-            ACIdentityManager identityManager = new ACIdentityManager();
-            identityManager.augmentSubject(subject);
+            this.augmentSubject(subject);
             prof.checkpoint("AuthenticatorImpl.augmentSubject()");
-
-            if (subject.getPrincipals(NumericPrincipal.class).isEmpty()) // no matching internal account
-            {
-                // if the caller had an invalid or forged CADC_SSO cookie, we could get
-                // in here and then not match any known identity: drop to anon
-                log.debug("NumericPrincipal not found - dropping to anon: " + subject);
-                subject = AuthenticationUtil.getAnonSubject();
-            }
         }
 
         return subject;
     }
 
+    protected void augmentSubject(Subject subject)
+    {
+        ACIdentityManager identityManager = new ACIdentityManager();
+        identityManager.augmentSubject(subject);
+    }
+
     public static CheckResource getAvailabilityCheck()
     {
         RegistryClient regClient = new RegistryClient();
diff --git a/cadc-access-control-identity/src/test/java/ca/nrc/cadc/auth/AuthenticatorImplTest.java b/cadc-access-control-identity/src/test/java/ca/nrc/cadc/auth/AuthenticatorImplTest.java
new file mode 100644
index 00000000..8b05ca3c
--- /dev/null
+++ b/cadc-access-control-identity/src/test/java/ca/nrc/cadc/auth/AuthenticatorImplTest.java
@@ -0,0 +1,128 @@
+/*
+************************************************************************
+*******************  CANADIAN ASTRONOMY DATA CENTRE  *******************
+**************  CENTRE CANADIEN DE DONNÉES ASTRONOMIQUES  **************
+*
+*  (c) 2017.                            (c) 2017.
+*  Government of Canada                 Gouvernement du Canada
+*  National Research Council            Conseil national de recherches
+*  Ottawa, Canada, K1A 0R6              Ottawa, Canada, K1A 0R6
+*  All rights reserved                  Tous droits réservés
+*
+*  NRC disclaims any warranties,        Le CNRC dénie toute garantie
+*  expressed, implied, or               énoncée, implicite ou légale,
+*  statutory, of any kind with          de quelque nature que ce
+*  respect to the software,             soit, concernant le logiciel,
+*  including without limitation         y compris sans restriction
+*  any warranty of merchantability      toute garantie de valeur
+*  or fitness for a particular          marchande ou de pertinence
+*  purpose. NRC shall not be            pour un usage particulier.
+*  liable in any event for any          Le CNRC ne pourra en aucun cas
+*  damages, whether direct or           être tenu responsable de tout
+*  indirect, special or general,        dommage, direct ou indirect,
+*  consequential or incidental,         particulier ou général,
+*  arising from the use of the          accessoire ou fortuit, résultant
+*  software.  Neither the name          de l'utilisation du logiciel. Ni
+*  of the National Research             le nom du Conseil National de
+*  Council of Canada nor the            Recherches du Canada ni les noms
+*  names of its contributors may        de ses  participants ne peuvent
+*  be used to endorse or promote        être utilisés pour approuver ou
+*  products derived from this           promouvoir les produits dérivés
+*  software without specific prior      de ce logiciel sans autorisation
+*  written permission.                  préalable et particulière
+*                                       par écrit.
+*
+*  This file is part of the             Ce fichier fait partie du projet
+*  OpenCADC project.                    OpenCADC.
+*
+*  OpenCADC is free software:           OpenCADC est un logiciel libre ;
+*  you can redistribute it and/or       vous pouvez le redistribuer ou le
+*  modify it under the terms of         modifier suivant les termes de
+*  the GNU Affero General Public        la “GNU Affero General Public
+*  License as published by the          License” telle que publiée
+*  Free Software Foundation,            par la Free Software Foundation
+*  either version 3 of the              : soit la version 3 de cette
+*  License, or (at your option)         licence, soit (à votre gré)
+*  any later version.                   toute version ultérieure.
+*
+*  OpenCADC is distributed in the       OpenCADC est distribué
+*  hope that it will be useful,         dans l’espoir qu’il vous
+*  but WITHOUT ANY WARRANTY;            sera utile, mais SANS AUCUNE
+*  without even the implied             GARANTIE : sans même la garantie
+*  warranty of MERCHANTABILITY          implicite de COMMERCIALISABILITÉ
+*  or FITNESS FOR A PARTICULAR          ni d’ADÉQUATION À UN OBJECTIF
+*  PURPOSE.  See the GNU Affero         PARTICULIER. Consultez la Licence
+*  General Public License for           Générale Publique GNU Affero
+*  more details.                        pour plus de détails.
+*
+*  You should have received             Vous devriez avoir reçu une
+*  a copy of the GNU Affero             copie de la Licence Générale
+*  General Public License along         Publique GNU Affero avec
+*  with OpenCADC.  If not, see          OpenCADC ; si ce n’est
+*  <http://www.gnu.org/licenses/>.      pas le cas, consultez :
+*                                       <http://www.gnu.org/licenses/>.
+*
+*  $Revision: 4 $
+*
+************************************************************************
+*/
+
+package ca.nrc.cadc.auth;
+
+import javax.security.auth.Subject;
+import javax.security.auth.x500.X500Principal;
+
+import org.apache.log4j.Level;
+import org.apache.log4j.Logger;
+import org.junit.Assert;
+import org.junit.Test;
+
+import ca.nrc.cadc.util.Log4jInit;
+
+public class AuthenticatorImplTest
+{
+
+    private static final Logger log = Logger.getLogger(AuthenticatorImplTest.class);
+
+
+    static
+    {
+        Log4jInit.setLevel("ca.nrc.cadc.auth", Level.INFO);
+    }
+
+    public AuthenticatorImplTest() { }
+
+    /**
+     * A user with only a certificate in the subject should be allowed to
+     * continue and be identified as having an auth method of 'cert'.
+     */
+    @Test
+    public void testCertOnlyUser()
+    {
+        try
+        {
+            String dn = "cn=testuser, ou=cadc, o=nrc";
+            AuthenticatorImpl ai = new TestAuthenticatorImpl();
+            Subject subject = new Subject();
+            subject.getPrincipals().add(new X500Principal(dn));
+            subject.getPublicCredentials().add(AuthMethod.CERT);
+            subject = ai.getSubject(subject);
+
+            Assert.assertEquals(AuthMethod.CERT, AuthenticationUtil.getAuthMethod(subject));
+        }
+        catch (Throwable t)
+        {
+            log.error("unexpected throwable", t);
+            Assert.fail("Unexpected throwable");
+        }
+    }
+
+    class TestAuthenticatorImpl extends AuthenticatorImpl
+    {
+        @Override
+        public void augmentSubject(Subject subject)
+        {
+        }
+    }
+
+}
-- 
GitLab