use of keywhiz.client.KeywhizClient in project keywhiz by square.
the class DeleteActionTest method deleteCallsDeleteForClient.
@Test
public void deleteCallsDeleteForClient() throws Exception {
deleteAction.inputStream = yes;
deleteActionConfig.deleteType = Arrays.asList("client");
deleteActionConfig.name = "newClient";
Client client = new Client(657, "newClient", null, null, NOW, null, NOW, null, null, null, true, false);
when(keywhizClient.getClientByName(deleteActionConfig.name)).thenReturn(client);
deleteAction.run();
verify(keywhizClient).deleteClientWithId(client.getId());
}
use of keywhiz.client.KeywhizClient in project keywhiz by square.
the class CommandExecutor method executeCommand.
public void executeCommand() throws IOException {
if (command == null) {
if (config.version) {
System.out.println("Version: " + APP_VERSION);
} else {
System.err.println("Must specify a command.");
parentCommander.usage();
}
return;
}
HttpUrl url;
if (Strings.isNullOrEmpty(config.url)) {
url = HttpUrl.parse("https://localhost:4444");
System.out.println("Server URL not specified (--url flag), assuming " + url);
} else {
url = HttpUrl.parse(config.url);
if (url == null) {
System.err.print("Invalid URL " + config.url);
return;
}
}
KeywhizClient client;
OkHttpClient httpClient;
String user = config.getUser().orElse(USER_NAME.value());
Path cookiePath = cookieDir.resolve(format(".keywhiz.%s.cookie", user));
try {
List<HttpCookie> cookieList = ClientUtils.loadCookies(cookiePath);
httpClient = ClientUtils.sslOkHttpClient(config.getDevTrustStore(), cookieList);
client = new KeywhizClient(mapper, httpClient, url);
// Try a simple get request to determine whether or not the cookies are still valid
if (!client.isLoggedIn()) {
throw new UnauthorizedException();
}
} catch (IOException e) {
// Either could not find the cookie file, or the cookies were expired -- must login manually.
httpClient = ClientUtils.sslOkHttpClient(config.getDevTrustStore(), ImmutableList.of());
client = new KeywhizClient(mapper, httpClient, url);
char[] password = ClientUtils.readPassword(user);
client.login(user, password);
Arrays.fill(password, '\0');
}
// Save/update the cookies if we logged in successfully
ClientUtils.saveCookies(cookiePath);
Printing printing = new Printing(client);
Command cmd = Command.valueOf(command.toUpperCase().trim());
switch(cmd) {
case LIST:
new ListAction((ListActionConfig) commands.get(command), client, printing).run();
break;
case DESCRIBE:
new DescribeAction((DescribeActionConfig) commands.get(command), client, printing).run();
break;
case ADD:
new AddAction((AddActionConfig) commands.get(command), client, mapper).run();
break;
case UPDATE:
new UpdateAction((UpdateActionConfig) commands.get(command), client, mapper).run();
break;
case DELETE:
new DeleteAction((DeleteActionConfig) commands.get(command), client).run();
break;
case ASSIGN:
new AssignAction((AssignActionConfig) commands.get(command), client).run();
break;
case UNASSIGN:
new UnassignAction((UnassignActionConfig) commands.get(command), client).run();
break;
case VERSIONS:
new ListVersionsAction((ListVersionsActionConfig) commands.get(command), client, printing).run();
break;
case ROLLBACK:
new RollbackAction((RollbackActionConfig) commands.get(command), client).run();
break;
case RENAME:
new RenameAction((RenameActionConfig) commands.get(command), client).run();
case LOGIN:
// User is already logged in at this point
break;
default:
commander.usage();
}
}
use of keywhiz.client.KeywhizClient in project keywhiz by square.
the class AssignActionTest method assignClientAddsClientsThatDoNotExist.
@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);
// Group exists
when(keywhizClient.getGroupByName(group.getName())).thenReturn(group);
// Client does not exist, but 2nd call returns the created client.
when(keywhizClient.getClientByName(client.getName())).thenThrow(new NotFoundException()).thenReturn(client);
// Client not assigned to group
when(keywhizClient.groupDetailsForId(group.getId())).thenReturn(groupDetailResponse);
assignAction.run();
verify(keywhizClient).createClient(assignActionConfig.name, "", "");
verify(keywhizClient).enrollClientInGroupByIds(client.getId(), group.getId());
}
use of keywhiz.client.KeywhizClient in project keywhiz by square.
the class AssignActionTest method assignCallsAssignForClient.
@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);
// Group exists
when(keywhizClient.getGroupByName(group.getName())).thenReturn(group);
// Client exists
when(keywhizClient.getClientByName(assignActionConfig.name)).thenReturn(client);
// Client not assigned to group
when(keywhizClient.groupDetailsForId(group.getId())).thenReturn(groupDetailResponse);
assignAction.run();
verify(keywhizClient, never()).createClient(anyString(), anyString(), anyString());
verify(keywhizClient).enrollClientInGroupByIds(client.getId(), group.getId());
}
use of keywhiz.client.KeywhizClient in project keywhiz by square.
the class TestClients method keywhizClient.
public static KeywhizClient keywhizClient() {
String password = "ponies";
KeyStore trustStore = keyStoreFromResource("dev_and_test_truststore.p12", password);
OkHttpClient httpClient = HttpClients.builder().addRequestInterceptors(new AuthHelper.AcceptRequestInterceptor(MediaType.APPLICATION_JSON)).build(trustStore);
ObjectMapper mapper = KeywhizService.customizeObjectMapper(Jackson.newObjectMapper());
return new KeywhizClient(mapper, httpClient, TEST_URL);
}
Aggregations