jdk/test/javax/management/security/TestJMXAuthenticator.java
author olagneau
Fri, 18 Dec 2015 17:42:06 +0100
changeset 34827 de746e7eb1e3
permissions -rw-r--r--
8058865: JMX Test Refactoring Reviewed-by: jbachorik
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
34827
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
     1
/*
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
     2
 * Copyright (c) 2006, 2015, Oracle and/or its affiliates. All rights reserved.
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
     4
 *
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
     7
 * published by the Free Software Foundation.
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
     8
 *
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    13
 * accompanied this code).
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    14
 *
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    18
 *
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    21
 * questions.
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    22
 */
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    23
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    24
import java.security.Principal;
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    25
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    26
import javax.management.Attribute;
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    27
import javax.management.MBeanServer;
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    28
import javax.management.ObjectName;
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    29
import javax.management.remote.JMXAuthenticator;
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    30
import javax.management.remote.JMXPrincipal;
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    31
import javax.security.auth.Subject;
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    32
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    33
public final class TestJMXAuthenticator implements JMXAuthenticator {
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    34
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    35
    private String protocol = "";
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    36
    private MBeanServer mbs = null;
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    37
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    38
    public TestJMXAuthenticator() {
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    39
    }
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    40
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    41
    public TestJMXAuthenticator(String protocol) {
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    42
        this.protocol = protocol;
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    43
    }
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    44
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    45
    public TestJMXAuthenticator(String protocol, MBeanServer mbs) {
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    46
        this.protocol = protocol;
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    47
        this.mbs = mbs;
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    48
    }
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    49
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    50
    public Subject authenticate(Object credentials) {
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    51
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    52
        String credentials_username = "";
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    53
        String credentials_password = "";
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    54
        Principal aPrincipal = null;
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    55
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    56
        credentials_username = ((String[]) credentials)[0];
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    57
        credentials_password = ((String[]) credentials)[1];
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    58
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    59
        String authenticated_username = System.getProperty("susername");
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    60
        String authenticated_password = System.getProperty("spassword");
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    61
        String principal = System.getProperty("principal");
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    62
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    63
        System.out.println("TestJMXAuthenticator::authenticate: Start");
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    64
        System.out.println("TestJMXAuthenticator::authenticate: credentials username = " +
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    65
                credentials_username);
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    66
        System.out.println("TestJMXAuthenticator::authenticate: credentials password = " +
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    67
                credentials_password);
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    68
        System.out.println("TestJMXAuthenticator::authenticate: authenticated username = " +
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    69
                authenticated_username);
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    70
        System.out.println("TestJMXAuthenticator::authenticate: authenticated password = " +
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    71
                authenticated_password);
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    72
        System.out.println("TestJMXAuthenticator::authenticate: principal used for " +
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    73
                "authorization = " + principal);
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    74
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    75
        if (credentials_username.equals(authenticated_username) &&
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    76
                credentials_password.equals(authenticated_password)) {
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    77
            System.out.println("TestJMXAuthenticator::authenticate: " +
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    78
                    "Authenticator should succeed");
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    79
        } else {
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    80
            System.out.println("TestJMXAuthenticator::authenticate: " +
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    81
                    "Authenticator should reject");
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    82
            throw new SecurityException("TestJMXAuthenticator throws EXCEPTION");
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    83
        }
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    84
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    85
        // At this point, authentication has succeeded
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    86
        // (no SecurityException thrown).
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    87
        //
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    88
        // If no authorization is required, the returned subject (empty or not)
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    89
        // is useless.
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    90
        // Otherwise, the returned subject must define a principal
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    91
        // and authorization will be performed against this principal.
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    92
        //
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    93
        // Note that this custom JMXAuthenticator is used for test purpose and
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    94
        // the username used to perform authentication may be different from the
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    95
        // username used to perform authorization.
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    96
        //
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    97
        Subject subject = new Subject();
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    98
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
    99
        if (principal != null) {
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
   100
            System.out.println("TestJMXAuthenticator::authenticate: " +
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
   101
                    "Add " + principal + " principal to the returned subject");
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
   102
            subject.getPrincipals().add(new JMXPrincipal(principal));
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
   103
        }
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
   104
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
   105
        return subject;
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
   106
    }
de746e7eb1e3 8058865: JMX Test Refactoring
olagneau
parents:
diff changeset
   107
}