Skip to content

Commit

Permalink
KubernetesMockServer should not read local .kubeconfig
Browse files Browse the repository at this point in the history
KubernetesMockServer is using ConfigBuilder which tries to autoconfigure
Config by reading it automatically from local kubeconfig. this results
in issues where some code might accidentally read token/context set in
KubernetesClient's config which is set by Config autoconfigure.

This PR explicitly sets token, contexts to null/empty so that mockServer
tests run independently of local Kubernetes config
  • Loading branch information
rohanKanojia authored and manusa committed May 25, 2021
1 parent 6932324 commit 3dad6fa
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### 5.5-SNAPSHOT

#### Bugs
* KubernetesMockServer should not read local `.kube/config` while initializing client

#### Improvements
* Fix #3135: added mock crud support for patch status, and will return exceptions for unsupported patch types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import okhttp3.mockwebserver.RecordedRequest;

import java.net.InetAddress;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Queue;
Expand Down Expand Up @@ -88,12 +89,17 @@ public String[] getRootPaths() {
}

public NamespacedKubernetesClient createClient() {
Config config = new ConfigBuilder()
Config config = getMockConfiguration();
return new DefaultKubernetesClient(createHttpClientForMockServer(config), config);
}

protected Config getMockConfiguration() {
Config config = new ConfigBuilder(Config.empty())
.withMasterUrl(url("/"))
.withTrustCerts(true)
.withTlsVersions(TLS_1_2)
.withNamespace("test")
.build();
return new DefaultKubernetesClient(createHttpClientForMockServer(config), config);
return config;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,8 @@ public class KubernetesMockServerExtensionStaticTests {
@Test
void testExample() {
Assertions.assertNotNull(client);
Assertions.assertNull(client.getConfiguration().getOauthToken());
Assertions.assertNull(client.getConfiguration().getCurrentContext());
Assertions.assertTrue(client.getConfiguration().getContexts().isEmpty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,8 @@ public class KubernetesMockServerExtensionTests {
@Test
void testExample() {
Assertions.assertNotNull(client);
Assertions.assertNull(client.getConfiguration().getOauthToken());
Assertions.assertNull(client.getConfiguration().getCurrentContext());
Assertions.assertTrue(client.getConfiguration().getContexts().isEmpty());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.fabric8.kubernetes.client.server.mock;

import io.fabric8.kubernetes.client.KubernetesClient;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import org.junit.jupiter.api.Assertions;

import java.util.Objects;

class MockServerKubeconfigTest {

@BeforeEach
void setKubeConfigProperty() {
System.setProperty("kubeconfig", Objects.requireNonNull(getClass().getResource("/testkubeconfig")).getFile());
}

@Test
void mockServerShouldNotPickTokenAndNameContextIfKubeConfigFound() {
// Given
KubernetesMockServer server = new KubernetesMockServer();

// When
KubernetesClient client = server.createClient();

// Then
Assertions.assertNotNull(client);
Assertions.assertNull(client.getConfiguration().getOauthToken());
Assertions.assertNull(client.getConfiguration().getCurrentContext());
Assertions.assertTrue(client.getConfiguration().getContexts().isEmpty());
}

@AfterEach
public void cleanup() {
System.clearProperty("kubeconfig");
}
}
19 changes: 19 additions & 0 deletions kubernetes-server-mock/src/test/resources/testkubeconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apiVersion: v1
clusters:
- cluster:
insecure-skip-tls-verify: true
server: https://api.crc.testing:6443
name: api-crc-testing:6443
contexts:
- context:
cluster: api-crc-testing:6443
namespace: default
user: kubeadmin/api-crc-testing:6443
name: default/api-crc-testing:6443/kubeadmin
current-context: default/api-crc-testing:6443/kubeadmin
kind: Config
preferences: {}
users:
- name: kubeadmin/api-crc-testing:6443
user:
token: sha256~iYtvbJNJEE0_QSxYE0Wl1MJJxpSvDUsNyYfzkCIoDkw
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,13 @@
import io.fabric8.openshift.client.DefaultOpenShiftClient;
import io.fabric8.openshift.client.NamespacedOpenShiftClient;
import io.fabric8.openshift.client.OpenShiftConfig;
import io.fabric8.openshift.client.OpenShiftConfigBuilder;
import okhttp3.mockwebserver.Dispatcher;
import okhttp3.mockwebserver.MockWebServer;

import java.util.Map;
import java.util.Queue;

import static io.fabric8.kubernetes.client.utils.HttpClientUtils.createHttpClientForMockServer;
import static okhttp3.TlsVersion.TLS_1_2;

public class OpenShiftMockServer extends KubernetesMockServer {
private boolean disableApiGroupCheck = true;
Expand All @@ -54,13 +52,8 @@ public String[] getRootPaths() {
}

public NamespacedOpenShiftClient createOpenShiftClient() {
OpenShiftConfig config = new OpenShiftConfigBuilder()
.withMasterUrl(url("/"))
.withNamespace("test")
.withTrustCerts(true)
.withTlsVersions(TLS_1_2)
.withDisableApiGroupCheck(disableApiGroupCheck)
.build();
OpenShiftConfig config = OpenShiftConfig.wrap(getMockConfiguration());
config.setDisableApiGroupCheck(disableApiGroupCheck);
return new DefaultOpenShiftClient(createHttpClientForMockServer(config), config);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

@EnableOpenShiftMockClient(crud = true)
class OpenShiftMockServerExtensionStaticTests {
Expand All @@ -27,5 +29,8 @@ class OpenShiftMockServerExtensionStaticTests {
@Test
void testStaticOpenShiftClientGetsInitialized() {
assertNotNull(openShiftClient);
assertNull(openShiftClient.getConfiguration().getOauthToken());
assertNull(openShiftClient.getConfiguration().getCurrentContext());
assertTrue(openShiftClient.getConfiguration().getContexts().isEmpty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

@EnableOpenShiftMockClient(crud = true)
class OpenShiftMockServerExtensionTests {
Expand All @@ -27,5 +29,8 @@ class OpenShiftMockServerExtensionTests {
@Test
void testOpenShiftClientGetsInitialized() {
assertNotNull(client);
assertNull(client.getConfiguration().getOauthToken());
assertNull(client.getConfiguration().getCurrentContext());
assertTrue(client.getConfiguration().getContexts().isEmpty());
}
}

0 comments on commit 3dad6fa

Please sign in to comment.