/
EnvironmentVariableConfig.java
287 lines (240 loc) · 9.75 KB
/
EnvironmentVariableConfig.java
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*************************GO-LICENSE-START*********************************
* Copyright 2014 ThoughtWorks, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*************************GO-LICENSE-END***********************************/
package com.thoughtworks.go.config;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.PostConstruct;
import com.thoughtworks.go.domain.ConfigErrors;
import com.thoughtworks.go.domain.PersistentObject;
import com.thoughtworks.go.security.GoCipher;
import com.thoughtworks.go.util.StringUtil;
import com.thoughtworks.go.util.command.EnvironmentVariableContext;
import org.apache.commons.lang.BooleanUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.bouncycastle.crypto.InvalidCipherTextException;
/**
* @understands an environment variable value that will be passed to a job when it is run
*/
@ConfigTag("variable")
public class EnvironmentVariableConfig extends PersistentObject implements Serializable, Validatable, ParamsAttributeAware, PasswordEncrypter {
@ConfigAttribute(value = "name", optional = false)
private String name;
@ConfigAttribute(value = "secure", optional = true)
private boolean isSecure = false;
@ConfigSubtag
private VariableValueConfig value;
@ConfigSubtag
private EncryptedVariableValueConfig encryptedValue;
private long entityId;
private String entityType;
private final ConfigErrors configErrors = new ConfigErrors();
public static final String NAME = "name";
public static final String VALUE = "valueForDisplay";
public static final String SECURE = "secure";
private GoCipher goCipher = null;
public static final String ISCHANGED = "isChanged";
public EnvironmentVariableConfig() {
this.goCipher = new GoCipher();
}
public EnvironmentVariableConfig(String name, String value) {
this(new GoCipher(), name, value, false);
}
public EnvironmentVariableConfig(GoCipher goCipher, String name, String value, boolean isSecure) {
this(goCipher);
this.name = name;
this.isSecure = isSecure;
setValue(value);
}
public EnvironmentVariableConfig(EnvironmentVariableConfig variable) {
this(variable.goCipher, variable.name, variable.getValue(), variable.isSecure);
}
private String encrypt(String value) {
try {
return goCipher.encrypt(value);
} catch (InvalidCipherTextException e) {
throw new RuntimeException(e);
}
}
public EnvironmentVariableConfig(GoCipher goCipher) {
this.goCipher = goCipher;
}
@Override public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
EnvironmentVariableConfig that = (EnvironmentVariableConfig) o;
if (isSecure != that.isSecure) {
return false;
}
if (configErrors != null ? !configErrors.equals(that.configErrors) : that.configErrors != null) {
return false;
}
if (encryptedValue != null ? !encryptedValue.equals(that.encryptedValue) : that.encryptedValue != null) {
return false;
}
if (name != null ? !name.equals(that.name) : that.name != null) {
return false;
}
if (value != null ? !value.equals(that.value) : that.value != null) {
return false;
}
return true;
}
@Override
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + (isSecure ? 1 : 0);
result = 31 * result + (value != null ? value.hashCode() : 0);
result = 31 * result + (encryptedValue != null ? encryptedValue.hashCode() : 0);
result = 31 * result + (configErrors != null ? configErrors.hashCode() : 0);
return result;
}
public String getName() {
return name;
}
void addTo(EnvironmentVariableContext context) {
context.setProperty(name, getValue(), isSecure());
}
public void addToIfExists(EnvironmentVariableContext context) {
if (context.hasProperty(name)) {
addTo(context);
}
}
public void validateName(Map<String, EnvironmentVariableConfig> variableConfigMap, ValidationContext validationContext) {
String currentVariableName = name.toLowerCase();
String parentDisplayName = validationContext.getParentDisplayName();
CaseInsensitiveString parentName = getParentNameFrom(validationContext);
if (StringUtil.isBlank(currentVariableName)) {
configErrors.add(NAME, String.format("Environment Variable cannot have an empty name for %s '%s'.", parentDisplayName, parentName));
return;
}
EnvironmentVariableConfig configWithSameName = variableConfigMap.get(currentVariableName);
if (configWithSameName == null) {
variableConfigMap.put(currentVariableName, this);
} else {
configWithSameName.addNameConflictError(parentDisplayName, parentName);
this.addNameConflictError(parentDisplayName, parentName);
}
}
private void addNameConflictError(String parentDisplayName, Object parentName) {
configErrors.add(NAME, String.format("Environment Variable name '%s' is not unique for %s '%s'.", this.name, parentDisplayName, parentName));
}
private CaseInsensitiveString getParentNameFrom(ValidationContext validationContext) {
EnvironmentVariableScope parent = (EnvironmentVariableScope) validationContext.getParent();
return parent.name();
}
/**
* We do this to avoid breaking encapsulation.
* We should remove this method when we move to Hibernate.
*/
public Map<String, Object> getSqlCriteria() {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("variableName", name);
map.put("variableValue", getValue());
map.put("isSecure", isSecure);
return map;
}
public boolean hasName(String variableName) {
return name.equals(variableName);
}
public void validate(ValidationContext validationContext) {
try {
getValue();
} catch (Exception e) {
errors().add(VALUE, String.format("Encrypted value for variable named '%s' is invalid. This usually happens when the cipher text is modified to have an invalid value.", getName()));
}
}
public ConfigErrors errors() {
return configErrors;
}
public void addError(String fieldName, String message) {
configErrors.add(fieldName, message);
}
public boolean isSecure() {
return isSecure;
}
public boolean isPlain() {
return !isSecure();
}
private void setValue(String value){
if (isSecure) {
encryptedValue = new EncryptedVariableValueConfig(encrypt(value));
} else {
this.value = new VariableValueConfig(value);
}
}
public String getValue() {
if (isSecure) {
try {
return goCipher.decrypt(encryptedValue.getValue());
} catch (InvalidCipherTextException e) {
throw new RuntimeException(String.format("Could not decrypt secure environment variable value for name %s", getName()), e);
}
} else {
return value.getValue();
}
}
public String getDisplayValue(){
if(isSecure()) return "****";
return getValue();
}
public String getEncryptedValue() {
return encryptedValue.getValue();
}
public void setConfigAttributes(Object attributes) {
Map attributeMap = (Map) attributes;
this.name = (String) attributeMap.get(EnvironmentVariableConfig.NAME);
String value = (String) attributeMap.get(EnvironmentVariableConfig.VALUE);
if(StringUtil.isBlank(name) && StringUtil.isBlank(value)){
throw new IllegalArgumentException(String.format("Need not null/empty name & value %s:%s",this.name, value));
}
this.isSecure = BooleanUtils.toBoolean((String) attributeMap.get(EnvironmentVariableConfig.SECURE));
Boolean isChanged = BooleanUtils.toBoolean((String) attributeMap.get(EnvironmentVariableConfig.ISCHANGED));
if(isSecure){
this.encryptedValue = isChanged? new EncryptedVariableValueConfig(encrypt(value)) : new EncryptedVariableValueConfig(value);
}else{
this.value = new VariableValueConfig(value);
}
}
@PostConstruct
public void ensureEncrypted() {
if(isSecure && value != null) {
encryptedValue = new EncryptedVariableValueConfig(encrypt(value.getValue()));
value = null;
}
}
public String getValueForDisplay(){
if(isSecure){
return getEncryptedValue();
}
return value.getValue();
}
public void setEntityId(Long entityId) {
this.entityId = entityId;
}
public void setEntityType(String entityType) {
this.entityType = entityType;
}
}