Skip to content

Commit

Permalink
Remove unsecure username and password conn configuration
Browse files Browse the repository at this point in the history
Signed-off-by: David Kornel <[email protected]>
  • Loading branch information
kornys committed Mar 26, 2024
1 parent 5ea7f47 commit 8053253
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 69 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,5 @@ Examples are stored in [test-frame-test](test-frame-test/src/test/java/io/skodjo

## Config environment variables
* **ENV_FILE** - path to YAML file with env variables values
* **KUBE_USERNAME** - user of the cluster
* **KUBE_PASSWORD** - password of Kube user
* **KUBE_TOKEN** - token of Kube access (use instead of username/password)
* **KUBE_URL** - URL of the cluster (API URL)
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ public class TestFrameEnv {

private static final String CONFIG_FILE_PATH_ENV = "ENV_FILE";
private static final String CLIENT_TYPE_ENV = "CLIENT_TYPE";
private static final String USERNAME_ENV = "KUBE_USERNAME";
private static final String PASSWORD_ENV = "KUBE_PASSWORD";
private static final String TOKEN_ENV = "KUBE_TOKEN";
private static final String URL_ENV = "KUBE_URL";

Expand All @@ -43,15 +41,6 @@ public class TestFrameEnv {
*/
public static final String CLIENT_TYPE = getOrDefault(CLIENT_TYPE_ENV, TestFrameConstants.KUBERNETES_CLIENT);

/**
* The username for accessing the Kubernetes cluster.
*/
public static final String KUBE_USERNAME = getOrDefault(USERNAME_ENV, null);

/**
* The password for accessing the Kubernetes cluster.
*/
public static final String KUBE_PASSWORD = getOrDefault(PASSWORD_ENV, null);

/**
* The token for accessing the Kubernetes cluster.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,7 @@ public List<HasMetadata> readResourcesFromFile(InputStream is) throws IOExceptio
* @return Config The Kubernetes client configuration.
*/
private Config getConfig() {
if (TestFrameEnv.KUBE_USERNAME != null && TestFrameEnv.KUBE_PASSWORD != null && TestFrameEnv.KUBE_URL != null) {
kubeconfigPath = createLocalKubeconfig();
return new ConfigBuilder()
.withUsername(TestFrameEnv.KUBE_USERNAME)
.withPassword(TestFrameEnv.KUBE_PASSWORD)
.withMasterUrl(TestFrameEnv.KUBE_URL)
.withDisableHostnameVerification(true)
.withTrustCerts(true)
.build();
} else if (TestFrameEnv.KUBE_URL != null && TestFrameEnv.KUBE_TOKEN != null) {
if (TestFrameEnv.KUBE_URL != null && TestFrameEnv.KUBE_TOKEN != null) {
kubeconfigPath = createLocalKubeconfig();
return new ConfigBuilder()
.withOauthToken(TestFrameEnv.KUBE_TOKEN)
Expand All @@ -134,19 +125,9 @@ private Config getConfig() {
private String createLocalKubeconfig() {
try {
if (TestFrameEnv.CLIENT_TYPE.equals(TestFrameConstants.OPENSHIFT_CLIENT)) {
if (TestFrameEnv.KUBE_URL != null && TestFrameEnv.KUBE_TOKEN != null) {
createLocalOcKubeconfig(TestFrameEnv.KUBE_TOKEN, TestFrameEnv.KUBE_URL);
} else {
createLocalOcKubeconfig(
TestFrameEnv.KUBE_USERNAME, TestFrameEnv.KUBE_PASSWORD, TestFrameEnv.KUBE_URL);
}
createLocalOcKubeconfig(TestFrameEnv.KUBE_TOKEN, TestFrameEnv.KUBE_URL);
} else {
if (TestFrameEnv.KUBE_URL != null && TestFrameEnv.KUBE_TOKEN != null) {
createLocalKubectlContext(TestFrameEnv.KUBE_TOKEN, TestFrameEnv.KUBE_URL);
} else {
createLocalKubectlContext(
TestFrameEnv.KUBE_USERNAME, TestFrameEnv.KUBE_PASSWORD, TestFrameEnv.KUBE_URL);
}
createLocalKubectlContext(TestFrameEnv.KUBE_TOKEN, TestFrameEnv.KUBE_URL);
}
return TestFrameEnv.USER_PATH + "/test.kubeconfig";
} catch (Exception ex) {
Expand All @@ -155,22 +136,6 @@ private String createLocalKubeconfig() {
}
}

/**
* Configures a local kubeconfig for OpenShift using oc login with username and password.
*
* @param username The username for OpenShift login.
* @param password The password for OpenShift login.
* @param apiUrl The URL of the OpenShift cluster API.
*/
private void createLocalOcKubeconfig(String username, String password, String apiUrl) {
Exec.exec(null, Arrays.asList("oc", "login",
"-u", username,
"-p", password,
"--insecure-skip-tls-verify",
"--kubeconfig", TestFrameEnv.USER_PATH + "/test.kubeconfig",
apiUrl), 0, false, true);
}

/**
* Configures a local kubeconfig for OpenShift using oc login with a token.
*
Expand All @@ -184,23 +149,6 @@ private void createLocalOcKubeconfig(String token, String apiUrl) {
apiUrl), 0, false, true);
}

/**
* Configures a local kubeconfig for Kubernetes using kubectl to set credentials with username and password.
*
* @param username The username for Kubernetes cluster.
* @param password The password for Kubernetes cluster.
* @param apiUrl The URL of the Kubernetes cluster API.
*/
private void createLocalKubectlContext(String username, String password, String apiUrl) {
Exec.exec(null, Arrays.asList("kubectl", "config",
"set-credentials", "test-user",
"--username", username,
"--password", password,
"--kubeconfig", TestFrameEnv.USER_PATH + "/test.kubeconfig"),
0, false, true);
buildKubectlContext(apiUrl);
}

/**
* Configures a local kubeconfig for Kubernetes using kubectl to set credentials with a token.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static synchronized KubeResourceManager getInstance() {
instance.resourceTypes = new ResourceType[]{};
client = new KubeClient();
if (TestFrameEnv.CLIENT_TYPE.equals(TestFrameConstants.KUBERNETES_CLIENT)) {
kubeCmdClient = new Kubectl();
kubeCmdClient = new Kubectl(client.getKubeconfigPath());
} else {
kubeCmdClient = new Oc(client.getKubeconfigPath());
}
Expand Down

0 comments on commit 8053253

Please sign in to comment.