Skip to content

Commit ac28571

Browse files
RodrigoDLopezLopez
andauthored
externalizes used protocol to upload isos, volumes and templates (apache#6506)
Co-authored-by: Lopez <rodrigo@scclouds.com.br>
1 parent 3cba426 commit ac28571

9 files changed

Lines changed: 80 additions & 17 deletions

File tree

api/src/main/java/com/cloud/storage/VolumeApiService.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ public interface VolumeApiService {
4646
, true // not sure if this is to be dynamic
4747
, ConfigKey.Scope.Global);
4848

49+
ConfigKey<Boolean> UseHttpsToUpload = new ConfigKey<Boolean>("Advanced",
50+
Boolean.class,
51+
"use.https.to.upload",
52+
"true",
53+
"Determines the protocol (HTTPS or HTTP) ACS will use to generate links to upload ISOs, volumes, and templates. When set as 'true', ACS will use protocol HTTPS, otherwise, it will use protocol HTTP. Default value is 'true'.",
54+
true,
55+
ConfigKey.Scope.StoragePool);
56+
4957
/**
5058
* Creates the database object for a volume based on the given criteria
5159
*

server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,8 +429,9 @@ public GetUploadParamsResponse doInTransaction(TransactionStatus status) throws
429429
GetUploadParamsResponse response = new GetUploadParamsResponse();
430430

431431
String ssvmUrlDomain = _configDao.getValue(Config.SecStorageSecureCopyCert.key());
432+
String protocol = UseHttpsToUpload.value() ? "https" : "http";
432433

433-
String url = ImageStoreUtil.generatePostUploadUrl(ssvmUrlDomain, ep.getPublicAddr(), vol.getUuid());
434+
String url = ImageStoreUtil.generatePostUploadUrl(ssvmUrlDomain, ep.getPublicAddr(), vol.getUuid(), protocol);
434435
response.setPostURL(new URL(url));
435436

436437
// set the post url, this is used in the monitoring thread to determine the SSVM
@@ -4445,6 +4446,6 @@ public String getConfigComponentName() {
44454446

44464447
@Override
44474448
public ConfigKey<?>[] getConfigKeys() {
4448-
return new ConfigKey<?>[] {ConcurrentMigrationsThresholdPerDatastore, AllowUserExpungeRecoverVolume, MatchStoragePoolTagsWithDiskOffering};
4449+
return new ConfigKey<?>[] {ConcurrentMigrationsThresholdPerDatastore, AllowUserExpungeRecoverVolume, MatchStoragePoolTagsWithDiskOffering, UseHttpsToUpload};
44494450
}
44504451
}

server/src/main/java/com/cloud/template/TemplateManagerImpl.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import javax.inject.Inject;
3535
import javax.naming.ConfigurationException;
3636

37+
import com.cloud.storage.VolumeApiService;
3738
import org.apache.cloudstack.acl.SecurityChecker.AccessType;
3839
import org.apache.cloudstack.api.ApiConstants;
3940
import org.apache.cloudstack.api.BaseListTemplateOrIsoPermissionsCmd;
@@ -379,8 +380,9 @@ private GetUploadParamsResponse registerPostUploadInternal(TemplateAdapter adapt
379380
TemplateOrVolumePostUploadCommand firstCommand = payload.get(0);
380381

381382
String ssvmUrlDomain = _configDao.getValue(Config.SecStorageSecureCopyCert.key());
383+
String protocol = VolumeApiService.UseHttpsToUpload.value() ? "https" : "http";
382384

383-
String url = ImageStoreUtil.generatePostUploadUrl(ssvmUrlDomain, firstCommand.getRemoteEndPoint(), firstCommand.getEntityUUID());
385+
String url = ImageStoreUtil.generatePostUploadUrl(ssvmUrlDomain, firstCommand.getRemoteEndPoint(), firstCommand.getEntityUUID(), protocol);
384386
response.setPostURL(new URL(url));
385387

386388
// set the post url, this is used in the monitoring thread to determine the SSVM

services/secondary-storage/controller/src/main/java/org/apache/cloudstack/secondarystorage/SecondaryStorageManagerImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import javax.inject.Inject;
3131
import javax.naming.ConfigurationException;
3232

33+
import com.cloud.storage.VolumeApiService;
3334
import com.cloud.utils.PasswordGenerator;
3435
import org.apache.cloudstack.agent.lb.IndirectAgentLB;
3536
import org.apache.cloudstack.ca.CAManager;
@@ -1175,6 +1176,10 @@ public boolean finalizeVirtualMachineProfile(VirtualMachineProfile profile, Depl
11751176
s_logger.debug(String.format("Boot args for machine profile [%s]: [%s].", profile.toString(), bootArgs));
11761177
}
11771178

1179+
boolean useHttpsToUpload = BooleanUtils.toBooleanDefaultIfNull(VolumeApiService.UseHttpsToUpload.value(), true);
1180+
s_logger.debug(String.format("Setting UseHttpsToUpload config on cmdline with [%s] value.", useHttpsToUpload));
1181+
buf.append(" useHttpsToUpload=").append(useHttpsToUpload);
1182+
11781183
return true;
11791184
}
11801185

systemvm/debian/etc/apache2/vhost.template

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
Allow from 127.0.0.0/255.0.0.0 ::1/128
4040
</Directory>
4141

42+
# Include HTTP configuration **IF SET**
43+
IncludeOptional /etc/apache2/http.conf
44+
4245
</VirtualHost>
4346

4447
<IfModule mod_ssl.c>
@@ -83,8 +86,8 @@
8386
Allow from 127.0.0.0/255.0.0.0 ::1/128
8487
</Directory>
8588

86-
# Include CORS configuration **IF SET**
87-
IncludeOptional /etc/apache2/[cC][oO][rR][sS].conf
89+
# Include HTTPS configuration **IF SET**
90+
IncludeOptional /etc/apache2/https.conf
8891

8992
# SSL Engine Switch:
9093
# Enable/Disable SSL for this virtual host.

systemvm/debian/opt/cloud/bin/setup/common.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,9 @@ parse_cmd_line() {
882882
privatekey)
883883
export PRIVATEKEY=$VALUE
884884
;;
885+
useHttpsToUpload)
886+
export USEHTTPS=$VALUE
887+
;;
885888
esac
886889
done
887890
echo -e "\n\t}\n}" >> ${CHEF_TMP_FILE}

systemvm/debian/opt/cloud/bin/setup/secstorage.sh

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,33 @@ setup_secstorage() {
5050
a2enmod proxy_http
5151
a2enmod headers
5252

53-
cat >/etc/apache2/cors.conf <<CORS
53+
if [ -z $USEHTTPS ] | $USEHTTPS ; then
54+
if [ -f /etc/apache2/http.conf ]; then
55+
rm -rf /etc/apache2/http.conf
56+
fi
57+
cat >/etc/apache2/https.conf <<HTTPS
5458
RewriteEngine On
5559
RewriteCond %{HTTPS} =on
5660
RewriteCond %{REQUEST_METHOD} =POST
5761
RewriteRule ^/upload/(.*) http://127.0.0.1:8210/upload?uuid=\$1 [P,L]
5862
Header always set Access-Control-Allow-Origin "*"
5963
Header always set Access-Control-Allow-Methods "POST, OPTIONS"
6064
Header always set Access-Control-Allow-Headers "x-requested-with, Content-Type, origin, authorization, accept, client-security-token, x-signature, x-metadata, x-expires"
61-
CORS
65+
HTTPS
66+
else
67+
if [ -f /etc/apache2/https.conf ]; then
68+
rm -rf /etc/apache2/https.conf
69+
fi
70+
cat >/etc/apache2/http.conf <<HTTP
71+
RewriteEngine On
72+
RewriteCond %{REQUEST_METHOD} =POST
73+
RewriteRule ^/upload/(.*) http://127.0.0.1:8210/upload?uuid=\$1 [P,L]
74+
Header always set Access-Control-Allow-Origin "*"
75+
Header always set Access-Control-Allow-Methods "POST, OPTIONS"
76+
Header always set Access-Control-Allow-Headers "x-requested-with, Content-Type, origin, authorization, accept, client-security-token, x-signature, x-metadata, x-expires"
77+
HTTP
78+
fi
79+
6280

6381
disable_rpfilter
6482
enable_fwding 0

utils/src/main/java/org/apache/cloudstack/utils/imagestore/ImageStoreUtil.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
public class ImageStoreUtil {
2727
public static final Logger s_logger = Logger.getLogger(ImageStoreUtil.class.getName());
2828

29-
public static String generatePostUploadUrl(String ssvmUrlDomain, String ipAddress, String uuid) {
29+
public static String generatePostUploadUrl(String ssvmUrlDomain, String ipAddress, String uuid, String protocol) {
3030
String hostname = ipAddress;
3131

3232
//if ssvm url domain is present, use it to construct hostname in the format 1-2-3-4.domain
@@ -39,7 +39,7 @@ public static String generatePostUploadUrl(String ssvmUrlDomain, String ipAddres
3939
}
4040

4141
//only https works with postupload and url format is fixed
42-
return "https://" + hostname + "/upload/" + uuid;
42+
return String.format("%s://%s/upload/%s", protocol, hostname, uuid);
4343
}
4444

4545
// given a path, returns empty if path is supported image, and the file type if unsupported

utils/src/test/java/org/apache/cloudstack/utils/imagestore/ImageStoreUtilTest.java

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,52 @@
2626
public class ImageStoreUtilTest {
2727

2828
@Test
29-
public void testgeneratePostUploadUrl() throws MalformedURLException {
29+
public void testgenerateHttpsPostUploadUrl() throws MalformedURLException {
3030
String ssvmdomain = "*.realhostip.com";
3131
String ipAddress = "10.147.28.14";
3232
String uuid = UUID.randomUUID().toString();
33+
String protocol = "https";
3334

3435
//ssvm domain is not set
35-
String url = ImageStoreUtil.generatePostUploadUrl(null, ipAddress, uuid);
36-
assertPostUploadUrl(url, ipAddress, uuid);
36+
String url = ImageStoreUtil.generatePostUploadUrl(null, ipAddress, uuid, protocol);
37+
assertPostUploadUrl(url, ipAddress, uuid, protocol);
3738

3839
//ssvm domain is set to empty value
39-
url = ImageStoreUtil.generatePostUploadUrl("", ipAddress, uuid);
40-
assertPostUploadUrl(url, ipAddress, uuid);
40+
url = ImageStoreUtil.generatePostUploadUrl("", ipAddress, uuid, protocol);
41+
assertPostUploadUrl(url, ipAddress, uuid, protocol);
4142

4243
//ssvm domain is set to a valid value
43-
url = ImageStoreUtil.generatePostUploadUrl(ssvmdomain, ipAddress, uuid);
44-
assertPostUploadUrl(url, ipAddress.replace(".", "-") + ssvmdomain.substring(1), uuid);
44+
url = ImageStoreUtil.generatePostUploadUrl(ssvmdomain, ipAddress, uuid, protocol);
45+
assertPostUploadUrl(url, ipAddress.replace(".", "-") + ssvmdomain.substring(1), uuid, protocol);
4546
}
4647

47-
private void assertPostUploadUrl(String urlStr, String domain, String uuid) throws MalformedURLException {
48+
@Test
49+
public void testgenerateHttpPostUploadUrl() throws MalformedURLException {
50+
String ssvmdomain = "*.realhostip.com";
51+
String ipAddress = "10.147.28.14";
52+
String uuid = UUID.randomUUID().toString();
53+
String protocol = "http";
54+
55+
//ssvm domain is not set
56+
String url = ImageStoreUtil.generatePostUploadUrl(null, ipAddress, uuid, protocol);
57+
assertPostUploadUrl(url, ipAddress, uuid, protocol);
58+
59+
//ssvm domain is set to empty value
60+
url = ImageStoreUtil.generatePostUploadUrl("", ipAddress, uuid, protocol);
61+
assertPostUploadUrl(url, ipAddress, uuid, protocol);
62+
63+
//ssvm domain is set to a valid value
64+
url = ImageStoreUtil.generatePostUploadUrl(ssvmdomain, ipAddress, uuid, protocol);
65+
assertPostUploadUrl(url, ipAddress.replace(".", "-") + ssvmdomain.substring(1), uuid, protocol);
66+
}
67+
68+
private void assertPostUploadUrl(String urlStr, String domain, String uuid, String protocol) throws MalformedURLException {
4869
URL url = new URL(urlStr);
4970
Assert.assertNotNull(url);
5071
Assert.assertEquals(url.getHost(), domain);
5172
Assert.assertEquals(url.getPath(), "/upload/" + uuid);
73+
Assert.assertEquals(url.getProtocol(), protocol);
5274
}
5375

76+
5477
}

0 commit comments

Comments
 (0)