예제 #1
0
    public bool ExecuteDnsActions(DnsActionDetail request, DnsHandlerConfig config, bool isDryRun = false)
    {
        if (request == null || config == null)
        {
            return(false);
        }

        string domainSuffix = GetDomainSuffix(request.Hostname);

        if (string.IsNullOrWhiteSpace(domainSuffix))
        {
            throw new Exception($"Domain suffix {domainSuffix} is not valid.");
        }

        string dnsServer = GetDnsServer(config, domainSuffix);

        if (string.IsNullOrWhiteSpace(dnsServer))
        {
            throw new Exception($"Unable to find a matching DNS server for domain suffix {domainSuffix}.");
        }

        bool isSuccess = false;

        if (request.Action.ToLower() == "add" && request.RecordType.ToLower() == "atype")
        {
            OnLogMessage("Execute", "Adding type A DNS record...");
            DnsServices.CreateARecord(request.Hostname, request.IpAddress, "", dnsServer, isDryRun);
            OnLogMessage("Execute", "Operation is successful.");
            isSuccess = true;
        }

        if (request.Action.ToLower() == "delete" && request.RecordType.ToLower() == "atype")
        {
            OnLogMessage("Execute", "Deleting type A DNS record and its associated PTR record...");
            DnsServices.DeleteARecord(request.Hostname, request.IpAddress, dnsServer, isDryRun);
            OnLogMessage("Execute", "Operation is successful.");
            isSuccess = true;
        }

        if (request.Action.ToLower() == "add" && request.RecordType.ToLower() == "ptrtype")
        {
            OnLogMessage("Execute", "Adding type PTR DNS record...");
            DnsServices.CreatePtrRecord(request.Hostname, request.IpAddress, request.DnsZone, dnsServer, isDryRun);
            OnLogMessage("Execute", "Operation is successful.");
            isSuccess = true;
        }

        if (request.Action.ToLower() == "delete" && request.RecordType.ToLower() == "ptrtype")
        {
            OnLogMessage("Execute", "Deleting type PTR DNS record...");
            DnsServices.DeletePtrRecord(request.Hostname, request.IpAddress, dnsServer, isDryRun);
            OnLogMessage("Execute", "Operation is successful.");
            isSuccess = true;
        }

        return(isSuccess);
    }
예제 #2
0
        public void DeletePtrRecord_Without_IP_Address_Throw_Exception()
        {
            // Arrange
            string hostname  = Utility.GenerateToken(8) + "." + DnsServices.GetJoinedDomainName();
            string ipAddress = "";

            // Act
            Exception ex = Assert.Throws <Exception>(() => DnsServices.DeletePtrRecord(hostname, ipAddress));

            // Assert
            Assert.That(ex.Message, Is.EqualTo("IP address is not specified."));
        }
예제 #3
0
        public void DeletePtrRecord_With_NonExistent_Hostname_IP_Address_Throw_Exception()
        {
            // Arrange
            string hostname  = Utility.GenerateToken(8) + "." + DnsServices.GetJoinedDomainName();
            string ipAddress = Utility.GetRandomIpAddress();

            // Act
            Exception ex = Assert.Throws <Exception>(() => DnsServices.DeletePtrRecord(hostname, ipAddress));

            // Assert
            Assert.That(ex.Message, Is.EqualTo("DNS PTR record is not found."));
        }
예제 #4
0
        public void DeletePtrRecord_Without_Hostname_Throw_Exception()
        {
            // Arrange
            string hostname  = "";
            string ipAddress = "";

            // Act
            Exception ex = Assert.Throws <Exception>(() => DnsServices.DeletePtrRecord(hostname, ipAddress));

            // Assert
            Assert.That(ex.Message, Is.EqualTo("Fully qualified host name is not specified."));
        }
예제 #5
0
        public void CreateARecord_Without_IP_Address_Throw_Exception()
        {
            // Arrange
            string hostname  = "XXXXXX";
            string ipAddress = "";

            // Act
            Exception ex = Assert.Throws <Exception>(() => DnsServices.CreateARecord(hostname, ipAddress));

            // Assert
            Assert.That(ex.Message, Is.EqualTo("IP address is not specified."));
        }
예제 #6
0
        public void DeleteARecord_With_NonExistent_Hostname_Throw_Exception()
        {
            // Arrange
            string hostname  = "XXXXXX";
            string ipAddress = "";

            // Act
            Exception ex = Assert.Throws <Exception>(() => DnsServices.DeleteARecord(hostname, ipAddress));

            // Assert
            Assert.That(ex.Message, Is.EqualTo("DNS A record is not found."));
        }
예제 #7
0
        public void CreateARecord_With_Invalid_Dns_Server_Throw_Exception()
        {
            // Arrange
            string hostname      = "XXXXXX";
            string ipAddress     = "10.2.0.9";
            string dnsServerName = "XXXXXX";

            // Act
            Exception ex = Assert.Throws <Exception>(() => DnsServices.CreateARecord(hostname, ipAddress, dnsServerName: dnsServerName));

            // Assert
            Assert.IsTrue(ex.Message.Contains("The RPC server is unavailable."));
        }
예제 #8
0
        public void CreateARecord_With_Invalid_Dns_Zone_Throw_Exception()
        {
            // Arrange
            string hostname      = "XXXXXX";
            string ipAddress     = "10.2.0.9";
            string dnsServerName = ".";
            string dnsZone       = "XXXXXX";

            // Act
            Exception ex = Assert.Throws <Exception>(() => DnsServices.CreateARecord(hostname, ipAddress, dnsZone, dnsServerName));

            // Assert
            Assert.IsTrue(ex.Message.Contains("Generic failure"));
        }
예제 #9
0
        public void CreatePtrRecord_With_Invalid_Dns_Zone_Throw_Exception()
        {
            // Arrange
            string hostname      = $"{Utility.GenerateToken( 8 )}.{DnsServices.GetJoinedDomainName()}";
            string ipAddress     = "10.2.0.97";
            string dnsServerName = ".";
            string dnsZone       = "XXXXXX";

            // Act
            Exception ex = Assert.Throws <Exception>(() => DnsServices.CreatePtrRecord(hostname, ipAddress, dnsZone, dnsServerName));

            // Assert
            Assert.IsTrue(ex.Message.Contains("Generic failure"));
        }
예제 #10
0
        public void CreatePtrRecord_With_Invalid_Dns_Server_Throw_Exception()
        {
            // Arrange
            string hostname      = $"{Utility.GenerateToken( 8 )}.{DnsServices.GetJoinedDomainName()}";
            string ipAddress     = Utility.GetRandomIpAddress();
            string dnsServerName = "XXXXXX";
            string dnsZone       = "XXXXXX";

            // Act
            Exception ex = Assert.Throws <Exception>(() => DnsServices.CreatePtrRecord(hostname, ipAddress, dnsZone, dnsServerName));

            // Assert
            Assert.IsTrue(ex.Message.Contains("The RPC server is unavailable."));
        }
예제 #11
0
        public void CreatePtrRecord_Without_Dns_Zone_Throw_Exception()
        {
            // Arrange
            string hostname      = $"{Utility.GenerateToken( 8 )}.{DnsServices.GetJoinedDomainName()}";
            string ipAddress     = "10.2.0.97";
            string dnsServerName = ".";
            string dnsZone       = "";

            // Act
            Exception ex = Assert.Throws <Exception>(() => DnsServices.CreatePtrRecord(hostname, ipAddress, dnsZone, dnsServerName));

            // Assert
            Assert.That(ex.Message, Is.EqualTo("DNS zone is not specified."));
        }
예제 #12
0
        public void DeletePtrRecord_With_Valid_Details_Succeed()
        {
            // Arrange
            string hostname      = $"{Utility.GenerateToken( 8 )}.{DnsServices.GetJoinedDomainName()}";
            string ipAddress     = "10.2.0.97"; // TODO: Generate dynamic ip address
            string dnsServerName = ".";
            string dnsZone       = "0.2.10.in-addr.arpa";

            // Act
            DnsServices.CreatePtrRecord(hostname, ipAddress, dnsZone, dnsServerName);
            DnsServices.DeletePtrRecord(hostname, ipAddress);

            // Assert
            Assert.IsFalse(DnsServices.IsExistingPtrRecord(hostname, ipAddress));
        }
예제 #13
0
        public void DeleteARecord_Valid_Hostname_Succeed()
        {
            // Arrange
            string hostname      = Utility.GenerateToken(8) + "." + DnsServices.GetJoinedDomainName();
            string ipAddress     = Utility.GetRandomIpAddress();
            string dnsServerName = ".";
            string dnsZone       = DnsServices.GetJoinedDomainName();

            // Act
            DnsServices.CreateARecord(hostname, ipAddress, dnsZone, dnsServerName);
            DnsServices.DeleteARecord(hostname, ipAddress);

            // Assert
            Assert.IsFalse(DnsServices.IsExistingARecord(hostname, ipAddress));
        }