Merge remote-tracking branch 'apache/4.18' into main

This commit is contained in:
Abhishek Kumar 2023-04-26 00:39:55 +05:30
commit 575ffc6acc
6 changed files with 118 additions and 538 deletions

View File

@ -1,25 +0,0 @@
// 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.
package org.apache.cloudstack.quota.dao;
import org.apache.cloudstack.quota.vo.ServiceOfferingVO;
import com.cloud.utils.db.GenericDao;
public interface ServiceOfferingDao extends GenericDao<ServiceOfferingVO, Long> {
ServiceOfferingVO findServiceOffering(Long vmId, long serviceOfferingId);
}

View File

@ -1,83 +0,0 @@
// 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.
package org.apache.cloudstack.quota.dao;
import java.util.Map;
import javax.inject.Inject;
import org.apache.cloudstack.quota.vo.ServiceOfferingVO;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import com.cloud.event.UsageEventVO;
import com.cloud.utils.db.DB;
import com.cloud.utils.db.GenericDaoBase;
import com.cloud.utils.db.Transaction;
import com.cloud.utils.db.TransactionCallback;
import com.cloud.utils.db.TransactionLegacy;
import com.cloud.utils.db.TransactionStatus;
import com.cloud.utils.exception.CloudRuntimeException;
@Component
@DB()
public class ServiceOfferingDaoImpl extends GenericDaoBase<ServiceOfferingVO, Long> implements ServiceOfferingDao {
protected static final Logger s_logger = Logger.getLogger(ServiceOfferingDaoImpl.class);
@Inject
UserVmDetailsDao userVmDetailsDao;
@Override
public ServiceOfferingVO findServiceOffering(final Long vmId, final long serviceOfferingId) {
return Transaction.execute(TransactionLegacy.CLOUD_DB, new TransactionCallback<ServiceOfferingVO>() {
@Override
public ServiceOfferingVO doInTransaction(final TransactionStatus status) {
ServiceOfferingVO offering = findById(serviceOfferingId);
if (offering.isDynamic()) {
if (vmId == null) {
throw new CloudRuntimeException("missing argument vmId");
}
offering.setDynamicFlag(true);
Map<String, String> dynamicOffering = userVmDetailsDao.listDetailsKeyPairs(vmId);
return getcomputeOffering(offering, dynamicOffering);
}
return offering;
}
});
}
private ServiceOfferingVO getcomputeOffering(final ServiceOfferingVO serviceOffering, final Map<String, String> customParameters) {
return Transaction.execute(TransactionLegacy.CLOUD_DB, new TransactionCallback<ServiceOfferingVO>() {
@Override
public ServiceOfferingVO doInTransaction(final TransactionStatus status) {
ServiceOfferingVO dummyoffering = new ServiceOfferingVO(serviceOffering);
dummyoffering.setDynamicFlag(true);
if (customParameters.containsKey(UsageEventVO.DynamicParameters.cpuNumber.name())) {
dummyoffering.setCpu(Integer.parseInt(customParameters.get(UsageEventVO.DynamicParameters.cpuNumber.name())));
}
if (customParameters.containsKey(UsageEventVO.DynamicParameters.cpuSpeed.name())) {
dummyoffering.setSpeed(Integer.parseInt(customParameters.get(UsageEventVO.DynamicParameters.cpuSpeed.name())));
}
if (customParameters.containsKey(UsageEventVO.DynamicParameters.memory.name())) {
dummyoffering.setRamSize(Integer.parseInt(customParameters.get(UsageEventVO.DynamicParameters.memory.name())));
}
return dummyoffering;
}
});
}
}

View File

@ -1,388 +0,0 @@
//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.
package org.apache.cloudstack.quota.vo;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Id;
import javax.persistence.GenerationType;
import javax.persistence.GeneratedValue;
import javax.persistence.TemporalType;
import javax.persistence.Temporal;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Transient;
import com.cloud.offering.ServiceOffering;
import com.cloud.utils.db.GenericDao;
@Entity
@Table(name = "service_offering")
public class ServiceOfferingVO implements ServiceOffering {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
long id;
@Column(name = "uuid")
private String uuid;
@Column(name = "name")
private String name = null;
@Column(name = "unique_name")
private String uniqueName;
@Column(name = "display_text", length = 4096)
private String displayText = null;
@Column(name = "customized")
private boolean customized;
@Column(name = GenericDao.REMOVED_COLUMN)
@Temporal(TemporalType.TIMESTAMP)
private Date removed;
@Column(name = GenericDao.CREATED_COLUMN)
private Date created;
@Enumerated(EnumType.STRING)
@Column(name = "state")
ServiceOffering.State state = ServiceOffering.State.Active;
@Column(name = "disk_offering_id")
private Long diskOfferingId;
@Column(name = "disk_offering_strictness")
private boolean diskOfferingStrictness = false;
@Column(name = "cpu")
private Integer cpu;
@Column(name = "speed")
private Integer speed;
@Column(name = "ram_size")
private Integer ramSize;
@Column(name = "nw_rate")
private Integer rateMbps;
@Column(name = "mc_rate")
private Integer multicastRateMbps;
@Column(name = "ha_enabled")
private boolean offerHA;
@Column(name = "limit_cpu_use")
private boolean limitCpuUse;
@Column(name = "is_volatile")
private boolean volatileVm;
@Column(name = "host_tag")
private String hostTag;
@Column(name = "default_use")
private boolean defaultUse;
@Column(name = "vm_type")
private String vmType;
@Column(name = "sort_key")
int sortKey;
@Column(name = "deployment_planner")
private String deploymentPlanner = null;
@Column(name = "dynamic_scaling_enabled")
private boolean dynamicScalingEnabled;
@Column(name = "system_use")
private boolean systemUse;
@Transient
Map<String, String> details = new HashMap<String, String>();
@Transient
boolean isDynamic;
protected ServiceOfferingVO() {
super();
}
public ServiceOfferingVO(ServiceOfferingVO offering) {
id = offering.getId();
diskOfferingId = offering.getDiskOfferingId();
name = offering.getName();
displayText = offering.getDisplayText();
customized = true;
cpu = offering.getCpu();
ramSize = offering.getRamSize();
speed = offering.getSpeed();
rateMbps = offering.getRateMbps();
multicastRateMbps = offering.getMulticastRateMbps();
offerHA = offering.isOfferHA();
limitCpuUse = offering.getLimitCpuUse();
volatileVm = offering.isVolatileVm();
hostTag = offering.getHostTag();
vmType = offering.getSystemVmType();
systemUse = offering.isSystemUse();
dynamicScalingEnabled = offering.isDynamicScalingEnabled();
diskOfferingStrictness = offering.diskOfferingStrictness;
}
@Override
public boolean isOfferHA() {
return offerHA;
}
public void setOfferHA(boolean offerHA) {
this.offerHA = offerHA;
}
@Override
public boolean getLimitCpuUse() {
return limitCpuUse;
}
public void setLimitResourceUse(boolean limitCpuUse) {
this.limitCpuUse = limitCpuUse;
}
@Override
public boolean getDefaultUse() {
return defaultUse;
}
@Override
public Integer getCpu() {
return cpu;
}
public void setCpu(int cpu) {
this.cpu = cpu;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public void setRamSize(int ramSize) {
this.ramSize = ramSize;
}
@Override
public Integer getSpeed() {
return speed;
}
@Override
public Integer getRamSize() {
return ramSize;
}
public void setRateMbps(Integer rateMbps) {
this.rateMbps = rateMbps;
}
@Override
public Integer getRateMbps() {
return rateMbps;
}
public void setMulticastRateMbps(Integer multicastRateMbps) {
this.multicastRateMbps = multicastRateMbps;
}
@Override
public Integer getMulticastRateMbps() {
return multicastRateMbps;
}
public void setHostTag(String hostTag) {
this.hostTag = hostTag;
}
@Override
public String getHostTag() {
return hostTag;
}
@Override
public String getSystemVmType() {
return vmType;
}
@Override
public void setSortKey(int key) {
sortKey = key;
}
@Override
public int getSortKey() {
return sortKey;
}
@Override
public boolean isVolatileVm() {
return volatileVm;
}
@Override
public String getDeploymentPlanner() {
return deploymentPlanner;
}
public Map<String, String> getDetails() {
return details;
}
public String getDetail(String name) {
return details.get(name);
}
public void addDetail(String name, String value) {
details.put(name, value);
}
public void setDetails(Map<String, String> details) {
this.details = details;
}
@Override
public boolean isDynamic() {
return cpu == null || speed == null || ramSize == null || isDynamic;
}
public void setDynamicFlag(boolean isdynamic) {
isDynamic = isdynamic;
}
@Override
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Override
public String getName() {
return name;
}
@Override
public boolean isSystemUse() {
return systemUse;
}
@Override
public void setName(String name) {
this.name = name;
}
@Override
public String getUniqueName() {
return uniqueName;
}
@Override
public void setUniqueName(String uniqueName) {
this.uniqueName = uniqueName;
}
@Override
public String getDisplayText() {
return displayText;
}
@Override
public void setDisplayText(String displayText) {
this.displayText = displayText;
}
@Override
public boolean isCustomized() {
return customized;
}
@Override
public void setCustomized(boolean customized) {
this.customized = customized;
}
public void setRemoved(Date removed) {
this.removed = removed;
}
@Override
public Date getRemoved() {
return removed;
}
@Override
public Date getCreated() {
return created;
}
@Override
public ServiceOffering.State getState() {
return state;
}
@Override
public void setState(ServiceOffering.State state) {
this.state = state;
}
@Override
public Long getDiskOfferingId() {
return diskOfferingId;
}
@Override
public Boolean getDiskOfferingStrictness() {
return diskOfferingStrictness;
}
@Override
public void setDiskOfferingStrictness(boolean diskOfferingStrictness) {
}
public void setDiskOfferingId(Long diskOfferingId) {
this.diskOfferingId = diskOfferingId;
}
@Override
public String getUuid() {
return uuid;
}
@Override
public boolean isDynamicScalingEnabled() {
return dynamicScalingEnabled;
}
}

View File

@ -25,7 +25,6 @@
<bean id="QuotaEmailTemplatesDao"
class="org.apache.cloudstack.quota.dao.QuotaEmailTemplatesDaoImpl" />
<bean id="QuotaUsageDao" class="org.apache.cloudstack.quota.dao.QuotaUsageDaoImpl" />
<bean id="ServiceOfferingDao" class="org.apache.cloudstack.quota.dao.ServiceOfferingDaoImpl" />
<bean id="UserVmDetailsDao" class="org.apache.cloudstack.quota.dao.UserVmDetailsDaoImpl" />
<bean id="QuotaManager" class="org.apache.cloudstack.quota.QuotaManagerImpl" />

View File

@ -18,16 +18,15 @@ package org.apache.cloudstack.storage.resource;
import static io.netty.buffer.Unpooled.copiedBuffer;
import static io.netty.handler.codec.http.HttpHeaders.Names.CONNECTION;
import static io.netty.handler.codec.http.HttpHeaders.Names.CONTENT_LENGTH;
import static io.netty.handler.codec.http.HttpHeaders.Names.CONTENT_TYPE;
import java.io.IOException;
import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import io.netty.handler.codec.DecoderException;
import org.apache.cloudstack.storage.template.UploadEntity;
import org.apache.cloudstack.utils.imagestore.ImageStoreUtil;
import org.apache.commons.lang3.StringUtils;
@ -41,6 +40,7 @@ import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.DecoderException;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpContent;
@ -79,16 +79,46 @@ public class HttpUploadServerHandler extends SimpleChannelInboundHandler<HttpObj
private boolean requestProcessed = false;
private static final String HEADER_SIGNATURE = "x-signature";
enum UploadHeader {
SIGNATURE("x-signature"),
METADATA("x-metadata"),
EXPIRES("x-expires"),
HOST("x-forwarded-host"),
CONTENT_LENGTH("content-length");
private static final String HEADER_METADATA = "x-metadata";
private static final String HEADER_EXPIRES = "x-expires";
private static final String HEADER_HOST = "x-forwarded-host";
private final String name;
UploadHeader(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public static UploadHeader fromName(String name) {
for (UploadHeader type : values()) {
if (type.getName().equalsIgnoreCase(name)) {
return type;
}
}
return null;
}
}
private static long processTimeout;
protected Map<UploadHeader, String> getUploadRequestUsefulHeaders(HttpHeaders headers) {
Map<UploadHeader, String> headerMap = new HashMap<>();
for (Entry<String, String> entry : headers) {
UploadHeader headerType = UploadHeader.fromName(entry.getKey());
if (headerType != null) {
headerMap.put(headerType, entry.getValue());
}
}
for (UploadHeader type : UploadHeader.values()) {
logger.info(String.format("HEADER: %s=%s", type, headerMap.get(type)));
}
return headerMap;
}
public HttpUploadServerHandler(NfsSecondaryStorageResource storageResource) {
this.storageResource = storageResource;
}
@ -123,36 +153,8 @@ public class HttpUploadServerHandler extends SimpleChannelInboundHandler<HttpObj
URI uri = new URI(request.getUri());
String signature = null;
String expires = null;
String metadata = null;
String hostname = null;
long contentLength = 0;
for (Entry<String, String> entry : request.headers()) {
switch (entry.getKey()) {
case HEADER_SIGNATURE:
signature = entry.getValue();
break;
case HEADER_METADATA:
metadata = entry.getValue();
break;
case HEADER_EXPIRES:
expires = entry.getValue();
break;
case HEADER_HOST:
hostname = entry.getValue();
break;
case HttpHeaders.Names.CONTENT_LENGTH:
contentLength = Long.parseLong(entry.getValue());
break;
}
}
logger.info("HEADER: signature=" + signature);
logger.info("HEADER: metadata=" + metadata);
logger.info("HEADER: expires=" + expires);
logger.info("HEADER: hostname=" + hostname);
logger.info("HEADER: content-length=" + contentLength);
Map<UploadHeader, String> headers = getUploadRequestUsefulHeaders(request.headers());
long contentLength = headers.get(UploadHeader.CONTENT_LENGTH) != null ? Long.parseLong(headers.get(UploadHeader.CONTENT_LENGTH)) : 0;
QueryStringDecoder decoderQuery = new QueryStringDecoder(uri);
Map<String, List<String>> uriAttributes = decoderQuery.parameters();
uuid = uriAttributes.get("uuid").get(0);
@ -160,9 +162,10 @@ public class HttpUploadServerHandler extends SimpleChannelInboundHandler<HttpObj
UploadEntity uploadEntity = null;
try {
// Validate the request here
storageResource.validatePostUploadRequest(signature, metadata, expires, hostname, contentLength, uuid);
storageResource.validatePostUploadRequest(headers.get(UploadHeader.SIGNATURE), headers.get(UploadHeader.METADATA),
headers.get(UploadHeader.EXPIRES), headers.get(UploadHeader.HOST), contentLength, uuid);
//create an upload entity. This will fail if entity already exists.
uploadEntity = storageResource.createUploadEntity(uuid, metadata, contentLength);
uploadEntity = storageResource.createUploadEntity(uuid, headers.get(UploadHeader.METADATA), contentLength);
} catch (InvalidParameterValueException ex) {
logger.error("post request validation failed", ex);
responseContent.append(ex.getMessage());
@ -280,7 +283,7 @@ public class HttpUploadServerHandler extends SimpleChannelInboundHandler<HttpObj
response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8");
if (!close) {
// There's no need to add 'content-length' header if this is the last response.
response.headers().set(CONTENT_LENGTH, buf.readableBytes());
response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, buf.readableBytes());
}
// Write the response.
ChannelFuture future = channel.writeAndFlush(response);

View File

@ -0,0 +1,74 @@
// 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.
package org.apache.cloudstack.storage.resource;
import java.util.Map;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mockito;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnitRunner;
import io.netty.handler.codec.http.DefaultHttpHeaders;
import io.netty.handler.codec.http.HttpHeaders;
@RunWith(MockitoJUnitRunner.class)
public class HttpUploadServerHandlerTest {
@Spy
@InjectMocks
HttpUploadServerHandler httpUploadServerHandler = new HttpUploadServerHandler(Mockito.mock(NfsSecondaryStorageResource.class));
private void runGetUploadRequestUsefulHeadersTestForHost(String hostHeaderKey, String hostHeaderValue) {
HttpHeaders request = new DefaultHttpHeaders();
request.add(hostHeaderKey, hostHeaderValue);
Map<HttpUploadServerHandler.UploadHeader, String> headers = httpUploadServerHandler.getUploadRequestUsefulHeaders(request);
Assert.assertEquals(hostHeaderValue, headers.get(HttpUploadServerHandler.UploadHeader.HOST));
}
@Test
public void testGetUploadRequestUsefulHeadersHeaderKeyDifferentCase() {
String host = "SomeHost";
runGetUploadRequestUsefulHeadersTestForHost(HttpUploadServerHandler.UploadHeader.HOST.getName(), host);
runGetUploadRequestUsefulHeadersTestForHost(HttpUploadServerHandler.UploadHeader.HOST.getName().toUpperCase(), host);
runGetUploadRequestUsefulHeadersTestForHost("X-Forwarded-Host", host);
}
@Test
public void testGetUploadRequestUsefulHeadersAllKeys() {
HttpHeaders request = new DefaultHttpHeaders();
String sign = "Sign";
String metadata = "met";
String expires = "ex";
String host = "SomeHost";
long contentLength = 100L;
request.add("x-Signature", sign);
request.add("X-metadata", metadata);
request.add("X-EXPIRES", expires);
request.add("X-Forwarded-Host", host);
request.add(HttpHeaders.Names.CONTENT_LENGTH, String.valueOf(contentLength));
Map<HttpUploadServerHandler.UploadHeader, String> headers = httpUploadServerHandler.getUploadRequestUsefulHeaders(request);
Assert.assertEquals(sign, headers.get(HttpUploadServerHandler.UploadHeader.SIGNATURE));
Assert.assertEquals(metadata, headers.get(HttpUploadServerHandler.UploadHeader.METADATA));
Assert.assertEquals(expires, headers.get(HttpUploadServerHandler.UploadHeader.EXPIRES));
Assert.assertEquals(host, headers.get(HttpUploadServerHandler.UploadHeader.HOST));
Assert.assertEquals(String.valueOf(contentLength), headers.get(HttpUploadServerHandler.UploadHeader.CONTENT_LENGTH));
}
}