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

square / keywhiz / 3963561994

pending completion
3963561994

Pull #1189

github

GitHub
Merge 0e51f2a1d into c1e040c06
Pull Request #1189: Adding CLI option to the "describe secrets" action to include deleted secrets

33 of 33 new or added lines in 4 files covered. (100.0%)

5238 of 6959 relevant lines covered (75.27%)

0.75 hits per line

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

84.0
/cli/src/main/java/keywhiz/cli/commands/DescribeAction.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

17
package keywhiz.cli.commands;
18

19
import com.google.common.base.Throwables;
20
import java.io.IOException;
21
import java.util.List;
22
import javax.annotation.Nullable;
23
import keywhiz.api.model.Client;
24
import keywhiz.api.model.Group;
25
import keywhiz.api.model.SanitizedSecret;
26
import keywhiz.cli.Printing;
27
import keywhiz.cli.configs.DescribeActionConfig;
28
import keywhiz.client.KeywhizClient;
29
import keywhiz.client.KeywhizClient.NotFoundException;
30

31
import static java.lang.String.format;
32
import static keywhiz.cli.Utilities.VALID_NAME_PATTERN;
33
import static keywhiz.cli.Utilities.validName;
34

35
public class DescribeAction implements Runnable {
36

37
  private final DescribeActionConfig describeActionConfig;
38
  private final KeywhizClient keywhizClient;
39
  private final Printing printing;
40

41
  public DescribeAction(DescribeActionConfig describeActionConfig, KeywhizClient client,
42
      Printing printing) {
1✔
43
    this.describeActionConfig = describeActionConfig;
1✔
44
    this.keywhizClient = client;
1✔
45
    this.printing = printing;
1✔
46
  }
1✔
47

48
  @Override public void run() {
49
    List<String> describeType = describeActionConfig.describeType;
1✔
50

51
    if (describeType == null || describeType.isEmpty()) {
1✔
52
      throw new IllegalArgumentException("Must specify a single type to describe.");
1✔
53
    }
54

55
    if (describeActionConfig.name == null || !validName(describeActionConfig.name)) {
1✔
56
      throw new IllegalArgumentException(format("Invalid name, must match %s", VALID_NAME_PATTERN));
1✔
57
    }
58

59
    String firstType = describeType.get(0).toLowerCase().trim();
1✔
60
    String name = describeActionConfig.name;
1✔
61

62
    switch (firstType) {
1✔
63

64
      case "group":
65
        try {
66
          Group group = keywhizClient.getGroupByName(name);
1✔
67
          printing.printGroupWithDetails(group);
1✔
68
        } catch (NotFoundException e) {
1✔
69
          throw new AssertionError("Group not found.");
1✔
70
        } catch (IOException e) {
×
71
          throw Throwables.propagate(e);
×
72
        }
1✔
73
        break;
74

75
      case "client":
76
        try {
77
          Client client = keywhizClient.getClientByName(name);
1✔
78
          printing.printClientWithDetails(client);
1✔
79
        } catch (NotFoundException e) {
1✔
80
          throw new AssertionError("Client not found.");
1✔
81
        } catch (IOException e) {
×
82
          throw Throwables.propagate(e);
×
83
        }
1✔
84
        break;
85

86
      case "secret":
87
        describeSecret();
1✔
88
        break;
1✔
89

90
      default:
91
        throw new IllegalArgumentException("Invalid describe type specified: " + firstType);
1✔
92
    }
93
  }
1✔
94

95
  private void describeSecret() {
96
    SanitizedSecret sanitizedSecret = getSanitizedSecret();
1✔
97
    List<SanitizedSecret> deletedSecrets = describeActionConfig.includeDeleted
1✔
98
        ? getDeletedSecrets()
1✔
99
        : null;
1✔
100

101
    if (sanitizedSecret != null) {
1✔
102
      printing.printSanitizedSecretWithDetails(sanitizedSecret);
1✔
103
    }
104

105
    if (deletedSecrets != null) {
1✔
106
      printing.printDeletedSecretsWithDetails(deletedSecrets);
1✔
107
    }
108
  }
1✔
109

110
  @Nullable
111
  private SanitizedSecret getSanitizedSecret() {
112
    try {
113
      return keywhizClient.getSanitizedSecretByName(describeActionConfig.name);
1✔
114
    } catch (NotFoundException e) {
1✔
115
      if (!describeActionConfig.includeDeleted) {
1✔
116
        throw new AssertionError("Secret not found.");
1✔
117
      }
118
      // If we're including deleted secrets, it's ok if no non-deleted secret was found
119
      return null;
1✔
120
    } catch (IOException e) {
×
121
      throw Throwables.propagate(e);
×
122
    }
123
  }
124

125
  private List<SanitizedSecret> getDeletedSecrets() {
126
    try {
127
      return keywhizClient.getDeletedSecretsByName(describeActionConfig.name);
1✔
128
    } catch (IOException e) {
×
129
      throw Throwables.propagate(e);
×
130
    }
131
  }
132
}
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

© 2026 Coveralls, Inc