1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
44
45
46
47
48
49 @ToString
50 @EqualsAndHashCode(of = { "key", "secret" })
51 @Loggable(Loggable.DEBUG)
52 final class ServerCredentials implements AWSCredentials {
53
54
55
56
57 private final transient String key;
58
59
60
61
62 private final transient String secret;
63
64
65
66
67
68
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
106
107 @Override
108 public String getAWSAccessKeyId() {
109 return this.key;
110 }
111
112
113
114
115 @Override
116 public String getAWSSecretKey() {
117 return this.secret;
118 }
119
120 }