Coverage Report - com.jcabi.ssl.maven.plugin.Keytool
 
Classes in this File Line Coverage Branch Coverage Complexity
Keytool
94%
36/38
11%
2/18
1.143
Keytool$AjcClosure1
100%
1/1
N/A
1.143
Keytool$AjcClosure3
100%
1/1
N/A
1.143
Keytool$AjcClosure5
100%
1/1
N/A
1.143
 
 1  1
 /**
 2  
  * Copyright (c) 2012-2017, jcabi.com
 3  
  * All rights reserved.
 4  
  *
 5  
  * Redistribution and use in source and binary forms, with or without
 6  
  * modification, are permitted provided that the following conditions
 7  
  * are met: 1) Redistributions of source code must retain the above
 8  
  * copyright notice, this list of conditions and the following
 9  
  * disclaimer. 2) Redistributions in binary form must reproduce the above
 10  
  * copyright notice, this list of conditions and the following
 11  
  * disclaimer in the documentation and/or other materials provided
 12  
  * with the distribution. 3) Neither the name of the jcabi.com nor
 13  
  * the names of its contributors may be used to endorse or promote
 14  
  * products derived from this software without specific prior written
 15  
  * permission.
 16  
  *
 17  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 18  
  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
 19  
  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 20  
  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 21  
  * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 22  
  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 23  
  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 24  
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 25  
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 26  
  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 27  
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 28  
  * OF THE POSSIBILITY OF SUCH DAMAGE.
 29  
  */
 30  
 package com.jcabi.ssl.maven.plugin;
 31  
 
 32  
 import com.jcabi.aspects.Immutable;
 33  
 import com.jcabi.aspects.Loggable;
 34  
 import com.jcabi.log.Logger;
 35  
 import com.jcabi.log.VerboseProcess;
 36  
 import java.io.File;
 37  
 import java.io.IOException;
 38  
 import java.io.OutputStreamWriter;
 39  
 import java.io.PrintWriter;
 40  
 import java.util.ArrayList;
 41  
 import java.util.List;
 42  
 import java.util.Locale;
 43  
 import lombok.EqualsAndHashCode;
 44  
 import lombok.ToString;
 45  
 import org.apache.commons.io.FileUtils;
 46  
 
 47  
 /**
 48  
  * Keytool abstraction.
 49  
  *
 50  
  * @author Yegor Bugayenko (yegor256@gmail.com)
 51  
  * @version $Id: 5a33ff96f9bb7d54ff4577d75ada659c3bfa28f2 $
 52  
  * @since 0.5
 53  
  */
 54  
 @Immutable
 55  0
 @ToString
 56  0
 @EqualsAndHashCode(of = { "keystore", "password" })
 57  
 final class Keytool {
 58  
     /**
 59  
      * Localhost, input to the keytool.
 60  
      */
 61  
     private static final String LOCALHOST = "localhost";
 62  
 
 63  
     /**
 64  
      * Platform-dependent line separator.
 65  
      */
 66  1
     private static final String NEWLINE = System.getProperty("line.separator");
 67  
 
 68  
     /**
 69  
      * Keystore location.
 70  
      */
 71  
     private final transient String keystore;
 72  
 
 73  
     /**
 74  
      * Keystore password.
 75  
      */
 76  
     private final transient String password;
 77  
 
 78  
     /**
 79  
      * Public ctor.
 80  
      * @param store The location of keystore
 81  
      * @param pwd The password
 82  
      */
 83  9
     public Keytool(final File store, final String pwd) {
 84  9
         this.keystore = store.getAbsolutePath();
 85  9
         this.password = pwd;
 86  9
     }
 87  
 
 88  
     /**
 89  
      * List content of the keystore.
 90  
      * @return The content of it
 91  
      * @throws IOException If fails
 92  
      */
 93  
     @Loggable(Loggable.DEBUG)
 94  
     public String list() throws IOException {
 95  10
         return new VerboseProcess(this.proc("-list", "-v")).stdout();
 96  
     }
 97  
 
 98  
     /**
 99  
      * Generate key.
 100  
      * @throws IOException If fails
 101  
      */
 102  
     @Loggable(Loggable.DEBUG)
 103  
     public void genkey() throws IOException {
 104  8
         final Process proc = this.proc(
 105  
             "-genkeypair",
 106  
             "-alias",
 107  
             Keytool.LOCALHOST,
 108  
             "-keyalg",
 109  
             "RSA",
 110  
             "-keysize",
 111  
             "2048",
 112  
             "-keypass",
 113  
             this.password
 114  
         ).start();
 115  4
         final PrintWriter writer = new PrintWriter(
 116  
             new OutputStreamWriter(proc.getOutputStream())
 117  
         );
 118  4
         writer.print(this.appendNewLine(Keytool.LOCALHOST));
 119  4
         writer.print(this.appendNewLine("ACME Co."));
 120  4
         writer.print(this.appendNewLine("software developers"));
 121  4
         writer.print(this.appendNewLine("San Francisco"));
 122  4
         writer.print(this.appendNewLine("California"));
 123  4
         writer.print(this.appendNewLine("US"));
 124  4
         writer.print(this.appendNewLine(this.createLocaleDependentYes()));
 125  4
         writer.close();
 126  4
         new VerboseProcess(proc).stdout();
 127  4
         Logger.info(
 128  
             this,
 129  
             "Keystore created in '%s' (%s)",
 130  
             this.keystore,
 131  
             FileUtils.byteCountToDisplaySize(this.keystore.length())
 132  
         );
 133  4
     }
 134  
 
 135  
     /**
 136  
      * Import certificate into this store.
 137  
      * @param file The file to import
 138  
      * @param pwd The password there
 139  
      * @throws IOException If fails
 140  
      */
 141  
     @Loggable(Loggable.DEBUG)
 142  
     public void imprt(final File file, final String pwd) throws IOException {
 143  2
         new VerboseProcess(
 144  
             this.proc(
 145  
                 "-importkeystore",
 146  
                 "-srckeystore",
 147  
                 file.getAbsolutePath(),
 148  
                 "-srcstorepass",
 149  
                 pwd,
 150  
                 "-destkeystore",
 151  
                 this.keystore,
 152  
                 "-deststorepass",
 153  
                 this.password
 154  
             )
 155  
         ).stdout();
 156  1
     }
 157  
 
 158  
     /**
 159  
      * Creates a string, which consists of string with an appended
 160  
      * platform-dependent line separator.
 161  
      * @param text Text, to which the line separator needs to be appended
 162  
      * @return Contents of text with appended line separator
 163  
      */
 164  
     private String appendNewLine(final String text) {
 165  28
         return String.format("%s%s", text, NEWLINE);
 166  
     }
 167  
 
 168  
     /**
 169  
      * Creates a text, which represents "yes" in the language,
 170  
      * specified by the current locale.
 171  
      * @return The word "Yes" translated to the current language
 172  
      */
 173  
     private String createLocaleDependentYes() {
 174  4
         return new Yes().translate(Locale.getDefault());
 175  
     }
 176  
 
 177  
     /**
 178  
      * Create process builder.
 179  
      * @param args Arguments
 180  
      * @return Process just created and started
 181  
      * @throws IOException If fails
 182  
      */
 183  
     private ProcessBuilder proc(final String... args) throws IOException {
 184  10
         final List<String> cmds = new ArrayList<String>(args.length + 1);
 185  10
         cmds.add(
 186  
             String.format(
 187  
                 "%s/bin/keytool",
 188  
                 System.getProperty("java.home")
 189  
             )
 190  
         );
 191  65
         for (final String arg : args) {
 192  55
             cmds.add(arg);
 193  
         }
 194  10
         cmds.add("-storetype");
 195  10
         cmds.add("jks");
 196  10
         cmds.add("-noprompt");
 197  10
         cmds.add("-storepass");
 198  10
         cmds.add(this.password);
 199  10
         cmds.add("-keystore");
 200  10
         cmds.add(this.keystore);
 201  10
         return new ProcessBuilder(cmds);
 202  
     }
 203  
 }