Skip to content

Commit 158fe4f

Browse files
DaanHooglandsudo87weizhouapache
authored
DNS Provider URL Validation (#13821)
* validate DNS server URLs in provider framework * fixes * Apply suggestion from @DaanHoogland * address (some) review comments * restrict pvt/site-local urls to root admin only * trim url before passing to validation method * fix minor comment * restrict Add/Update/Delete Dns server api for root admin * fix minor comment * fix unit test * address review comments * fix build error * server: optimize DnsProviderManagerImplTest --------- Co-authored-by: Daan Hoogland <dahn@apache.org> Co-authored-by: Manoj Kumar <manojkr.itbhu@gmail.com> Co-authored-by: Wei Zhou <weizhou@apache.org>
1 parent 4e4a26b commit 158fe4f

6 files changed

Lines changed: 274 additions & 181 deletions

File tree

‎api/src/main/java/org/apache/cloudstack/api/command/user/dns/AddDnsServerCmd.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
requestHasSensitiveInfo = true,
4747
responseHasSensitiveInfo = false,
4848
since = "4.23.0",
49-
authorized = {RoleType.Admin, RoleType.ResourceAdmin, RoleType.DomainAdmin, RoleType.User})
49+
authorized = {RoleType.Admin})
5050
public class AddDnsServerCmd extends BaseCmd {
5151

5252
/////////////////////////////////////////////////////

‎api/src/main/java/org/apache/cloudstack/api/command/user/dns/DeleteDnsServerCmd.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
entityType = {DnsServer.class},
4141
requestHasSensitiveInfo = false, responseHasSensitiveInfo = false,
4242
since = "4.23.0",
43-
authorized = {RoleType.Admin, RoleType.ResourceAdmin, RoleType.DomainAdmin, RoleType.User})
43+
authorized = {RoleType.Admin})
4444
public class DeleteDnsServerCmd extends BaseAsyncCmd {
4545

4646
/////////////////////////////////////////////////////

‎api/src/main/java/org/apache/cloudstack/api/command/user/dns/UpdateDnsServerCmd.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
entityType = {DnsServer.class},
4242
requestHasSensitiveInfo = true, responseHasSensitiveInfo = false,
4343
since = "4.23.0",
44-
authorized = {RoleType.Admin, RoleType.ResourceAdmin, RoleType.DomainAdmin, RoleType.User})
44+
authorized = {RoleType.Admin})
4545
public class UpdateDnsServerCmd extends BaseCmd {
4646

4747
/////////////////////////////////////////////////////

engine/schema/src/main/resources/META-INF/db/views/nic_dns_view.sql renamed to engine/schema/src/main/resources/META-INF/db/views/cloud.nic_dns_view.sql

File renamed without changes.

‎server/src/main/java/org/apache/cloudstack/dns/DnsProviderManagerImpl.java‎

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
import com.cloud.user.dao.AccountDao;
9797
import com.cloud.utils.Pair;
9898
import com.cloud.utils.StringUtils;
99+
import com.cloud.utils.UriUtils;
99100
import com.cloud.utils.component.ManagerBase;
100101
import com.cloud.utils.component.PluggableService;
101102
import com.cloud.utils.db.Filter;
@@ -107,9 +108,7 @@
107108
import com.cloud.vm.Nic;
108109
import com.cloud.vm.VirtualMachine;
109110
import com.cloud.vm.VirtualMachineManager;
110-
import com.cloud.vm.dao.NicDao;
111111
import com.cloud.vm.dao.NicDetailsDao;
112-
import com.cloud.vm.dao.UserVmDao;
113112
import com.cloud.vm.dao.VMInstanceDao;
114113

115114
@Component
@@ -126,10 +125,6 @@ public class DnsProviderManagerImpl extends ManagerBase implements DnsProviderMa
126125
@Inject
127126
DnsZoneNetworkMapDao dnsZoneNetworkMapDao;
128127
@Inject
129-
UserVmDao userVmDao;
130-
@Inject
131-
NicDao nicDao;
132-
@Inject
133128
DomainDao domainDao;
134129
@Inject
135130
DnsZoneJoinDao dnsZoneJoinDao;
@@ -162,14 +157,36 @@ private DnsProvider getProviderByType(DnsProviderType type) {
162157
throw new CloudRuntimeException("No plugin found for DNS provider type: " + type);
163158
}
164159

160+
/**
161+
* Rejects a DNS provider URL that resolves to an illegal address before any provider client
162+
* is given the chance to connect to it. See {@link UriUtils#validateUrl(String)} for the exact rules
163+
* enforced (including the requirement that the URL declares an {@code http}/{@code https} scheme).
164+
*
165+
* @throws InvalidParameterValueException if the URL is blank, fails validation
166+
*/
167+
private void validateDnsServerUrl(String trimmedUrl) {
168+
if (StringUtils.isBlank(trimmedUrl)) {
169+
throw new InvalidParameterValueException("URL cannot be blank.");
170+
}
171+
try {
172+
UriUtils.validateUrl(trimmedUrl);
173+
} catch (IllegalArgumentException e) {
174+
throw new InvalidParameterValueException(e.getMessage());
175+
}
176+
}
177+
165178
@Override
166179
@ActionEvent(eventType = EventTypes.EVENT_DNS_SERVER_ADD, eventDescription = "Adding a DNS Server")
167180
public DnsServer addDnsServer(AddDnsServerCmd cmd) {
168181
Account caller = CallContext.current().getCallingAccount();
169-
DnsServer existing = dnsServerDao.findByUrlAndAccount(cmd.getUrl(), caller.getId());
182+
enforceRootAdminOnly(caller.getId());
183+
184+
String dnsUrl = StringUtils.trim(cmd.getUrl());
185+
validateDnsServerUrl(dnsUrl);
186+
DnsServer existing = dnsServerDao.findByUrlAndAccount(dnsUrl, caller.getId());
170187
if (existing != null) {
171188
throw new InvalidParameterValueException(
172-
"This Account already has a DNS server integration for URL: " + cmd.getUrl());
189+
"This Account already has a DNS server integration for URL: " + dnsUrl);
173190
}
174191

175192
boolean isDnsPublic = cmd.isPublic();
@@ -190,7 +207,7 @@ public DnsServer addDnsServer(AddDnsServerCmd cmd) {
190207
}
191208

192209
DnsProviderType type = cmd.getProvider();
193-
DnsServerVO server = new DnsServerVO(cmd.getName(), cmd.getUrl(), cmd.getPort(), type,
210+
DnsServerVO server = new DnsServerVO(cmd.getName(), dnsUrl, cmd.getPort(), type,
194211
cmd.getDnsUserName(), cmd.getDnsApiKey(), isDnsPublic, publicDomainSuffix, cmd.getNameServers(),
195212
caller.getAccountId(), caller.getDomainId());
196213

@@ -245,6 +262,8 @@ public DnsServer updateDnsServer(UpdateDnsServerCmd cmd) {
245262
}
246263

247264
Account caller = CallContext.current().getCallingAccount();
265+
enforceRootAdminOnly(caller.getId());
266+
248267
accountMgr.checkAccess(caller, null, true, dnsServer);
249268

250269
boolean validationRequired = false;
@@ -255,13 +274,15 @@ public DnsServer updateDnsServer(UpdateDnsServerCmd cmd) {
255274
dnsServer.setName(cmd.getName());
256275
}
257276

258-
if (cmd.getUrl() != null) {
259-
if (!cmd.getUrl().equals(originalUrl)) {
260-
DnsServer duplicate = dnsServerDao.findByUrlAndAccount(cmd.getUrl(), dnsServer.getAccountId());
277+
if (StringUtils.isNotBlank(cmd.getUrl())) {
278+
String dnsUrl = StringUtils.trim(cmd.getUrl());
279+
if (!dnsUrl.equals(originalUrl)) {
280+
validateDnsServerUrl(dnsUrl);
281+
DnsServer duplicate = dnsServerDao.findByUrlAndAccount(dnsUrl, dnsServer.getAccountId());
261282
if (duplicate != null && duplicate.getId() != dnsServer.getId()) {
262283
throw new InvalidParameterValueException("Another DNS server with this URL already exists.");
263284
}
264-
dnsServer.setUrl(cmd.getUrl());
285+
dnsServer.setUrl(dnsUrl);
265286
validationRequired = true;
266287
}
267288
}
@@ -330,6 +351,7 @@ public boolean deleteDnsServer(DeleteDnsServerCmd cmd) {
330351
throw new InvalidParameterValueException(String.format("DNS server with ID: %s not found.", dnsServerId));
331352
}
332353
Account caller = CallContext.current().getCallingAccount();
354+
enforceRootAdminOnly(caller.getId());
333355
accountMgr.checkAccess(caller, null, true, dnsServer);
334356
return Transaction.execute((TransactionCallback<Boolean>) status -> {
335357
if (cmd.getCleanup()) {
@@ -1263,4 +1285,10 @@ public void syncDnsRecordsState(Long instanceId, String dnsRecordUrl, long dnsZo
12631285
provider.addRecord(dnsServer, dnsZone, recordIpv6);
12641286
}
12651287
}
1288+
1289+
void enforceRootAdminOnly(Long callerId) {
1290+
if (!accountMgr.isRootAdmin(callerId)) {
1291+
throw new PermissionDeniedException("This API can only be called by root admin");
1292+
}
1293+
}
12661294
}

0 commit comments

Comments
 (0)