Search in sources :

Example 11 with JsonRpcClientException

use of com.googlecode.jsonrpc4j.JsonRpcClientException in project cloudbreak by hortonworks.

the class DnsRecordServiceTest method testCnameRecordAddEmptyModListIgnored.

@Test
public void testCnameRecordAddEmptyModListIgnored() throws FreeIpaClientException {
    AddDnsCnameRecordRequest request = new AddDnsCnameRecordRequest();
    request.setEnvironmentCrn(ENV_CRN);
    request.setCname("Asdf");
    request.setTargetFqdn(TARGET_FQDN);
    Stack stack = createStack();
    when(stackService.getByEnvironmentCrnAndAccountId(ENV_CRN, ACCOUNT_ID)).thenReturn(stack);
    FreeIpa freeIpa = createFreeIpa();
    when(freeIpaService.findByStack(stack)).thenReturn(freeIpa);
    when(freeIpaClientFactory.getFreeIpaClientForStack(stack)).thenReturn(freeIpaClient);
    JsonRpcClientException noModEx = new JsonRpcClientException(FreeIpaErrorCodes.EMPTY_MODLIST.getValue(), "no modifications to be performed", null);
    when(freeIpaClient.addDnsCnameRecord(DOMAIN, request.getCname(), request.getTargetFqdn())).thenThrow(new FreeIpaClientException("can't create", noModEx));
    underTest.addDnsCnameRecord(ACCOUNT_ID, request);
    verify(freeIpaClient).addDnsCnameRecord(DOMAIN, request.getCname(), request.getTargetFqdn());
}
Also used : JsonRpcClientException(com.googlecode.jsonrpc4j.JsonRpcClientException) FreeIpa(com.sequenceiq.freeipa.entity.FreeIpa) FreeIpaClientException(com.sequenceiq.freeipa.client.FreeIpaClientException) AddDnsCnameRecordRequest(com.sequenceiq.freeipa.api.v1.dns.model.AddDnsCnameRecordRequest) Stack(com.sequenceiq.freeipa.entity.Stack) Test(org.junit.jupiter.api.Test)

Example 12 with JsonRpcClientException

use of com.googlecode.jsonrpc4j.JsonRpcClientException in project cloudbreak by hortonworks.

the class FreeIpaUsersStateProviderTest method testGetFilteredFreeIpaState.

@Test
void testGetFilteredFreeIpaState() throws Exception {
    List<String> user1GroupNames = List.of("group1", "group2");
    List<String> user2GroupNames = List.of("group2", "group3", IPA_UNMANAGED_GROUPS.get(0));
    List<String> groupsWithoutMembers = List.of("group4");
    com.sequenceiq.freeipa.client.model.User user1 = createIpaUser("user1", user1GroupNames);
    String userNotFound = "userNotFound";
    Set<com.sequenceiq.freeipa.client.model.Group> groupsFindAll = Stream.of(user1GroupNames.stream(), user2GroupNames.stream(), groupsWithoutMembers.stream(), IPA_UNMANAGED_GROUPS.stream()).flatMap(groupName -> groupName).map(this::createIpaGroup).collect(Collectors.toSet());
    JsonRpcClientException jsonRpcException = new JsonRpcClientException(FreeIpaErrorCodes.NOT_FOUND.getValue(), "group not found", null);
    FreeIpaClientException notFoundException = new FreeIpaClientException("Invoke FreeIPA failed", jsonRpcException);
    when(freeIpaClient.userShow(user1.getUid())).thenReturn(user1);
    when(freeIpaClient.userShow(userNotFound)).thenThrow(notFoundException);
    when(freeIpaClient.groupFindAll()).thenReturn(groupsFindAll);
    Set<String> expectedUsers = Sets.newHashSet(user1.getUid());
    Set<String> expectedGroups = groupsFindAll.stream().map(com.sequenceiq.freeipa.client.model.Group::getCn).filter(groupName -> !IPA_UNMANAGED_GROUPS.contains(groupName)).collect(Collectors.toSet());
    UserMetadata user1Metadata = new UserMetadata("user1-crn", 1L);
    doReturn(Optional.of(user1Metadata)).when(userMetadataConverter).toUserMetadata(argThat(arg -> user1.getUid().equals(arg.getUid())));
    Map<String, UserMetadata> expectedUserMetadata = Map.of(user1.getUid(), user1Metadata);
    UsersState ipaState = underTest.getFilteredFreeIpaState(freeIpaClient, Set.of(user1.getUid(), userNotFound));
    for (FmsUser fmsUser : ipaState.getUsers()) {
        assertTrue(expectedUsers.contains(fmsUser.getName()));
        expectedUsers.remove(fmsUser.getName());
    }
    assertTrue(expectedUsers.isEmpty());
    for (FmsGroup fmsGroup : ipaState.getGroups()) {
        assertTrue(expectedGroups.contains(fmsGroup.getName()));
        expectedGroups.remove(fmsGroup.getName());
    }
    assertTrue(expectedGroups.isEmpty());
    assertEquals(expectedUserMetadata, ipaState.getUserMetadataMap());
}
Also used : FmsGroup(com.sequenceiq.freeipa.service.freeipa.user.model.FmsGroup) IPA_UNMANAGED_GROUPS(com.sequenceiq.freeipa.client.FreeIpaChecks.IPA_UNMANAGED_GROUPS) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) Mock(org.mockito.Mock) Pair(org.apache.commons.lang3.tuple.Pair) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Map(java.util.Map) UsersState(com.sequenceiq.freeipa.service.freeipa.user.model.UsersState) UserMetadata(com.sequenceiq.freeipa.service.freeipa.user.model.UserMetadata) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) UserMetadataConverter(com.sequenceiq.freeipa.service.freeipa.user.conversion.UserMetadataConverter) Mockito.doReturn(org.mockito.Mockito.doReturn) Stack(com.sequenceiq.freeipa.entity.Stack) InjectMocks(org.mockito.InjectMocks) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) IPA_PROTECTED_USERS(com.sequenceiq.freeipa.client.FreeIpaChecks.IPA_PROTECTED_USERS) Set(java.util.Set) FreeIpaClientException(com.sequenceiq.freeipa.client.FreeIpaClientException) Mockito.when(org.mockito.Mockito.when) UUID(java.util.UUID) FreeIpaClient(com.sequenceiq.freeipa.client.FreeIpaClient) FmsUser(com.sequenceiq.freeipa.service.freeipa.user.model.FmsUser) Collectors(java.util.stream.Collectors) Maps(com.google.common.collect.Maps) FreeIpaErrorCodes(com.sequenceiq.freeipa.client.FreeIpaErrorCodes) Sets(com.google.common.collect.Sets) Test(org.junit.jupiter.api.Test) List(java.util.List) Stream(java.util.stream.Stream) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) JsonRpcClientException(com.googlecode.jsonrpc4j.JsonRpcClientException) FreeIpaClientFactory(com.sequenceiq.freeipa.service.freeipa.FreeIpaClientFactory) Optional(java.util.Optional) FmsGroup(com.sequenceiq.freeipa.service.freeipa.user.model.FmsGroup) FmsUser(com.sequenceiq.freeipa.service.freeipa.user.model.FmsUser) FmsGroup(com.sequenceiq.freeipa.service.freeipa.user.model.FmsGroup) UserMetadata(com.sequenceiq.freeipa.service.freeipa.user.model.UserMetadata) UsersState(com.sequenceiq.freeipa.service.freeipa.user.model.UsersState) JsonRpcClientException(com.googlecode.jsonrpc4j.JsonRpcClientException) FreeIpaClientException(com.sequenceiq.freeipa.client.FreeIpaClientException) Test(org.junit.jupiter.api.Test)

Example 13 with JsonRpcClientException

use of com.googlecode.jsonrpc4j.JsonRpcClientException in project cloudbreak by hortonworks.

the class ServiceKeytabServiceTest method testGenerateExistingNotCachedServiceMissingAddThrowDuplicate.

@Test
public void testGenerateExistingNotCachedServiceMissingAddThrowDuplicate() throws FreeIpaClientException {
    ServiceKeytabRequest request = new ServiceKeytabRequest();
    request.setEnvironmentCrn(ENVIRONMENT_CRN);
    request.setServiceName(SERVICE_NAME);
    request.setServerHostName(HOST);
    request.setDoNotRecreateKeytab(Boolean.TRUE);
    request.setServerHostNameAlias(ALIAS);
    RoleRequest roleRequest = new RoleRequest();
    request.setRoleRequest(roleRequest);
    when(keytabCacheService.findByEnvironmentCrnAndPrincipal(ENVIRONMENT_CRN, PRINCIPAL)).thenReturn(Optional.empty());
    FreeIpaClient ipaClient = mock(FreeIpaClient.class);
    when(freeIpaClientFactory.getFreeIpaClientForStack(stack)).thenReturn(ipaClient);
    when(roleComponent.privilegesExist(roleRequest, ipaClient)).thenReturn(Boolean.TRUE);
    Service service = new Service();
    service.setKrbcanonicalname(PRINCIPAL);
    service.setHasKeytab(Boolean.TRUE);
    when(ipaClient.showService(PRINCIPAL)).thenThrow(new FreeIpaClientException("notfound", new JsonRpcClientException(NOT_FOUND.getValue(), "notfound", null))).thenReturn(service);
    when(ipaClient.addService(PRINCIPAL)).thenThrow(new FreeIpaClientException("notfound", new JsonRpcClientException(DUPLICATE_ENTRY.getValue(), "notfound", null)));
    when(keytabCommonService.constructPrincipal(SERVICE_NAME, ALIAS, REALM)).thenReturn(ALIAS_PRINCIPAL);
    when(keytabCommonService.getExistingKeytab(ENVIRONMENT_CRN, PRINCIPAL, HOST, ipaClient)).thenReturn(keytabCache);
    ServiceKeytabResponse result = underTest.generateServiceKeytab(request, ACCOUNT_ID);
    verify(ipaClient).addServiceAlias(PRINCIPAL, ALIAS_PRINCIPAL);
    verify(roleComponent).addRoleAndPrivileges(Optional.of(service), Optional.empty(), roleRequest, ipaClient);
    assertEquals(keytabResponse, result.getKeytab());
    assertEquals(principalResponse, result.getServicePrincipal());
}
Also used : ServiceKeytabRequest(com.sequenceiq.freeipa.api.v1.kerberosmgmt.model.ServiceKeytabRequest) JsonRpcClientException(com.googlecode.jsonrpc4j.JsonRpcClientException) FreeIpaClient(com.sequenceiq.freeipa.client.FreeIpaClient) Service(com.sequenceiq.freeipa.client.model.Service) FreeIpaClientException(com.sequenceiq.freeipa.client.FreeIpaClientException) ServiceKeytabResponse(com.sequenceiq.freeipa.api.v1.kerberosmgmt.model.ServiceKeytabResponse) RoleRequest(com.sequenceiq.freeipa.api.v1.kerberosmgmt.model.RoleRequest) Test(org.junit.jupiter.api.Test)

Example 14 with JsonRpcClientException

use of com.googlecode.jsonrpc4j.JsonRpcClientException 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 15 with JsonRpcClientException

use of com.googlecode.jsonrpc4j.JsonRpcClientException in project cloudbreak by hortonworks.

the class DnsRecordServiceTest method testDeleteIgnoreNotFound.

@Test
public void testDeleteIgnoreNotFound() throws FreeIpaClientException {
    Stack stack = createStack();
    when(stackService.getByEnvironmentCrnAndAccountId(ENV_CRN, ACCOUNT_ID)).thenReturn(stack);
    FreeIpa freeIpa = createFreeIpa();
    when(freeIpaService.findByStack(stack)).thenReturn(freeIpa);
    when(freeIpaClientFactory.getFreeIpaClientForStack(stack)).thenReturn(freeIpaClient);
    when(freeIpaClient.deleteDnsRecord("asdf", DOMAIN)).thenThrow(new FreeIpaClientException("Not found", new JsonRpcClientException(FreeIpaErrorCodes.NOT_FOUND.getValue(), "Not found", null)));
    underTest.deleteDnsRecord(ACCOUNT_ID, ENV_CRN, null, "asdf");
}
Also used : JsonRpcClientException(com.googlecode.jsonrpc4j.JsonRpcClientException) FreeIpa(com.sequenceiq.freeipa.entity.FreeIpa) FreeIpaClientException(com.sequenceiq.freeipa.client.FreeIpaClientException) Stack(com.sequenceiq.freeipa.entity.Stack) Test(org.junit.jupiter.api.Test)

Aggregations

JsonRpcClientException (com.googlecode.jsonrpc4j.JsonRpcClientException)43 Test (org.junit.jupiter.api.Test)40 FreeIpaClientException (com.sequenceiq.freeipa.client.FreeIpaClientException)38 Map (java.util.Map)13 RoleRequest (com.sequenceiq.freeipa.api.v1.kerberosmgmt.model.RoleRequest)10 FreeIpaClient (com.sequenceiq.freeipa.client.FreeIpaClient)8 Stack (com.sequenceiq.freeipa.entity.Stack)7 Host (com.sequenceiq.freeipa.client.model.Host)6 FreeIpa (com.sequenceiq.freeipa.entity.FreeIpa)6 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)6 HashSet (java.util.HashSet)5 Optional (java.util.Optional)5 Role (com.sequenceiq.freeipa.client.model.Role)4 Service (com.sequenceiq.freeipa.client.model.Service)4 AddDnsARecordRequest (com.sequenceiq.freeipa.api.v1.dns.model.AddDnsARecordRequest)3 ServiceKeytabRequest (com.sequenceiq.freeipa.api.v1.kerberosmgmt.model.ServiceKeytabRequest)3 ServiceKeytabResponse (com.sequenceiq.freeipa.api.v1.kerberosmgmt.model.ServiceKeytabResponse)3 Privilege (com.sequenceiq.freeipa.client.model.Privilege)3 Set (java.util.Set)3 AddDnsCnameRecordRequest (com.sequenceiq.freeipa.api.v1.dns.model.AddDnsCnameRecordRequest)2