• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

square / keywhiz / 4555816003

pending completion
4555816003

Pull #1205

github

GitHub
Merge c48a5f2b4 into 2bb01dacb
Pull Request #1205: Chloeb/implement undelete

97 of 97 new or added lines in 8 files covered. (100.0%)

5385 of 7159 relevant lines covered (75.22%)

0.75 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/cli/src/main/java/keywhiz/cli/CommandExecutor.java
1
/*
2
 * Copyright (C) 2015 Square, Inc.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package keywhiz.cli;
17

18
import com.beust.jcommander.JCommander;
19
import com.fasterxml.jackson.databind.ObjectMapper;
20
import com.google.common.base.Strings;
21
import com.google.common.collect.ImmutableList;
22
import com.google.inject.name.Named;
23
import java.io.IOException;
24
import java.net.HttpCookie;
25
import java.nio.file.Path;
26
import java.nio.file.Paths;
27
import java.util.Arrays;
28
import java.util.List;
29
import java.util.Map;
30
import javax.annotation.Nullable;
31
import javax.inject.Inject;
32
import keywhiz.cli.commands.AddAction;
33
import keywhiz.cli.commands.AssignAction;
34
import keywhiz.cli.commands.DeleteAction;
35
import keywhiz.cli.commands.DescribeAction;
36
import keywhiz.cli.commands.ListAction;
37
import keywhiz.cli.commands.ListVersionsAction;
38
import keywhiz.cli.commands.RenameAction;
39
import keywhiz.cli.commands.RollbackAction;
40
import keywhiz.cli.commands.UnassignAction;
41
import keywhiz.cli.commands.UndeleteAction;
42
import keywhiz.cli.commands.UpdateAction;
43
import keywhiz.cli.configs.AddActionConfig;
44
import keywhiz.cli.configs.AssignActionConfig;
45
import keywhiz.cli.configs.CliConfiguration;
46
import keywhiz.cli.configs.DeleteActionConfig;
47
import keywhiz.cli.configs.DescribeActionConfig;
48
import keywhiz.cli.configs.ListActionConfig;
49
import keywhiz.cli.configs.ListVersionsActionConfig;
50
import keywhiz.cli.configs.RenameActionConfig;
51
import keywhiz.cli.configs.RollbackActionConfig;
52
import keywhiz.cli.configs.UnassignActionConfig;
53
import keywhiz.cli.configs.UndeleteActionConfig;
54
import keywhiz.cli.configs.UpdateActionConfig;
55
import keywhiz.client.KeywhizClient;
56
import keywhiz.client.KeywhizClient.UnauthorizedException;
57
import okhttp3.HttpUrl;
58
import okhttp3.OkHttpClient;
59

60
import static com.google.common.base.StandardSystemProperty.USER_HOME;
61
import static com.google.common.base.StandardSystemProperty.USER_NAME;
62
import static java.lang.String.format;
63

64
public class CommandExecutor {
65
  public static final String APP_VERSION = "2.1";
66

67
  public enum Command {
×
68
    ADD,
×
69
    ASSIGN,
×
70
    DELETE,
×
71
    UNDELETE,
×
72
    DESCRIBE,
×
73
    LIST,
×
74
    LOGIN,
×
75
    RENAME,
×
76
    ROLLBACK,
×
77
    UNASSIGN,
×
78
    UPDATE,
×
79
    VERSIONS,
×
80
  }
81

82
  private final Path cookieDir = Paths.get(USER_HOME.value());
×
83

84
  private final String command;
85
  private final Map commands;
86
  private final JCommander parentCommander;
87
  private final JCommander commander;
88
  private final CliConfiguration config;
89
  private final ObjectMapper mapper;
90

91
  @Inject
92
  public CommandExecutor(
93
      CliConfiguration config,
94
      @Named("Command") @Nullable String command,
95
      @Named("CommandMap") Map commands,
96
      @Named("ParentCommander") JCommander parentCommander,
97
      @Named("Commander") @Nullable JCommander commander,
98
      ObjectMapper mapper) {
×
99
    this.command = command;
×
100
    this.commands = commands;
×
101
    this.parentCommander = parentCommander;
×
102
    this.commander = commander;
×
103
    this.config = config;
×
104
    this.mapper = mapper;
×
105
  }
×
106

107
  public void executeCommand() throws IOException {
108
    if (command == null) {
×
109
      if (config.version) {
×
110
        System.out.println("Version: " + APP_VERSION);
×
111
      } else {
112
        System.err.println("Must specify a command.");
×
113
        parentCommander.usage();
×
114
      }
115

116
      return;
×
117
    }
118

119
    HttpUrl url;
120
    if (Strings.isNullOrEmpty(config.url)) {
×
121
      url = HttpUrl.parse("https://localhost:4444");
×
122
      System.out.println("Server URL not specified (--url flag), assuming " + url);
×
123
    } else {
124
      url = HttpUrl.parse(config.url);
×
125
      if (url == null) {
×
126
        System.err.print("Invalid URL " + config.url);
×
127
        return;
×
128
      }
129
    }
130

131
    KeywhizClient client;
132
    OkHttpClient httpClient;
133

134
    String user = config.getUser().orElse(USER_NAME.value());
×
135
    Path cookiePath = cookieDir.resolve(format(".keywhiz.%s.cookie", user));
×
136

137
    try {
138
      List<HttpCookie> cookieList = ClientUtils.loadCookies(cookiePath);
×
139
      httpClient = ClientUtils.sslOkHttpClient(config.getDevTrustStore(), cookieList);
×
140
      client = new KeywhizClient(mapper, httpClient, url);
×
141

142
      // Try a simple get request to determine whether or not the cookies are still valid
143
      if(!client.isLoggedIn()) {
×
144
        throw new UnauthorizedException();
×
145
      }
146
    } catch (IOException e) {
×
147
      // Either could not find the cookie file, or the cookies were expired -- must login manually.
148
      httpClient = ClientUtils.sslOkHttpClient(config.getDevTrustStore(), ImmutableList.of());
×
149
      client = new KeywhizClient(mapper, httpClient, url);
×
150

151
      char[] password = ClientUtils.readPassword(user);
×
152
      client.login(user, password);
×
153
      Arrays.fill(password, '\0');
×
154
    }
×
155
    // Save/update the cookies if we logged in successfully
156
    ClientUtils.saveCookies(cookiePath);
×
157

158
    Printing printing = new Printing(client);
×
159

160
    Command cmd = Command.valueOf(command.toUpperCase().trim());
×
161
    switch (cmd) {
×
162
      case LIST:
163
        new ListAction((ListActionConfig) commands.get(command), client, printing).run();
×
164
        break;
×
165

166
      case DESCRIBE:
167
        new DescribeAction((DescribeActionConfig) commands.get(command), client, printing).run();
×
168
        break;
×
169

170
      case ADD:
171
        new AddAction((AddActionConfig) commands.get(command), client, mapper).run();
×
172
        break;
×
173

174
      case UPDATE:
175
        new UpdateAction((UpdateActionConfig) commands.get(command), client, mapper).run();
×
176
        break;
×
177

178
      case DELETE:
179
        new DeleteAction((DeleteActionConfig) commands.get(command), client).run();
×
180
        break;
×
181

182
      case UNDELETE:
183
        new UndeleteAction((UndeleteActionConfig) commands.get(command), client).run();
×
184
        break;
×
185

186
      case ASSIGN:
187
        new AssignAction((AssignActionConfig) commands.get(command), client).run();
×
188
        break;
×
189

190
      case UNASSIGN:
191
        new UnassignAction((UnassignActionConfig) commands.get(command), client).run();
×
192
        break;
×
193

194
      case VERSIONS:
195
        new ListVersionsAction((ListVersionsActionConfig) commands.get(command), client, printing).run();
×
196
        break;
×
197

198
      case ROLLBACK:
199
        new RollbackAction((RollbackActionConfig) commands.get(command), client).run();
×
200
        break;
×
201

202
      case RENAME:
203
        new RenameAction((RenameActionConfig) commands.get(command), client).run();
×
204
        break;
×
205

206
      case LOGIN:
207
        // User is already logged in at this point
208
        break;
×
209

210
      default:
211
        commander.usage();
×
212
    }
213
  }
×
214
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc