AssignAction implements Runnable { @Override public void run() { List<String> assignType = assignActionConfig.assignType; if (assignType == null || assignType.isEmpty()) { throw new IllegalArgumentException("Must specify a single type to assign."); } if (assignActionConfig.name == null || !validName(assignActionConfig.name) || assignActionConfig.group == null || !validName(assignActionConfig.group)) { throw new IllegalArgumentException(format("Invalid name, must match %s", VALID_NAME_PATTERN)); } Group group; try { group = keywhizClient.getGroupByName(assignActionConfig.group); } catch (KeywhizClient.NotFoundException e) { throw new AssertionError("Group doesn't exist."); } catch (IOException e) { throw Throwables.propagate(e); } String type = assignType.get(0).toLowerCase().trim(); switch (type) { case "client": Client client = null; boolean createClient = false; try { client = keywhizClient.getClientByName(assignActionConfig.name); } catch (KeywhizClient.NotFoundException e) { logger.info("Creating client '{}'.", assignActionConfig.name); createClient = true; } catch (IOException e) { throw Throwables.propagate(e); } if (createClient) { try { keywhizClient.createClient(assignActionConfig.name, "", ""); client = keywhizClient.getClientByName(assignActionConfig.name); } catch (IOException e) { throw Throwables.propagate(e); } } try { if (keywhizClient.groupDetailsForId(group.getId()).getClients().contains(client)) { throw new AssertionError( format("Client '%s' already assigned to group '%s'", assignActionConfig.name, group.getName())); } logger.info("Enrolling client '{}' in group '{}'.", client.getName(), group.getName()); keywhizClient.enrollClientInGroupByIds(client.getId(), group.getId()); } catch (IOException e) { throw Throwables.propagate(e); } break; case "secret": try { long groupId = group.getId(); SanitizedSecret sanitizedSecret = keywhizClient.getSanitizedSecretByName(assignActionConfig.name); if (keywhizClient.groupDetailsForId(groupId).getSecrets().contains(sanitizedSecret)) { throw new AssertionError( format("Secret '%s' already assigned to group '%s'", assignActionConfig.name, group.getName())); } logger.info("Allowing group '{}' access to secret '{}'.", group.getName(), sanitizedSecret.name()); keywhizClient.grantSecretToGroupByIds(sanitizedSecret.id(), groupId); } catch (KeywhizClient.NotFoundException e) { throw new AssertionError("Secret doesn't exist."); } catch (IOException e) { throw Throwables.propagate(e); } break; default: throw new IllegalArgumentException("Invalid assign type specified: " + type); } } AssignAction(AssignActionConfig assignActionConfig, KeywhizClient client); @Override void run();  }
@Test public void assignClientAddsClientsThatDoNotExist() throws Exception { assignActionConfig.assignType = Arrays.asList("client"); assignActionConfig.name = "non-existent-client-name"; assignActionConfig.group = group.getName(); Client client = new Client(543, assignActionConfig.name, null, null, null, null, null, null, null, null, false, false ); when(keywhizClient.getGroupByName(group.getName())).thenReturn(group); when(keywhizClient.getClientByName(client.getName())) .thenThrow(new NotFoundException()).thenReturn(client); when(keywhizClient.groupDetailsForId(group.getId())).thenReturn(groupDetailResponse); assignAction.run(); verify(keywhizClient).createClient(assignActionConfig.name, "", ""); verify(keywhizClient).enrollClientInGroupByIds(client.getId(), group.getId()); }
@Test public void assignCallsAssignForClient() throws Exception { assignActionConfig.assignType = Arrays.asList("client"); assignActionConfig.name = "existing-client-name"; assignActionConfig.group = group.getName(); Client client = new Client(5673, assignActionConfig.name, null, null, null, null, null, null, null, null, false, true ); when(keywhizClient.getGroupByName(group.getName())).thenReturn(group); when(keywhizClient.getClientByName(assignActionConfig.name)).thenReturn(client); when(keywhizClient.groupDetailsForId(group.getId())).thenReturn(groupDetailResponse); assignAction.run(); verify(keywhizClient, never()).createClient(anyString(), anyString(), anyString()); verify(keywhizClient).enrollClientInGroupByIds(client.getId(), group.getId()); }
@Test public void assignCallsAssignForSecret() throws Exception { assignActionConfig.assignType = Arrays.asList("secret"); assignActionConfig.name = secret.getDisplayName(); assignActionConfig.group = group.getName(); when(keywhizClient.getGroupByName(group.getName())).thenReturn(group); when(keywhizClient.getSanitizedSecretByName(secret.getName())) .thenReturn(sanitizedSecret); when(keywhizClient.groupDetailsForId(group.getId())).thenReturn(groupDetailResponse); assignAction.run(); verify(keywhizClient).grantSecretToGroupByIds(secret.getId(), group.getId()); }
@Test(expected = IllegalArgumentException.class) public void assignThrowsIfNoTypeSpecified() throws Exception { assignActionConfig.assignType = null; assignAction.run(); }
@Test(expected = IllegalArgumentException.class) public void assignThrowsIfInvalidType() throws Exception { assignActionConfig.assignType = Arrays.asList("invalid_type"); assignActionConfig.name = group.getName(); assignActionConfig.group = group.getName(); when(keywhizClient.getGroupByName(group.getName())).thenReturn(group); assignAction.run(); }
@Test(expected = IllegalArgumentException.class) public void assignValidatesGroupName() throws Exception { assignActionConfig.assignType = Arrays.asList("secret"); assignActionConfig.name = "General_Password"; assignActionConfig.group = "Invalid Name"; assignAction.run(); }
@Test(expected = IllegalArgumentException.class) public void assignValidatesSecretName() throws Exception { assignActionConfig.assignType = Arrays.asList("secret"); assignActionConfig.name = "Invalid Name"; assignActionConfig.group = "Web"; assignAction.run(); }