Saturday 15 July 2017

Update policy file to grant permissions

Below step-by-step procedure explains how to open a policy file and add permissions to it.

Step 1: Open terminal (or) command prompt and run the ‘policytool’ command.

It opens below window.

Step2: File -> Open.

Open the policy file. In my previous post, I created a policy file ‘demoPolicy’. I am going to open that file.

Step 3: Let’s give permissions to the java application to access the properties  'user.home', 'java.home'.

Click on the button ‘Add Policy Entry’. It opens ‘Policy Entry’ window like below.


Type the url ‘file:C:/Users/krishna/Documents/Study/Security/examples’ in codebase field. ‘file:C:/Users/krishna/Documents/Study/Security/examples’ is the location where my Test file is located.

Click on the button ‘Add Permission’.

It opens below dialog box.

From the ‘Permission”’ drop down, select the drop down as ‘PropertyPermission’, it automatically populates the right hand side text fields.

Update the second text field to ‘user.home’.

Select the Actions: as ‘read’.

Press the button Ok.

In the same way, you can add permission to the property ‘java.home’.

Once you added the permission, the window will be updated like below.

Click on the button ‘Done’. You can able to see the newly added code base location below.


Go to File -> Save the policy file.

Contents of the demoPolicy file are updated like below.

demoPolicy
/* AUTOMATICALLY GENERATED ON Sat Jul 15 14:49:43 IST 2017*/
/* DO NOT EDIT */

grant codeBase "https://github.com" {
};

grant codeBase "file:C:/Users/krishna/Documents/Study/Security/examples" {
  permission java.util.PropertyPermission "user.home", "read";
  permission java.util.PropertyPermission "java.home", "read";
};

Now we can run our ‘Test.java’ application, by specify the policy file.


Test.java
public class Test {
 private static void printProperties() {

  String osName = System.getProperty("os.name");
  System.out.println("osName: " + osName);

  String javaVersion = System.getProperty("java.version");
  System.out.println("javaVersion: " + javaVersion);

  String userHome = System.getProperty("user.home");
  System.out.println("userHome: " + userHome);

  String javaHome = System.getProperty("java.home");
  System.out.println("javaHome: " + javaHome);

 }

 public static void main(String args[]) {
  printProperties();
 }
}



Previous                                                 Next                                                 Home

No comments:

Post a Comment