6060import org .springframework .stereotype .Component ;
6161
6262import com .cloud .domain .dao .DomainDao ;
63+ import com .cloud .event .ActionEvent ;
64+ import com .cloud .event .EventTypes ;
6365import com .cloud .exception .InvalidParameterValueException ;
6466import com .cloud .exception .PermissionDeniedException ;
6567import com .cloud .network .Network ;
6668import com .cloud .network .dao .NetworkDao ;
6769import com .cloud .network .dao .NetworkVO ;
68- import com .cloud .projects .Project ;
6970import com .cloud .user .Account ;
7071import com .cloud .user .AccountManager ;
7172import com .cloud .utils .Pair ;
7273import com .cloud .utils .StringUtils ;
73- import com .cloud .utils .Ternary ;
7474import com .cloud .utils .component .ManagerBase ;
7575import com .cloud .utils .component .PluggableService ;
7676import com .cloud .utils .db .Filter ;
77- import com .cloud .utils .db .SearchBuilder ;
78- import com .cloud .utils .db .SearchCriteria ;
7977import com .cloud .utils .exception .CloudRuntimeException ;
8078import com .cloud .vm .Nic ;
8179import com .cloud .vm .VirtualMachine ;
@@ -119,6 +117,7 @@ private DnsProvider getProviderByType(DnsProviderType type) {
119117 }
120118
121119 @ Override
120+ @ ActionEvent (eventType = EventTypes .EVENT_DNS_SERVER_ADD , eventDescription = "Adding a DNS Server" )
122121 public DnsServer addDnsServer (AddDnsServerCmd cmd ) {
123122 Account caller = CallContext .current ().getCallingAccount ();
124123 DnsServer existing = dnsServerDao .findByUrlAndAccount (cmd .getUrl (), caller .getId ());
@@ -139,7 +138,7 @@ public DnsServer addDnsServer(AddDnsServerCmd cmd) {
139138 publicDomainSuffix = DnsProviderUtil .normalizeDomain (publicDomainSuffix );
140139 }
141140
142- DnsProviderType type = cmd .getProviderType ();
141+ DnsProviderType type = cmd .getProvider ();
143142 DnsServerVO server = new DnsServerVO (cmd .getName (), cmd .getUrl (), cmd .getPort (), cmd .getExternalServerId (), type ,
144143 cmd .getDnsUserName (), cmd .getCredentials (), isDnsPublic , publicDomainSuffix , cmd .getNameServers (),
145144 caller .getAccountId (), caller .getDomainId ());
@@ -159,12 +158,15 @@ public DnsServer addDnsServer(AddDnsServerCmd cmd) {
159158 @ Override
160159 public ListResponse <DnsServerResponse > listDnsServers (ListDnsServersCmd cmd ) {
161160 Pair <List <DnsServerVO >, Integer > result = searchForDnsServerInternal (cmd );
161+ ListResponse <DnsServerResponse > response = new ListResponse <>();
162+ if (result == null ) {
163+ return response ;
164+ }
162165 List <String > serverIds = new ArrayList <>();
163166 for (DnsServer server : result .first ()) {
164167 serverIds .add (server .getUuid ());
165168 }
166169 List <DnsServerJoinVO > joinResult = dnsServerJoinDao .listByUuids (serverIds );
167- ListResponse <DnsServerResponse > response = new ListResponse <>();
168170 List <DnsServerResponse > serverResponses = new ArrayList <>();
169171 for (DnsServerJoinVO server : joinResult ) {
170172 serverResponses .add (createDnsServerResponse (server ));
@@ -176,64 +178,20 @@ public ListResponse<DnsServerResponse> listDnsServers(ListDnsServersCmd cmd) {
176178 private Pair <List <DnsServerVO >, Integer > searchForDnsServerInternal (ListDnsServersCmd cmd ) {
177179 Long dnsServerId = cmd .getId ();
178180 Account caller = CallContext .current ().getCallingAccount ();
179- Long domainId = cmd .getDomainId ();
180- boolean isRecursive = cmd .isRecursive ();
181-
182- // Step 1: Build ACL search parameters based on caller permissions
183- List <Long > permittedAccountIds = new ArrayList <>();
184- Ternary <Long , Boolean , Project .ListProjectResourcesCriteria > domainIdRecursiveListProject = new Ternary <>(
185- domainId , isRecursive , null );
186- accountMgr .buildACLSearchParameters (caller , dnsServerId , cmd .getAccountName (), null , permittedAccountIds ,
187- domainIdRecursiveListProject , cmd .listAll (), false );
188-
189- domainId = domainIdRecursiveListProject .first ();
190- isRecursive = domainIdRecursiveListProject .second ();
191- Project .ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject .third ();
192- Filter searchFilter = new Filter (DnsServerVO .class , ApiConstants .ID , true , cmd .getStartIndex (), cmd .getPageSizeVal ());
193-
194- // Step 2: Search for caller's own DNS servers using standard ACL pattern
195- SearchBuilder <DnsServerVO > sb = dnsServerDao .createSearchBuilder ();
196- accountMgr .buildACLSearchBuilder (sb , domainId , isRecursive , permittedAccountIds , listProjectResourcesCriteria );
197- sb .and (ApiConstants .STATE , sb .entity ().getState (), SearchCriteria .Op .EQ );
198- sb .and (ApiConstants .PROVIDER_TYPE , sb .entity ().getProviderType (), SearchCriteria .Op .EQ );
199- sb .done ();
200-
201- SearchCriteria <DnsServerVO > sc = sb .create ();
202- accountMgr .buildACLSearchCriteria (sc , domainId , isRecursive , permittedAccountIds , listProjectResourcesCriteria );
203- sc .setParameters (ApiConstants .STATE , DnsServer .State .Enabled );
204- sc .setParameters (ApiConstants .PROVIDER_TYPE , cmd .getProviderType ());
205-
206- Pair <List <DnsServerVO >, Integer > ownServersPair = dnsServerDao .searchAndCount (sc , searchFilter );
207- List <DnsServerVO > dnsServers = new ArrayList <>(ownServersPair .first ());
208- int count = ownServersPair .second ();
209- if (cmd .getId () == null ) {
210- Set <Long > parentDomainIds = domainDao .getDomainParentIds (caller .getDomainId ());
211- if (!parentDomainIds .isEmpty ()) {
212- SearchBuilder <DnsServerVO > publicSb = dnsServerDao .createSearchBuilder ();
213- publicSb .and (ApiConstants .IS_PUBLIC , publicSb .entity ().isPublicServer (), SearchCriteria .Op .EQ );
214- publicSb .and (ApiConstants .DOMAIN_IDS , publicSb .entity ().getDomainId (), SearchCriteria .Op .IN );
215- publicSb .and (ApiConstants .STATE , publicSb .entity ().getState (), SearchCriteria .Op .EQ );
216- publicSb .and (ApiConstants .PROVIDER_TYPE , publicSb .entity ().getProviderType (), SearchCriteria .Op .EQ );
217- publicSb .done ();
218- SearchCriteria <DnsServerVO > publicSc = publicSb .create ();
219- publicSc .setParameters (ApiConstants .IS_PUBLIC , 1 );
220- publicSc .setParameters (ApiConstants .DOMAIN_IDS , parentDomainIds .toArray ());
221- publicSc .setParameters (ApiConstants .STATE , DnsServer .State .Enabled );
222- publicSc .setParameters (ApiConstants .PROVIDER_TYPE , cmd .getProviderType ());
223- List <DnsServerVO > publicServers = dnsServerDao .search (publicSc , null );
224- List <Long > ownServerIds = dnsServers .stream ().map (DnsServerVO ::getId ).collect (Collectors .toList ());
225- for (DnsServerVO publicServer : publicServers ) {
226- if (!ownServerIds .contains (publicServer .getId ())) {
227- dnsServers .add (publicServer );
228- count ++;
229- }
230- }
181+ if (dnsServerId != null ) {
182+ DnsServerVO dnsServerVO = dnsServerDao .findById (dnsServerId );
183+ if (dnsServerVO == null ) {
184+ return null ;
231185 }
186+ return new Pair <>(Collections .singletonList (dnsServerVO ), 1 );
232187 }
233- return new Pair <>(dnsServers , count );
188+ Set <Long > parentDomainIds = domainDao .getDomainParentIds (caller .getDomainId ());
189+ Filter searchFilter = new Filter (DnsServerVO .class , ApiConstants .ID , true , cmd .getStartIndex (), cmd .getPageSizeVal ());
190+ return dnsServerDao .searchDnsServer (dnsServerId , caller .getAccountId (), parentDomainIds , cmd .getProviderType (), cmd .getKeyword (), searchFilter );
234191 }
235192
236193 @ Override
194+ @ ActionEvent (eventType = EventTypes .EVENT_DNS_SERVER_UPDATE , eventDescription = "Updating DNS Server" )
237195 public DnsServer updateDnsServer (UpdateDnsServerCmd cmd ) {
238196 Long dnsServerId = cmd .getId ();
239197 DnsServerVO dnsServer = dnsServerDao .findById (dnsServerId );
@@ -242,7 +200,7 @@ public DnsServer updateDnsServer(UpdateDnsServerCmd cmd) {
242200 }
243201
244202 Account caller = CallContext .current ().getCallingAccount ();
245- accountMgr .checkAccess (caller , null , true , dnsServer );
203+ accountMgr .checkAccess (caller , dnsServer );
246204
247205 boolean validationRequired = false ;
248206 String originalUrl = dnsServer .getUrl ();
@@ -305,18 +263,20 @@ public DnsServer updateDnsServer(UpdateDnsServerCmd cmd) {
305263 }
306264
307265 @ Override
266+ @ ActionEvent (eventType = EventTypes .EVENT_DNS_SERVER_DELETE , eventDescription = "Deleting DNS Server" )
308267 public boolean deleteDnsServer (DeleteDnsServerCmd cmd ) {
309268 Long dnsServerId = cmd .getId ();
310269 DnsServerVO dnsServer = dnsServerDao .findById (dnsServerId );
311270 if (dnsServer == null ) {
312271 throw new InvalidParameterValueException (String .format ("DNS server with ID: %s not found." , dnsServerId ));
313272 }
314273 Account caller = CallContext .current ().getCallingAccount ();
315- accountMgr .checkAccess (caller , null , true , dnsServer );
274+ accountMgr .checkAccess (caller , dnsServer );
316275 return dnsServerDao .remove (dnsServerId );
317276 }
318277
319278 @ Override
279+ @ ActionEvent (eventType = EventTypes .EVENT_DNS_ZONE_DELETE , eventDescription = "Deleting DNS Zone" )
320280 public boolean deleteDnsZone (Long zoneId ) {
321281 DnsZoneVO zone = dnsZoneDao .findById (zoneId );
322282 if (zone == null ) {
@@ -340,6 +300,7 @@ public boolean deleteDnsZone(Long zoneId) {
340300 }
341301
342302 @ Override
303+ @ ActionEvent (eventType = EventTypes .EVENT_DNS_ZONE_UPDATE , eventDescription = "Updating DNS Zone" )
343304 public DnsZone updateDnsZone (UpdateDnsZoneCmd cmd ) {
344305 DnsZoneVO dnsZone = dnsZoneDao .findById (cmd .getId ());
345306 if (dnsZone == null ) {
@@ -396,7 +357,7 @@ private Pair<List<DnsZoneVO>, Integer> searchForDnsZonesInternal(ListDnsZonesCmd
396357 Account caller = CallContext .current ().getCallingAccount ();
397358 if (cmd .getDnsServerId () != null ) {
398359 DnsServer dnsServer = dnsServerDao .findById (cmd .getDnsServerId ());
399- accountMgr .checkAccess (caller , null , false , dnsServer );
360+ accountMgr .checkAccess (caller , dnsServer );
400361 }
401362 List <Long > ownDnsServerIds = dnsServerDao .listDnsServerIdsByAccountId (caller .getAccountId ());
402363 String keyword = cmd .getKeyword ();
@@ -408,6 +369,7 @@ private Pair<List<DnsZoneVO>, Integer> searchForDnsZonesInternal(ListDnsZonesCmd
408369 }
409370
410371 @ Override
372+ @ ActionEvent (eventType = EventTypes .EVENT_DNS_RECORD_CREATE , eventDescription = "Creating DNS Record" )
411373 public DnsRecordResponse createDnsRecord (CreateDnsRecordCmd cmd ) {
412374 String recordName = StringUtils .trimToEmpty (cmd .getName ()).toLowerCase ();
413375 if (StringUtils .isBlank (recordName )) {
@@ -436,6 +398,7 @@ public DnsRecordResponse createDnsRecord(CreateDnsRecordCmd cmd) {
436398 }
437399
438400 @ Override
401+ @ ActionEvent (eventType = EventTypes .EVENT_DNS_RECORD_DELETE , eventDescription = "Deleting DNS Record" )
439402 public boolean deleteDnsRecord (DeleteDnsRecordCmd cmd ) {
440403 DnsZoneVO zone = dnsZoneDao .findById (cmd .getDnsZoneId ());
441404 if (zone == null ) {
@@ -651,21 +614,6 @@ public boolean disassociateZoneFromNetwork(DisassociateDnsZoneFromNetworkCmd cmd
651614 return dnsZoneNetworkMapDao .remove (mapping .getId ());
652615 }
653616
654-
655- @ Override
656- public void checkDnsServerPermissions (Account caller , DnsServer server ) {
657- if (caller .getId () == server .getAccountId ()) {
658- return ;
659- }
660- if (!server .isPublicServer ()) {
661- throw new PermissionDeniedException (caller + "is not allowed to access the DNS server " + server .getName ());
662- }
663- Account owner = accountMgr .getAccount (server .getAccountId ());
664- if (!domainDao .isChildDomain (caller .getDomainId (), owner .getDomainId ())) {
665- throw new PermissionDeniedException (caller + "is not allowed to access the DNS server " + server .getName ());
666- }
667- }
668-
669617 @ Override
670618 public String processDnsRecordForInstance (VirtualMachine instance , Network network , Nic nic , boolean isAdd ) {
671619 long networkId = network .getId ();
0 commit comments