Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix mask token on current context display #131

Merged
merged 1 commit into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/main/java/com/michelin/kafkactl/command/Get.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ public Integer onAuthSuccess() throws IOException {
try {
// Get individual resources for given types (k get topic topic1)
Resource singleResource =
resourceService.getSingleResourceWithType(apiResources.get(0), namespace, resourceName.get(), true);
resourceService.getSingleResourceWithType(apiResources.getFirst(), namespace, resourceName.get(), true);
formatService.displaySingle(singleResource, output, commandSpec);
return 0;
} catch (HttpClientResponseException e) {
formatService.displayError(e, apiResources.get(0).getKind(), resourceName.get(), commandSpec);
formatService.displayError(e, apiResources.getFirst().getKind(), resourceName.get(), commandSpec);
return 1;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.michelin.kafkactl.command.config;

import static com.michelin.kafkactl.mixin.UnmaskTokenMixin.MASKED;
import static com.michelin.kafkactl.service.FormatService.TABLE;
import static com.michelin.kafkactl.util.constant.ResourceKind.CONTEXT;

import com.michelin.kafkactl.config.KafkactlConfig;
import com.michelin.kafkactl.hook.ValidCurrentContextHook;
import com.michelin.kafkactl.mixin.UnmaskTokenMixin;
import com.michelin.kafkactl.model.Metadata;
import com.michelin.kafkactl.model.Resource;
import com.michelin.kafkactl.service.FormatService;
Expand All @@ -16,6 +18,7 @@
import java.util.List;
import java.util.Map;
import picocli.CommandLine.Command;
import picocli.CommandLine.Mixin;

/**
* Config current context subcommand.
Expand All @@ -40,12 +43,20 @@ public class ConfigCurrentContext extends ValidCurrentContextHook {
@ReflectiveAccess
private FormatService formatService;

@Mixin
public UnmaskTokenMixin unmaskTokenMixin;

@Override
public Integer onContextValid() {
Map<String, Object> specs = new HashMap<>();
specs.put("namespace", kafkactlConfig.getCurrentNamespace());
specs.put("api", kafkactlConfig.getApi());
specs.put("token", kafkactlConfig.getUserToken());

if (unmaskTokenMixin.unmaskTokens) {
specs.put("token", kafkactlConfig.getUserToken());
} else {
specs.put("token", MASKED);
}

String currentContextName = configService.getCurrentContextName();
Resource currentContextAsResource = Resource.builder()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.michelin.kafkactl.command.config;

import static com.michelin.kafkactl.mixin.UnmaskTokenMixin.MASKED;
import static com.michelin.kafkactl.service.FormatService.TABLE;
import static com.michelin.kafkactl.util.constant.ResourceKind.CONTEXT;

import com.michelin.kafkactl.config.KafkactlConfig;
import com.michelin.kafkactl.mixin.UnmaskTokenMixin;
import com.michelin.kafkactl.model.Metadata;
import com.michelin.kafkactl.model.Resource;
import com.michelin.kafkactl.service.FormatService;
Expand All @@ -15,8 +17,8 @@
import java.util.Map;
import java.util.concurrent.Callable;
import picocli.CommandLine.Command;
import picocli.CommandLine.Mixin;
import picocli.CommandLine.Model.CommandSpec;
import picocli.CommandLine.Option;
import picocli.CommandLine.Spec;

/**
Expand All @@ -34,8 +36,6 @@
versionProvider = VersionProvider.class,
mixinStandardHelpOptions = true)
public class ConfigGetContexts implements Callable<Integer> {
private static final String MASKED = "[MASKED]";

@Inject
@ReflectiveAccess
private KafkactlConfig kafkactlConfig;
Expand All @@ -47,8 +47,8 @@ public class ConfigGetContexts implements Callable<Integer> {
@Spec
public CommandSpec commandSpec;

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

@Override
public Integer call() {
Expand All @@ -62,7 +62,7 @@ public Integer call() {
specs.put("namespace", userContext.getDefinition().getNamespace());
specs.put("api", userContext.getDefinition().getApi());

if (unmaskTokens) {
if (unmaskTokenMixin.unmaskTokens) {
specs.put("token", userContext.getDefinition().getUserToken());
} else {
specs.put("token", MASKED);
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/michelin/kafkactl/mixin/UnmaskTokenMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.michelin.kafkactl.mixin;

import picocli.CommandLine;

/**
* Unmask token mixin.
*/
public class UnmaskTokenMixin {
public static final String MASKED = "[MASKED]";

@CommandLine.Option(names = {"-u", "--unmask-tokens"}, description = "Unmask tokens.")
public boolean unmaskTokens;
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ public int listAll(List<ApiResource> apiResources, String namespace, String outp
// Get a single kind of resources
if (apiResources.size() == 1) {
try {
List<Resource> resources = listResourcesWithType(apiResources.get(0), namespace);
List<Resource> resources = listResourcesWithType(apiResources.getFirst(), namespace);
if (!resources.isEmpty()) {
formatService.displayList(resources.get(0).getKind(), resources, output, commandSpec);
formatService.displayList(resources.getFirst().getKind(), resources, output, commandSpec);
} else {
commandSpec.commandLine().getOut()
.println("No " + formatService.prettifyKind(apiResources.get(0).getKind()).toLowerCase()
.println("No " + formatService.prettifyKind(apiResources.getFirst().getKind()).toLowerCase()
+ " to display.");
}
return 0;
Expand All @@ -95,7 +95,7 @@ public int listAll(List<ApiResource> apiResources, String namespace, String outp
try {
List<Resource> resources = listResourcesWithType(apiResource, namespace);
if (!resources.isEmpty()) {
formatService.displayList(resources.get(0).getKind(), resources, output, commandSpec);
formatService.displayList(resources.getFirst().getKind(), resources, output, commandSpec);
}
return 0;
} catch (HttpClientResponseException exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ void shouldDisplayApiResources() {
int code = cmd.execute();
assertEquals(0, code);
verify(formatService).displayList(eq(RESOURCE_DEFINITION),
argThat(resources -> resources.get(0).getMetadata().getName().equals("Topic")),
argThat(resources -> resources.getFirst().getMetadata().getName().equals("Topic")),
eq(TABLE), eq(cmd.getCommandSpec()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ void shouldChangeState() {
int code = cmd.execute("pause", "my-connector", "-n", "namespace");
assertEquals(0, code);
verify(formatService).displayList(eq(CHANGE_CONNECTOR_STATE),
argThat(connectors -> connectors.get(0).equals(resource)),
argThat(connectors -> connectors.getFirst().equals(resource)),
eq(TABLE), eq(cmd.getCommandSpec()));
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ void shouldUpdateCompat() {
int code = cmd.execute("backward", "mySubject", "-n", "namespace");
assertEquals(0, code);
verify(formatService).displayList(eq(SCHEMA_COMPATIBILITY_STATE),
argThat(schemas -> schemas.get(0).equals(resource)),
argThat(schemas -> schemas.getFirst().equals(resource)),
eq(TABLE), eq(cmd.getCommandSpec()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,13 @@ class ConfigCurrentContextTest {
ConfigCurrentContext subcommand;

@Test
void shouldGetCurrentContext() {
void shouldGetCurrentContextWithMaskedTokens() {
when(configService.isCurrentContextValid())
.thenReturn(true);
when(kafkactlConfig.getCurrentNamespace())
.thenReturn("namespace");
when(kafkactlConfig.getApi())
.thenReturn("ns4kafka.com");
when(kafkactlConfig.getUserToken())
.thenReturn("user-token");
when(configService.getCurrentContextName())
.thenReturn("current-context");

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

@Test
void shouldGetCurrentContextWithUnmaskedTokens() {
when(configService.isCurrentContextValid())
.thenReturn(true);
when(kafkactlConfig.getCurrentNamespace())
.thenReturn("namespace");
when(kafkactlConfig.getApi())
.thenReturn("ns4kafka.com");
when(kafkactlConfig.getUserToken())
.thenReturn("user-token");
when(configService.getCurrentContextName())
.thenReturn("current-context");

CommandLine cmd = new CommandLine(subcommand);
StringWriter sw = new StringWriter();
cmd.setOut(new PrintWriter(sw));

int code = cmd.execute("-u");
assertEquals(0, code);
verify(formatService).displayList(eq("Context"),
argThat(currentContext -> currentContext.getFirst().getMetadata().getName().equals("current-context")
&& currentContext.getFirst().getSpec().get("token").equals("user-token")),
eq(TABLE), eq(cmd.getCommandSpec()));
}

@Test
void shouldNotGetCurrentContextWhenInvalid() {
when(configService.isCurrentContextValid())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ void shouldGetContextsWithMaskedTokens() {
int code = cmd.execute();
assertEquals(0, code);
verify(formatService).displayList(eq("Context"),
argThat(currentContext -> currentContext.get(0).getMetadata().getName().equals("name")
&& currentContext.get(0).getSpec().get("token").equals("[MASKED]")),
argThat(currentContext -> currentContext.getFirst().getMetadata().getName().equals("name")
&& currentContext.getFirst().getSpec().get("token").equals("[MASKED]")),
eq(TABLE), eq(cmd.getCommandSpec()));
}

Expand All @@ -95,8 +95,8 @@ void shouldGetContextsWithUnmaskedTokens() {
int code = cmd.execute("-u");
assertEquals(0, code);
verify(formatService).displayList(eq("Context"),
argThat(currentContext -> currentContext.get(0).getMetadata().getName().equals("name")
&& currentContext.get(0).getSpec().get("token").equals("userToken")),
argThat(currentContext -> currentContext.getFirst().getMetadata().getName().equals("name")
&& currentContext.getFirst().getSpec().get("token").equals("userToken")),
eq(TABLE), eq(cmd.getCommandSpec()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,6 @@ void shouldValidateResourceTypesInvalid() {

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

assertEquals(resource, actual.get(0));
assertEquals(resource, actual.getFirst());
}
}
16 changes: 8 additions & 8 deletions src/test/java/com/michelin/kafkactl/service/FileServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,24 @@ void shouldComputeYamlFileListRecursive() {
@Test
void shouldComputeYamlFileListNonRecursive() {
List<File> actual = fileService.computeYamlFileList(new File("src/test/resources"), false);
assertEquals("config.yml", actual.get(0).getName());
assertEquals("config.yml", actual.getFirst().getName());
assertEquals(1, actual.size());
}

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

@Test
void shouldParseResourceListFromFiles() {
List<Resource> actual = fileService.parseResourceListFromFiles(
Collections.singletonList(new File("src/test/resources/topics/topic.yml")));
assertEquals("Topic", actual.get(0).getKind());
assertEquals("myPrefix.topic", actual.get(0).getMetadata().getName());
assertEquals(3, actual.get(0).getSpec().get("replicationFactor"));
assertEquals("Topic", actual.getFirst().getKind());
assertEquals("myPrefix.topic", actual.getFirst().getMetadata().getName());
assertEquals(3, actual.getFirst().getSpec().get("replicationFactor"));
assertEquals(1, actual.size());
}

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

assertEquals(1, actual.size());
assertEquals("v1", actual.get(0).getApiVersion());
assertEquals("Topic", actual.get(0).getKind());
assertEquals("myTopic", actual.get(0).getMetadata().getName());
assertEquals("v1", actual.getFirst().getApiVersion());
assertEquals("Topic", actual.getFirst().getKind());
assertEquals("myTopic", actual.getFirst().getMetadata().getName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ void shouldReadJwtFile() throws IOException {

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

assertEquals("anotherNamespace", actual.getRoleBindings().get(0).getNamespace());
assertEquals("anotherNamespace", actual.getRoleBindings().getFirst().getNamespace());
assertIterableEquals(List.of(GET), actual.getRoleBindings().get(0).getVerbs());
assertIterableEquals(List.of("quota"), actual.getRoleBindings().get(0).getResourceTypes());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1195,10 +1195,10 @@ void shouldParse() {
cmd.getCommandSpec());

assertEquals(1, actual.size());
assertEquals("Topic", actual.get(0).getKind());
assertEquals("myPrefix.topic", actual.get(0).getMetadata().getName());
assertEquals(3, actual.get(0).getSpec().get("replicationFactor"));
assertEquals(3, actual.get(0).getSpec().get("partitions"));
assertEquals("Topic", actual.getFirst().getKind());
assertEquals("myPrefix.topic", actual.getFirst().getMetadata().getName());
assertEquals(3, actual.getFirst().getSpec().get("replicationFactor"));
assertEquals(3, actual.getFirst().getSpec().get("partitions"));
}

@Test
Expand Down
Loading