View Javadoc
1   /*
2    * Copyright (c) 2016 ingenieux Labs
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package br.com.ingenieux.mojo.beanstalk.sec;
18  
19  import org.apache.commons.lang.StringUtils;
20  import org.apache.maven.plugin.MojoExecutionException;
21  import org.apache.maven.plugin.MojoFailureException;
22  import org.apache.maven.plugins.annotations.Mojo;
23  import org.apache.maven.plugins.annotations.Parameter;
24  import org.apache.maven.project.MavenProject;
25  import org.sonatype.plexus.build.incremental.BuildContext;
26  
27  import br.com.ingenieux.mojo.aws.Expose;
28  import br.com.ingenieux.mojo.beanstalk.AbstractBeanstalkMojo;
29  
30  /**
31   * Exposes (i.e., copies) the security credentials from settings.xml into project properties
32   *
33   * <p> You can define the server, or not. If you don't, it will work if you did something like that
34   * </p>
35   *
36   * <pre>
37   * &lt;configuration&gt;
38   * &nbsp;&nbsp;&lt;exposes&gt;
39   * &nbsp;&nbsp;&nbsp;&nbsp;&lt;expose&gt;
40   * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;serverId&gt;${beanstalk.serverId}&lt;/serverId&gt;
41   * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;accessKey&gt;aws.accessKey&lt;/accessKey&gt;
42   * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;secretKey&gt;aws.accessKey&lt;/secretKey&gt;
43   * &nbsp;&nbsp;&nbsp;&nbsp;&lt;/expose&gt;
44   * &nbsp;&nbsp;&lt;/exposes&gt;
45   * &lt;/configuration&gt;
46   * </pre>
47   *
48   * <p> While it might look silly (and silly enough to get its own Plugin instead of beanstalker), it
49   * power comes when combined with the <a href="http://mojo.codehaus.org/properties-maven-plugin/">Properties
50   * Maven Plugin</a> </p>
51   *
52   * @since 0.2.7-RC4
53   */
54  @Mojo(name = "expose-security-credentials", requiresProject = true)
55  public class ExposeSecurityCredentialsMojo extends AbstractBeanstalkMojo {
56  
57    /**
58     * Which Server Settings to Expose?
59     */
60    @Parameter Expose[] exposes = new Expose[0];
61  
62    @Parameter(defaultValue = "${project}")
63    MavenProject project;
64    /**
65     * @component
66     */
67    BuildContext buildContext;
68  
69    protected Object executeInternal() throws MojoExecutionException, MojoFailureException {
70      /**
71       * Fill in defaults if needed
72       */
73      if (0 == exposes.length) {
74        exposes = new Expose[1];
75        exposes[0] = new Expose();
76        exposes[0].setServerId(this.serverId);
77        exposes[0].setAccessKey("aws.accessKey");
78        exposes[0].setSharedKey("aws.secretKey");
79      } else {
80        /**
81         * Validate parameters, for gods sake
82         */
83        try {
84          for (Expose e : exposes) {
85            assertOrWarn(StringUtils.isNotBlank(e.getServerId()), "serverId must be supplied");
86            assertOrWarn(StringUtils.isNotBlank(e.getAccessKey()), "accessKey must be supplied");
87            assertOrWarn(StringUtils.isNotBlank(e.getSharedKey()), "sharedKey must be supplied");
88          }
89        } catch (IllegalStateException e) {
90          return null;
91        }
92      }
93  
94      for (Expose e : exposes) {
95        Expose realExpose = null;
96  
97        try {
98          realExpose = super.exposeSettings(e.getServerId());
99        } catch (Exception exc) {
100         getLog().warn("Failed to Expose Settings from serverId ('" + e.getServerId() + "')");
101         continue;
102       }
103 
104       getLog()
105           .info(
106               String.format(
107                   "Writing Security Settings from serverId ('%s') into properties '%s' (accessKey) and '%s' (secretKey)",
108                   e.getServerId(),
109                   e.getAccessKey(),
110                   e.getSharedKey()));
111 
112       project.getProperties().put(e.getAccessKey(), realExpose.getAccessKey());
113 
114       project.getProperties().put(e.getSharedKey(), realExpose.getSharedKey());
115     }
116 
117     return null;
118   }
119 
120   private void assertOrWarn(boolean condition, String message) {
121     if (condition) {
122       return;
123     }
124 
125     if (null != buildContext) {
126       buildContext.addMessage(project.getFile(), 1, 1, message, BuildContext.SEVERITY_WARNING, null);
127     } else {
128       getLog().warn(message);
129     }
130 
131     throw new IllegalStateException(message);
132   }
133 }