From f992ce2baa758cb21e09f3885eb4d9c6ee016a0c Mon Sep 17 00:00:00 2001 From: Stavros Kois <47820033+stavros-k@users.noreply.github.com> Date: Sat, 4 Mar 2023 11:49:01 +0200 Subject: [PATCH] add NET_BIND_SERVICE on ports <1024 (#340) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit **Description** ⚒️ Fixes # **⚙️ Type of change** - [x] ⚙️ Feature/App addition - [ ] 🪛 Bugfix - [ ] ⚠️ Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] 🔃 Refactor of current code **🧪 How Has This Been Tested?** **📃 Notes:** **✔️ Checklist:** - [x] ⚖️ My code follows the style guidelines of this project - [x] 👀 I have performed a self-review of my own code - [ ] #️⃣ I have commented my code, particularly in hard-to-understand areas - [ ] 📄 I have made corresponding changes to the documentation - [x] ⚠️ My changes generate no new warnings - [x] 🧪 I have added tests to this description that prove my fix is effective or that my feature works - [x] ⬆️ I increased versions for any altered app according to semantic versioning **➕ App addition** If this PR is an app addition please make sure you have done the following. - [ ] 🪞 I have opened a PR on [truecharts/containers](https://github.com/truecharts/containers) adding the container to TrueCharts mirror repo. - [ ] 🖼️ I have added an icon in the Chart's root directory called `icon.png` --- _Please don't blindly check all the boxes. Read them and only check those that apply. Those checkboxes are there for the reviewer to see what is this all about and the status of this PR with a quick glance._ --- library/common-test/tests/container/ports_test.yaml | 13 +++++++++++++ .../common-test/tests/secret/validation_test.yaml | 2 +- library/common/Chart.yaml | 2 +- .../templates/lib/container/_securityContext.tpl | 10 ++++++++++ 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/library/common-test/tests/container/ports_test.yaml b/library/common-test/tests/container/ports_test.yaml index 6523d5b2..5fdee78a 100644 --- a/library/common-test/tests/container/ports_test.yaml +++ b/library/common-test/tests/container/ports_test.yaml @@ -136,6 +136,13 @@ tests: - name: port-name containerPort: 1234 protocol: TCP + - documentIndex: *deploymentDoc + isNotSubset: + path: spec.template.spec.containers[0].securityContext.capabilities + content: + add: + - NET_BIND_SERVICE + - documentIndex: &otherDeploymentDoc 1 isKind: of: Deployment @@ -147,6 +154,12 @@ tests: - name: port-name containerPort: 53 protocol: TCP + - documentIndex: *otherDeploymentDoc + isSubset: + path: spec.template.spec.containers[0].securityContext.capabilities + content: + add: + - NET_BIND_SERVICE - it: should create the correct ports with hostPort set: diff --git a/library/common-test/tests/secret/validation_test.yaml b/library/common-test/tests/secret/validation_test.yaml index e91fc68e..6c5ccf4f 100644 --- a/library/common-test/tests/secret/validation_test.yaml +++ b/library/common-test/tests/secret/validation_test.yaml @@ -66,7 +66,7 @@ tests: data: {} asserts: - failedTemplate: - errorMessage: Secret - Expected non-empty + errorMessage: Secret - Expected non-empty or - it: should fail with empty type key set: diff --git a/library/common/Chart.yaml b/library/common/Chart.yaml index 614f0249..d02169b7 100644 --- a/library/common/Chart.yaml +++ b/library/common/Chart.yaml @@ -15,4 +15,4 @@ maintainers: name: common sources: null type: library -version: 12.2.11 +version: 12.2.12 diff --git a/library/common/templates/lib/container/_securityContext.tpl b/library/common/templates/lib/container/_securityContext.tpl index 2a9cff42..ce8e14ef 100644 --- a/library/common/templates/lib/container/_securityContext.tpl +++ b/library/common/templates/lib/container/_securityContext.tpl @@ -122,6 +122,16 @@ objectData: The object data to be used to render the container. {{- fail "Container - Expected to be defined" -}} {{- end -}} + {{- $tempObjectData := (dict "shortName" $objectData.podShortName "primary" $objectData.podPrimary) -}} + {{- $portRange := fromJson (include "tc.v1.common.lib.helpers.securityContext.getPortRange" (dict "rootCtx" $rootCtx "objectData" $tempObjectData)) -}} + {{- if and $portRange.low (le (int $portRange.low) 1024) -}} {{/* If a container wants to bind a port <= 1024 add NET_BIND_SERVICE */}} + {{- $addCap := $secContext.capabilities.add -}} + {{- if not (mustHas "NET_BIND_SERIVCE" $addCap) -}} + {{- $addCap = mustAppend $addCap "NET_BIND_SERVICE" -}} + {{- end -}} + {{- $_ := set $secContext.capabilities "add" $addCap -}} + {{- end -}} + {{- range $key := (list "add" "drop") -}} {{- $item := (get $secContext.capabilities $key) -}} {{- if not (kindIs "slice" $item) -}}