9696import com .cloud .user .dao .AccountDao ;
9797import com .cloud .utils .Pair ;
9898import com .cloud .utils .StringUtils ;
99+ import com .cloud .utils .UriUtils ;
99100import com .cloud .utils .component .ManagerBase ;
100101import com .cloud .utils .component .PluggableService ;
101102import com .cloud .utils .db .Filter ;
107108import com .cloud .vm .Nic ;
108109import com .cloud .vm .VirtualMachine ;
109110import com .cloud .vm .VirtualMachineManager ;
110- import com .cloud .vm .dao .NicDao ;
111111import com .cloud .vm .dao .NicDetailsDao ;
112- import com .cloud .vm .dao .UserVmDao ;
113112import 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