mathlearner
mathlearner

Reputation: 7629

java.security.AccessControlException: access denied (java.io.FilePermission

i am creating an app on google app engine java in which i have to read content of a static file.there is a file in war directory named as myrsakey11.pk8 .

my code segment for reading the file is

public static PrivateKey getPrivateKey(String privKeyFileName) throws IOException {
          File privKeyFile = new File(privKeyFileName);
          FileInputStream fis = new FileInputStream(privKeyFile);
          DataInputStream dis  = new DataInputStream(fis);

          byte[] privKeyBytes = new byte[(int) privKeyFile.length()];
          try {
            dis.read(privKeyBytes);
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
          dis.close();
          fis.close();

          String BEGIN = "-----BEGIN PRIVATE KEY-----";
          String END = "-----END PRIVATE KEY-----";
          String str = new String(privKeyBytes);

          if (str.contains(BEGIN) && str.contains(END)) {
            str = str.substring(BEGIN.length(), str.lastIndexOf(END));
          }

          KeyFactory fac;
        try {
            fac = KeyFactory.getInstance("RSA");

             EncodedKeySpec privKeySpec = new PKCS8EncodedKeySpec(Base64.decode(str));
              return fac.generatePrivate(privKeySpec);

        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Base64DecoderException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvalidKeySpecException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
         return null;
    }

and in this function i am passing string "/myrsakey11.pk8"

and the full error log i am getting is

java.security.AccessControlException: access denied (java.io.FilePermission /myrsakey11.pk8 read)
    at java.security.AccessControlContext.checkPermission(AccessControlContext.java:355)
    at java.security.AccessController.checkPermission(AccessController.java:567)
    at java.lang.SecurityManager.checkPermission(SecurityManager.java:549)
    at com.google.apphosting.runtime.security.CustomSecurityManager.checkPermission(CustomSecurityManager.java:56)
    at java.lang.SecurityManager.checkRead(SecurityManager.java:888)
    at java.io.FileInputStream.<init>(FileInputStream.java:133)
    at org.ritesh.HelloWorldServlet.getPrivateKey(HelloWorldServlet.java:129)
    at org.ritesh.HelloWorldServlet.doGet(HelloWorldServlet.java:110)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
    at com.google.apphosting.utils.servlet.ParseBlobUploadFilter.doFilter(ParseBlobUploadFilter.java:102)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.apphosting.runtime.jetty.SaveSessionFilter.doFilter(SaveSessionFilter.java:35)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
    at com.google.apphosting.runtime.jetty.AppVersionHandlerMap.handle(AppVersionHandlerMap.java:266)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.Server.handle(Server.java:326)
    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
    at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:923)
    at com.google.apphosting.runtime.jetty.RpcRequestParser.parseAvailable(RpcRequestParser.java:76)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
    at com.google.apphosting.runtime.jetty.JettyServletEngineAdapter.serviceRequest(JettyServletEngineAdapter.java:146)
    at com.google.apphosting.runtime.JavaRuntime$RequestRunnable.run(JavaRuntime.java:447)
    at com.google.tracing.TraceContext$TraceContextRunnable.runInContext(TraceContext.java:454)
    at com.google.tracing.TraceContext$TraceContextRunnable$1.run(TraceContext.java:461)
    at com.google.tracing.TraceContext.runInContext(TraceContext.java:703)
    at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContextNoUnref(TraceContext.java:338)
    at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContext(TraceContext.java:330)
    at com.google.tracing.TraceContext$TraceContextRunnable.run(TraceContext.java:458)
    at com.google.apphosting.runtime.ThreadGroupPool$PoolEntry.run(ThreadGroupPool.java:251)
    at java.lang.Thread.run(Thread.java:679)

can any one please tell me how to rectify this error??

Upvotes: 2

Views: 6849

Answers (3)

user1933178
user1933178

Reputation: 340

Getting a deployed appengine app to access the datastore was difficult and not explained well by this tutorial. Instead of using environment variables as described by the tutorial, I used constants in my app. The p12 file needs to be in a place accessible by the deployed app - such as /src/main/resources/.

The way I did it was like this:

  1. Put the app-xxx.p12 file in backend/src/main/resources, but also add it in .gitignore since it needs to be secret.
  2. Instead of constructing the entire credential from the environment like this

datastore = DatastoreFactory.get().create(DatastoreHelper.getOptionsfromEnv() .dataset(DATASET_ID).build());

as suggested by the tutorial, I did the following

Credential credential = getCredential();       
DatastoreOptions.Builder options = DatastoreHelper.getOptionsFromEnv().credential(credential);
datastore = DatastoreFactory.get().create(options.dataset(DATASET_ID).build());

where getCredential() is defined like this:

private Credential getCredential() throws GeneralSecurityException, IOException {
        final String serviceAccount = "[email protected]";
        final String FILE_NAME = "/app-xxx.p12";

        URL url = getClass().getResource(FILE_NAME);
        String filename = url.getFile();
        return DatastoreHelper.getServiceAccountCredential(serviceAccount, filename);
    }

Upvotes: 0

OhadR
OhadR

Reputation: 8839

It happened to me as well, and took me a while to figure it out:

If you declare a wrong path to a file, Google AE does not throw FileNotFoundException, but instead they throw "AccessControlException: access denied (read)".

Make sure your path is valid!

Upvotes: 2

Eich
Eich

Reputation: 3788

Did you grant access to this static file as explained in [1]. How do you get the path of the static file? Maybe you can use this file as a ressource (put it into the WEB-INF folder) and access it as followed:

ServletContext context = getContext();
InputStream resourceContent = context.getResourceAsStream("/WEB-INF/testinger.txt");

[1] https://developers.google.com/appengine/docs/java/config/appconfig#include-exclude

Upvotes: 0

Related Questions