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);
}

}
1 change: 0 additions & 1 deletion ui/src/utils/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ export const pollJobPlugin = {
export const notifierPlugin = {
install (app) {
app.config.globalProperties.$notifyError = function (error) {
console.log(error)
var msg = i18n.global.t('message.request.failed')
var desc = ''
if (error && error.response) {
Expand Down
1 change: 0 additions & 1 deletion ui/src/utils/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ const err = (error) => {
const response = error.response
let countNotify = store.getters.countNotify
if (response) {
console.log(response)
Comment thread
Dogface2k marked this conversation as resolved.
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
99 changes: 99 additions & 0 deletions ui/tests/unit/views/infra/AddObjectStorage.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// 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.

import mockAxios from '../../../mock/mockAxios'
import AddObjectStorage from '@/views/infra/AddObjectStorage'
import { mount } from '@vue/test-utils'

jest.mock('axios', () => mockAxios)
jest.mock('@/vue-app', () => ({
vueProps: {
$localStorage: {
get: jest.fn(() => null)
}
}
}))

describe('Views > infra > AddObjectStorage.vue', () => {
beforeEach(() => {
mockAxios.mockReset()
})

it('submits addObjectStoragePool using POST with the request in the body', async () => {
mockAxios.mockResolvedValue({})

const params = {
name: 'test-store',
provider: 'MinIO',
url: 'https://object-storage.example.test',
'details[0].key': 'accesskey',
'details[0].value': 'test-access-key',
'details[1].key': 'secretkey',
'details[1].value': 'test-secret+key&with=symbols'
}

await AddObjectStorage.methods.addObjectStore(params)

expect(mockAxios).toHaveBeenCalledTimes(1)
const request = mockAxios.mock.calls[0][0]
expect(request).toMatchObject({
url: '/',
method: 'POST'
})
expect(request.params).toBeUndefined()
expect(request.data).toBeInstanceOf(URLSearchParams)
expect(request.data.get('command')).toBe('addObjectStoragePool')
expect(request.data.get('response')).toBe('json')
expect(request.data.get('url')).toBe(params.url)
expect(request.data.get('details[0].value')).toBe(params['details[0].value'])
expect(request.data.get('details[1].value')).toBe(params['details[1].value'])
})

it('propagates an addObjectStoragePool API failure', async () => {
const error = new Error('request failed')
mockAxios.mockRejectedValue(error)

await expect(AddObjectStorage.methods.addObjectStore({})).rejects.toBe(error)
})

it('masks the object storage secret key', () => {
const wrapper = mount(AddObjectStorage, {
props: {
resource: {}
},
global: {
mocks: {
$getApiParams: jest.fn(() => ({
name: {},
provider: {},
url: {},
size: {}
})),
$t: key => key
},
provide: {
parentFetchData: jest.fn()
}
}
})

const secretKeyInput = wrapper.find('input[type="password"]')

expect(secretKeyInput.exists()).toBe(true)
expect(secretKeyInput.attributes('autocomplete')).toBe('off')
})
})
Loading