use of keywhiz.api.model.Client in project keywhiz by square.
the class ClientDAO method sawClient.
public void sawClient(Client client) {
Instant now = Instant.now();
Instant lastSeen = Optional.ofNullable(client.getLastSeen()).map(ls -> Instant.ofEpochSecond(ls.toEpochSecond())).orElse(null);
// this way we can have less granularity on lastSeen and save db writes
if (lastSeen == null || now.isAfter(lastSeen.plus(lastSeenThreshold, SECONDS))) {
dslContext.transaction(configuration -> {
Param<Long> val = DSL.val(now.getEpochSecond(), CLIENTS.LASTSEEN);
DSL.using(configuration).update(CLIENTS).set(CLIENTS.LASTSEEN, DSL.when(CLIENTS.LASTSEEN.isNull(), val).otherwise(DSL.greatest(CLIENTS.LASTSEEN, val))).where(CLIENTS.ID.eq(client.getId())).execute();
});
}
}
use of keywhiz.api.model.Client in project keywhiz by square.
the class AutomationGroupResource method getGroupByName.
/**
* Retrieve Group by a specified name, or all Groups if no name given
*
* @param automationClient the client with automation access performing this operation
* @param name the name of the Group to retrieve, if provided
* @return details on the specified group, or an all groups if no name specified
*
* 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();
}
use of keywhiz.api.model.Client in project keywhiz by square.
the class GroupsResource method groupDetailResponseFromId.
private GroupDetailResponse groupDetailResponseFromId(long groupId) {
Optional<Group> optionalGroup = groupDAO.getGroupById(groupId);
if (!optionalGroup.isPresent()) {
throw new NotFoundException("Group not found.");
}
Group group = optionalGroup.get();
ImmutableList<SanitizedSecret> secrets = ImmutableList.copyOf(aclDAO.getSanitizedSecretsFor(group));
ImmutableList<Client> clients = ImmutableList.copyOf(aclDAO.getClientsFor(group));
return GroupDetailResponse.fromGroup(group, secrets, clients);
}
use of keywhiz.api.model.Client in project keywhiz by square.
the class AutomationClientResource method createClient.
/**
* Create Client
*
* @param automationClient the client with automation access performing this operation
* @param clientRequest the JSON client request used to formulate the Client
* @return information about the created client on success
*
* description Creates a Client with the name from a valid client request
* responseMessage 200 Successfully created Client
* responseMessage 409 Client with given name already exists
*/
@Timed
@ExceptionMetered
@POST
@Consumes(APPLICATION_JSON)
public ClientDetailResponse createClient(@Auth AutomationClient automationClient, @Valid CreateClientRequest clientRequest) {
Optional<Client> client = clientDAO.getClientByName(clientRequest.name);
if (client.isPresent()) {
logger.info("Automation ({}) - Client {} already exists", automationClient.getName(), clientRequest.name);
throw new ConflictException("Client name already exists.");
}
long id = clientDAO.createClient(clientRequest.name, automationClient.getName(), "", null);
client = clientDAO.getClientById(id);
if (client.isPresent()) {
Map<String, String> extraInfo = new HashMap<>();
extraInfo.put("deprecated", "true");
auditLog.recordEvent(new Event(Instant.now(), EventTag.CLIENT_CREATE, automationClient.getName(), client.get().getName(), extraInfo));
}
return ClientDetailResponse.fromClient(client.get(), ImmutableList.of(), ImmutableList.of());
}
use of keywhiz.api.model.Client 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();
}
Aggregations