From 411cc59ac0b9d0c2850fd209fccbf7c9aea84d83 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Wed, 4 Feb 2015 15:41:20 +0100 Subject: [PATCH] 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 --- .../encrypt/BaseEncryptOptionHandler.java | 3 +++ .../cli/command/encrypt/EncryptCommand.java | 3 ++- .../command/encrypt/MissingKeyException.java | 26 +++++++++++++++++++ .../command/encrypt/EncryptCommandTests.java | 5 ++++ 4 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 spring-cloud-cli/src/main/java/org/springframework/cloud/cli/command/encrypt/MissingKeyException.java diff --git a/spring-cloud-cli/src/main/java/org/springframework/cloud/cli/command/encrypt/BaseEncryptOptionHandler.java b/spring-cloud-cli/src/main/java/org/springframework/cloud/cli/command/encrypt/BaseEncryptOptionHandler.java index 9cb2822..39c1961 100644 --- a/spring-cloud-cli/src/main/java/org/springframework/cloud/cli/command/encrypt/BaseEncryptOptionHandler.java +++ b/spring-cloud-cli/src/main/java/org/springframework/cloud/cli/command/encrypt/BaseEncryptOptionHandler.java @@ -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); diff --git a/spring-cloud-cli/src/main/java/org/springframework/cloud/cli/command/encrypt/EncryptCommand.java b/spring-cloud-cli/src/main/java/org/springframework/cloud/cli/command/encrypt/EncryptCommand.java index ab5e423..5734b34 100644 --- a/spring-cloud-cli/src/main/java/org/springframework/cloud/cli/command/encrypt/EncryptCommand.java +++ b/spring-cloud-cli/src/main/java/org/springframework/cloud/cli/command/encrypt/EncryptCommand.java @@ -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()); } diff --git a/spring-cloud-cli/src/main/java/org/springframework/cloud/cli/command/encrypt/MissingKeyException.java b/spring-cloud-cli/src/main/java/org/springframework/cloud/cli/command/encrypt/MissingKeyException.java new file mode 100644 index 0000000..da6d161 --- /dev/null +++ b/spring-cloud-cli/src/main/java/org/springframework/cloud/cli/command/encrypt/MissingKeyException.java @@ -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)"); + } +} diff --git a/spring-cloud-cli/src/test/java/org/springframework/cloud/cli/command/encrypt/EncryptCommandTests.java b/spring-cloud-cli/src/test/java/org/springframework/cloud/cli/command/encrypt/EncryptCommandTests.java index 84fca2a..13aaf2b 100644 --- a/spring-cloud-cli/src/test/java/org/springframework/cloud/cli/command/encrypt/EncryptCommandTests.java +++ b/spring-cloud-cli/src/test/java/org/springframework/cloud/cli/command/encrypt/EncryptCommandTests.java @@ -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"));