Search in sources :

Example 1 with CreateClientRequestV2

use of keywhiz.api.automation.v2.CreateClientRequestV2 in project keywhiz by square.

the class ClientResourceTest method modifyClient_success.

@Ignore
@Test
public void modifyClient_success() throws Exception {
    // Create sample client
    create(CreateClientRequestV2.builder().name("client9").build());
    ClientDetailResponseV2 originalClient = lookup("client9");
    // Modify client
    ModifyClientRequestV2 request = ModifyClientRequestV2.forName("client9b");
    ClientDetailResponseV2 clientDetail = modify("client9", request);
    assertThat(clientDetail.name()).isEqualTo("client9b");
    assertThat(clientDetail).isEqualToIgnoringGivenFields(originalClient, "name", "updateDate");
    assertThat(clientDetail.updatedAtSeconds()).isGreaterThan(originalClient.updatedAtSeconds());
}
Also used : ClientDetailResponseV2(keywhiz.api.automation.v2.ClientDetailResponseV2) ModifyClientRequestV2(keywhiz.api.automation.v2.ModifyClientRequestV2) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with CreateClientRequestV2

use of keywhiz.api.automation.v2.CreateClientRequestV2 in project keywhiz by square.

the class ClientResourceTest method modifyClientGroups_success.

@Test
public void modifyClientGroups_success() throws Exception {
    // Create sample client and groups
    createGroup("group8a");
    createGroup("group8b");
    createGroup("group8c");
    create(CreateClientRequestV2.builder().name("client8").groups("group8a", "group8b").build());
    // Modify client
    ModifyGroupsRequestV2 request = ModifyGroupsRequestV2.builder().addGroups("group8c", "non-existent1").removeGroups("group8a", "non-existent2").build();
    List<String> groups = modifyGroups("client8", request);
    assertThat(groups).containsOnly("group8b", "group8c");
}
Also used : ModifyGroupsRequestV2(keywhiz.api.automation.v2.ModifyGroupsRequestV2) Test(org.junit.Test)

Example 3 with CreateClientRequestV2

use of keywhiz.api.automation.v2.CreateClientRequestV2 in project keywhiz by square.

the class ClientResourceTest method clientInfo_groupExists.

@Test
public void clientInfo_groupExists() throws Exception {
    // Sample client
    create(CreateClientRequestV2.builder().name("client3").build());
    ClientDetailResponseV2 clientDetail = lookup("client3");
    assertThat(clientDetail.name()).isEqualTo("client3");
    assertThat(clientDetail.description()).isEmpty();
    assertThat(clientDetail.createdBy()).isEqualTo(clientDetail.updatedBy()).isEqualTo("client");
}
Also used : ClientDetailResponseV2(keywhiz.api.automation.v2.ClientDetailResponseV2) Test(org.junit.Test)

Example 4 with CreateClientRequestV2

use of keywhiz.api.automation.v2.CreateClientRequestV2 in project keywhiz by square.

the class ClientResource method createClient.

/**
   * Creates a client and assigns to given groups
   *
   * @excludeParams automationClient
   * @param request JSON request to create a client
   *
   * @responseMessage 201 Created client and assigned to given groups
   * @responseMessage 409 Client already exists
   */
@Timed
@ExceptionMetered
@POST
@Consumes(APPLICATION_JSON)
public Response createClient(@Auth AutomationClient automationClient, @Valid CreateClientRequestV2 request) {
    String creator = automationClient.getName();
    String client = request.name();
    clientDAOReadWrite.getClient(client).ifPresent((c) -> {
        logger.info("Automation ({}) - Client {} already exists", creator, client);
        throw new ConflictException("Client name already exists.");
    });
    // Creates new client record
    long clientId = clientDAOReadWrite.createClient(client, creator, request.description());
    auditLog.recordEvent(new Event(Instant.now(), EventTag.CLIENT_CREATE, creator, client));
    // Enrolls client in any requested groups
    groupsToGroupIds(request.groups()).forEach((maybeGroupId) -> maybeGroupId.ifPresent((groupId) -> aclDAOReadWrite.findAndEnrollClient(clientId, groupId, auditLog, creator, new HashMap<>())));
    URI uri = UriBuilder.fromResource(ClientResource.class).path(client).build();
    return Response.created(uri).build();
}
Also used : NotImplementedException(org.apache.commons.lang3.NotImplementedException) 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) ClientDetailResponseV2(keywhiz.api.automation.v2.ClientDetailResponseV2) Event(keywhiz.log.Event) 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) ClientDAOFactory(keywhiz.service.daos.ClientDAO.ClientDAOFactory) Consumes(javax.ws.rs.Consumes) ExceptionMetered(com.codahale.metrics.annotation.ExceptionMetered) ModifyGroupsRequestV2(keywhiz.api.automation.v2.ModifyGroupsRequestV2) UriBuilder(javax.ws.rs.core.UriBuilder) URI(java.net.URI) Client(keywhiz.api.model.Client) APPLICATION_JSON(javax.ws.rs.core.MediaType.APPLICATION_JSON) GroupDAO(keywhiz.service.daos.GroupDAO) Collectors.toSet(java.util.stream.Collectors.toSet) DELETE(javax.ws.rs.DELETE) CreateClientRequestV2(keywhiz.api.automation.v2.CreateClientRequestV2) AuditLog(keywhiz.log.AuditLog) ModifyClientRequestV2(keywhiz.api.automation.v2.ModifyClientRequestV2) Group(keywhiz.api.model.Group) POST(javax.ws.rs.POST) Logger(org.slf4j.Logger) Set(java.util.Set) AclDAOFactory(keywhiz.service.daos.AclDAO.AclDAOFactory) ConflictException(keywhiz.service.exceptions.ConflictException) Instant(java.time.Instant) Sets(com.google.common.collect.Sets) NotFoundException(javax.ws.rs.NotFoundException) String.format(java.lang.String.format) Timed(com.codahale.metrics.annotation.Timed) EventTag(keywhiz.log.EventTag) Stream(java.util.stream.Stream) Response(javax.ws.rs.core.Response) Optional(java.util.Optional) SanitizedSecret(keywhiz.api.model.SanitizedSecret) PUT(javax.ws.rs.PUT) ConflictException(keywhiz.service.exceptions.ConflictException) Event(keywhiz.log.Event) URI(java.net.URI) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Timed(com.codahale.metrics.annotation.Timed) ExceptionMetered(com.codahale.metrics.annotation.ExceptionMetered)

Example 5 with CreateClientRequestV2

use of keywhiz.api.automation.v2.CreateClientRequestV2 in project keywhiz by square.

the class ClientResourceTest method createClient_duplicate.

@Test
public void createClient_duplicate() throws Exception {
    CreateClientRequestV2 request = CreateClientRequestV2.builder().name("client2").build();
    // Initial request OK
    create(request);
    // Duplicate request fails
    Response httpResponse = create(request);
    assertThat(httpResponse.code()).isEqualTo(409);
}
Also used : Response(okhttp3.Response) CreateClientRequestV2(keywhiz.api.automation.v2.CreateClientRequestV2) Test(org.junit.Test)

Aggregations

ClientDetailResponseV2 (keywhiz.api.automation.v2.ClientDetailResponseV2)3 Test (org.junit.Test)3 CreateClientRequestV2 (keywhiz.api.automation.v2.CreateClientRequestV2)2 ModifyClientRequestV2 (keywhiz.api.automation.v2.ModifyClientRequestV2)2 ModifyGroupsRequestV2 (keywhiz.api.automation.v2.ModifyGroupsRequestV2)2 ExceptionMetered (com.codahale.metrics.annotation.ExceptionMetered)1 Timed (com.codahale.metrics.annotation.Timed)1 Sets (com.google.common.collect.Sets)1 Auth (io.dropwizard.auth.Auth)1 String.format (java.lang.String.format)1 URI (java.net.URI)1 Instant (java.time.Instant)1 HashMap (java.util.HashMap)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Collectors.toSet (java.util.stream.Collectors.toSet)1 Stream (java.util.stream.Stream)1 Inject (javax.inject.Inject)1 Valid (javax.validation.Valid)1 Consumes (javax.ws.rs.Consumes)1