1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 @Mojo(name = "expose-security-credentials", requiresProject = true)
55 public class ExposeSecurityCredentialsMojo extends AbstractBeanstalkMojo {
56
57
58
59
60 @Parameter Expose[] exposes = new Expose[0];
61
62 @Parameter(defaultValue = "${project}")
63 MavenProject project;
64
65
66
67 BuildContext buildContext;
68
69 protected Object executeInternal() throws MojoExecutionException, MojoFailureException {
70
71
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
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 }