Search in sources :

Example 6 with RoleRequest

use of com.sequenceiq.freeipa.api.v1.kerberosmgmt.model.RoleRequest in project cloudbreak by hortonworks.

the class ServiceKeytabServiceTest method testGenerateExistingCachedDoNotRecreateFalse.

@Test
public void testGenerateExistingCachedDoNotRecreateFalse() throws FreeIpaClientException {
    ServiceKeytabRequest request = new ServiceKeytabRequest();
    request.setEnvironmentCrn(ENVIRONMENT_CRN);
    request.setServiceName(SERVICE_NAME);
    request.setServerHostName(HOST);
    request.setDoNotRecreateKeytab(Boolean.FALSE);
    request.setServerHostNameAlias(ALIAS);
    RoleRequest roleRequest = new RoleRequest();
    request.setRoleRequest(roleRequest);
    when(keytabCacheService.findByEnvironmentCrnAndPrincipal(ENVIRONMENT_CRN, PRINCIPAL)).thenReturn(Optional.of(keytabCache));
    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);
    when(ipaClient.showService(PRINCIPAL)).thenReturn(service);
    when(keytabCommonService.constructPrincipal(SERVICE_NAME, ALIAS, REALM)).thenReturn(ALIAS_PRINCIPAL);
    when(keytabCommonService.getKeytab(ENVIRONMENT_CRN, PRINCIPAL, HOST, ipaClient)).thenReturn(keytabCache);
    ServiceKeytabResponse result = underTest.generateServiceKeytab(request, ACCOUNT_ID);
    verify(keytabCommonService).addHost(eq(HOST), isNull(), eq(ipaClient));
    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) FreeIpaClient(com.sequenceiq.freeipa.client.FreeIpaClient) Service(com.sequenceiq.freeipa.client.model.Service) 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 7 with RoleRequest

use of com.sequenceiq.freeipa.api.v1.kerberosmgmt.model.RoleRequest 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 8 with RoleRequest

use of com.sequenceiq.freeipa.api.v1.kerberosmgmt.model.RoleRequest in project cloudbreak by hortonworks.

the class KerberosMgmtRoleComponentV1Test method testAddRoleAndPrivilegesForHostWithRoleRaceCondition.

@Test
public void testAddRoleAndPrivilegesForHostWithRoleRaceCondition() throws Exception {
    Host host = new Host();
    host.setFqdn(HOST);
    RoleRequest roleRequest = new RoleRequest();
    roleRequest.setRoleName(ROLE);
    Set<String> privileges = new HashSet<>();
    privileges.add(PRIVILEGE1);
    privileges.add(PRIVILEGE2);
    roleRequest.setPrivileges(privileges);
    Role role = new Role();
    role.setCn(ROLE);
    Mockito.when(mockIpaClient.addRole(anyString())).thenThrow(new FreeIpaClientException("duplicate", new JsonRpcClientException(FreeIpaErrorCodes.DUPLICATE_ENTRY.getValue(), "duplicate", null)));
    Privilege privilege = new Privilege();
    Set<String> hosts = new HashSet<>();
    hosts.add(HOST);
    Set<String> noServices = new HashSet<>();
    Mockito.when(mockIpaClient.showRole(roleRequest.getRoleName())).thenThrow(new FreeIpaClientException("notfound", new JsonRpcClientException(NOT_FOUND, "notfound", null))).thenReturn(role);
    Mockito.when(mockIpaClient.showPrivilege(any())).thenReturn(privilege);
    Mockito.when(mockIpaClient.addRolePrivileges(any(), any())).thenReturn(role);
    Mockito.when(mockIpaClient.addRoleMember(any(), any(), any(), any(), any(), any())).thenReturn(role);
    underTest.addRoleAndPrivileges(Optional.empty(), Optional.of(host), roleRequest, mockIpaClient);
    Mockito.verify(mockIpaClient).addRole(ROLE);
    Mockito.verify(mockIpaClient).addRolePrivileges(ROLE, privileges);
    Mockito.verify(mockIpaClient).addRoleMember(ROLE, null, null, hosts, null, noServices);
}
Also used : Role(com.sequenceiq.freeipa.client.model.Role) JsonRpcClientException(com.googlecode.jsonrpc4j.JsonRpcClientException) FreeIpaClientException(com.sequenceiq.freeipa.client.FreeIpaClientException) Host(com.sequenceiq.freeipa.client.model.Host) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Privilege(com.sequenceiq.freeipa.client.model.Privilege) RoleRequest(com.sequenceiq.freeipa.api.v1.kerberosmgmt.model.RoleRequest) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 9 with RoleRequest

use of com.sequenceiq.freeipa.api.v1.kerberosmgmt.model.RoleRequest in project cloudbreak by hortonworks.

the class KerberosMgmtRoleComponentV1Test method testAddRoleAndPrivilegesForHostWithRoleThatAlreadyExists.

@Test
public void testAddRoleAndPrivilegesForHostWithRoleThatAlreadyExists() throws Exception {
    Host host = new Host();
    host.setFqdn(HOST);
    RoleRequest roleRequest = new RoleRequest();
    roleRequest.setRoleName(ROLE);
    Set<String> privileges = new HashSet<>();
    privileges.add(PRIVILEGE1);
    privileges.add(PRIVILEGE2);
    roleRequest.setPrivileges(privileges);
    Role role = new Role();
    role.setCn(ROLE);
    Privilege privilege = new Privilege();
    Set<String> hosts = new HashSet<>();
    hosts.add(HOST);
    Set<String> noServices = new HashSet<>();
    Mockito.when(mockIpaClient.showPrivilege(any())).thenReturn(privilege);
    Mockito.when(mockIpaClient.addRolePrivileges(any(), any())).thenReturn(role);
    Mockito.when(mockIpaClient.showRole(anyString())).thenReturn(role);
    Mockito.when(mockIpaClient.addRoleMember(any(), any(), any(), any(), any(), any())).thenReturn(role);
    underTest.addRoleAndPrivileges(Optional.empty(), Optional.of(host), roleRequest, mockIpaClient);
    Mockito.verify(mockIpaClient).addRolePrivileges(ROLE, privileges);
    Mockito.verify(mockIpaClient).addRoleMember(ROLE, null, null, hosts, null, noServices);
}
Also used : Role(com.sequenceiq.freeipa.client.model.Role) Host(com.sequenceiq.freeipa.client.model.Host) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Privilege(com.sequenceiq.freeipa.client.model.Privilege) RoleRequest(com.sequenceiq.freeipa.api.v1.kerberosmgmt.model.RoleRequest) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 10 with RoleRequest

use of com.sequenceiq.freeipa.api.v1.kerberosmgmt.model.RoleRequest in project cloudbreak by hortonworks.

the class KerberosMgmtRoleComponentV1Test method testAddRoleAndPrivilegesForHostWithoutRole.

@Test
public void testAddRoleAndPrivilegesForHostWithoutRole() throws Exception {
    Host host = new Host();
    host.setFqdn(HOST);
    RoleRequest roleRequest = null;
    underTest.addRoleAndPrivileges(Optional.empty(), Optional.of(host), roleRequest, mockIpaClient);
    Mockito.verifyNoInteractions(mockIpaClient);
}
Also used : Host(com.sequenceiq.freeipa.client.model.Host) RoleRequest(com.sequenceiq.freeipa.api.v1.kerberosmgmt.model.RoleRequest) Test(org.junit.jupiter.api.Test)

Aggregations

RoleRequest (com.sequenceiq.freeipa.api.v1.kerberosmgmt.model.RoleRequest)28 Test (org.junit.jupiter.api.Test)26 FreeIpaClient (com.sequenceiq.freeipa.client.FreeIpaClient)17 FreeIpaClientException (com.sequenceiq.freeipa.client.FreeIpaClientException)14 Host (com.sequenceiq.freeipa.client.model.Host)13 JsonRpcClientException (com.googlecode.jsonrpc4j.JsonRpcClientException)10 ServiceKeytabRequest (com.sequenceiq.freeipa.api.v1.kerberosmgmt.model.ServiceKeytabRequest)8 Service (com.sequenceiq.freeipa.client.model.Service)7 HashSet (java.util.HashSet)7 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)7 Privilege (com.sequenceiq.freeipa.client.model.Privilege)6 Role (com.sequenceiq.freeipa.client.model.Role)6 HostKeytabRequest (com.sequenceiq.freeipa.api.v1.kerberosmgmt.model.HostKeytabRequest)5 ServiceKeytabResponse (com.sequenceiq.freeipa.api.v1.kerberosmgmt.model.ServiceKeytabResponse)5 RetryableFreeIpaClientException (com.sequenceiq.freeipa.client.RetryableFreeIpaClientException)5 Stack (com.sequenceiq.freeipa.entity.Stack)4 Optional (java.util.Optional)4 Secret (com.sequenceiq.cloudbreak.service.secret.domain.Secret)3 SecretResponse (com.sequenceiq.cloudbreak.service.secret.model.SecretResponse)3 HostKeytabResponse (com.sequenceiq.freeipa.api.v1.kerberosmgmt.model.HostKeytabResponse)3