Skip to content

Commit

Permalink
fix(config): Fix kubeconfig loading when exec auth command contains s…
Browse files Browse the repository at this point in the history
…paces

Signed-off-by: Sun Seng David TAN <[email protected]>
  • Loading branch information
sunix authored and manusa committed Mar 10, 2023
1 parent 11daf25 commit 445f602
Show file tree
Hide file tree
Showing 9 changed files with 208 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -813,14 +813,27 @@ protected static List<String> getAuthenticatorCommandFromExecConfig(ExecConfig e
}
List<String> argv = new ArrayList<>(Utils.getCommandPlatformPrefix());
command = getCommandWithFullyQualifiedPath(command, systemPathValue);

command = shellQuote(command);

List<String> args = exec.getArgs();
if (args != null && !args.isEmpty()) {
command += " " + String.join(" ", args);
command += " " + args
.stream()
.map(Config::shellQuote)
.collect(Collectors.joining(" "));
}
argv.add(command);
return argv;
}

private static String shellQuote(String value) {
if (value.contains(" ") || value.contains("\"") || value.contains("'")) {
return "\"" + value.replace("\"", "\\\"") + "\"";
}
return value;
}

protected static String getCommandWithFullyQualifiedPath(String command, String pathValue) {
String[] pathParts = pathValue.split(File.pathSeparator);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public class ConfigTest {

private static final String TEST_KUBECONFIG_EXEC_FILE = Utils.filePath(ConfigTest.class.getResource("/test-kubeconfig-exec"));
private static final String TEST_TOKEN_GENERATOR_FILE = Utils.filePath(ConfigTest.class.getResource("/token-generator"));
private static final String TEST_TOKEN_GENERATOR_FILE_WITH_SPACES = Utils
.filePath(ConfigTest.class.getResource("/token-generator with spaces"));

private static final String TEST_KUBECONFIG_EXEC_WIN_FILE = Utils
.filePath(ConfigTest.class.getResource("/test-kubeconfig-exec-win"));
Expand All @@ -66,6 +68,18 @@ public class ConfigTest {
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_EXEC_FILE_WITH_SPACES_WIN = Utils
.filePath(ConfigTest.class.getResource("/test-kubeconfig-exec-with-spaces-windows"));

private static final String TEST_KUBECONFIG_EXEC_FILE_WITH_SPACES = Utils
.filePath(ConfigTest.class.getResource("/test-kubeconfig-exec-with-spaces"));

private static final String TEST_KUBECONFIG_EXEC_FILE_ARGS_WITH_SPACES_WIN = Utils
.filePath(ConfigTest.class.getResource("/test-kubeconfig-exec-args-with-spaces-windows"));

private static final String TEST_KUBECONFIG_EXEC_FILE_ARGS_WITH_SPACES = Utils
.filePath(ConfigTest.class.getResource("/test-kubeconfig-exec-args-with-spaces"));

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

Expand Down Expand Up @@ -489,6 +503,44 @@ void should_accept_client_authentication_commands_with_null_args() throws Except
}
}

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

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

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

Config config = Config.autoConfigure(null);
assertNotNull(config);
assertEquals("HELLO WORLD", 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,23 @@
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:
- "w o r l d"
command: "./token-generator"
env:
- name: PART1
value: hello
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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:
- "w o r l d"
command: ".\\token-generator-win.bat"
env:
- name: PART1
value: hello
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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:
- world
command: "./token-generator with spaces"
env:
- name: PART1
value: hello
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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:
- world
command: ".\\token-generator-win with spaces.bat"
env:
- name: PART1
value: hello
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/sh
token=`echo $PART1 $1 | tr '[a-z]' '[A-Z]'`
cat <<EOF
{
"kind": "ExecCredential",
"apiVersion": "client.authentication.k8s.io/v1alpha1",
"spec": {},
"status": {
"token": "$token"
}
}
EOF
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
@REM
@REM Copyright (C) 2015 Red Hat, Inc.
@REM
@REM Licensed under the Apache License, Version 2.0 (the "License");
@REM you may not use this file except in compliance with the License.
@REM You may obtain a copy of the License at
@REM
@REM http://www.apache.org/licenses/LICENSE-2.0
@REM
@REM Unless required by applicable law or agreed to in writing, software
@REM distributed under the License is distributed on an "AS IS" BASIS,
@REM WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@REM See the License for the specific language governing permissions and
@REM limitations under the License.
@REM

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

CALL :upper token

echo {
echo "kind": "ExecCredential",
echo "apiVersion": "client.authentication.k8s.io/v1alpha1",
echo "spec": {},
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%%%"
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
IF [%1]==[] (
SET token=%PART1%
) ELSE (
SET token=%PART1% %1
SET token=%PART1% %~1
)

CALL :upper token
Expand Down

0 comments on commit 445f602

Please sign in to comment.