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

square / keywhiz / 3998983595

pending completion
3998983595

push

github

GitHub
Merge pull request #1189 from square/chloeb/datasec-677

Adding CLI option to the "describe secrets" action to include deleted secrets

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

5237 of 6968 relevant lines covered (75.16%)

0.75 hits per line

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

82.35
/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
    describeNonDeletedSecret();
1✔
97
    if (describeActionConfig.includeDeleted) {
1✔
98
      describeDeletedSecrets();
1✔
99
    }
100
  }
1✔
101

102
  private void describeNonDeletedSecret() {
103
    SanitizedSecret sanitizedSecret = getNonDeletedSecret();
1✔
104
    printing.printNonDeletedSecretWithDetails(sanitizedSecret);
1✔
105
  }
1✔
106

107
  private void describeDeletedSecrets() {
108
    List<SanitizedSecret> deletedSecrets = getDeletedSecrets();
1✔
109
    printing.printDeletedSecretsWithDetails(deletedSecrets);
1✔
110
  }
1✔
111

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

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