Skip to content

Commit

Permalink
Merge branch 'release/0.3.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
overheadhunter committed Jun 13, 2017
2 parents 422d69a + a7fe127 commit b969a4b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 12 deletions.
16 changes: 8 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.cryptomator</groupId>
<artifactId>cli</artifactId>
<version>0.2.1</version>
<version>0.3.0</version>
<name>Cryptomator CLI</name>
<description>Command line program to access encrypted files via WebDAV.</description>
<url>https://github.com/cryptomator/cli</url>

<properties>
<java.version>1.8</java.version>
<commons.cli.version>1.3.1</commons.cli.version>
<cryptofs.version>1.0.1</cryptofs.version>
<webdav-nio.version>0.2.3</webdav-nio.version>
<cryptofs.version>1.3.1</cryptofs.version>
<webdav-nio.version>0.4.0</webdav-nio.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

Expand Down Expand Up @@ -56,21 +56,20 @@
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.7</version>
<version>2.8</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.7</version>
<version>2.8</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<version>3.6.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
Expand All @@ -80,6 +79,7 @@

<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>make-assembly</id>
Expand All @@ -105,4 +105,4 @@
</plugin>
</plugins>
</build>
</project>
</project>
38 changes: 36 additions & 2 deletions src/main/java/org/cryptomator/cli/Args.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@
*******************************************************************************/
package org.cryptomator.cli;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.DefaultParser;
Expand All @@ -27,7 +32,8 @@ public class Args {
private static final String USAGE = "java -jar cryptomator-cli.jar" //
+ " --bind localhost --port 8080" //
+ " --vault mySecretVault=/path/to/vault --password mySecretVault=FooBar3000" //
+ " --vault myOtherVault=/path/to/other/vault --password myOtherVault=BarFoo4000";
+ " --vault myOtherVault=/path/to/other/vault --password myOtherVault=BarFoo4000" //
+ " --vault myThirdVault=/path/to/third/vault --passwordfile myThirdVault=/path/to/passwordfile";
private static final Options OPTIONS = new Options();
static {
OPTIONS.addOption(Option.builder() //
Expand Down Expand Up @@ -56,18 +62,31 @@ public class Args {
.valueSeparator() //
.hasArgs() //
.build());
OPTIONS.addOption(Option.builder() //
.longOpt("passwordfile") //
.argName("Passwordfile for a vault") //
.desc("Format must be vaultName=passwordfile") //
.valueSeparator() //
.hasArgs() //
.build());
}

private final String bindAddr;
private final int port;
private final Properties vaultPaths;
private final Properties vaultPasswords;
private final Properties vaultPasswordFiles;

private boolean hasPasswordOrPasswordFile(Object vaultPath) {
return vaultPasswords.containsKey(vaultPath) || vaultPasswordFiles.containsKey(vaultPath);
}

public Args(CommandLine commandLine) throws ParseException {
this.bindAddr = commandLine.getOptionValue("bind", "localhost");
this.port = Integer.parseInt(commandLine.getOptionValue("port", "0"));
this.vaultPaths = commandLine.getOptionProperties("vault");
this.vaultPasswords = commandLine.getOptionProperties("password");
this.vaultPasswordFiles = commandLine.getOptionProperties("passwordfile");
}

public String getBindAddr() {
Expand All @@ -79,14 +98,29 @@ public int getPort() {
}

public Set<String> getVaultNames() {
return vaultPaths.keySet().stream().filter(vaultPasswords::containsKey).map(String.class::cast).collect(Collectors.toSet());
return vaultPaths.keySet().stream().filter(this::hasPasswordOrPasswordFile).map(String.class::cast).collect(Collectors.toSet());
}

public String getVaultPath(String vaultName) {
return vaultPaths.getProperty(vaultName);
}

public String getVaultPasswordPath(String vaultName) {
return vaultPasswordFiles.getProperty(vaultName);
}

public String getVaultPassword(String vaultName) {
if (vaultPasswords.getProperty(vaultName) == null) {
Path vaultPasswordPath = Paths.get(vaultPasswordFiles.getProperty(vaultName));
if (Files.isReadable(vaultPasswordPath) && Files.isRegularFile(vaultPasswordPath)) {
try (Stream<String> lines = Files.lines(vaultPasswordPath)) {
return lines.findFirst().get().toString();
} catch (IOException e) {
return null;
}
}
return null;
}
return vaultPasswords.getProperty(vaultName);
}

Expand Down
10 changes: 8 additions & 2 deletions src/main/java/org/cryptomator/cli/CryptomatorCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.cryptomator.cryptofs.CryptoFileSystemProperties;
import org.cryptomator.cryptofs.CryptoFileSystemProvider;
import org.cryptomator.frontend.webdav.WebDavServer;
import org.cryptomator.frontend.webdav.servlet.WebDavServletController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -49,14 +50,18 @@ private static void validate(Args args) throws IllegalArgumentException {

for (String vaultName : args.getVaultNames()) {
Path vaultPath = Paths.get(args.getVaultPath(vaultName));
if ((args.getVaultPasswordPath(vaultName) != null) && args.getVaultPassword(vaultName) == null) {
throw new IllegalArgumentException("Cannot read password from file: " + Paths.get(args.getVaultPasswordPath(vaultName)));
}
if (!Files.isDirectory(vaultPath)) {
throw new IllegalArgumentException("Not a directory: " + vaultPath);
}
}
}

private static void startup(Args args) throws IOException {
WebDavServer server = WebDavServer.create(args.getBindAddr(), args.getPort());
WebDavServer server = WebDavServer.create();
server.bind(args.getBindAddr(), args.getPort());
server.start();

for (String vaultName : args.getVaultNames()) {
Expand All @@ -65,7 +70,8 @@ private static void startup(Args args) throws IOException {
String vaultPassword = args.getVaultPassword(vaultName);
CryptoFileSystemProperties properties = CryptoFileSystemProperties.cryptoFileSystemProperties().withPassphrase(vaultPassword).build();
Path vaultRoot = CryptoFileSystemProvider.newFileSystem(vaultPath, properties).getPath("/");
server.startWebDavServlet(vaultRoot, vaultName);
WebDavServletController servlet = server.createWebDavServlet(vaultRoot, vaultName);
servlet.start();
}

waitForShutdown(() -> {
Expand Down

0 comments on commit b969a4b

Please sign in to comment.