View Javadoc
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.aspects.Immutable;
33  import com.jcabi.aspects.Loggable;
34  import com.jcabi.log.Logger;
35  import java.io.File;
36  import java.io.IOException;
37  import java.util.Properties;
38  import javax.validation.constraints.NotNull;
39  import lombok.EqualsAndHashCode;
40  
41  /**
42   * Keystore abstraction.
43   *
44   * @author Yegor Bugayenko (yegor256@gmail.com)
45   * @version $Id: 992033deae14e77a614948e178e61ddb2ab92b1f $
46   * @since 0.5
47   */
48  @Immutable
49  @EqualsAndHashCode(of = "password")
50  final class Keystore {
51  
52      /**
53       * Constant {@code javax.net.ssl.keyStore}.
54       */
55      public static final String KEY = "javax.net.ssl.keyStore";
56  
57      /**
58       * Constant {@code javax.net.ssl.keyStorePassword}.
59       */
60      public static final String KEY_PWD = "javax.net.ssl.keyStorePassword";
61  
62      /**
63       * Unique password of it.
64       */
65      private final transient String password;
66  
67      /**
68       * Public ctor.
69       * @param pwd The password
70       */
71      public Keystore(@NotNull final String pwd) {
72          this.password = pwd;
73      }
74  
75      /**
76       * {@inheritDoc}
77       */
78      @Override
79      public String toString() {
80          final String[] names = new String[] {
81              Keystore.KEY,
82              Keystore.KEY_PWD,
83          };
84          final StringBuilder text = new StringBuilder();
85          text.append('[');
86          for (final String name : names) {
87              if (text.length() > 1) {
88                  text.append(", ");
89              }
90              text.append(name).append('=');
91              final String value = System.getProperty(name);
92              if (name == null) {
93                  text.append("NULL");
94              } else {
95                  text.append(value);
96              }
97          }
98          text.append(']');
99          return text.toString();
100     }
101 
102     /**
103      * Is it active now in the JVM?
104      * @return TRUE if JVM is using our keystore
105      */
106     @Loggable(Loggable.DEBUG)
107     public boolean isActive() {
108         final String pwd = System.getProperty(Keystore.KEY_PWD);
109         return pwd != null && pwd.equals(this.password);
110     }
111 
112     /**
113      * Activate it, in the given file.
114      * @param file The file to use
115      * @throws IOException If fails
116      */
117     @Loggable(Loggable.DEBUG)
118     public void activate(final File file) throws IOException {
119         file.getParentFile().mkdirs();
120         file.delete();
121         new Keytool(file, this.password).genkey();
122         System.setProperty(Keystore.KEY, file.getAbsolutePath());
123         System.setProperty(Keystore.KEY_PWD, this.password);
124         new Keytool(file, this.password).list();
125     }
126 
127     /**
128      * Populate given properties with this keystore's path and password.
129      * @param props The properties
130      */
131     @Loggable(Loggable.DEBUG)
132     public void populate(final Properties props) {
133         final String[] names = new String[] {
134             Keystore.KEY,
135             Keystore.KEY_PWD,
136         };
137         for (final String name : names) {
138             final String value = System.getProperty(name);
139             if (value == null) {
140                 continue;
141             }
142             props.put(name, value);
143             Logger.info(
144                 this,
145                 "Maven property ${%s} set to '%s'",
146                 name,
147                 System.getProperty(name)
148             );
149         }
150     }
151 
152 }