Skip to content

Commit

Permalink
Merge pull request #183 from stefankonig/checkValidBase64Cert
Browse files Browse the repository at this point in the history
Check whether provided base64 encoded data in KubeConfig is valid
  • Loading branch information
rennokki authored Dec 31, 2021
2 parents 9f72121 + 9edef5f commit 09b15a5
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/Exceptions/KubeConfigBaseEncodedDataInvalid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace RenokiCo\PhpK8s\Exceptions;

class KubeConfigBaseEncodedDataInvalid extends PhpK8sException
{
//
}
9 changes: 8 additions & 1 deletion src/Traits/Cluster/LoadsFromKubeConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Exception;
use Illuminate\Support\Arr;
use RenokiCo\PhpK8s\Exceptions\KubeConfigBaseEncodedDataInvalid;
use RenokiCo\PhpK8s\Exceptions\KubeConfigClusterNotFound;
use RenokiCo\PhpK8s\Exceptions\KubeConfigContextNotFound;
use RenokiCo\PhpK8s\Exceptions\KubeConfigUserNotFound;
Expand Down Expand Up @@ -219,7 +220,13 @@ protected function writeTempFileForContext(string $context, string $fileName, st
return $tempFilePath;
}

if (file_put_contents($tempFilePath, base64_decode($contents, true)) === false) {
$decodedContents = base64_decode($contents, true);

if ($decodedContents === false) {
throw new KubeConfigBaseEncodedDataInvalid("Failed to decode base64-encoded data for: {$fileName}");
}

if (file_put_contents($tempFilePath, $decodedContents) === false) {
throw new Exception("Failed to write content to temp file: {$tempFilePath}");
}

Expand Down
8 changes: 8 additions & 0 deletions tests/KubeConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace RenokiCo\PhpK8s\Test;

use RenokiCo\PhpK8s\Exceptions\KubeConfigBaseEncodedDataInvalid;
use RenokiCo\PhpK8s\Exceptions\KubeConfigClusterNotFound;
use RenokiCo\PhpK8s\Exceptions\KubeConfigContextNotFound;
use RenokiCo\PhpK8s\Exceptions\KubeConfigUserNotFound;
Expand Down Expand Up @@ -157,6 +158,13 @@ public function test_kube_config_from_yaml_cannot_load_if_wrong_context()
KubernetesCluster::fromKubeConfigYamlFile(__DIR__.'/cluster/kubeconfig.yaml', 'inexistent-context');
}

public function test_kube_config_from_yaml_invalid_base64_ca()
{
$this->expectException(KubeConfigBaseEncodedDataInvalid::class);

KubernetesCluster::fromKubeConfigYamlFile(__DIR__.'/cluster/kubeconfig.yaml', 'minikube-invalid-base64-ca');
}

public function test_http_authentication()
{
$cluster = KubernetesCluster::fromUrl('http://127.0.0.1:8080')->httpAuthentication('some-user', 'some-password');
Expand Down
9 changes: 9 additions & 0 deletions tests/cluster/kubeconfig.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ clusters:
server: https://minikube-2:8443
insecure-skip-tls-verify: true
name: minikube-skip-tls
- cluster:
certificate-authority-data: c29tZS1j1YQo= # invalid base64
server: https://minikube:8443
name: minikube-invalid-base64-ca
contexts:
- context:
cluster: minikube
Expand All @@ -39,6 +43,11 @@ contexts:
user: no-user
name: minikube-without-user
namespace: some-namespace
- context:
cluster: minikube-invalid-base64-ca
user: minikube
name: minikube-invalid-base64-ca
namespace: some-namespace
current-context: minikube
kind: Config
preferences: {}
Expand Down

0 comments on commit 09b15a5

Please sign in to comment.