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: Config client authentication exec command should not fail when args is null #4381

Merged
merged 2 commits into from
Sep 22, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#### Bugs
* Fix #4369: Informers will retry with a backoff on list/watch failure as they did in 5.12 and prior.
* Fix #4350: SchemaSwap annotation is now repeatable and is applied multiple times if classes are used more than once in the class hierarchy.
* Fix #3733: The authentication command from the .kube/config won't be discarded if no arguments are specified

#### Improvements
* Fix #4348: Introduce specific annotations for the generators
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -763,9 +763,10 @@ protected static List<String> getAuthenticatorCommandFromExecConfig(ExecConfig e
List<String> argv = new ArrayList<>(Utils.getCommandPlatformPrefix());
command = getCommandWithFullyQualifiedPath(command, systemPathValue);
List<String> args = exec.getArgs();
if (args != null) {
argv.add(command + " " + String.join(" ", args));
if (args != null && !args.isEmpty()) {
command += " " + String.join(" ", args);
}
argv.add(command);
return argv;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public class ConfigTest {
private static final String TEST_KUBECONFIG_EXEC_WIN_FILE = Utils
.filePath(ConfigTest.class.getResource("/test-kubeconfig-exec-win"));

private static final String TEST_KUBECONFIG_EXEC_FILE_NULL_ARGS = Utils
.filePath(ConfigTest.class.getResource("/test-kubeconfig-exec-null-args"));
private static final String TEST_KUBECONFIG_EXEC_FILE_WIN_NULL_ARGS = Utils
.filePath(ConfigTest.class.getResource("/test-kubeconfig-exec-win-null-args"));

private static final String TEST_KUBECONFIG_NO_CURRENT_CONTEXT_FILE = Utils
.filePath(ConfigTest.class.getResource("/test-kubeconfig-nocurrentctxt.yml"));

Expand Down Expand Up @@ -446,6 +451,24 @@ void honorClientAuthenticatorCommands() throws Exception {
assertEquals("HELLO WORLD", config.getOauthToken());
}

@Test
void should_accept_client_authentication_commands_with_null_args() throws Exception {
try {
if (FileSystem.getCurrent() == FileSystem.WINDOWS) {
System.setProperty(Config.KUBERNETES_KUBECONFIG_FILE, TEST_KUBECONFIG_EXEC_FILE_WIN_NULL_ARGS);
} else {
Files.setPosixFilePermissions(Paths.get(TEST_TOKEN_GENERATOR_FILE), PosixFilePermissions.fromString("rwxrwxr-x"));
System.setProperty(Config.KUBERNETES_KUBECONFIG_FILE, TEST_KUBECONFIG_EXEC_FILE_NULL_ARGS);
}

Config config = Config.autoConfigure(null);
assertNotNull(config);
assertEquals("HELLO", config.getOauthToken());
} finally {
System.clearProperty(Config.KUBERNETES_KUBECONFIG_FILE);
}
}

@Test
void shouldBeUsedTokenSuppliedByProvider() {

Expand Down Expand Up @@ -582,6 +605,30 @@ void testGetAuthenticatorCommandFromExecConfig() throws IOException {
assertEquals("api-eks.example.com", commandParts.get(6));
}

@Test
void testGetAuthenticatorCommandFromExecConfigNullArgs() throws IOException {
// Given
File commandFolder = Files.createTempDirectory("test").toFile();
File commandFile = new File(commandFolder, "gke-gcloud-auth-plugin");
String systemPathValue = getTestPathValue(commandFolder);
ExecConfig execConfigNoArgs = new ExecConfigBuilder()
.withApiVersion("client.authentication.k8s.io/v1alpha1")
.withCommand(commandFile.getPath())
.build();
// Simulate "user.exec.args: null" like e.g. in the configuration for the gke-gcloud-auth-plugin.
execConfigNoArgs.setArgs(null);

// When
List<String> processBuilderArgs = Config.getAuthenticatorCommandFromExecConfig(
execConfigNoArgs, null, systemPathValue);

// Then
assertNotNull(processBuilderArgs);
assertEquals(3, processBuilderArgs.size());
assertPlatformPrefixes(processBuilderArgs);
assertEquals(commandFile.getPath(), processBuilderArgs.get(2));
}

private void assertPlatformPrefixes(List<String> processBuilderArgs) {
List<String> platformArgsExpected = Utils.getCommandPlatformPrefix();
assertEquals(platformArgsExpected.get(0), processBuilderArgs.get(0));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
apiVersion: v1
kind: Config
clusters:
- cluster:
server: https://wherever
name: test
contexts:
- context:
cluster: test
user: test
name: test
current-context: test
users:
- name: test
user:
exec:
apiVersion: client.authentication.k8s.io/v1alpha1
args: null
command: ./token-generator
env:
- name: PART1
value: hello
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
apiVersion: v1
kind: Config
clusters:
- cluster:
server: https://wherever
name: test
contexts:
- context:
cluster: test
user: test
name: test
current-context: test
users:
- name: test
user:
exec:
apiVersion: client.authentication.k8s.io/v1alpha1
args: null
command: ".\\token-generator-win.bat"
env:
- name: PART1
value: hello
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@
@REM

@echo off
SET token=%PART1% %1
IF [%1]==[] (
SET token=%PART1%
) ELSE (
SET token=%PART1% %1
)

CALL :upper token

echo {
Expand All @@ -26,7 +31,7 @@ echo "status": {
echo "token": "%token%"
echo }
echo }
GOTO :EOF

:upper
FOR %%a IN (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) DO CALL SET "%1=%%%1:%%a=%%a%%%"
GOTO :EOF