001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.activemq.console.command; 018 019import java.util.List; 020 021import org.jasypt.encryption.pbe.StandardPBEStringEncryptor; 022import org.jasypt.iv.RandomIvGenerator; 023 024public class EncryptCommand extends AbstractCommand { 025 026 protected String[] helpFile = new String[] { 027 "Task Usage: Main encrypt --password <password> --input <input>", 028 "Description: Encrypts given text.", 029 "", 030 "Encrypt Options:", 031 " --password <password> Password to be used by the encryptor. Defaults to", 032 " the value in the ACTIVEMQ_ENCRYPTION_PASSWORD env variable.", 033 " --input <input> Text to be encrypted.", 034 " --algorithm <algorithm> Algorithm to use.", 035 " --version Display the version information.", 036 " -h,-?,--help Display the stop broker help information.", 037 "" 038 }; 039 040 StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor(); 041 String input; 042 String password; 043 String algorithm; 044 045 @Override 046 public String getName() { 047 return "encrypt"; 048 } 049 050 @Override 051 public String getOneLineDescription() { 052 return "Encrypts given text"; 053 } 054 055 @Override 056 protected void printHelp() { 057 context.printHelp(helpFile); 058 } 059 060 @Override 061 protected void runTask(List<String> tokens) throws Exception { 062 if( password == null ) { 063 password = System.getenv("ACTIVEMQ_ENCRYPTION_PASSWORD"); 064 } 065 if (password == null || input == null) { 066 context.printException(new IllegalArgumentException("input and password parameters are mandatory")); 067 return; 068 } 069 encryptor.setPassword(password); 070 if (algorithm != null) { 071 encryptor.setAlgorithm(algorithm); 072 // From Jasypt: for PBE-AES-based algorithms, the IV generator is MANDATORY" 073 if (algorithm.startsWith("PBE") && algorithm.contains("AES")) { 074 encryptor.setIvGenerator(new RandomIvGenerator()); 075 } 076 } 077 context.print("Encrypted text: " + encryptor.encrypt(input)); 078 } 079 080 @Override 081 protected void handleOption(String token, List<String> tokens) throws Exception { 082 if (token.startsWith("--input")) { 083 if (tokens.isEmpty() || ((String)tokens.get(0)).startsWith("-")) { 084 context.printException(new IllegalArgumentException("input not specified")); 085 return; 086 } 087 088 input=(String)tokens.remove(0); 089 } else if (token.startsWith("--password")) { 090 if (tokens.isEmpty() || ((String)tokens.get(0)).startsWith("-")) { 091 context.printException(new IllegalArgumentException("password not specified")); 092 return; 093 } 094 095 password=(String)tokens.remove(0); 096 } else if (token.startsWith("--algorithm")) { 097 if (tokens.isEmpty() || ((String)tokens.get(0)).startsWith("-")) { 098 context.printException(new IllegalArgumentException("algorithm not specified")); 099 return; 100 } 101 102 algorithm=(String)tokens.remove(0); 103 } else { 104 super.handleOption(token, tokens); 105 } 106 } 107 108 109 110}