Skip to content

Commit 2d22dce

Browse files
committed
Honoring basedir for relative command paths.
1 parent a4163e2 commit 2d22dce

File tree

4 files changed

+73
-6
lines changed

4 files changed

+73
-6
lines changed

util/src/main/java/io/kubernetes/client/util/ClientBuilder.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,21 +78,23 @@ public static ClientBuilder standard() throws IOException {
7878
public static ClientBuilder standard(boolean persistConfig) throws IOException {
7979
final File kubeConfig = findConfigFromEnv();
8080
if (kubeConfig != null) {
81-
try (FileReader kubeConfigReader = new FileReader(kubeConfig)) {
81+
try (FileReader kubeConfigReader = new FileReader(kubeConfig)) { // TODO UTF-8
8282
KubeConfig kc = KubeConfig.loadKubeConfig(kubeConfigReader);
8383
if (persistConfig) {
8484
kc.setPersistConfig(new FilePersister(kubeConfig));
8585
}
86+
kc.setFile(kubeConfig);
8687
return kubeconfig(kc);
8788
}
8889
}
8990
final File config = findConfigInHomeDir();
9091
if (config != null) {
91-
try (FileReader configReader = new FileReader(config)) {
92+
try (FileReader configReader = new FileReader(config)) { // TODO UTF-8
9293
KubeConfig kc = KubeConfig.loadKubeConfig(configReader);
9394
if (persistConfig) {
9495
kc.setPersistConfig(new FilePersister(config));
9596
}
97+
kc.setFile(kubeConfig);
9698
return kubeconfig(kc);
9799
}
98100
}

util/src/main/java/io/kubernetes/client/util/Config.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import io.kubernetes.client.ApiClient;
1616
import io.kubernetes.client.util.credentials.AccessTokenAuthentication;
1717
import io.kubernetes.client.util.credentials.UsernamePasswordAuthentication;
18+
import java.io.File;
1819
import java.io.FileReader;
1920
import java.io.IOException;
2021
import java.io.InputStream;
@@ -73,11 +74,13 @@ public static ApiClient fromToken(String url, String token, boolean validateSSL)
7374
}
7475

7576
public static ApiClient fromConfig(String fileName) throws IOException {
76-
return fromConfig(new FileReader(fileName));
77+
KubeConfig config = KubeConfig.loadKubeConfig(new FileReader(fileName)); // TODO UTF-8
78+
config.setFile(new File(fileName));
79+
return fromConfig(config);
7780
}
7881

7982
public static ApiClient fromConfig(InputStream stream) throws IOException {
80-
return fromConfig(new InputStreamReader(stream));
83+
return fromConfig(new InputStreamReader(stream)); // TODO UTF-8
8184
}
8285

8386
public static ApiClient fromConfig(Reader input) throws IOException {

util/src/main/java/io/kubernetes/client/util/KubeConfig.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import io.kubernetes.client.util.authenticators.Authenticator;
2020
import io.kubernetes.client.util.authenticators.AzureActiveDirectoryAuthenticator;
2121
import io.kubernetes.client.util.authenticators.GCPAuthenticator;
22+
import java.io.File;
2223
import java.io.IOException;
2324
import java.io.InputStream;
2425
import java.io.InputStreamReader;
@@ -61,6 +62,7 @@ public class KubeConfig {
6162
String currentNamespace;
6263
Object preferences;
6364
ConfigPersister persister;
65+
private File file;
6466

6567
public static void registerAuthenticator(Authenticator auth) {
6668
synchronized (authenticators) {
@@ -283,9 +285,18 @@ private String tokenViaExecCredential(Map<String, Object> execMap) {
283285
}
284286

285287
private JsonElement runExec(String command, List<String> args, List<Map<String, String>> env) {
286-
// TODO relativize command to basedir of config file (requires KubeConfig to be given a basedir)
287288
List<String> argv = new ArrayList<>();
288-
argv.add(command);
289+
if (command.startsWith("./")) {
290+
// TODO spec is unclear on what should be treated as a “relative command path”
291+
File resolvedCommand = new File(file.getParentFile(), command);
292+
if (!resolvedCommand.isFile()) {
293+
log.error("No such file: {}", resolvedCommand);
294+
return null;
295+
}
296+
argv.add(resolvedCommand.getAbsolutePath());
297+
} else {
298+
argv.add(command);
299+
}
289300
if (args != null) {
290301
argv.addAll(args);
291302
}
@@ -337,6 +348,15 @@ public void setPersistConfig(ConfigPersister persister) {
337348
this.persister = persister;
338349
}
339350

351+
/**
352+
* Indicates a file from which this configuration was loaded.
353+
*
354+
* @param file a file path, available for use when resolving relative file paths
355+
*/
356+
public void setFile(File file) {
357+
this.file = file;
358+
}
359+
340360
public void setPreferences(Object preferences) {
341361
this.preferences = preferences;
342362
}

util/src/test/java/io/kubernetes/client/util/KubeConfigTest.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ public void testRefreshToken() {
294294
@Test
295295
public void testExecCredentials() throws Exception {
296296
KubeConfig kc = KubeConfig.loadKubeConfig(new StringReader(KUBECONFIG_EXEC));
297+
kc.setFile(folder.newFile()); // just making sure it is ignored
297298
assertEquals("abc123", kc.getAccessToken());
298299
}
299300

@@ -323,4 +324,45 @@ public void testExecCredentialsEnv() throws Exception {
323324
KubeConfig kc = KubeConfig.loadKubeConfig(new StringReader(KUBECONFIG_EXEC_ENV));
324325
assertEquals("abc123", kc.getAccessToken());
325326
}
327+
328+
private static final String KUBECONFIG_EXEC_BASEDIR =
329+
"apiVersion: v1\n"
330+
+ "current-context: c\n"
331+
+ "contexts:\n"
332+
+ "- name: c\n"
333+
+ " context:\n"
334+
+ " user: u\n"
335+
+ "users:\n"
336+
+ "- name: u\n"
337+
+ " user:\n"
338+
+ " exec:\n"
339+
+ " apiVersion: client.authentication.k8s.io/v1beta1\n"
340+
+ " command: ./bin/authenticate\n";
341+
342+
private static final String AUTH_SCRIPT =
343+
"#!/bin/sh\n"
344+
+ "echo '{\"apiVersion\": \"client.authentication.k8s.io/v1beta1\", \"kind\": \"ExecCredential\", \"status\": {\"token\": \"abc123\"}}'\n";
345+
346+
@Test
347+
public void testExecCredentialsBasedir() throws Exception {
348+
File basedir = folder.newFolder();
349+
File config = new File(basedir, ".kubeconfig");
350+
try (FileWriter writer = new FileWriter(config)) {
351+
writer.write(KUBECONFIG_EXEC_BASEDIR);
352+
writer.flush();
353+
}
354+
File bindir = new File(basedir, "bin");
355+
bindir.mkdir();
356+
File script = new File(bindir, "authenticate");
357+
try (FileWriter writer = new FileWriter(script)) {
358+
writer.write(AUTH_SCRIPT);
359+
writer.flush();
360+
}
361+
script.setExecutable(true);
362+
try (FileReader reader = new FileReader(config)) {
363+
KubeConfig kc = KubeConfig.loadKubeConfig(reader);
364+
kc.setFile(config);
365+
assertEquals("abc123", kc.getAccessToken());
366+
}
367+
}
326368
}

0 commit comments

Comments
 (0)