test/micro/org/openjdk/micro/util/SecurityManagerHelper.java
branchJEP-230-microbenchmarks-branch
changeset 56929 b8756e94db7a
parent 56928 8957fe0c94f3
child 56930 079075866d11
equal deleted inserted replaced
56928:8957fe0c94f3 56929:b8756e94db7a
     1 /*
       
     2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 package org.openjdk.micro.util;
       
    26 
       
    27 import java.lang.management.ManagementPermission;
       
    28 import java.lang.reflect.ReflectPermission;
       
    29 import java.io.File;
       
    30 import java.io.FilePermission;
       
    31 import java.io.IOException;
       
    32 import java.io.PrintStream;
       
    33 import java.net.URI;
       
    34 import java.security.NoSuchAlgorithmException;
       
    35 import java.security.Permission;
       
    36 import java.security.Policy;
       
    37 import java.security.SecurityPermission;
       
    38 import java.security.URIParameter;
       
    39 import java.util.PropertyPermission;
       
    40 
       
    41 /**
       
    42  * Help class to create and load Security Policy file from a set of Permissions
       
    43  */
       
    44 public class SecurityManagerHelper {
       
    45 
       
    46     /**
       
    47      * Create and load a security manager using the provided permissions.
       
    48      *
       
    49      * @param perms Permissions to add to the file
       
    50      *
       
    51      * @throws IOException If file could not be created or written to.
       
    52      * @throws NoSuchAlgorithmException if no Provider supports a PolicySpi
       
    53      * implementation for the specified type.
       
    54      */
       
    55     public static void setupSecurityManager(Permission... perms)
       
    56             throws IOException, NoSuchAlgorithmException {
       
    57 
       
    58         URI policyURI = createSecurityFile(perms);
       
    59         Policy.setPolicy(Policy.getInstance("JavaPolicy", new URIParameter(policyURI)));
       
    60         System.setSecurityManager(new SecurityManager());
       
    61     }
       
    62 
       
    63     private static URI createSecurityFile(Permission... perms) throws IOException {
       
    64 
       
    65         File policyFile = File.createTempFile("security", ".policy");
       
    66         policyFile.deleteOnExit();
       
    67 
       
    68         try (PrintStream writer = new PrintStream(policyFile)) {
       
    69             writer.println("grant {");
       
    70             for (Permission p : perms) {
       
    71                 appendPermission(writer, p);
       
    72             }
       
    73             // Permissions required by JMH
       
    74             appendPermission(writer, new RuntimePermission("modifyThread"));
       
    75             // Required when running without forking
       
    76             appendPermission(writer, new FilePermission("<<ALL FILES>>", "read,write,delete,execute"));
       
    77             appendPermission(writer, new RuntimePermission("accessDeclaredMembers"));
       
    78             appendPermission(writer, new RuntimePermission("createSecurityManager"));
       
    79             appendPermission(writer, new ReflectPermission("suppressAccessChecks"));
       
    80             appendPermission(writer, new ManagementPermission("monitor"));
       
    81             appendPermission(writer, new PropertyPermission("*", "read"));
       
    82             appendPermission(writer, new SecurityPermission("createPolicy.JavaPolicy"));
       
    83             appendPermission(writer, new SecurityPermission("setPolicy"));
       
    84             appendPermission(writer, new RuntimePermission("setSecurityManager"));
       
    85             writer.println("};");
       
    86         }
       
    87 
       
    88         return policyFile.toURI();
       
    89     }
       
    90 
       
    91     private static void appendPermission(PrintStream writer, Permission p) {
       
    92         writer.printf("\tpermission %s \"%s\", \"%s\";\n",
       
    93                       p.getClass().getName(), p.getName(), p.getActions());
       
    94     }
       
    95 }