Skip to content

Commit ce6a5d7

Browse files
committed
CLOUDSTACK-3274: API Refactoring: secretkey and accesskey of the backing
store is found in plaintext in the logs.
1 parent 25b33ec commit ce6a5d7

5 files changed

Lines changed: 141 additions & 31 deletions

File tree

‎api/src/com/cloud/agent/api/to/S3TO.java‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,18 @@
1818

1919
import java.util.Date;
2020

21+
import com.cloud.agent.api.LogLevel;
22+
import com.cloud.agent.api.LogLevel.Log4jLevel;
2123
import com.cloud.storage.DataStoreRole;
2224
import com.cloud.utils.S3Utils;
2325

2426
public final class S3TO implements S3Utils.ClientOptions, DataStoreTO {
2527

2628
private Long id;
2729
private String uuid;
30+
@LogLevel(Log4jLevel.Off)
2831
private String accessKey;
32+
@LogLevel(Log4jLevel.Off)
2933
private String secretKey;
3034
private String endPoint;
3135
private String bucketName;
@@ -68,10 +72,12 @@ public S3TO(final Long id, final String uuid, final String accessKey,
6872
@Override
6973
public boolean equals(final Object thatObject) {
7074

71-
if (this == thatObject)
75+
if (this == thatObject) {
7276
return true;
73-
if (thatObject == null || getClass() != thatObject.getClass())
77+
}
78+
if (thatObject == null || getClass() != thatObject.getClass()) {
7479
return false;
80+
}
7581

7682
final S3TO thatS3TO = (S3TO) thatObject;
7783

‎server/src/com/cloud/api/ApiServer.java‎

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -184,14 +184,14 @@ void initComponent() {
184184
public static ApiServer getInstance() {
185185
return s_instance;
186186
}
187-
188-
@Override
189-
public boolean configure(String name, Map<String, Object> params)
190-
throws ConfigurationException {
191-
init();
192-
return true;
193-
}
194-
187+
188+
@Override
189+
public boolean configure(String name, Map<String, Object> params)
190+
throws ConfigurationException {
191+
init();
192+
return true;
193+
}
194+
195195
public void init() {
196196
Integer apiPort = null; // api port, null by default
197197
SearchCriteria<ConfigurationVO> sc = _configDao.createSearchCriteria();
@@ -288,12 +288,13 @@ public void handle(HttpRequest request, HttpResponse response, HttpContext conte
288288
parameterMap.put(param.getName(), new String[] { param.getValue() });
289289
}
290290

291-
// Get the type of http method being used.
291+
// Get the type of http method being used.
292292
parameterMap.put("httpmethod", new String[] { request.getRequestLine().getMethod() });
293293

294294
// Check responseType, if not among valid types, fallback to JSON
295-
if (!(responseType.equals(BaseCmd.RESPONSE_TYPE_JSON) || responseType.equals(BaseCmd.RESPONSE_TYPE_XML)))
295+
if (!(responseType.equals(BaseCmd.RESPONSE_TYPE_JSON) || responseType.equals(BaseCmd.RESPONSE_TYPE_XML))) {
296296
responseType = BaseCmd.RESPONSE_TYPE_XML;
297+
}
297298

298299
try {
299300
// always trust commands from API port, user context will always be UID_SYSTEM/ACCOUNT_ID_SYSTEM
@@ -313,7 +314,7 @@ public void handle(HttpRequest request, HttpResponse response, HttpContext conte
313314
throw e;
314315
}
315316
} finally {
316-
s_accessLogger.info(sb.toString());
317+
s_accessLogger.info(StringUtils.cleanString(sb.toString()));
317318
UserContext.unregisterContext();
318319
}
319320
}
@@ -365,7 +366,7 @@ public String handleRequest(Map params, String responseType, StringBuffer auditT
365366
cmdObj.configure();
366367
cmdObj.setFullUrlParams(paramMap);
367368
cmdObj.setResponseType(responseType);
368-
cmdObj.setHttpMethod(paramMap.get("httpmethod").toString());
369+
cmdObj.setHttpMethod(paramMap.get("httpmethod").toString());
369370

370371
// This is where the command is either serialized, or directly dispatched
371372
response = queueCommand(cmdObj, paramMap);

‎server/src/com/cloud/api/ApiServlet.java‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ public ApiServlet() {
5858

5959
@Override
6060
public void init(ServletConfig config) throws ServletException {
61-
SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, config.getServletContext());
61+
SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, config.getServletContext());
6262
}
63-
63+
6464
@Override
6565
protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
6666
processRequest(req, resp);
@@ -72,8 +72,9 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
7272
}
7373

7474
private void utf8Fixup(HttpServletRequest req, Map<String, Object[]> params) {
75-
if (req.getQueryString() == null)
75+
if (req.getQueryString() == null) {
7676
return;
77+
}
7778

7879
String[] paramsInQueryString = req.getQueryString().split("&");
7980
if (paramsInQueryString != null) {
@@ -318,14 +319,14 @@ private void processRequest(HttpServletRequest req, HttpServletResponse resp) {
318319
}
319320
} catch (ServerApiException se) {
320321
String serializedResponseText = _apiServer.getSerializedApiError(se, params, responseType);
321-
resp.setHeader("X-Description", se.getDescription());
322+
resp.setHeader("X-Description", se.getDescription());
322323
writeResponse(resp, serializedResponseText, se.getErrorCode().getHttpCode(), responseType);
323-
auditTrailSb.append(" " + se.getErrorCode() + " " + se.getDescription());
324+
auditTrailSb.append(" " + se.getErrorCode() + " " + se.getDescription());
324325
} catch (Exception ex) {
325-
s_logger.error("unknown exception writing api response", ex);
326-
auditTrailSb.append(" unknown exception writing api response");
326+
s_logger.error("unknown exception writing api response", ex);
327+
auditTrailSb.append(" unknown exception writing api response");
327328
} finally {
328-
s_accessLogger.info(auditTrailSb.toString());
329+
s_accessLogger.info(StringUtils.cleanString(auditTrailSb.toString()));
329330
if (s_logger.isDebugEnabled()) {
330331
s_logger.debug("===END=== " + StringUtils.cleanString(reqStr));
331332
}

‎utils/src/com/cloud/utils/StringUtils.java‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,10 @@ public static String listToCsvTags(List<String> tagsList) {
9191
tags += ",";
9292
}
9393
}
94-
}
94+
}
9595

9696
return tags;
97-
}
97+
}
9898

9999
public static String getExceptionStackInfo(Throwable e) {
100100
StringBuffer sb = new StringBuffer();
@@ -131,22 +131,24 @@ public static String unicodeEscape(String s) {
131131
}
132132

133133
public static String getMaskedPasswordForDisplay(String password) {
134-
if(password == null || password.isEmpty())
134+
if(password == null || password.isEmpty()) {
135135
return "*";
136+
}
136137

137138
StringBuffer sb = new StringBuffer();
138139
sb.append(password.charAt(0));
139-
for(int i = 1; i < password.length(); i++)
140+
for(int i = 1; i < password.length(); i++) {
140141
sb.append("*");
142+
}
141143

142144
return sb.toString();
143145
}
144146

145147
// removes a password request param and it's value
146-
private static final Pattern REGEX_PASSWORD_QUERYSTRING = Pattern.compile("&?password=.*?(?=[&'\"])");
148+
private static final Pattern REGEX_PASSWORD_QUERYSTRING = Pattern.compile("&?(password|accesskey|secretkey)=.*?(?=[&'\"])");
147149

148-
// removes a password property from a response json object
149-
private static final Pattern REGEX_PASSWORD_JSON = Pattern.compile("\"password\":\".*?\",?");
150+
// removes a password/accesskey/ property from a response json object
151+
private static final Pattern REGEX_PASSWORD_JSON = Pattern.compile("\"(password|accesskey|secretkey)\":\".*?\",?");
150152

151153
// Responsible for stripping sensitive content from request and response strings
152154
public static String cleanString(String stringToClean){

‎utils/test/com/cloud/utils/StringUtilsTest.java‎

Lines changed: 102 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class StringUtilsTest {
2424
@Test
2525
public void testCleanPasswordFromJsonObjectAtEnd() {
2626
String input = "{\"foo\":\"bar\",\"password\":\"test\"}";
27-
//TODO: It would be nice to clean up the regex in question to not
27+
//TODO: It would be nice to clean up the regex in question to not
2828
//have to return the trailing comma in the expected string below
2929
String expected = "{\"foo\":\"bar\",}";
3030
String result = StringUtils.cleanString(input);
@@ -78,7 +78,7 @@ public void testCleanPasswordFromRequestStringWithMultiplePasswords() {
7878
String result = StringUtils.cleanString(input);
7979
assertEquals(result, expected);
8080
}
81-
81+
8282
@Test
8383
public void testCleanPasswordFromRequestStringMatchedAtEndSingleQuote() {
8484
String input = "'username=foo&password=bar'";
@@ -108,4 +108,104 @@ public void testJoin() {
108108
assertEquals("a-b-c", StringUtils.join("-", "a", "b", "c"));
109109
assertEquals("", StringUtils.join("-"));
110110
}
111+
112+
@Test
113+
public void testCleanSecretkeyFromJsonObjectAtEnd() {
114+
String input = "{\"foo\":\"bar\",\"secretkey\":\"test\"}";
115+
// TODO: It would be nice to clean up the regex in question to not
116+
// have to return the trailing comma in the expected string below
117+
String expected = "{\"foo\":\"bar\",}";
118+
String result = StringUtils.cleanString(input);
119+
assertEquals(result, expected);
120+
}
121+
122+
@Test
123+
public void testCleanSecretkeyFromJsonObjectInMiddle() {
124+
String input = "{\"foo\":\"bar\",\"secretkey\":\"test\",\"test\":\"blah\"}";
125+
String expected = "{\"foo\":\"bar\",\"test\":\"blah\"}";
126+
String result = StringUtils.cleanString(input);
127+
assertEquals(result, expected);
128+
}
129+
130+
@Test
131+
public void testCleanSecretkeyFromJsonObjectAlone() {
132+
String input = "{\"secretkey\":\"test\"}";
133+
String expected = "{}";
134+
String result = StringUtils.cleanString(input);
135+
assertEquals(result, expected);
136+
}
137+
138+
@Test
139+
public void testCleanSecretkeyFromJsonObjectAtStart() {
140+
String input = "{\"secretkey\":\"test\",\"test\":\"blah\"}";
141+
String expected = "{\"test\":\"blah\"}";
142+
String result = StringUtils.cleanString(input);
143+
assertEquals(result, expected);
144+
}
145+
146+
@Test
147+
public void testCleanSecretkeyFromJsonObjectWithMultiplePasswords() {
148+
String input = "{\"description\":\"foo\"}],\"secretkey\":\"bar\",\"nic\":[{\"secretkey\":\"bar2\",\"id\":\"1\"}]}";
149+
String expected = "{\"description\":\"foo\"}],\"nic\":[{\"id\":\"1\"}]}";
150+
String result = StringUtils.cleanString(input);
151+
assertEquals(result, expected);
152+
}
153+
154+
@Test
155+
public void testCleanAccesskeyFromJsonObjectAtEnd() {
156+
String input = "{\"foo\":\"bar\",\"accesskey\":\"test\"}";
157+
// TODO: It would be nice to clean up the regex in question to not
158+
// have to return the trailing comma in the expected string below
159+
String expected = "{\"foo\":\"bar\",}";
160+
String result = StringUtils.cleanString(input);
161+
assertEquals(result, expected);
162+
}
163+
164+
@Test
165+
public void testCleanAccesskeyFromJsonObjectInMiddle() {
166+
String input = "{\"foo\":\"bar\",\"accesskey\":\"test\",\"test\":\"blah\"}";
167+
String expected = "{\"foo\":\"bar\",\"test\":\"blah\"}";
168+
String result = StringUtils.cleanString(input);
169+
assertEquals(result, expected);
170+
}
171+
172+
@Test
173+
public void testCleanAccesskeyFromJsonObjectAlone() {
174+
String input = "{\"accesskey\":\"test\"}";
175+
String expected = "{}";
176+
String result = StringUtils.cleanString(input);
177+
assertEquals(result, expected);
178+
}
179+
180+
@Test
181+
public void testCleanAccesskeyFromJsonObjectAtStart() {
182+
String input = "{\"accesskey\":\"test\",\"test\":\"blah\"}";
183+
String expected = "{\"test\":\"blah\"}";
184+
String result = StringUtils.cleanString(input);
185+
assertEquals(result, expected);
186+
}
187+
188+
@Test
189+
public void testCleanAccesskeyFromJsonObjectWithMultiplePasswords() {
190+
String input = "{\"description\":\"foo\"}],\"accesskey\":\"bar\",\"nic\":[{\"accesskey\":\"bar2\",\"id\":\"1\"}]}";
191+
String expected = "{\"description\":\"foo\"}],\"nic\":[{\"id\":\"1\"}]}";
192+
String result = StringUtils.cleanString(input);
193+
assertEquals(result, expected);
194+
}
195+
196+
@Test
197+
public void testCleanAccesskeyFromRequestString() {
198+
String input = "username=foo&accesskey=bar&url=foobar";
199+
String expected = "username=foo&url=foobar";
200+
String result = StringUtils.cleanString(input);
201+
assertEquals(result, expected);
202+
}
203+
204+
@Test
205+
public void testCleanSecretkeyFromRequestString() {
206+
String input = "username=foo&secretkey=bar&url=foobar";
207+
String expected = "username=foo&url=foobar";
208+
String result = StringUtils.cleanString(input);
209+
assertEquals(result, expected);
210+
}
111211
}

0 commit comments

Comments
 (0)