jdk/test/java/security/Policy/PolicyProvider/CustomPolicy.java
author mullan
Tue, 12 May 2015 13:13:06 -0400
changeset 30444 054604aed79c
permissions -rw-r--r--
8075706: Policy implementation does not allow policy.provider to be on the class path Reviewed-by: alanb, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
30444
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
     1
/*
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
     4
 *
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
     7
 * published by the Free Software Foundation.
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
     8
 *
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    13
 * accompanied this code).
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    14
 *
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    18
 *
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    21
 * questions.
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    22
 */
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    23
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    24
import java.security.AccessController;
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    25
import java.security.Permission;
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    26
import java.security.Policy;
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    27
import java.security.PrivilegedAction;
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    28
import java.security.ProtectionDomain;
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    29
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    30
public class CustomPolicy extends Policy {
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    31
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    32
    // the ProtectionDomain of CustomPolicy
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    33
    private final ProtectionDomain policyPd;
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    34
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    35
    public CustomPolicy() {
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    36
        policyPd = AccessController.doPrivileged(
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    37
            (PrivilegedAction<ProtectionDomain>)
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    38
                () -> this.getClass().getProtectionDomain());
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    39
    }
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    40
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    41
    @Override
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    42
    public boolean implies(ProtectionDomain pd, Permission perm) {
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    43
        System.out.println("CustomPolicy.implies");
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    44
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    45
        // If the protection domain is the same as CustomPolicy, then
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    46
        // we return true. This is to prevent recursive permission checks
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    47
        // that lead to StackOverflow errors when the policy implementation
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    48
        // performs a sensitive operation that triggers a permission check,
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    49
        // for example, as below.
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    50
        if (pd == policyPd) {
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    51
            return true;
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    52
        }
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    53
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    54
        // Do something that triggers a permission check to make sure that
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    55
        // we don't cause a StackOverflow error.
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    56
        String home = AccessController.doPrivileged(
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    57
            (PrivilegedAction<String>) () -> System.getProperty("user.home"));
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    58
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    59
        return true;
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    60
    }
054604aed79c 8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
diff changeset
    61
}