Skip to content

Commit

Permalink
fix: Config client authentication exec command should not fail when a…
Browse files Browse the repository at this point in the history
…rgs is null

Signed-off-by: Sun Seng David TAN <[email protected]>

(cherry picked from commit ec0aba8)
Signed-off-by: Marc Nuri <[email protected]>
  • Loading branch information
sunix authored and manusa committed Sep 28, 2022
1 parent fdbe6f3 commit 2900229
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -696,8 +696,9 @@ protected static List<String> getAuthenticatorCommandFromExecConfig(ExecConfig e
command = getCommandWithFullyQualifiedPath(command, systemPathValue);
List<String> args = exec.getArgs();
if (args != null && !args.isEmpty()) {
argv.add(command + " " + String.join(" ", args));
command += " " + String.join(" ", args);
}
argv.add(command);
return argv;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,13 @@ 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"));

@BeforeEach
public void setUp() {
System.getProperties().remove(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY);
Expand Down Expand Up @@ -495,6 +500,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
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
9 changes: 7 additions & 2 deletions kubernetes-client/src/test/resources/token-generator-win.bat
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

0 comments on commit 2900229

Please sign in to comment.