Search in sources :

Example 1 with DnsZone

use of com.sequenceiq.freeipa.client.model.DnsZone in project cloudbreak by hortonworks.

the class DnsZoneService method addDnsZonesForSubnetIds.

@Retryable(value = RetryableFreeIpaClientException.class, maxAttemptsExpression = RetryableFreeIpaClientException.MAX_RETRIES_EXPRESSION, backoff = @Backoff(delayExpression = RetryableFreeIpaClientException.DELAY_EXPRESSION, multiplierExpression = RetryableFreeIpaClientException.MULTIPLIER_EXPRESSION))
public AddDnsZoneForSubnetsResponse addDnsZonesForSubnetIds(AddDnsZoneForSubnetIdsRequest request, String accountId) throws FreeIpaClientException {
    Stack stack = stackService.getByEnvironmentCrnAndAccountId(request.getEnvironmentCrn(), accountId);
    MDCBuilder.buildMdcContext(stack);
    Multimap<String, String> subnetWithCidr = networkService.getFilteredSubnetWithCidr(request.getEnvironmentCrn(), stack, request.getAddDnsZoneNetwork().getNetworkId(), request.getAddDnsZoneNetwork().getSubnetIds());
    FreeIpaClient client = freeIpaClientFactory.getFreeIpaClientForStack(stack);
    AddDnsZoneForSubnetsResponse response = new AddDnsZoneForSubnetsResponse();
    for (Entry<String, String> subnet : subnetWithCidr.entries()) {
        try {
            LOGGER.info("Add subnet's [{}] reverse DNS zone", subnet);
            String subnetCidr = subnet.getValue();
            Set<DnsZone> dnsZones = client.findDnsZone(subnetCidr);
            if (dnsZones.isEmpty()) {
                LOGGER.debug("Subnet reverse DNS zone does not exists [{}], add it now", subnet);
                client.addReverseDnsZone(subnetCidr);
                response.getSuccess().add(subnet.getKey());
                LOGGER.debug("Subnet [{}] added", subnet);
            }
        } catch (RetryableFreeIpaClientException e) {
            throw e;
        } catch (FreeIpaClientException e) {
            LOGGER.warn("Can't add subnet's [{}] reverse DNS zone with cidr [{}]", subnet, subnet.getValue(), e);
            response.getFailed().putIfAbsent(subnet.getKey(), e.getMessage());
        }
    }
    return response;
}
Also used : RetryableFreeIpaClientException(com.sequenceiq.freeipa.client.RetryableFreeIpaClientException) AddDnsZoneForSubnetsResponse(com.sequenceiq.freeipa.api.v1.dns.model.AddDnsZoneForSubnetsResponse) FreeIpaClient(com.sequenceiq.freeipa.client.FreeIpaClient) FreeIpaClientException(com.sequenceiq.freeipa.client.FreeIpaClientException) RetryableFreeIpaClientException(com.sequenceiq.freeipa.client.RetryableFreeIpaClientException) DnsZone(com.sequenceiq.freeipa.client.model.DnsZone) Stack(com.sequenceiq.freeipa.entity.Stack) Retryable(org.springframework.retry.annotation.Retryable)

Example 2 with DnsZone

use of com.sequenceiq.freeipa.client.model.DnsZone in project cloudbreak by hortonworks.

the class CleanupServiceTest method testRemoveDnsEntries.

@Test
public void testRemoveDnsEntries() throws FreeIpaClientException {
    FreeIpaClient client = mock(FreeIpaClient.class);
    when(freeIpaClientFactory.getFreeIpaClientForStackId(STACK_ID)).thenReturn(client);
    DnsZone dnsZone = new DnsZone();
    String domain = "test.com";
    dnsZone.setIdnsname(domain);
    DnsZone reverseZone = new DnsZone();
    reverseZone.setIdnsname("0.10.in-addr.arpa.");
    DnsZone disappearingZone = new DnsZone();
    disappearingZone.setIdnsname("disappear");
    when(client.findAllDnsZone()).thenReturn(Set.of(dnsZone, reverseZone, disappearingZone));
    DnsRecord deleteMe = new DnsRecord();
    deleteMe.setIdnsname("deleteMe");
    deleteMe.setArecord(List.of("ignored"));
    DnsRecord notFound = new DnsRecord();
    notFound.setIdnsname("notfound");
    notFound.setArecord(List.of("ignored"));
    DnsRecord failed = new DnsRecord();
    failed.setIdnsname("failed");
    failed.setArecord(List.of("ignored"));
    DnsRecord ptrRecord = new DnsRecord();
    ptrRecord.setIdnsname("1.0");
    ptrRecord.setPtrrecord(List.of("ptrRecord"));
    when(client.findAllDnsRecordInZone(dnsZone.getIdnsname())).thenReturn(Set.of(deleteMe, notFound, failed));
    when(client.deleteDnsRecord(failed.getIdnsname(), domain)).thenThrow(new FreeIpaClientException("delete failed"));
    when(client.deleteDnsRecord(notFound.getIdnsname(), domain)).thenThrow(new FreeIpaClientException("Not found", new JsonRpcClientException(FreeIpaErrorCodes.NOT_FOUND.getValue(), "Not found", null)));
    when(client.findAllDnsRecordInZone(reverseZone.getIdnsname())).thenReturn(Set.of(ptrRecord));
    when(client.findAllDnsRecordInZone(disappearingZone.getIdnsname())).thenThrow(new FreeIpaClientException("Not found zone", new JsonRpcClientException(FreeIpaErrorCodes.NOT_FOUND.getValue(), "Not found", null)));
    Pair<Set<String>, Map<String, String>> result = cleanupService.removeDnsEntries(STACK_ID, Set.of(deleteMe.getIdnsname(), notFound.getIdnsname(), failed.getIdnsname(), "ptrRecord"), Set.of("10.0.0.1", "10.1.0.1"), domain);
    verify(client).deleteDnsRecord(deleteMe.getIdnsname(), domain);
    assertTrue(result.getFirst().containsAll(Set.of(deleteMe.getIdnsname(), notFound.getIdnsname(), "10.0.0.1")));
    assertTrue(result.getSecond().containsKey(failed.getIdnsname()));
    assertEquals("delete failed", result.getSecond().get(failed.getIdnsname()));
    assertEquals(1, result.getSecond().size());
    assertEquals(3, result.getFirst().size());
}
Also used : JsonRpcClientException(com.googlecode.jsonrpc4j.JsonRpcClientException) Set(java.util.Set) FreeIpaClient(com.sequenceiq.freeipa.client.FreeIpaClient) FreeIpaClientException(com.sequenceiq.freeipa.client.FreeIpaClientException) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) DnsRecord(com.sequenceiq.freeipa.client.model.DnsRecord) Map(java.util.Map) DnsZone(com.sequenceiq.freeipa.client.model.DnsZone) Test(org.junit.Test)

Example 3 with DnsZone

use of com.sequenceiq.freeipa.client.model.DnsZone in project cloudbreak by hortonworks.

the class UpdateDnsSoaRecordsHandlerTest method testResultContainsServerFqdns.

@Test
void testResultContainsServerFqdns() throws Exception {
    String zoneName = "example.com.";
    String fqdn1 = "foo1.example.com";
    String fqdn2 = "foo2.example.com";
    FreeIpaClient mockIpaClient = mock(FreeIpaClient.class);
    IpaServer mockIpaServer1 = mock(IpaServer.class);
    IpaServer mockIpaServer2 = mock(IpaServer.class);
    DnsZone dnsZone = mock(DnsZone.class);
    when(freeIpaClientFactory.getFreeIpaClientForStackId(any())).thenReturn(mockIpaClient);
    when(mockIpaClient.findAllServers()).thenReturn(Set.of(mockIpaServer1, mockIpaServer2));
    Mockito.lenient().when(mockIpaServer1.getFqdn()).thenReturn(fqdn1);
    when(mockIpaServer2.getFqdn()).thenReturn(fqdn2);
    when(mockIpaClient.findAllDnsZone()).thenReturn(Set.of(dnsZone));
    when(dnsZone.getIdnssoamname()).thenReturn(fqdn1 + ".");
    when(dnsZone.getIdnsname()).thenReturn(zoneName);
    CleanupEvent cleanupEvent = new CleanupEvent(1L, Set.of(), Set.of(fqdn1), Set.of(), Set.of(), Set.of(), "", "", "", "");
    UpdateDnsSoaRecordsRequest request = new UpdateDnsSoaRecordsRequest(cleanupEvent);
    underTest.accept(new Event<>(request));
    verify(eventBus).notify(eq("UPDATEDNSSOARECORDSRESPONSE"), any(Event.class));
    verify(mockIpaClient).setDnsZoneAuthoritativeNameserver(eq(zoneName), eq(fqdn2 + "."));
}
Also used : FreeIpaClient(com.sequenceiq.freeipa.client.FreeIpaClient) IpaServer(com.sequenceiq.freeipa.client.model.IpaServer) CleanupEvent(com.sequenceiq.freeipa.flow.freeipa.cleanup.CleanupEvent) UpdateDnsSoaRecordsRequest(com.sequenceiq.freeipa.flow.freeipa.downscale.event.dnssoarecords.UpdateDnsSoaRecordsRequest) Event(reactor.bus.Event) CleanupEvent(com.sequenceiq.freeipa.flow.freeipa.cleanup.CleanupEvent) DnsZone(com.sequenceiq.freeipa.client.model.DnsZone) Test(org.junit.jupiter.api.Test)

Example 4 with DnsZone

use of com.sequenceiq.freeipa.client.model.DnsZone in project cloudbreak by hortonworks.

the class DnsRecordService method deleteSrvRecords.

private void deleteSrvRecords(FreeIpaClient freeIpaClient, DnsZone dnsZone, Set<DnsRecord> allDnsRecordsInZone, List<String> fqdns) throws FreeIpaClientException {
    Set<DnsRecord> srvRecordsToDelete = allDnsRecordsInZone.stream().filter(record -> fqdns.stream().anyMatch(record::isHostRelatedSrvRecord)).collect(Collectors.toSet());
    for (DnsRecord dnsRecord : srvRecordsToDelete) {
        for (String fqdn : fqdns) {
            List<String> srvRecords = dnsRecord.getHostRelatedSrvRecords(fqdn);
            if (!srvRecords.isEmpty()) {
                LOGGER.info("Delete DNS SRV record [{}] for [{}] in zone [{}]", dnsRecord.getIdnsname(), fqdn, dnsZone);
                ignoreNotFoundException(() -> freeIpaClient.deleteDnsSrvRecord(dnsRecord.getIdnsname(), dnsZone.getIdnsname(), srvRecords), "DNS SRV record [{}] for [{}] not found in zone [{}]", dnsRecord.getIdnsname(), fqdn, dnsZone);
            }
        }
    }
}
Also used : FreeIpaClientExceptionUtil.ignoreNotFoundException(com.sequenceiq.freeipa.client.FreeIpaClientExceptionUtil.ignoreNotFoundException) FreeIpaClientExceptionUtil.ignoreNotFoundExceptionWithValue(com.sequenceiq.freeipa.client.FreeIpaClientExceptionUtil.ignoreNotFoundExceptionWithValue) LoggerFactory(org.slf4j.LoggerFactory) MDCBuilder(com.sequenceiq.cloudbreak.logger.MDCBuilder) Measure(com.sequenceiq.cloudbreak.aspect.Measure) StringUtils(org.apache.commons.lang3.StringUtils) AddDnsARecordRequest(com.sequenceiq.freeipa.api.v1.dns.model.AddDnsARecordRequest) Inject(javax.inject.Inject) Valid(javax.validation.Valid) FreeIpaClientExceptionUtil.ignoreEmptyModExceptionWithValue(com.sequenceiq.freeipa.client.FreeIpaClientExceptionUtil.ignoreEmptyModExceptionWithValue) DnsZone(com.sequenceiq.freeipa.client.model.DnsZone) RetryableFreeIpaClientException(com.sequenceiq.freeipa.client.RetryableFreeIpaClientException) Service(org.springframework.stereotype.Service) StackService(com.sequenceiq.freeipa.service.stack.StackService) Retryable(org.springframework.retry.annotation.Retryable) Stack(com.sequenceiq.freeipa.entity.Stack) BadRequestException(com.sequenceiq.cloudbreak.common.exception.BadRequestException) Logger(org.slf4j.Logger) FreeIpa(com.sequenceiq.freeipa.entity.FreeIpa) FreeIpaClientExceptionUtil(com.sequenceiq.freeipa.client.FreeIpaClientExceptionUtil) Set(java.util.Set) FreeIpaClientException(com.sequenceiq.freeipa.client.FreeIpaClientException) DnsRecord(com.sequenceiq.freeipa.client.model.DnsRecord) FreeIpaClient(com.sequenceiq.freeipa.client.FreeIpaClient) Collectors(java.util.stream.Collectors) Backoff(org.springframework.retry.annotation.Backoff) List(java.util.List) FreeIpaService(com.sequenceiq.freeipa.service.freeipa.FreeIpaService) AddDnsCnameRecordRequest(com.sequenceiq.freeipa.api.v1.dns.model.AddDnsCnameRecordRequest) FreeIpaClientFactory(com.sequenceiq.freeipa.service.freeipa.FreeIpaClientFactory) Optional(java.util.Optional) DnsRecord(com.sequenceiq.freeipa.client.model.DnsRecord)

Example 5 with DnsZone

use of com.sequenceiq.freeipa.client.model.DnsZone in project cloudbreak by hortonworks.

the class DnsRecordService method deleteDnsRecordByFqdn.

@Retryable(value = RetryableFreeIpaClientException.class, maxAttemptsExpression = RetryableFreeIpaClientException.MAX_RETRIES_EXPRESSION, backoff = @Backoff(delayExpression = RetryableFreeIpaClientException.DELAY_EXPRESSION, multiplierExpression = RetryableFreeIpaClientException.MULTIPLIER_EXPRESSION))
@Measure(DnsRecordService.class)
public void deleteDnsRecordByFqdn(String environmentCrn, String accountId, List<String> fqdns) throws FreeIpaClientException {
    FreeIpaAndClient freeIpaAndClient = createFreeIpaAndClient(environmentCrn, accountId);
    for (DnsZone dnsZone : freeIpaAndClient.getClient().findAllDnsZone()) {
        LOGGER.debug("Looking for records in zone [{}]", dnsZone.getIdnsname());
        Set<DnsRecord> allDnsRecordsInZone = freeIpaAndClient.getClient().findAllDnsRecordInZone(dnsZone.getIdnsname());
        deleteRegularRecords(freeIpaAndClient.getClient(), dnsZone, allDnsRecordsInZone, fqdns, freeIpaAndClient.getFreeIpa().getDomain());
        deleteSrvRecords(freeIpaAndClient.getClient(), dnsZone, allDnsRecordsInZone, fqdns);
    }
}
Also used : DnsRecord(com.sequenceiq.freeipa.client.model.DnsRecord) DnsZone(com.sequenceiq.freeipa.client.model.DnsZone) Retryable(org.springframework.retry.annotation.Retryable) Measure(com.sequenceiq.cloudbreak.aspect.Measure)

Aggregations

DnsZone (com.sequenceiq.freeipa.client.model.DnsZone)9 FreeIpaClient (com.sequenceiq.freeipa.client.FreeIpaClient)7 FreeIpaClientException (com.sequenceiq.freeipa.client.FreeIpaClientException)5 Retryable (org.springframework.retry.annotation.Retryable)5 DnsRecord (com.sequenceiq.freeipa.client.model.DnsRecord)4 Set (java.util.Set)4 Measure (com.sequenceiq.cloudbreak.aspect.Measure)3 RetryableFreeIpaClientException (com.sequenceiq.freeipa.client.RetryableFreeIpaClientException)3 Stack (com.sequenceiq.freeipa.entity.Stack)3 FreeIpaClientFactory (com.sequenceiq.freeipa.service.freeipa.FreeIpaClientFactory)3 Collectors (java.util.stream.Collectors)3 Inject (javax.inject.Inject)3 StringUtils (org.apache.commons.lang3.StringUtils)3 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3 BadRequestException (com.sequenceiq.cloudbreak.common.exception.BadRequestException)2 MDCBuilder (com.sequenceiq.cloudbreak.logger.MDCBuilder)2 AddDnsARecordRequest (com.sequenceiq.freeipa.api.v1.dns.model.AddDnsARecordRequest)2 AddDnsCnameRecordRequest (com.sequenceiq.freeipa.api.v1.dns.model.AddDnsCnameRecordRequest)2 FreeIpaClientExceptionUtil (com.sequenceiq.freeipa.client.FreeIpaClientExceptionUtil)2