Skip to content

Commit d2104a6

Browse files
authored
Fix mask token on current context display (#131)
1 parent c47bc15 commit d2104a6

14 files changed

+87
-39
lines changed

src/main/java/com/michelin/kafkactl/command/Get.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ public Integer onAuthSuccess() throws IOException {
7474
try {
7575
// Get individual resources for given types (k get topic topic1)
7676
Resource singleResource =
77-
resourceService.getSingleResourceWithType(apiResources.get(0), namespace, resourceName.get(), true);
77+
resourceService.getSingleResourceWithType(apiResources.getFirst(), namespace, resourceName.get(), true);
7878
formatService.displaySingle(singleResource, output, commandSpec);
7979
return 0;
8080
} catch (HttpClientResponseException e) {
81-
formatService.displayError(e, apiResources.get(0).getKind(), resourceName.get(), commandSpec);
81+
formatService.displayError(e, apiResources.getFirst().getKind(), resourceName.get(), commandSpec);
8282
return 1;
8383
}
8484
}

src/main/java/com/michelin/kafkactl/command/config/ConfigCurrentContext.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package com.michelin.kafkactl.command.config;
22

3+
import static com.michelin.kafkactl.mixin.UnmaskTokenMixin.MASKED;
34
import static com.michelin.kafkactl.service.FormatService.TABLE;
45
import static com.michelin.kafkactl.util.constant.ResourceKind.CONTEXT;
56

67
import com.michelin.kafkactl.config.KafkactlConfig;
78
import com.michelin.kafkactl.hook.ValidCurrentContextHook;
9+
import com.michelin.kafkactl.mixin.UnmaskTokenMixin;
810
import com.michelin.kafkactl.model.Metadata;
911
import com.michelin.kafkactl.model.Resource;
1012
import com.michelin.kafkactl.service.FormatService;
@@ -16,6 +18,7 @@
1618
import java.util.List;
1719
import java.util.Map;
1820
import picocli.CommandLine.Command;
21+
import picocli.CommandLine.Mixin;
1922

2023
/**
2124
* Config current context subcommand.
@@ -40,12 +43,20 @@ public class ConfigCurrentContext extends ValidCurrentContextHook {
4043
@ReflectiveAccess
4144
private FormatService formatService;
4245

46+
@Mixin
47+
public UnmaskTokenMixin unmaskTokenMixin;
48+
4349
@Override
4450
public Integer onContextValid() {
4551
Map<String, Object> specs = new HashMap<>();
4652
specs.put("namespace", kafkactlConfig.getCurrentNamespace());
4753
specs.put("api", kafkactlConfig.getApi());
48-
specs.put("token", kafkactlConfig.getUserToken());
54+
55+
if (unmaskTokenMixin.unmaskTokens) {
56+
specs.put("token", kafkactlConfig.getUserToken());
57+
} else {
58+
specs.put("token", MASKED);
59+
}
4960

5061
String currentContextName = configService.getCurrentContextName();
5162
Resource currentContextAsResource = Resource.builder()

src/main/java/com/michelin/kafkactl/command/config/ConfigGetContexts.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.michelin.kafkactl.command.config;
22

3+
import static com.michelin.kafkactl.mixin.UnmaskTokenMixin.MASKED;
34
import static com.michelin.kafkactl.service.FormatService.TABLE;
45
import static com.michelin.kafkactl.util.constant.ResourceKind.CONTEXT;
56

67
import com.michelin.kafkactl.config.KafkactlConfig;
8+
import com.michelin.kafkactl.mixin.UnmaskTokenMixin;
79
import com.michelin.kafkactl.model.Metadata;
810
import com.michelin.kafkactl.model.Resource;
911
import com.michelin.kafkactl.service.FormatService;
@@ -15,8 +17,8 @@
1517
import java.util.Map;
1618
import java.util.concurrent.Callable;
1719
import picocli.CommandLine.Command;
20+
import picocli.CommandLine.Mixin;
1821
import picocli.CommandLine.Model.CommandSpec;
19-
import picocli.CommandLine.Option;
2022
import picocli.CommandLine.Spec;
2123

2224
/**
@@ -34,8 +36,6 @@
3436
versionProvider = VersionProvider.class,
3537
mixinStandardHelpOptions = true)
3638
public class ConfigGetContexts implements Callable<Integer> {
37-
private static final String MASKED = "[MASKED]";
38-
3939
@Inject
4040
@ReflectiveAccess
4141
private KafkactlConfig kafkactlConfig;
@@ -47,8 +47,8 @@ public class ConfigGetContexts implements Callable<Integer> {
4747
@Spec
4848
public CommandSpec commandSpec;
4949

50-
@Option(names = {"-u", "--unmask-tokens"}, description = "Unmask tokens.")
51-
public boolean unmaskTokens;
50+
@Mixin
51+
public UnmaskTokenMixin unmaskTokenMixin;
5252

5353
@Override
5454
public Integer call() {
@@ -62,7 +62,7 @@ public Integer call() {
6262
specs.put("namespace", userContext.getDefinition().getNamespace());
6363
specs.put("api", userContext.getDefinition().getApi());
6464

65-
if (unmaskTokens) {
65+
if (unmaskTokenMixin.unmaskTokens) {
6666
specs.put("token", userContext.getDefinition().getUserToken());
6767
} else {
6868
specs.put("token", MASKED);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.michelin.kafkactl.mixin;
2+
3+
import picocli.CommandLine;
4+
5+
/**
6+
* Unmask token mixin.
7+
*/
8+
public class UnmaskTokenMixin {
9+
public static final String MASKED = "[MASKED]";
10+
11+
@CommandLine.Option(names = {"-u", "--unmask-tokens"}, description = "Unmask tokens.")
12+
public boolean unmaskTokens;
13+
}

src/main/java/com/michelin/kafkactl/service/ResourceService.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,12 @@ public int listAll(List<ApiResource> apiResources, String namespace, String outp
7373
// Get a single kind of resources
7474
if (apiResources.size() == 1) {
7575
try {
76-
List<Resource> resources = listResourcesWithType(apiResources.get(0), namespace);
76+
List<Resource> resources = listResourcesWithType(apiResources.getFirst(), namespace);
7777
if (!resources.isEmpty()) {
78-
formatService.displayList(resources.get(0).getKind(), resources, output, commandSpec);
78+
formatService.displayList(resources.getFirst().getKind(), resources, output, commandSpec);
7979
} else {
8080
commandSpec.commandLine().getOut()
81-
.println("No " + formatService.prettifyKind(apiResources.get(0).getKind()).toLowerCase()
81+
.println("No " + formatService.prettifyKind(apiResources.getFirst().getKind()).toLowerCase()
8282
+ " to display.");
8383
}
8484
return 0;
@@ -95,7 +95,7 @@ public int listAll(List<ApiResource> apiResources, String namespace, String outp
9595
try {
9696
List<Resource> resources = listResourcesWithType(apiResource, namespace);
9797
if (!resources.isEmpty()) {
98-
formatService.displayList(resources.get(0).getKind(), resources, output, commandSpec);
98+
formatService.displayList(resources.getFirst().getKind(), resources, output, commandSpec);
9999
}
100100
return 0;
101101
} catch (HttpClientResponseException exception) {

src/test/java/com/michelin/kafkactl/command/ApiResourcesTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ void shouldDisplayApiResources() {
102102
int code = cmd.execute();
103103
assertEquals(0, code);
104104
verify(formatService).displayList(eq(RESOURCE_DEFINITION),
105-
argThat(resources -> resources.get(0).getMetadata().getName().equals("Topic")),
105+
argThat(resources -> resources.getFirst().getMetadata().getName().equals("Topic")),
106106
eq(TABLE), eq(cmd.getCommandSpec()));
107107
}
108108

src/test/java/com/michelin/kafkactl/command/ConnectorTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ void shouldChangeState() {
135135
int code = cmd.execute("pause", "my-connector", "-n", "namespace");
136136
assertEquals(0, code);
137137
verify(formatService).displayList(eq(CHANGE_CONNECTOR_STATE),
138-
argThat(connectors -> connectors.get(0).equals(resource)),
138+
argThat(connectors -> connectors.getFirst().equals(resource)),
139139
eq(TABLE), eq(cmd.getCommandSpec()));
140140
}
141141

@@ -175,7 +175,7 @@ void shouldChangeStateOfAll() {
175175
int code = cmd.execute("pause", "all", "-n", "namespace");
176176
assertEquals(0, code);
177177
verify(formatService).displayList(eq(CHANGE_CONNECTOR_STATE),
178-
argThat(connectors -> connectors.get(0).equals(resource)),
178+
argThat(connectors -> connectors.getFirst().equals(resource)),
179179
eq(TABLE), eq(cmd.getCommandSpec()));
180180
}
181181

src/test/java/com/michelin/kafkactl/command/SchemaTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ void shouldUpdateCompat() {
126126
int code = cmd.execute("backward", "mySubject", "-n", "namespace");
127127
assertEquals(0, code);
128128
verify(formatService).displayList(eq(SCHEMA_COMPATIBILITY_STATE),
129-
argThat(schemas -> schemas.get(0).equals(resource)),
129+
argThat(schemas -> schemas.getFirst().equals(resource)),
130130
eq(TABLE), eq(cmd.getCommandSpec()));
131131
}
132132
}

src/test/java/com/michelin/kafkactl/command/config/ConfigCurrentContextTest.java

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,13 @@ class ConfigCurrentContextTest {
3838
ConfigCurrentContext subcommand;
3939

4040
@Test
41-
void shouldGetCurrentContext() {
41+
void shouldGetCurrentContextWithMaskedTokens() {
4242
when(configService.isCurrentContextValid())
4343
.thenReturn(true);
4444
when(kafkactlConfig.getCurrentNamespace())
4545
.thenReturn("namespace");
4646
when(kafkactlConfig.getApi())
4747
.thenReturn("ns4kafka.com");
48-
when(kafkactlConfig.getUserToken())
49-
.thenReturn("user-token");
5048
when(configService.getCurrentContextName())
5149
.thenReturn("current-context");
5250

@@ -57,10 +55,36 @@ void shouldGetCurrentContext() {
5755
int code = cmd.execute();
5856
assertEquals(0, code);
5957
verify(formatService).displayList(eq("Context"),
60-
argThat(currentContext -> currentContext.get(0).getMetadata().getName().equals("current-context")),
58+
argThat(currentContext -> currentContext.getFirst().getMetadata().getName().equals("current-context")
59+
&& currentContext.getFirst().getSpec().get("token").equals("[MASKED]")),
6160
eq(TABLE), eq(cmd.getCommandSpec()));
6261
}
6362

63+
@Test
64+
void shouldGetCurrentContextWithUnmaskedTokens() {
65+
when(configService.isCurrentContextValid())
66+
.thenReturn(true);
67+
when(kafkactlConfig.getCurrentNamespace())
68+
.thenReturn("namespace");
69+
when(kafkactlConfig.getApi())
70+
.thenReturn("ns4kafka.com");
71+
when(kafkactlConfig.getUserToken())
72+
.thenReturn("user-token");
73+
when(configService.getCurrentContextName())
74+
.thenReturn("current-context");
75+
76+
CommandLine cmd = new CommandLine(subcommand);
77+
StringWriter sw = new StringWriter();
78+
cmd.setOut(new PrintWriter(sw));
79+
80+
int code = cmd.execute("-u");
81+
assertEquals(0, code);
82+
verify(formatService).displayList(eq("Context"),
83+
argThat(currentContext -> currentContext.getFirst().getMetadata().getName().equals("current-context")
84+
&& currentContext.getFirst().getSpec().get("token").equals("user-token")),
85+
eq(TABLE), eq(cmd.getCommandSpec()));
86+
}
87+
6488
@Test
6589
void shouldNotGetCurrentContextWhenInvalid() {
6690
when(configService.isCurrentContextValid())

src/test/java/com/michelin/kafkactl/command/config/ConfigGetContextsTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ void shouldGetContextsWithMaskedTokens() {
6969
int code = cmd.execute();
7070
assertEquals(0, code);
7171
verify(formatService).displayList(eq("Context"),
72-
argThat(currentContext -> currentContext.get(0).getMetadata().getName().equals("name")
73-
&& currentContext.get(0).getSpec().get("token").equals("[MASKED]")),
72+
argThat(currentContext -> currentContext.getFirst().getMetadata().getName().equals("name")
73+
&& currentContext.getFirst().getSpec().get("token").equals("[MASKED]")),
7474
eq(TABLE), eq(cmd.getCommandSpec()));
7575
}
7676

@@ -95,8 +95,8 @@ void shouldGetContextsWithUnmaskedTokens() {
9595
int code = cmd.execute("-u");
9696
assertEquals(0, code);
9797
verify(formatService).displayList(eq("Context"),
98-
argThat(currentContext -> currentContext.get(0).getMetadata().getName().equals("name")
99-
&& currentContext.get(0).getSpec().get("token").equals("userToken")),
98+
argThat(currentContext -> currentContext.getFirst().getMetadata().getName().equals("name")
99+
&& currentContext.getFirst().getSpec().get("token").equals("userToken")),
100100
eq(TABLE), eq(cmd.getCommandSpec()));
101101
}
102102
}

src/test/java/com/michelin/kafkactl/service/ApiResourcesServiceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,6 @@ void shouldValidateResourceTypesInvalid() {
217217

218218
List<Resource> actual = apiResourcesService.filterNotAllowedResourceTypes(Collections.singletonList(resource));
219219

220-
assertEquals(resource, actual.get(0));
220+
assertEquals(resource, actual.getFirst());
221221
}
222222
}

src/test/java/com/michelin/kafkactl/service/FileServiceTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,24 @@ void shouldComputeYamlFileListRecursive() {
2727
@Test
2828
void shouldComputeYamlFileListNonRecursive() {
2929
List<File> actual = fileService.computeYamlFileList(new File("src/test/resources"), false);
30-
assertEquals("config.yml", actual.get(0).getName());
30+
assertEquals("config.yml", actual.getFirst().getName());
3131
assertEquals(1, actual.size());
3232
}
3333

3434
@Test
3535
void shouldComputeYamlFileListFile() {
3636
List<File> actual = fileService.computeYamlFileList(new File("src/test/resources/topics/topic.yml"), false);
37-
assertEquals("topic.yml", actual.get(0).getName());
37+
assertEquals("topic.yml", actual.getFirst().getName());
3838
assertEquals(1, actual.size());
3939
}
4040

4141
@Test
4242
void shouldParseResourceListFromFiles() {
4343
List<Resource> actual = fileService.parseResourceListFromFiles(
4444
Collections.singletonList(new File("src/test/resources/topics/topic.yml")));
45-
assertEquals("Topic", actual.get(0).getKind());
46-
assertEquals("myPrefix.topic", actual.get(0).getMetadata().getName());
47-
assertEquals(3, actual.get(0).getSpec().get("replicationFactor"));
45+
assertEquals("Topic", actual.getFirst().getKind());
46+
assertEquals("myPrefix.topic", actual.getFirst().getMetadata().getName());
47+
assertEquals(3, actual.getFirst().getSpec().get("replicationFactor"));
4848
assertEquals(1, actual.size());
4949
}
5050

@@ -55,8 +55,8 @@ void shouldParseResourceListFromString() {
5555
"{\"apiVersion\": \"v1\", \"kind\": \"Topic\", \"metadata\": {\"name\": \"myTopic\"}}");
5656

5757
assertEquals(1, actual.size());
58-
assertEquals("v1", actual.get(0).getApiVersion());
59-
assertEquals("Topic", actual.get(0).getKind());
60-
assertEquals("myTopic", actual.get(0).getMetadata().getName());
58+
assertEquals("v1", actual.getFirst().getApiVersion());
59+
assertEquals("Topic", actual.getFirst().getKind());
60+
assertEquals("myTopic", actual.getFirst().getMetadata().getName());
6161
}
6262
}

src/test/java/com/michelin/kafkactl/service/LoginServiceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ void shouldReadJwtFile() throws IOException {
309309

310310
assertIterableEquals(List.of("isAdmin()"), actual.getRoles());
311311

312-
assertEquals("anotherNamespace", actual.getRoleBindings().get(0).getNamespace());
312+
assertEquals("anotherNamespace", actual.getRoleBindings().getFirst().getNamespace());
313313
assertIterableEquals(List.of(GET), actual.getRoleBindings().get(0).getVerbs());
314314
assertIterableEquals(List.of("quota"), actual.getRoleBindings().get(0).getResourceTypes());
315315

src/test/java/com/michelin/kafkactl/service/ResourceServiceTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,10 +1195,10 @@ void shouldParse() {
11951195
cmd.getCommandSpec());
11961196

11971197
assertEquals(1, actual.size());
1198-
assertEquals("Topic", actual.get(0).getKind());
1199-
assertEquals("myPrefix.topic", actual.get(0).getMetadata().getName());
1200-
assertEquals(3, actual.get(0).getSpec().get("replicationFactor"));
1201-
assertEquals(3, actual.get(0).getSpec().get("partitions"));
1198+
assertEquals("Topic", actual.getFirst().getKind());
1199+
assertEquals("myPrefix.topic", actual.getFirst().getMetadata().getName());
1200+
assertEquals(3, actual.getFirst().getSpec().get("replicationFactor"));
1201+
assertEquals(3, actual.getFirst().getSpec().get("partitions"));
12021202
}
12031203

12041204
@Test

0 commit comments

Comments
 (0)