Add MissingKeyException to catch users who don't add keys

on the command line for encrypt/decrypt. The key is (obviously)
mandatory, but I like the --key syntax, so it seems better to throw
an exception than try and make it a non-optional argument.

Fixes gh-5
This commit is contained in:
Dave Syer
2015-02-04 15:41:20 +01:00
parent aaaf07ef2d
commit 411cc59ac0
4 changed files with 36 additions and 1 deletions

View File

@@ -66,6 +66,9 @@ class BaseEncryptOptionHandler extends OptionHandler {
protected TextEncryptor createEncryptor(OptionSet options) {
String value = keyOption.value(options);
if (value==null) {
throw new MissingKeyException();
}
if (options.has(passwordOption)) { // it's a keystore
String password = options.valueOf(passwordOption);
String alias = options.valueOf(aliasOption);

View File

@@ -31,7 +31,8 @@ import org.springframework.util.StringUtils;
public class EncryptCommand extends OptionParsingCommand {
public EncryptCommand() {
super("encrypt", "Encrypt a string so, for isntance, it can be added to source control",
super("encrypt",
"Encrypt a string so, for isntance, it can be added to source control",
new EncryptOptionHandler());
}

View File

@@ -0,0 +1,26 @@
/*
* Copyright 2012-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.cli.command.encrypt;
import org.springframework.boot.cli.command.CommandException;
@SuppressWarnings("serial")
public class MissingKeyException extends CommandException {
public MissingKeyException() {
super("Error: missing key (please use --key)");
}
}

View File

@@ -34,6 +34,11 @@ public class EncryptCommandTests {
assertEquals(ExitStatus.OK, command.run("-k", "deadbeef", "foo"));
}
@Test(expected=MissingKeyException.class)
public void errorOnNoKey() throws Exception {
assertEquals(ExitStatus.ERROR, command.run("foo"));
}
@Test(expected=IllegalStateException.class)
public void failsWithBadFile() throws Exception {
assertEquals(ExitStatus.OK, command.run("-k", "@nosuchfile", "foo"));