Skip to content

Commit 85d67ca

Browse files
committed
Validate namespace parameter as DNS subdomain name
Namespace naming should conform to DNS subdomain name as defined in RFC 1123 https://tools.ietf.org/html/rfc1123 When enforcing such policy code injection should not be possible (no need to escape namespace name).
1 parent e5d9c9e commit 85d67ca

File tree

3 files changed

+54
-11
lines changed

3 files changed

+54
-11
lines changed

manifests/wait_for_default_sa.pp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
# == kubernetes::wait_for_default_sa
2+
#
3+
# @param namespace
4+
# Namespace name must be a valid DNS name (max. 63 characters)
5+
# see https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/#namespaces-and-dns
6+
#
27
define kubernetes::wait_for_default_sa (
3-
String $namespace = $title,
4-
Array $path = $kubernetes::default_path,
5-
Optional[Integer] $timeout = undef,
6-
Optional[Integer] $tries = $kubernetes::wait_for_default_sa_tries,
7-
Optional[Integer] $try_sleep = $kubernetes::wait_for_default_sa_try_sleep,
8-
Optional[Array] $env = $kubernetes::environment,
8+
Kubernetes::Namespace $namespace = $title,
9+
Array $path = $kubernetes::default_path,
10+
Optional[Integer] $timeout = undef,
11+
Optional[Integer] $tries = $kubernetes::wait_for_default_sa_tries,
12+
Optional[Integer] $try_sleep = $kubernetes::wait_for_default_sa_try_sleep,
13+
Optional[Array] $env = $kubernetes::environment,
914
) {
10-
$safe_namespace = shell_escape($namespace)
11-
1215
# This prevents a known race condition https://github.com/kubernetes/kubernetes/issues/66689
13-
exec { "wait for default serviceaccount creation in ${safe_namespace}":
14-
command => "kubectl -n ${safe_namespace} get serviceaccount default -o name",
15-
unless => ["kubectl -n ${safe_namespace} get serviceaccount default -o name"],
16+
exec { "wait for default serviceaccount creation in ${namespace}":
17+
command => "kubectl -n ${namespace} get serviceaccount default -o name",
18+
unless => "kubectl -n ${namespace} get serviceaccount default -o name",
1619
path => $path,
1720
environment => $env,
1821
timeout => $timeout,

spec/defines/wait_for_default_sa_spec.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,41 @@
4242
.with_command('kubectl -n foo get serviceaccount default -o name')
4343
.with_path(['/bar'])}
4444
end
45+
46+
describe 'namespace naming' do
47+
tests = [
48+
['01010', true],
49+
['abc', true],
50+
['A0c', true],
51+
['A0c-', false],
52+
['-A0c', false],
53+
['A-0c', true],
54+
['o123456701234567012345670123456701234567012345670123456701234567', false],
55+
['o12345670123456701234567012345670123456701234567012345670123456', true],
56+
['', false],
57+
['a', true],
58+
['0--0', true],
59+
["A0c\nA0c", false],
60+
['host;rm -rf /', false]
61+
]
62+
63+
tests.each do |namespace, expected|
64+
context "with namespace #{namespace}" do
65+
let(:params) do
66+
{
67+
'namespace' => namespace,
68+
}
69+
end
70+
71+
if expected
72+
it { is_expected.to compile.with_all_deps }
73+
it { is_expected.to contain_exec("wait for default serviceaccount creation in #{namespace}")
74+
.with_command("kubectl -n #{namespace} get serviceaccount default -o name")
75+
}
76+
else
77+
it { is_expected.to raise_error(/parameter 'namespace' expects a match for Kubernetes::Namespace/) }
78+
end
79+
end
80+
end
81+
end
4582
end

types/namespace.pp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# namespace should conform to RFC 1123
2+
# source https://stackoverflow.com/a/20945961/334831
3+
type Kubernetes::Namespace = Pattern['\A(?!-)[a-zA-Z0-9-]{1,63}(?<!-)\z']

0 commit comments

Comments
 (0)