Coverage Report - com.jcabi.ssl.maven.plugin.Keytool
 
Classes in this File Line Coverage Branch Coverage Complexity
Keytool
100%
33/33
11%
2/18
1.2
Keytool$AjcClosure1
100%
1/1
N/A
1.2
Keytool$AjcClosure3
100%
1/1
N/A
1.2
Keytool$AjcClosure5
100%
1/1
N/A
1.2
 
 1  2
 /**
 2  
  * Copyright (c) 2012-2013, 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 lombok.EqualsAndHashCode;
 43  
 import lombok.ToString;
 44  
 import org.apache.commons.io.FileUtils;
 45  
 
 46  
 /**
 47  
  * Keytool abstraction.
 48  
  *
 49  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 50  
  * @version $Id$
 51  
  * @since 0.5
 52  
  */
 53  
 @Immutable
 54  
 @ToString
 55  
 @EqualsAndHashCode(of = { "keystore", "password" })
 56  
 final class Keytool {
 57  
 
 58  
     /**
 59  
      * Keystore location.
 60  
      */
 61  
     private final transient String keystore;
 62  
 
 63  
     /**
 64  
      * Keystore password.
 65  
      */
 66  
     private final transient String password;
 67  
 
 68  
     /**
 69  
      * Public ctor.
 70  
      * @param store The location of keystore
 71  
      * @param pwd The password
 72  
      */
 73  7
     public Keytool(final File store, final String pwd) {
 74  7
         this.keystore = store.getAbsolutePath();
 75  7
         this.password = pwd;
 76  7
     }
 77  
 
 78  
     /**
 79  
      * List content of the keystore.
 80  
      * @return The content of it
 81  
      * @throws IOException If fails
 82  
      */
 83  
     @Loggable(Loggable.DEBUG)
 84  
     public String list() throws IOException {
 85  8
         return new VerboseProcess(this.proc("-list", "-v")).stdout();
 86  
     }
 87  
 
 88  
     /**
 89  
      * Generate key.
 90  
      * @throws IOException If fails
 91  
      */
 92  
     @Loggable(Loggable.DEBUG)
 93  
     public void genkey() throws IOException {
 94  6
         final Process proc = this.proc(
 95  
             "-genkeypair",
 96  
             "-alias",
 97  
             "localhost",
 98  
             "-keyalg",
 99  
             "RSA",
 100  
             "-keysize",
 101  
             "2048",
 102  
             "-keypass",
 103  
             this.password
 104  
         ).start();
 105  3
         final PrintWriter writer = new PrintWriter(
 106  
             new OutputStreamWriter(proc.getOutputStream())
 107  
         );
 108  3
         writer.print("localhost\n");
 109  3
         writer.print("ACME Co.\n");
 110  3
         writer.print("software developers\n");
 111  3
         writer.print("San Francisco\n");
 112  3
         writer.print("California\n");
 113  3
         writer.print("US\n");
 114  3
         writer.print("yes\n");
 115  3
         writer.close();
 116  3
         new VerboseProcess(proc).stdout();
 117  3
         Logger.info(
 118  
             this,
 119  
             "Keystore created in '%s' (%s)",
 120  
             this.keystore,
 121  
             FileUtils.byteCountToDisplaySize(this.keystore.length())
 122  
         );
 123  3
     }
 124  
 
 125  
     /**
 126  
      * Import certificate into this store.
 127  
      * @param file The file to import
 128  
      * @param pwd The password there
 129  
      * @throws IOException If fails
 130  
      */
 131  
     @Loggable(Loggable.DEBUG)
 132  
     public void imprt(final File file, final String pwd) throws IOException {
 133  2
         new VerboseProcess(
 134  
             this.proc(
 135  
                 "-importkeystore",
 136  
                 "-srckeystore",
 137  
                 file.getAbsolutePath(),
 138  
                 "-srcstorepass",
 139  
                 pwd,
 140  
                 "-destkeystore",
 141  
                 this.keystore,
 142  
                 "-deststorepass",
 143  
                 this.password
 144  
             )
 145  
         ).stdout();
 146  1
     }
 147  
 
 148  
     /**
 149  
      * Create process builder.
 150  
      * @param args Arguments
 151  
      * @return Process just created and started
 152  
      * @throws IOException If fails
 153  
      */
 154  
     private ProcessBuilder proc(final String... args) throws IOException {
 155  8
         final List<String> cmds = new ArrayList<String>(args.length + 1);
 156  8
         cmds.add(
 157  
             String.format(
 158  
                 "%s/bin/keytool",
 159  
                 System.getProperty("java.home")
 160  
             )
 161  
         );
 162  52
         for (String arg : args) {
 163  44
             cmds.add(arg);
 164  
         }
 165  8
         cmds.add("-storetype");
 166  8
         cmds.add("jks");
 167  8
         cmds.add("-noprompt");
 168  8
         cmds.add("-storepass");
 169  8
         cmds.add(this.password);
 170  8
         cmds.add("-keystore");
 171  8
         cmds.add(this.keystore);
 172  8
         return new ProcessBuilder(cmds);
 173  
     }
 174  
 
 175  
 }