1 /** 2 * Copyright (c) 2012-2022, 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.log.Logger; 33 import java.io.File; 34 import java.io.IOException; 35 import org.apache.commons.codec.digest.DigestUtils; 36 import org.apache.maven.plugin.AbstractMojo; 37 import org.apache.maven.plugin.MojoFailureException; 38 import org.apache.maven.project.MavenProject; 39 import org.slf4j.impl.StaticLoggerBinder; 40 41 /** 42 * Generate SSL keystore and configure in JVM. 43 * 44 * @author Yegor Bugayenko (yegor256@gmail.com) 45 * @version $Id: 67fccb6bbc46e084d037b8cb73ac576c8cec7fd5 $ 46 * @since 0.5 47 * @goal keygen 48 * @phase initialize 49 */ 50 public final class KeygenMojo extends AbstractMojo { 51 52 /** 53 * Maven project. 54 * @parameter name="project" default-value="${project}" 55 * @readonly 56 * @required 57 */ 58 private transient MavenProject project; 59 60 /** 61 * Shall we skip execution? 62 * @parameter name="skip" 63 */ 64 private transient boolean skip; 65 66 /** 67 * Name of keystore.jks file. 68 * @parameter name="keystore" 69 * default-value="${project.build.directory}/keystore.jks" 70 */ 71 private transient File keystore; 72 73 /** 74 * Name of cacerts.jks file. 75 * @parameter name="cacerts" 76 * default-value="${project.build.directory}/cacerts.jks" 77 */ 78 private transient File cacerts; 79 80 /** 81 * Keystore instance. 82 */ 83 private transient Keystore store; 84 85 /** 86 * Cacerts instance. 87 */ 88 private transient Cacerts truststore; 89 90 /** 91 * Creates KeygenMojo. 92 */ 93 public KeygenMojo() { 94 this( 95 null, new Keystore(DigestUtils.md5Hex(KeygenMojo.class.getName())), 96 null 97 ); 98 } 99 100 /** 101 * Creates KeygenMojo using custom KeystoreFactory. 102 * @param prj Maven project 103 * @param str Keystore instance 104 * @param crt Cacerts instance 105 */ 106 public KeygenMojo(final MavenProject prj, final Keystore str, 107 final Cacerts crt) { 108 super(); 109 this.project = prj; 110 this.store = str; 111 this.truststore = crt; 112 } 113 114 /** 115 * Set skip option. 116 * @param skp Shall we skip execution? 117 */ 118 public void setSkip(final boolean skp) { 119 this.skip = skp; 120 } 121 122 /** 123 * {@inheritDoc} 124 */ 125 @Override 126 public void execute() throws MojoFailureException { 127 StaticLoggerBinder.getSingleton().setMavenLog(this.getLog()); 128 if (this.skip) { 129 Logger.info(this, "execution skipped because of 'skip' option"); 130 return; 131 } 132 try { 133 if (this.truststore == null) { 134 this.truststore = new Cacerts(this.cacerts); 135 } 136 if (!this.store.isActive()) { 137 this.store.activate(this.keystore); 138 this.truststore.imprt(); 139 } 140 } catch (final IOException ex) { 141 throw new IllegalStateException(ex); 142 } 143 this.store.populate(this.project.getProperties()); 144 this.truststore.populate(this.project.getProperties()); 145 Logger.info(this, "Keystore is active: %s", this.store); 146 } 147 148 }