2020import java .util .ArrayList ;
2121import java .util .Collections ;
2222import java .util .List ;
23+ import java .util .stream .Collectors ;
2324
2425import javax .inject .Inject ;
2526
@@ -117,6 +118,11 @@ public DnsServer addDnsServer(AddDnsServerCmd cmd) {
117118 isDnsPublic = false ;
118119 publicDomainSuffix = null ;
119120 }
121+
122+ if (StringUtils .isNotBlank (publicDomainSuffix )) {
123+ publicDomainSuffix = DnsUtil .normalizeDomain (publicDomainSuffix );
124+ }
125+
120126 DnsProviderType type = DnsProviderType .fromString (cmd .getProvider ());
121127 DnsServerVO server = new DnsServerVO (cmd .getName (), cmd .getUrl (), cmd .getPort (), cmd .getExternalServerId (), type ,
122128 cmd .getDnsUserName (), cmd .getCredentials (), isDnsPublic , publicDomainSuffix , cmd .getNameServers (),
@@ -208,7 +214,7 @@ public DnsServer updateDnsServer(UpdateDnsServerCmd cmd) {
208214 }
209215
210216 if (cmd .getPublicDomainSuffix () != null ) {
211- dnsServer .setPublicDomainSuffix (cmd .getPublicDomainSuffix ());
217+ dnsServer .setPublicDomainSuffix (DnsUtil . normalizeDomain ( cmd .getPublicDomainSuffix () ));
212218 }
213219
214220 if (cmd .getNameServers () != null ) {
@@ -255,6 +261,7 @@ public DnsServerResponse createDnsServerResponse(DnsServer server) {
255261 response .setId (server .getUuid ());
256262 response .setName (server .getName ());
257263 response .setUrl (server .getUrl ());
264+ response .setPort (server .getPort ());
258265 response .setProvider (server .getProviderType ());
259266 response .setPublic (server .isPublic ());
260267 response .setNameServers (server .getNameServers ());
@@ -272,7 +279,7 @@ public DnsServer getDnsServer(Long id) {
272279 public boolean deleteDnsZone (Long zoneId ) {
273280 DnsZoneVO zone = dnsZoneDao .findById (zoneId );
274281 if (zone == null ) {
275- throw new InvalidParameterValueException ("DNS zone with ID " + zoneId + " not found ." );
282+ throw new InvalidParameterValueException ("DNS zone not found for the given ID ." );
276283 }
277284
278285 Account caller = CallContext .current ().getCallingAccount ();
@@ -347,23 +354,29 @@ public ListResponse<DnsZoneResponse> listDnsZones(ListDnsZonesCmd cmd) {
347354
348355 @ Override
349356 public DnsRecordResponse createDnsRecord (CreateDnsRecordCmd cmd ) {
357+ String recordName = StringUtils .trimToEmpty (cmd .getName ()).toLowerCase ();
358+ if (StringUtils .isBlank (recordName )) {
359+ throw new InvalidParameterValueException ("Empty DNS record name is not allowed" );
360+ }
350361 DnsZoneVO zone = dnsZoneDao .findById (cmd .getDnsZoneId ());
351362 if (zone == null ) {
352363 throw new InvalidParameterValueException ("DNS zone not found." );
353364 }
354-
355365 Account caller = CallContext .current ().getCallingAccount ();
356366 accountMgr .checkAccess (caller , null , true , zone );
357367 DnsServerVO server = dnsServerDao .findById (zone .getDnsServerId ());
358368 try {
369+ DnsRecord .RecordType type = cmd .getType ();
370+ List <String > normalizedContents = cmd .getContents ().stream ()
371+ .map (value -> DnsUtil .normalizeDnsRecordValue (value , type )).collect (Collectors .toList ());
372+ DnsRecord record = new DnsRecord (recordName , type , normalizedContents , cmd .getTtl ());
359373 DnsProvider provider = getProvider (server .getProviderType ());
360- DnsRecord record = new DnsRecord (cmd .getName (), cmd .getType (), cmd .getContents (), cmd .getTtl ());
361374 String normalizedRecordName = provider .addRecord (server , zone , record );
362375 record .setName (normalizedRecordName );
363376 return createDnsRecordResponse (record );
364377 } catch (Exception ex ) {
365378 logger .error ("Failed to add DNS record via provider" , ex );
366- throw new CloudRuntimeException (String .format ("Failed to add DNS record: %s" , cmd . getName () ));
379+ throw new CloudRuntimeException (String .format ("Failed to add DNS record: %s" , recordName ));
367380 }
368381 }
369382
@@ -397,7 +410,7 @@ public boolean deleteDnsRecord(DeleteDnsRecordCmd cmd) {
397410 public ListResponse <DnsRecordResponse > listDnsRecords (ListDnsRecordsCmd cmd ) {
398411 DnsZoneVO zone = dnsZoneDao .findById (cmd .getDnsZoneId ());
399412 if (zone == null ) {
400- throw new InvalidParameterValueException (String . format ( "DNS zone with ID %s not found." , cmd . getDnsZoneId ()) );
413+ throw new InvalidParameterValueException ("DNS zone not found for the given ID." );
401414 }
402415 Account caller = CallContext .current ().getCallingAccount ();
403416 accountMgr .checkAccess (caller , null , true , zone );
@@ -435,28 +448,30 @@ public List<String> listProviderNames() {
435448
436449 @ Override
437450 public DnsZone allocateDnsZone (CreateDnsZoneCmd cmd ) {
438- Account caller = CallContext .current ().getCallingAccount ();
451+ if (StringUtils .isBlank (cmd .getName ())) {
452+ throw new InvalidParameterValueException ("DNS zone name cannot be empty" );
453+ }
454+
455+ String dnsZoneName = DnsUtil .normalizeDomain (cmd .getName ());
439456 DnsServerVO server = dnsServerDao .findById (cmd .getDnsServerId ());
440457 if (server == null ) {
441- throw new InvalidParameterValueException ("DNS server not found" );
458+ throw new InvalidParameterValueException (String . format ( "DNS server not found for the given ID: %s" , cmd . getDnsServerId ()) );
442459 }
460+
461+ Account caller = CallContext .current ().getCallingAccount ();
443462 boolean isOwner = (server .getAccountId () == caller .getId ());
444- if (!server .isPublic () && !isOwner ) {
445- throw new PermissionDeniedException ("You do not have permission to use this DNS server." );
446- }
447- DnsZone .ZoneType type = DnsZone .ZoneType .Public ;
448- if (cmd .getType () != null ) {
449- try {
450- type = DnsZone .ZoneType .valueOf (cmd .getType ());
451- } catch (IllegalArgumentException e ) {
452- throw new InvalidParameterValueException ("Invalid DNS zone Type" );
463+ if (!isOwner ) {
464+ if (!server .isPublic ()) {
465+ throw new PermissionDeniedException ("You do not have permission to use this DNS server." );
453466 }
467+ dnsZoneName = DnsUtil .appendPublicSuffixToZone (dnsZoneName , DnsUtil .normalizeDomain (server .getPublicDomainSuffix ()));
454468 }
455- DnsZoneVO existing = dnsZoneDao .findByNameServerAndType (cmd .getName (), server .getId (), type );
469+ DnsZone .ZoneType type = cmd .getType ();
470+ DnsZoneVO existing = dnsZoneDao .findByNameServerAndType (dnsZoneName , server .getId (), type );
456471 if (existing != null ) {
457472 throw new InvalidParameterValueException ("DNS zone already exists on this server." );
458473 }
459- DnsZoneVO dnsZoneVO = new DnsZoneVO (cmd . getName () , type , server .getId (), caller .getId (), caller .getDomainId (), cmd .getDescription ());
474+ DnsZoneVO dnsZoneVO = new DnsZoneVO (dnsZoneName , type , server .getId (), caller .getId (), caller .getDomainId (), cmd .getDescription ());
460475 return dnsZoneDao .persist (dnsZoneVO );
461476 }
462477
0 commit comments