|
20 | 20 | import java.util.ArrayList; |
21 | 21 | import java.util.Collections; |
22 | 22 | import java.util.List; |
| 23 | +import java.util.stream.Collectors; |
23 | 24 |
|
24 | 25 | import javax.inject.Inject; |
25 | 26 |
|
@@ -117,6 +118,11 @@ public DnsServer addDnsServer(AddDnsServerCmd cmd) { |
117 | 118 | isDnsPublic = false; |
118 | 119 | publicDomainSuffix = null; |
119 | 120 | } |
| 121 | + |
| 122 | + if (StringUtils.isNotBlank(publicDomainSuffix)) { |
| 123 | + publicDomainSuffix = DnsUtil.normalizeDomain(publicDomainSuffix); |
| 124 | + } |
| 125 | + |
120 | 126 | DnsProviderType type = DnsProviderType.fromString(cmd.getProvider()); |
121 | 127 | DnsServerVO server = new DnsServerVO(cmd.getName(), cmd.getUrl(), cmd.getPort(), cmd.getExternalServerId(), type, |
122 | 128 | cmd.getDnsUserName(), cmd.getCredentials(), isDnsPublic, publicDomainSuffix, cmd.getNameServers(), |
@@ -208,7 +214,7 @@ public DnsServer updateDnsServer(UpdateDnsServerCmd cmd) { |
208 | 214 | } |
209 | 215 |
|
210 | 216 | if (cmd.getPublicDomainSuffix() != null) { |
211 | | - dnsServer.setPublicDomainSuffix(cmd.getPublicDomainSuffix()); |
| 217 | + dnsServer.setPublicDomainSuffix(DnsUtil.normalizeDomain(cmd.getPublicDomainSuffix())); |
212 | 218 | } |
213 | 219 |
|
214 | 220 | if (cmd.getNameServers() != null) { |
@@ -255,6 +261,7 @@ public DnsServerResponse createDnsServerResponse(DnsServer server) { |
255 | 261 | response.setId(server.getUuid()); |
256 | 262 | response.setName(server.getName()); |
257 | 263 | response.setUrl(server.getUrl()); |
| 264 | + response.setPort(server.getPort()); |
258 | 265 | response.setProvider(server.getProviderType()); |
259 | 266 | response.setPublic(server.isPublic()); |
260 | 267 | response.setNameServers(server.getNameServers()); |
@@ -347,23 +354,29 @@ public ListResponse<DnsZoneResponse> listDnsZones(ListDnsZonesCmd cmd) { |
347 | 354 |
|
348 | 355 | @Override |
349 | 356 | 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 | + } |
350 | 361 | DnsZoneVO zone = dnsZoneDao.findById(cmd.getDnsZoneId()); |
351 | 362 | if (zone == null) { |
352 | 363 | throw new InvalidParameterValueException("DNS zone not found."); |
353 | 364 | } |
354 | | - |
355 | 365 | Account caller = CallContext.current().getCallingAccount(); |
356 | 366 | accountMgr.checkAccess(caller, null, true, zone); |
357 | 367 | DnsServerVO server = dnsServerDao.findById(zone.getDnsServerId()); |
358 | 368 | 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()); |
359 | 373 | DnsProvider provider = getProvider(server.getProviderType()); |
360 | | - DnsRecord record = new DnsRecord(cmd.getName(), cmd.getType(), cmd.getContents(), cmd.getTtl()); |
361 | 374 | String normalizedRecordName = provider.addRecord(server, zone, record); |
362 | 375 | record.setName(normalizedRecordName); |
363 | 376 | return createDnsRecordResponse(record); |
364 | 377 | } catch (Exception ex) { |
365 | 378 | 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)); |
367 | 380 | } |
368 | 381 | } |
369 | 382 |
|
@@ -435,28 +448,30 @@ public List<String> listProviderNames() { |
435 | 448 |
|
436 | 449 | @Override |
437 | 450 | 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()); |
439 | 456 | DnsServerVO server = dnsServerDao.findById(cmd.getDnsServerId()); |
440 | 457 | 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())); |
442 | 459 | } |
| 460 | + |
| 461 | + Account caller = CallContext.current().getCallingAccount(); |
443 | 462 | 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."); |
453 | 466 | } |
| 467 | + dnsZoneName = DnsUtil.appendPublicSuffixToZone(dnsZoneName, DnsUtil.normalizeDomain(server.getPublicDomainSuffix())); |
454 | 468 | } |
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); |
456 | 471 | if (existing != null) { |
457 | 472 | throw new InvalidParameterValueException("DNS zone already exists on this server."); |
458 | 473 | } |
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()); |
460 | 475 | return dnsZoneDao.persist(dnsZoneVO); |
461 | 476 | } |
462 | 477 |
|
|
0 commit comments