Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import java.util.Map;

@APICommand(name = "addObjectStoragePool", description = "Adds a object storage pool", responseObject = ObjectStoreResponse.class, since = "4.19.0",
requestHasSensitiveInfo = false, responseHasSensitiveInfo = false)
requestHasSensitiveInfo = true, responseHasSensitiveInfo = false)
public class AddObjectStoragePoolCmd extends BaseCmd {

/////////////////////////////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import com.cloud.exception.DiscoveryException;
import com.cloud.storage.StorageService;
import org.apache.cloudstack.api.APICommand;
import org.apache.cloudstack.api.ResponseGenerator;
import org.apache.cloudstack.api.response.ObjectStoreResponse;
import org.apache.cloudstack.context.CallContext;
Expand All @@ -38,6 +39,8 @@
import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;

@RunWith(MockitoJUnitRunner.class)
Expand Down Expand Up @@ -98,4 +101,12 @@ public void testAddObjectStore() throws DiscoveryException {
Mockito.verify(storageService, Mockito.times(1))
.discoverObjectStore(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any());
}

@Test
public void testRequestIsMarkedAsContainingSensitiveInformation() {
APICommand apiCommand = AddObjectStoragePoolCmd.class.getAnnotation(APICommand.class);

assertNotNull(apiCommand);
assertTrue(apiCommand.requestHasSensitiveInfo());
}
}
22 changes: 17 additions & 5 deletions server/src/main/java/com/cloud/api/ApiServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public class ApiServlet extends HttpServlet {
private static final Pattern GET_REQUEST_COMMANDS = Pattern.compile("^(get|list|query|find)(\\w+)+$");
private static final HashSet<String> GET_REQUEST_COMMANDS_LIST = new HashSet<>(Set.of("isaccountallowedtocreateofferingswithtags",
"readyforshutdown", "cloudianisenabled", "quotabalance", "quotasummary", "quotatarifflist", "quotaisenabled", "quotastatement", "verifyoauthcodeandgetuser"));
private static final HashSet<String> POST_REQUESTS_TO_DISABLE_LOGGING = new HashSet<>(Set.of(
private static final HashSet<String> REQUESTS_TO_DISABLE_PARAMETER_LOGGING = new HashSet<>(Set.of(
"login",
"oauthlogin",
"createaccount",
Expand All @@ -100,6 +100,7 @@ public class ApiServlet extends HttpServlet {
"updaterolepermission",
"updateprojectrolepermission",
"createstoragepool",
"addobjectstoragepool",
"addhost",
"updatehostpassword",
"addcluster",
Expand Down Expand Up @@ -237,17 +238,15 @@ void processRequestInContext(final HttpServletRequest req, final HttpServletResp

// logging the request start and end in management log for easy debugging
String reqStr = "";
String cleanQueryString = StringUtils.cleanString(req.getQueryString());
String cleanQueryString = getCleanQueryString(command, req.getQueryString(), reqParams);
if (LOGGER.isDebugEnabled()) {
reqStr = auditTrailSb.toString() + " " + cleanQueryString;
if (req.getMethod().equalsIgnoreCase("POST") && org.apache.commons.lang3.StringUtils.isNotBlank(command)) {
if (!POST_REQUESTS_TO_DISABLE_LOGGING.contains(command.toLowerCase()) && !reqParams.containsKey(ApiConstants.USER_DATA)) {
if (shouldLogRequestParameters(command, reqParams)) {
String cleanParamsString = getCleanParamsString(reqParams);
if (org.apache.commons.lang3.StringUtils.isNotBlank(cleanParamsString)) {
reqStr += "\n" + cleanParamsString;
}
} else {
reqStr += " " + command;
}
}
LOGGER.debug("===START=== " + reqStr);
Expand Down Expand Up @@ -771,4 +770,17 @@ private String getCleanParamsString(Map<String, String[]> reqParams) {

return cleanParamsString.toString();
}

protected boolean shouldLogRequestParameters(String command, Map<String, String[]> reqParams) {
return (org.apache.commons.lang3.StringUtils.isBlank(command)
|| !REQUESTS_TO_DISABLE_PARAMETER_LOGGING.contains(command.toLowerCase(java.util.Locale.ROOT)))
&& !reqParams.containsKey(ApiConstants.USER_DATA);
}

protected String getCleanQueryString(String command, String queryString, Map<String, String[]> reqParams) {
if (!shouldLogRequestParameters(command, reqParams)) {
return org.apache.commons.lang3.StringUtils.isBlank(command) ? "" : "command=" + saveLogString(command);
}
return StringUtils.cleanString(queryString);
}
}
58 changes: 58 additions & 0 deletions server/src/test/java/com/cloud/api/ApiServletTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -461,4 +461,62 @@ public void testVerify2FAWhenExpectedCommandIsNotCalled() throws UnknownHostExce

Assert.assertEquals(false, result);
}

@Test
public void shouldNotLogRequestParametersForAddObjectStoragePool() {
boolean result = servlet.shouldLogRequestParameters("addObjectStoragePool", new HashMap<>());

Assert.assertFalse(result);
}

@Test
public void shouldLogRequestParametersForCommandWithoutSensitiveParameters() {
boolean result = servlet.shouldLogRequestParameters("listZones", new HashMap<>());

Assert.assertTrue(result);
}

@Test
public void shouldNotLogRequestParametersContainingUserData() {
Map<String, String[]> params = new HashMap<>();
params.put(ApiConstants.USER_DATA, new String[] {"sensitive-user-data"});

boolean result = servlet.shouldLogRequestParameters("deployVirtualMachine", params);

Assert.assertFalse(result);
}

@Test
public void shouldReplaceQueryStringContainingUserDataWithCommandName() {
Map<String, String[]> params = new HashMap<>();
params.put(ApiConstants.USER_DATA, new String[] {"SYNTHETIC_USER_DATA"});
String queryString = "command=deployVirtualMachine&userdata=SYNTHETIC_USER_DATA";

String result = servlet.getCleanQueryString("deployVirtualMachine", queryString, params);

Assert.assertEquals("command=deployVirtualMachine", result);
Assert.assertFalse(result.contains("SYNTHETIC_USER_DATA"));
}

@Test
public void shouldReplaceSensitiveQueryStringWithCommandName() {
Map<String, String[]> params = new HashMap<>();
String queryString = "command=addObjectStoragePool&details%5B1%5D.value=SYNTHETIC_SECRET_KEY";

String result = servlet.getCleanQueryString("addObjectStoragePool", queryString, params);

Assert.assertEquals("command=addObjectStoragePool", result);
Assert.assertFalse(result.contains("SYNTHETIC_SECRET_KEY"));
}

@Test
public void shouldKeepOrdinaryQueryString() {
Map<String, String[]> params = new HashMap<>();
String queryString = "command=listZones&response=json";

String result = servlet.getCleanQueryString("listZones", queryString, params);

Assert.assertEquals(queryString, result);
}

}
56 changes: 56 additions & 0 deletions ui/src/utils/apiError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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.

function cleanLogValue (value) {
if (typeof value !== 'string') {
return undefined
}
return value.replace(/[\n\r\t]/g, '_').slice(0, 256)
}

function getCommand (config) {
if (config?.params?.command) {
return config.params.command
}

const data = config?.data
if (typeof URLSearchParams !== 'undefined' && data instanceof URLSearchParams) {
return data.get('command')
}
if (typeof data === 'string') {
return new URLSearchParams(data).get('command')
}
}

export function getSafeApiErrorDetails (error) {
const response = error?.response
const config = response?.config || error?.config
const method = cleanLogValue(config?.method)

return {
name: cleanLogValue(error?.name),
code: cleanLogValue(error?.code),
status: Number.isInteger(response?.status) ? response.status : undefined,
statusText: cleanLogValue(response?.statusText),
method: typeof method === 'string' ? method.toUpperCase() : method,
command: cleanLogValue(getCommand(config))
}
}

export function logApiError (error) {
console.error('CloudStack API request failed', getSafeApiErrorDetails(error))
}
3 changes: 2 additions & 1 deletion ui/src/utils/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import eventBus from '@/config/eventBus'
import store from '@/store'
import { sourceToken } from '@/utils/request'
import { toLocalDate, toLocaleDate } from '@/utils/date'
import { logApiError } from '@/utils/apiError'

export const pollJobPlugin = {
install (app) {
Expand Down Expand Up @@ -217,7 +218,7 @@ export const pollJobPlugin = {
export const notifierPlugin = {
install (app) {
app.config.globalProperties.$notifyError = function (error) {
console.log(error)
logApiError(error)
var msg = i18n.global.t('message.request.failed')
var desc = ''
if (error && error.response) {
Expand Down
3 changes: 2 additions & 1 deletion ui/src/utils/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import notification from 'ant-design-vue/es/notification'
import { CURRENT_PROJECT } from '@/store/mutation-types'
import { i18n } from '@/locales'
import store from '@/store'
import { logApiError } from '@/utils/apiError'

let source
const service = axios.create({
Expand All @@ -33,8 +34,8 @@ const service = axios.create({
const err = (error) => {
const response = error.response
let countNotify = store.getters.countNotify
logApiError(error)
if (response) {
console.log(response)
if (response.status === 403) {
const data = response.data
countNotify++
Expand Down
6 changes: 3 additions & 3 deletions ui/src/views/infra/AddObjectStorage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
<a-input v-model:value="form.accessKey" />
</a-form-item>
<a-form-item name="secretKey" ref="secretKey" :label="$t('label.secret.key')">
<a-input v-model:value="form.secretKey" />
<a-input-password v-model:value="form.secretKey" autocomplete="off" />
</a-form-item>
<a-form-item name="size" ref="size">
<template #label>
Expand All @@ -106,7 +106,7 @@
</template>
<script>
import { ref, reactive, toRaw } from 'vue'
import { getAPI } from '@/api'
import { postAPI } from '@/api'
import { mixinForm } from '@/utils/mixin'
import ResourceIcon from '@/components/view/ResourceIcon'
import TooltipLabel from '@/components/widgets/TooltipLabel'
Expand Down Expand Up @@ -209,7 +209,7 @@ export default {
},
addObjectStore (params) {
return new Promise((resolve, reject) => {
getAPI('addObjectStoragePool', params).then(json => {
postAPI('addObjectStoragePool', params).then(json => {
resolve()
}).catch(error => {
reject(error)
Expand Down
Loading