Search in sources :

Example 1 with KeywhizClient

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());
}
Also used : Client(keywhiz.api.model.Client) KeywhizClient(keywhiz.client.KeywhizClient) Test(org.junit.Test)

Example 2 with KeywhizClient

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();
    }
}
Also used : OkHttpClient(okhttp3.OkHttpClient) AssignActionConfig(keywhiz.cli.configs.AssignActionConfig) UpdateActionConfig(keywhiz.cli.configs.UpdateActionConfig) ListVersionsAction(keywhiz.cli.commands.ListVersionsAction) AddAction(keywhiz.cli.commands.AddAction) UnassignActionConfig(keywhiz.cli.configs.UnassignActionConfig) ListActionConfig(keywhiz.cli.configs.ListActionConfig) UnauthorizedException(keywhiz.client.KeywhizClient.UnauthorizedException) UnassignAction(keywhiz.cli.commands.UnassignAction) AddActionConfig(keywhiz.cli.configs.AddActionConfig) RenameAction(keywhiz.cli.commands.RenameAction) RenameActionConfig(keywhiz.cli.configs.RenameActionConfig) Path(java.nio.file.Path) KeywhizClient(keywhiz.client.KeywhizClient) AssignAction(keywhiz.cli.commands.AssignAction) UpdateAction(keywhiz.cli.commands.UpdateAction) DeleteActionConfig(keywhiz.cli.configs.DeleteActionConfig) IOException(java.io.IOException) RollbackAction(keywhiz.cli.commands.RollbackAction) HttpUrl(okhttp3.HttpUrl) ListVersionsActionConfig(keywhiz.cli.configs.ListVersionsActionConfig) RollbackActionConfig(keywhiz.cli.configs.RollbackActionConfig) DescribeActionConfig(keywhiz.cli.configs.DescribeActionConfig) ListAction(keywhiz.cli.commands.ListAction) DeleteAction(keywhiz.cli.commands.DeleteAction) HttpCookie(java.net.HttpCookie) DescribeAction(keywhiz.cli.commands.DescribeAction)

Example 3 with KeywhizClient

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());
}
Also used : NotFoundException(keywhiz.client.KeywhizClient.NotFoundException) Client(keywhiz.api.model.Client) KeywhizClient(keywhiz.client.KeywhizClient) Test(org.junit.Test)

Example 4 with KeywhizClient

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());
}
Also used : Client(keywhiz.api.model.Client) KeywhizClient(keywhiz.client.KeywhizClient) Test(org.junit.Test)

Example 5 with KeywhizClient

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);
}
Also used : KeywhizClient(keywhiz.client.KeywhizClient) OkHttpClient(okhttp3.OkHttpClient) KeyStore(java.security.KeyStore) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

KeywhizClient (keywhiz.client.KeywhizClient)5 Client (keywhiz.api.model.Client)3 Test (org.junit.Test)3 OkHttpClient (okhttp3.OkHttpClient)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 IOException (java.io.IOException)1 HttpCookie (java.net.HttpCookie)1 Path (java.nio.file.Path)1 KeyStore (java.security.KeyStore)1 AddAction (keywhiz.cli.commands.AddAction)1 AssignAction (keywhiz.cli.commands.AssignAction)1 DeleteAction (keywhiz.cli.commands.DeleteAction)1 DescribeAction (keywhiz.cli.commands.DescribeAction)1 ListAction (keywhiz.cli.commands.ListAction)1 ListVersionsAction (keywhiz.cli.commands.ListVersionsAction)1 RenameAction (keywhiz.cli.commands.RenameAction)1 RollbackAction (keywhiz.cli.commands.RollbackAction)1 UnassignAction (keywhiz.cli.commands.UnassignAction)1 UpdateAction (keywhiz.cli.commands.UpdateAction)1 AddActionConfig (keywhiz.cli.configs.AddActionConfig)1