Search in sources :

Example 1 with RenameActionConfig

use of keywhiz.cli.configs.RenameActionConfig in project keywhiz by square.

the class RenameActionTest method renamesSecret.

@Test
public void renamesSecret() throws IOException {
    Long secretId = 1L;
    String secretName = "foo";
    String newName = "bar";
    SanitizedSecret secret = SanitizedSecret.of(secretId, secretName);
    when(keywhiz.getSanitizedSecretByName(secretName)).thenReturn(secret);
    RenameActionConfig config = new RenameActionConfig();
    config.resourceType = "secret";
    config.oldName = secretName;
    config.newName = newName;
    RenameAction action = new RenameAction(config, keywhiz);
    action.run();
    verify(keywhiz).renameSecret(secretId, newName);
}
Also used : SanitizedSecret(keywhiz.api.model.SanitizedSecret) RenameActionConfig(keywhiz.cli.configs.RenameActionConfig) Test(org.junit.Test)

Example 2 with RenameActionConfig

use of keywhiz.cli.configs.RenameActionConfig in project keywhiz by square.

the class RenameActionTest method rejectsNonSecretResourceType.

@Test
public void rejectsNonSecretResourceType() {
    RenameActionConfig config = new RenameActionConfig();
    config.resourceType = "client";
    RenameAction action = new RenameAction(config, keywhiz);
    thrown.expect(IllegalArgumentException.class);
    action.run();
}
Also used : RenameActionConfig(keywhiz.cli.configs.RenameActionConfig) Test(org.junit.Test)

Example 3 with RenameActionConfig

use of keywhiz.cli.configs.RenameActionConfig 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 4 with RenameActionConfig

use of keywhiz.cli.configs.RenameActionConfig in project keywhiz by square.

the class CliMainTest method parsesRenameCommand.

@Test
public void parsesRenameCommand() {
    CliMain.CommandLineParsingContext context = new CliMain.CommandLineParsingContext();
    JCommander commander = context.getCommander();
    commander.parse("rename", "secret", "--oldName", "foo", "--newName", "bar");
    String parsedCommand = commander.getParsedCommand();
    assertEquals("rename", parsedCommand);
    RenameActionConfig config = (RenameActionConfig) context.getCommands().get(parsedCommand);
    assertEquals("secret", config.resourceType);
    assertEquals("foo", config.oldName);
    assertEquals("bar", config.newName);
}
Also used : JCommander(com.beust.jcommander.JCommander) RenameActionConfig(keywhiz.cli.configs.RenameActionConfig) Test(org.junit.Test)

Aggregations

RenameActionConfig (keywhiz.cli.configs.RenameActionConfig)4 Test (org.junit.Test)3 JCommander (com.beust.jcommander.JCommander)1 IOException (java.io.IOException)1 HttpCookie (java.net.HttpCookie)1 Path (java.nio.file.Path)1 SanitizedSecret (keywhiz.api.model.SanitizedSecret)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 AssignActionConfig (keywhiz.cli.configs.AssignActionConfig)1 DeleteActionConfig (keywhiz.cli.configs.DeleteActionConfig)1