1 /**
2 * Copyright (c) 2012-2014, 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.beanstalk.maven.plugin;
31
32 import com.amazonaws.auth.AWSCredentials;
33 import com.jcabi.aspects.Loggable;
34 import com.jcabi.log.Logger;
35 import javax.validation.constraints.NotNull;
36 import lombok.EqualsAndHashCode;
37 import lombok.ToString;
38 import org.apache.maven.plugin.MojoFailureException;
39 import org.apache.maven.settings.Server;
40 import org.apache.maven.settings.Settings;
41
42 /**
43 * AWS credentials from settings.xml.
44 *
45 * @author Yegor Bugayenko (yegor@tpc2.com)
46 * @version $Id$
47 * @since 0.3
48 */
49 @ToString
50 @EqualsAndHashCode(of = { "key", "secret" })
51 @Loggable(Loggable.DEBUG)
52 final class ServerCredentials implements AWSCredentials {
53
54 /**
55 * AWS key.
56 */
57 private final transient String key;
58
59 /**
60 * AWS secret.
61 */
62 private final transient String secret;
63
64 /**
65 * Public ctor.
66 * @param settings Maven settings
67 * @param name Name of server ID
68 * @throws MojoFailureException If some error
69 */
70 protected ServerCredentials(@NotNull final Settings settings,
71 @NotNull final String name)
72 throws MojoFailureException {
73 final Server server = settings.getServer(name);
74 if (server == null) {
75 throw new MojoFailureException(
76 String.format("Server '%s' is absent in settings.xml", name)
77 );
78 }
79 this.key = server.getUsername().trim();
80 if (!this.key.matches("[A-Z0-9]{20}")) {
81 throw new MojoFailureException(
82 String.format(
83 "Key '%s' for server '%s' is not a valid AWS key",
84 this.key, name
85 )
86 );
87 }
88 this.secret = server.getPassword().trim();
89 if (!this.secret.matches("[a-zA-Z0-9\\+/]{40}")) {
90 throw new MojoFailureException(
91 String.format(
92 "Secret '%s' for server '%s' is not a valid AWS secret",
93 this.secret, name
94 )
95 );
96 }
97 Logger.info(
98 ServerCredentials.class,
99 "Using server '%s' with AWS key '%s'",
100 name, this.key
101 );
102 }
103
104 /**
105 * {@inheritDoc}
106 */
107 @Override
108 public String getAWSAccessKeyId() {
109 return this.key;
110 }
111
112 /**
113 * {@inheritDoc}
114 */
115 @Override
116 public String getAWSSecretKey() {
117 return this.secret;
118 }
119
120 }