Skip to content

Commit

Permalink
Merge pull request #179 from renoki-co/feature/skip-tls-checks-from-k…
Browse files Browse the repository at this point in the history
…ubeconfig

[feature] Add support for insecure-skip-tls-verify
  • Loading branch information
rennokki authored Dec 30, 2021
2 parents 2949399 + 5356a66 commit 9f72121
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Traits/Cluster/LoadsFromKubeConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public static function setTempFolder(string $tempFolder)
*/
public static function fromKubeConfigVariable(string $context = null)
{
/** @var \RenokiCo\PhpK8s\KubernetesCluster $this */
$cluster = new static;

if (! isset($_SERVER['KUBECONFIG'])) {
Expand Down Expand Up @@ -81,6 +82,7 @@ public static function fromKubeConfigVariable(string $context = null)
*/
public static function fromKubeConfigYaml(string $yaml, string $context = null)
{
/** @var \RenokiCo\PhpK8s\KubernetesCluster $this */
$cluster = new static;

return $cluster->loadKubeConfigFromArray(yaml_parse($yaml), $context);
Expand Down Expand Up @@ -126,6 +128,8 @@ public static function fromKubeConfigArray(array $kubeConfigArray, string $conte
*/
protected function loadKubeConfigFromArray(array $kubeconfig, string $context = null)
{
/** @var \RenokiCo\PhpK8s\KubernetesCluster $this */

// Compute the context from the method, or in case it is passed as null
// try to find it from the current kubeconfig's "current-context" field.
$context = $context ?: ($kubeconfig['current-context'] ?? null);
Expand Down Expand Up @@ -186,6 +190,10 @@ protected function loadKubeConfigFromArray(array $kubeconfig, string $context =
$this->withToken($userConfig['user']['token']);
}

if (isset($clusterConfig['cluster']['insecure-skip-tls-verify']) && $clusterConfig['cluster']['insecure-skip-tls-verify']) {
$this->withoutSslChecks();
}

return $this;
}

Expand All @@ -202,6 +210,7 @@ protected function loadKubeConfigFromArray(array $kubeconfig, string $context =
*/
protected function writeTempFileForContext(string $context, string $fileName, string $contents)
{
/** @var \RenokiCo\PhpK8s\KubernetesCluster $this */
$tempFolder = static::$tempFolder ?: sys_get_temp_dir();

$tempFilePath = $tempFolder.DIRECTORY_SEPARATOR."ctx-{$context}-{$fileName}";
Expand All @@ -226,6 +235,7 @@ protected function writeTempFileForContext(string $context, string $fileName, st
*/
protected static function mergeKubeconfigContents(array $kubeconfig1, array $kubeconfig2): array
{
/** @var \RenokiCo\PhpK8s\KubernetesCluster $this */
$kubeconfig1 += $kubeconfig2;

foreach ($kubeconfig1 as $key => $value) {
Expand Down
15 changes: 15 additions & 0 deletions tests/KubeConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,21 @@ public function test_kube_config_from_yaml_file_with_paths_to_ssl()
$this->assertEquals('/path/to/.minikube/client.key', $keyPath);
}

public function test_kube_config_from_yaml_file_with_skip_tols()
{
$cluster = KubernetesCluster::fromKubeConfigYamlFile(__DIR__.'/cluster/kubeconfig.yaml', 'minikube-skip-tls');

[
'verify' => $verify,
'cert' => $certPath,
'ssl_key' => $keyPath,
] = $cluster->getClient()->getConfig();

$this->assertFalse($verify);
$this->assertEquals('/path/to/.minikube/client3.crt', $certPath);
$this->assertEquals('/path/to/.minikube/client3.key', $keyPath);
}

public function test_cluster_can_get_correct_config_for_token_socket_connection()
{
$cluster = KubernetesCluster::fromUrl('http://127.0.0.1:8080')->loadTokenFromFile(__DIR__.'/cluster/token.txt');
Expand Down
14 changes: 14 additions & 0 deletions tests/cluster/kubeconfig.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ clusters:
certificate-authority: /path/to/.minikube/ca.crt
server: https://minikube-2:8443
name: minikube-2
- cluster:
certificate-authority: /path/to/.minikube/ca.crt
server: https://minikube-2:8443
insecure-skip-tls-verify: true
name: minikube-skip-tls
contexts:
- context:
cluster: minikube
Expand All @@ -19,6 +24,11 @@ contexts:
user: minikube-2
name: minikube-2
namespace: some-namespace
- context:
cluster: minikube-skip-tls
user: minikube-skip-tls
name: minikube-skip-tls
namespace: some-namespace
- context:
cluster: no-cluster
user: minikube
Expand All @@ -41,3 +51,7 @@ users:
user:
client-certificate: /path/to/.minikube/client.crt
client-key: /path/to/.minikube/client.key
- name: minikube-skip-tls
user:
client-certificate: /path/to/.minikube/client3.crt
client-key: /path/to/.minikube/client3.key

0 comments on commit 9f72121

Please sign in to comment.