Search in sources :

Example 1 with GroupDetailResponse

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

the class Printing method printGroupWithDetails.

public void printGroupWithDetails(Group group) {
    System.out.println(group.getName());
    GroupDetailResponse groupDetails;
    try {
        groupDetails = keywhizClient.groupDetailsForId(group.getId());
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
    System.out.println("\tClients:");
    groupDetails.getClients().stream().sorted(Comparator.comparing(Client::getName)).forEach(c -> System.out.println(INDENT + c.getName()));
    System.out.println("\tSecrets:");
    groupDetails.getSecrets().stream().sorted(Comparator.comparing(SanitizedSecret::name)).forEach(s -> System.out.println(INDENT + SanitizedSecret.displayName(s)));
    System.out.println("\tMetadata:");
    if (!groupDetails.getMetadata().isEmpty()) {
        String metadata;
        try {
            metadata = new ObjectMapper().writeValueAsString(groupDetails.getMetadata());
        } catch (JsonProcessingException e) {
            throw Throwables.propagate(e);
        }
        System.out.println(INDENT + metadata);
    }
    if (!groupDetails.getDescription().isEmpty()) {
        System.out.println("\tDescription:");
        System.out.println(INDENT + groupDetails.getDescription());
    }
    if (!groupDetails.getCreatedBy().isEmpty()) {
        System.out.println("\tCreated by:");
        System.out.println(INDENT + groupDetails.getCreatedBy());
    }
    System.out.println("\tCreated at:");
    Date d = new Date(groupDetails.getCreationDate().toEpochSecond() * 1000);
    System.out.println(INDENT + DateFormat.getDateTimeInstance().format(d));
    if (!groupDetails.getUpdatedBy().isEmpty()) {
        System.out.println("\tUpdated by:");
        System.out.println(INDENT + groupDetails.getUpdatedBy());
    }
    System.out.println("\tUpdated at:");
    d = new Date(groupDetails.getUpdateDate().toEpochSecond() * 1000);
    System.out.println(INDENT + DateFormat.getDateTimeInstance().format(d));
}
Also used : SanitizedSecret(keywhiz.api.model.SanitizedSecret) GroupDetailResponse(keywhiz.api.GroupDetailResponse) IOException(java.io.IOException) Client(keywhiz.api.model.Client) KeywhizClient(keywhiz.client.KeywhizClient) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Date(java.util.Date)

Example 2 with GroupDetailResponse

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

the class AutomationGroupResource method getGroupByName.

/**
   * Retrieve Group by a specified name, or all Groups if no name given
   *
   * @param name the name of the Group to retrieve, if provided
   * @excludeParams automationClient
   * @optionalParams name
   * @description Returns a single Group or a set of all Groups
   * @responseMessage 200 Found and retrieved Group(s)
   * @responseMessage 404 Group with given name not found (if name provided)
   */
@Timed
@ExceptionMetered
@GET
public Response getGroupByName(@Auth AutomationClient automationClient, @QueryParam("name") Optional<String> name) {
    if (name.isPresent()) {
        Group group = groupDAO.getGroup(name.get()).orElseThrow(NotFoundException::new);
        ImmutableList<Client> clients = ImmutableList.copyOf(aclDAO.getClientsFor(group));
        ImmutableList<SanitizedSecret> sanitizedSecrets = ImmutableList.copyOf(aclDAO.getSanitizedSecretsFor(group));
        return Response.ok().entity(GroupDetailResponse.fromGroup(group, sanitizedSecrets, clients)).build();
    }
    ImmutableList<SanitizedSecret> emptySecrets = ImmutableList.of();
    ImmutableList<Client> emptyClients = ImmutableList.of();
    List<GroupDetailResponse> groups = groupDAO.getGroups().stream().map((g) -> GroupDetailResponse.fromGroup(g, emptySecrets, emptyClients)).collect(toList());
    return Response.ok().entity(groups).build();
}
Also used : PathParam(javax.ws.rs.PathParam) AclDAO(keywhiz.service.daos.AclDAO) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) Event(keywhiz.log.Event) Strings.nullToEmpty(com.google.common.base.Strings.nullToEmpty) Path(javax.ws.rs.Path) LoggerFactory(org.slf4j.LoggerFactory) Auth(io.dropwizard.auth.Auth) GroupDAOFactory(keywhiz.service.daos.GroupDAO.GroupDAOFactory) HashMap(java.util.HashMap) Inject(javax.inject.Inject) Valid(javax.validation.Valid) AutomationClient(keywhiz.api.model.AutomationClient) GroupResource(keywhiz.service.resources.automation.v2.GroupResource) 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) GroupDAO(keywhiz.service.daos.GroupDAO) DELETE(javax.ws.rs.DELETE) 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) CreateGroupRequest(keywhiz.api.CreateGroupRequest) EventTag(keywhiz.log.EventTag) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Response(javax.ws.rs.core.Response) Optional(java.util.Optional) SanitizedSecret(keywhiz.api.model.SanitizedSecret) VisibleForTesting(com.google.common.annotations.VisibleForTesting) GroupDetailResponse(keywhiz.api.GroupDetailResponse) Group(keywhiz.api.model.Group) SanitizedSecret(keywhiz.api.model.SanitizedSecret) GroupDetailResponse(keywhiz.api.GroupDetailResponse) NotFoundException(javax.ws.rs.NotFoundException) AutomationClient(keywhiz.api.model.AutomationClient) Client(keywhiz.api.model.Client) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET) ExceptionMetered(com.codahale.metrics.annotation.ExceptionMetered)

Example 3 with GroupDetailResponse

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

the class AutomationGroupResourceTest method findGroupById.

@Test
public void findGroupById() {
    Group group = new Group(50, "testGroup", "testing group", now, "automation client", now, "automation client", ImmutableMap.of("app", "keywhiz"));
    when(groupDAO.getGroupById(50)).thenReturn(Optional.of(group));
    when(aclDAO.getClientsFor(group)).thenReturn(ImmutableSet.of());
    when(aclDAO.getSanitizedSecretsFor(group)).thenReturn(ImmutableSet.of());
    GroupDetailResponse expectedResponse = GroupDetailResponse.fromGroup(group, ImmutableList.of(), ImmutableList.of());
    GroupDetailResponse response = resource.getGroupById(automation, new LongParam("50"));
    assertThat(response).isEqualTo(expectedResponse);
}
Also used : Group(keywhiz.api.model.Group) GroupDetailResponse(keywhiz.api.GroupDetailResponse) LongParam(io.dropwizard.jersey.params.LongParam) Test(org.junit.Test)

Example 4 with GroupDetailResponse

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

the class AutomationGroupResourceTest method groupIncludesClientsAndSecrets.

@Test
public void groupIncludesClientsAndSecrets() {
    Group group = new Group(50, "testGroup", "testing group", now, "automation client", now, "automation client", ImmutableMap.of("app", "keywhiz"));
    Client groupClient = new Client(1, "firstClient", "Group client", now, "test", now, "test", null, true, true);
    SanitizedSecret firstGroupSecret = SanitizedSecret.of(1, "name1", "checksum", "desc", now, "test", now, "test", null, "", null, 1136214245, 125L);
    SanitizedSecret secondGroupSecret = SanitizedSecret.of(2, "name2", "checksum", "desc", now, "test", now, "test", null, "", null, 1136214245, 250L);
    when(groupDAO.getGroup("testGroup")).thenReturn(Optional.of(group));
    when(aclDAO.getClientsFor(group)).thenReturn(ImmutableSet.of(groupClient));
    when(aclDAO.getSanitizedSecretsFor(group)).thenReturn(ImmutableSet.of(firstGroupSecret, secondGroupSecret));
    GroupDetailResponse expectedResponse = GroupDetailResponse.fromGroup(group, ImmutableList.of(firstGroupSecret, secondGroupSecret), ImmutableList.of(groupClient));
    Response response = resource.getGroupByName(automation, Optional.of("testGroup"));
    assertThat(response.getEntity()).isEqualTo(expectedResponse);
}
Also used : Response(javax.ws.rs.core.Response) GroupDetailResponse(keywhiz.api.GroupDetailResponse) Group(keywhiz.api.model.Group) SanitizedSecret(keywhiz.api.model.SanitizedSecret) GroupDetailResponse(keywhiz.api.GroupDetailResponse) AutomationClient(keywhiz.api.model.AutomationClient) Client(keywhiz.api.model.Client) Test(org.junit.Test)

Example 5 with GroupDetailResponse

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

the class AutomationGroupResourceTest method findGroupByName.

@Test
public void findGroupByName() {
    Group group = new Group(50, "testGroup", "testing group", now, "automation client", now, "automation client", ImmutableMap.of("app", "keywhiz"));
    when(groupDAO.getGroup("testGroup")).thenReturn(Optional.of(group));
    when(aclDAO.getClientsFor(group)).thenReturn(ImmutableSet.of());
    when(aclDAO.getSanitizedSecretsFor(group)).thenReturn(ImmutableSet.of());
    GroupDetailResponse expectedResponse = GroupDetailResponse.fromGroup(group, ImmutableList.of(), ImmutableList.of());
    Response response = resource.getGroupByName(automation, Optional.of("testGroup"));
    assertThat(response.getEntity()).isEqualTo(expectedResponse);
}
Also used : Response(javax.ws.rs.core.Response) GroupDetailResponse(keywhiz.api.GroupDetailResponse) Group(keywhiz.api.model.Group) GroupDetailResponse(keywhiz.api.GroupDetailResponse) Test(org.junit.Test)

Aggregations

GroupDetailResponse (keywhiz.api.GroupDetailResponse)10 Test (org.junit.Test)8 Group (keywhiz.api.model.Group)5 Client (keywhiz.api.model.Client)4 SanitizedSecret (keywhiz.api.model.SanitizedSecret)4 LongParam (io.dropwizard.jersey.params.LongParam)3 Response (javax.ws.rs.core.Response)3 CreateGroupRequest (keywhiz.api.CreateGroupRequest)3 List (java.util.List)2 AutomationClient (keywhiz.api.model.AutomationClient)2 ExceptionMetered (com.codahale.metrics.annotation.ExceptionMetered)1 Timed (com.codahale.metrics.annotation.Timed)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Strings.nullToEmpty (com.google.common.base.Strings.nullToEmpty)1 ImmutableList (com.google.common.collect.ImmutableList)1 Auth (io.dropwizard.auth.Auth)1 IOException (java.io.IOException)1 Instant (java.time.Instant)1