Skip to content

Commit 09b15a5

Browse files
authored
Merge pull request #183 from stefankonig/checkValidBase64Cert
Check whether provided base64 encoded data in KubeConfig is valid
2 parents 9f72121 + 9edef5f commit 09b15a5

File tree

4 files changed

+33
-1
lines changed

4 files changed

+33
-1
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace RenokiCo\PhpK8s\Exceptions;
4+
5+
class KubeConfigBaseEncodedDataInvalid extends PhpK8sException
6+
{
7+
//
8+
}

src/Traits/Cluster/LoadsFromKubeConfig.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Exception;
66
use Illuminate\Support\Arr;
7+
use RenokiCo\PhpK8s\Exceptions\KubeConfigBaseEncodedDataInvalid;
78
use RenokiCo\PhpK8s\Exceptions\KubeConfigClusterNotFound;
89
use RenokiCo\PhpK8s\Exceptions\KubeConfigContextNotFound;
910
use RenokiCo\PhpK8s\Exceptions\KubeConfigUserNotFound;
@@ -219,7 +220,13 @@ protected function writeTempFileForContext(string $context, string $fileName, st
219220
return $tempFilePath;
220221
}
221222

222-
if (file_put_contents($tempFilePath, base64_decode($contents, true)) === false) {
223+
$decodedContents = base64_decode($contents, true);
224+
225+
if ($decodedContents === false) {
226+
throw new KubeConfigBaseEncodedDataInvalid("Failed to decode base64-encoded data for: {$fileName}");
227+
}
228+
229+
if (file_put_contents($tempFilePath, $decodedContents) === false) {
223230
throw new Exception("Failed to write content to temp file: {$tempFilePath}");
224231
}
225232

tests/KubeConfigTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace RenokiCo\PhpK8s\Test;
44

5+
use RenokiCo\PhpK8s\Exceptions\KubeConfigBaseEncodedDataInvalid;
56
use RenokiCo\PhpK8s\Exceptions\KubeConfigClusterNotFound;
67
use RenokiCo\PhpK8s\Exceptions\KubeConfigContextNotFound;
78
use RenokiCo\PhpK8s\Exceptions\KubeConfigUserNotFound;
@@ -157,6 +158,13 @@ public function test_kube_config_from_yaml_cannot_load_if_wrong_context()
157158
KubernetesCluster::fromKubeConfigYamlFile(__DIR__.'/cluster/kubeconfig.yaml', 'inexistent-context');
158159
}
159160

161+
public function test_kube_config_from_yaml_invalid_base64_ca()
162+
{
163+
$this->expectException(KubeConfigBaseEncodedDataInvalid::class);
164+
165+
KubernetesCluster::fromKubeConfigYamlFile(__DIR__.'/cluster/kubeconfig.yaml', 'minikube-invalid-base64-ca');
166+
}
167+
160168
public function test_http_authentication()
161169
{
162170
$cluster = KubernetesCluster::fromUrl('http://127.0.0.1:8080')->httpAuthentication('some-user', 'some-password');

tests/cluster/kubeconfig.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ clusters:
1313
server: https://minikube-2:8443
1414
insecure-skip-tls-verify: true
1515
name: minikube-skip-tls
16+
- cluster:
17+
certificate-authority-data: c29tZS1j1YQo= # invalid base64
18+
server: https://minikube:8443
19+
name: minikube-invalid-base64-ca
1620
contexts:
1721
- context:
1822
cluster: minikube
@@ -39,6 +43,11 @@ contexts:
3943
user: no-user
4044
name: minikube-without-user
4145
namespace: some-namespace
46+
- context:
47+
cluster: minikube-invalid-base64-ca
48+
user: minikube
49+
name: minikube-invalid-base64-ca
50+
namespace: some-namespace
4251
current-context: minikube
4352
kind: Config
4453
preferences: {}

0 commit comments

Comments
 (0)