How to make sure the file is created with the correct user or permissions?

The business model is a bit complicated, so please forgive me if the explanation is not 100% clear:

The Uploader interface ( String upload(String path, byte[] fileContents) ) defines various ways to upload a file (contained in an array of bytes), for example AmazonUploader , which accepts the contents and string path and uploads it to Amazon S3 with the specified key.

I have a class called LocalUploader , which is used in QA, writes this array of files, as it is, to the local drive.

Uploader is used in two different cases:

  • Web user interface
  • Command line interface run by ssh users as root

The command line interface is another jar from the web interface, but they both have an Uploader. Also, the command line executable is the bash script running java -jar .... at the end.

The idea is to save the files in a known location and then serve them through a simple static http server.

Brief problem . Since both processes are written to the same place when the file is written by the command line interface, it can no longer be writable via the web interface ( web can't access files made by root , obviously no problem on the contrary).

Now I am stuck with Java 6 for Uploader, so there is no nio package.

Workflow in a nutshell:

The working process

Here is what I tried:

  • Use java.io.File .setWritable() . If I do this before writing the file, it fails (returns false ), if I do it after, it will return true, but it will not set the writable flag.
  • Use su to start the jar as another user. But this is due to the insertion of parameters
  • Use umask , Java was immediately ignored. Perhaps because it is a different process.
  • Use group id flag for linux permissions - Java was immediately ignored. Perhaps because it is a different process.

What I have not tried:

  • Use ACLs - we cannot afford to communicate with basic partitions.
  • Return the file as output and bash write the file. Not all commands must write files, it may or may not be necessary to write more than one file. This is not the logic that I want to put in the bash script, that is the Uploader job.

Remarks:

  • I am using Java 6 in Uploader. No fancy stuff.
  • I am using Spring 3.2 for Uploader. No download.
  • Perhaps I can use the utility libraries in Uploader.
  • I am using Java 8 for the command line .

Some relevant code:

Uploader.java

 @Override public String upload(String path, byte[] fileContents) { final File file = new File(path); try { FileUtils.writeByteArrayToFile(file, fileContents); } catch (IOException e) { throw new RuntimeException("Error writing file to path " + path, e); } return ""; } 

init.sh

 #!/bin/bash CONFIGDIR=${spring.config.location} CONFIGFILE=$CONFIGDIR/${app.name}.conf # Loading a settings from CONFIGFILE [ -f $CONFIGFILE ] && source $CONFIGFILE # Setting JAVA_BIN if [ -z "$JAVACMD" ] ; then if [ -n "$JAVA_HOME" ] ; then if [ -x "$JAVA_HOME/jre/sh/java" ] ; then # IBM JDK on AIX uses strange locations for the executables JAVACMD="$JAVA_HOME/jre/sh/java" else JAVACMD="$JAVA_HOME/bin/java" fi else JAVACMD="`which java`" fi fi # Starting the application $JAVACMD $JAVA_OPTS -jar ${app.home}/bin/${build.finalName}.jar " $@ " --spring.config.location=$CONFIGDIR/ 

All this smells of some kind of trivial problem, which should not even be in the first place, but I'm at a standstill. I would be grateful for any help in this matter.

+7
java file bash maven
source share
1 answer

Have you tried using sudo?

 sudo -u web $JAVACMD $JAVA_OPTS -jar ${app.home}/bin/${build.finalName}.jar " $@ " --spring.config.location=$CONFIGDIR/ 

This should work, as the command line interface is invoked by users registered as root (which is not recommended, but I accept it as indicated).

+1
source share

All Articles