Search in sources :

Example 1 with ClientDetailResponse

use of keywhiz.api.ClientDetailResponse in project keywhiz by square.

the class AutomationClientResource method findClient.

/**
 * Retrieve Client by a specified name, or all Clients if no name given
 *
 * @param automationClient the client with automation access performing this operation
 * @param name the name of the Client to retrieve, if provided
 * @return the specified client if found, or all clients if name omitted
 *
 * optionalParams name
 * description Returns a single Client or a set of all Clients
 * responseMessage 200 Found and retrieved Client(s)
 * responseMessage 404 Client with given name not found (if name provided)
 */
@Timed
@ExceptionMetered
@GET
public Response findClient(@Auth AutomationClient automationClient, @QueryParam("name") Optional<String> name) {
    logger.info("Automation ({}) - Looking up a name {}", automationClient.getName(), name);
    if (name.isPresent()) {
        Client client = clientDAO.getClientByName(name.get()).orElseThrow(NotFoundException::new);
        ImmutableList<Group> groups = ImmutableList.copyOf(aclDAO.getGroupsFor(client));
        return Response.ok().entity(ClientDetailResponse.fromClient(client, groups, ImmutableList.of())).build();
    }
    List<ClientDetailResponse> clients = clientDAO.getClients().stream().map(c -> ClientDetailResponse.fromClient(c, ImmutableList.copyOf(aclDAO.getGroupsFor(c)), ImmutableList.of())).collect(toList());
    return Response.ok().entity(clients).build();
}
Also used : PathParam(javax.ws.rs.PathParam) AclDAO(keywhiz.service.daos.AclDAO) Produces(javax.ws.rs.Produces) ClientDAO(keywhiz.service.daos.ClientDAO) GET(javax.ws.rs.GET) Event(keywhiz.log.Event) Path(javax.ws.rs.Path) LoggerFactory(org.slf4j.LoggerFactory) Auth(io.dropwizard.auth.Auth) HashMap(java.util.HashMap) ClientsResource(keywhiz.service.resources.admin.ClientsResource) Inject(javax.inject.Inject) Valid(javax.validation.Valid) AutomationClient(keywhiz.api.model.AutomationClient) ClientDAOFactory(keywhiz.service.daos.ClientDAO.ClientDAOFactory) QueryParam(javax.ws.rs.QueryParam) ImmutableList(com.google.common.collect.ImmutableList) Consumes(javax.ws.rs.Consumes) Map(java.util.Map) ExceptionMetered(com.codahale.metrics.annotation.ExceptionMetered) Client(keywhiz.api.model.Client) APPLICATION_JSON(javax.ws.rs.core.MediaType.APPLICATION_JSON) DELETE(javax.ws.rs.DELETE) ClientResource(keywhiz.service.resources.automation.v2.ClientResource) AuditLog(keywhiz.log.AuditLog) Group(keywhiz.api.model.Group) POST(javax.ws.rs.POST) Logger(org.slf4j.Logger) AclDAOFactory(keywhiz.service.daos.AclDAO.AclDAOFactory) LongParam(io.dropwizard.jersey.params.LongParam) ConflictException(keywhiz.service.exceptions.ConflictException) Instant(java.time.Instant) NotFoundException(javax.ws.rs.NotFoundException) Timed(com.codahale.metrics.annotation.Timed) EventTag(keywhiz.log.EventTag) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ClientDetailResponse(keywhiz.api.ClientDetailResponse) CreateClientRequest(keywhiz.api.CreateClientRequest) Response(javax.ws.rs.core.Response) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Group(keywhiz.api.model.Group) NotFoundException(javax.ws.rs.NotFoundException) AutomationClient(keywhiz.api.model.AutomationClient) Client(keywhiz.api.model.Client) ClientDetailResponse(keywhiz.api.ClientDetailResponse) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET) ExceptionMetered(com.codahale.metrics.annotation.ExceptionMetered)

Example 2 with ClientDetailResponse

use of keywhiz.api.ClientDetailResponse in project keywhiz by square.

the class AutomationClientResourceTest method createNewClient.

@Test
public void createNewClient() {
    Client client = new Client(543L, "client", "2nd client", null, now, "test", now, "test", null, null, true, false);
    CreateClientRequest request = new CreateClientRequest("client");
    when(clientDAO.getClientByName("client")).thenReturn(Optional.empty());
    when(clientDAO.createClient("client", automation.getName(), "", null)).thenReturn(543L);
    when(clientDAO.getClientById(543L)).thenReturn(Optional.of(client));
    when(aclDAO.getGroupsFor(client)).thenReturn(ImmutableSet.of());
    ClientDetailResponse response = ClientDetailResponse.fromClient(client, ImmutableList.of(), ImmutableList.of());
    ClientDetailResponse response1 = resource.createClient(automation, request);
    assertThat(response.name).isEqualTo(response1.name);
}
Also used : CreateClientRequest(keywhiz.api.CreateClientRequest) AutomationClient(keywhiz.api.model.AutomationClient) Client(keywhiz.api.model.Client) ClientDetailResponse(keywhiz.api.ClientDetailResponse) Test(org.junit.Test)

Example 3 with ClientDetailResponse

use of keywhiz.api.ClientDetailResponse in project keywhiz by square.

the class AutomationClientResourceTest method createNewClientAlreadyExists.

@Test
public void createNewClientAlreadyExists() {
    Client client = new Client(543L, "client", "2nd client", null, now, "test", now, "test", null, null, true, false);
    CreateClientRequest request = new CreateClientRequest("client");
    when(clientDAO.getClientByName("client")).thenReturn(Optional.empty());
    when(clientDAO.createClient("client", automation.getName(), "", null)).thenReturn(543L);
    when(clientDAO.getClientById(543L)).thenReturn(Optional.of(client));
    ClientDetailResponse response = ClientDetailResponse.fromClient(client, ImmutableList.of(), ImmutableList.of());
    ClientDetailResponse response1 = resource.createClient(automation, request);
    assertThat(response.name).isEqualTo(response1.name);
}
Also used : CreateClientRequest(keywhiz.api.CreateClientRequest) AutomationClient(keywhiz.api.model.AutomationClient) Client(keywhiz.api.model.Client) ClientDetailResponse(keywhiz.api.ClientDetailResponse) Test(org.junit.Test)

Example 4 with ClientDetailResponse

use of keywhiz.api.ClientDetailResponse in project keywhiz by square.

the class ClientsResourceTest method includesTheClient.

@Test
public void includesTheClient() {
    when(clientDAO.getClientById(1)).thenReturn(Optional.of(client));
    when(aclDAO.getGroupsFor(client)).thenReturn(Collections.emptySet());
    when(aclDAO.getSanitizedSecretsFor(client)).thenReturn(ImmutableSet.of());
    ClientDetailResponse response = resource.getClient(user, new LongParam("1"));
    assertThat(response.id).isEqualTo(client.getId());
    assertThat(response.name).isEqualTo(client.getName());
    assertThat(response.description).isEqualTo(client.getDescription());
    assertThat(response.creationDate).isEqualTo(client.getCreatedAt());
    assertThat(response.createdBy).isEqualTo(client.getCreatedBy());
    assertThat(response.updateDate).isEqualTo(client.getUpdatedAt());
    assertThat(response.updatedBy).isEqualTo(client.getUpdatedBy());
}
Also used : LongParam(io.dropwizard.jersey.params.LongParam) ClientDetailResponse(keywhiz.api.ClientDetailResponse) Test(org.junit.Test)

Example 5 with ClientDetailResponse

use of keywhiz.api.ClientDetailResponse in project keywhiz by square.

the class ClientsResourceTest method handlesNoAssociations.

@Test
public void handlesNoAssociations() {
    when(clientDAO.getClientById(1)).thenReturn(Optional.of(client));
    when(aclDAO.getGroupsFor(client)).thenReturn(Collections.emptySet());
    when(aclDAO.getSanitizedSecretsFor(client)).thenReturn(ImmutableSet.of());
    ClientDetailResponse response = resource.getClient(user, new LongParam("1"));
    assertThat(response.groups).isEmpty();
    assertThat(response.secrets).isEmpty();
}
Also used : LongParam(io.dropwizard.jersey.params.LongParam) ClientDetailResponse(keywhiz.api.ClientDetailResponse) Test(org.junit.Test)

Aggregations

ClientDetailResponse (keywhiz.api.ClientDetailResponse)10 Test (org.junit.Test)8 LongParam (io.dropwizard.jersey.params.LongParam)4 AutomationClient (keywhiz.api.model.AutomationClient)4 Client (keywhiz.api.model.Client)4 Group (keywhiz.api.model.Group)4 CreateClientRequest (keywhiz.api.CreateClientRequest)3 Response (javax.ws.rs.core.Response)2 SanitizedSecret (keywhiz.api.model.SanitizedSecret)2 ExceptionMetered (com.codahale.metrics.annotation.ExceptionMetered)1 Timed (com.codahale.metrics.annotation.Timed)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ImmutableList (com.google.common.collect.ImmutableList)1 Auth (io.dropwizard.auth.Auth)1 IOException (java.io.IOException)1 Instant (java.time.Instant)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1