|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2016-2023 ArangoDB GmbH, Cologne, Germany |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +// Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | +// |
| 20 | + |
| 21 | +package v1 |
| 22 | + |
| 23 | +import ( |
| 24 | + "testing" |
| 25 | + |
| 26 | + "github.com/stretchr/testify/assert" |
| 27 | + core "k8s.io/api/core/v1" |
| 28 | + |
| 29 | + "github.com/arangodb/kube-arangodb/pkg/util" |
| 30 | +) |
| 31 | + |
| 32 | +func TestServerGroupSpecSecurityContext_NewPodSecurityContext(t *testing.T) { |
| 33 | + testCases := map[string]struct { |
| 34 | + sc *ServerGroupSpecSecurityContext |
| 35 | + secured bool |
| 36 | + want *core.PodSecurityContext |
| 37 | + }{ |
| 38 | + "default unsecured pod security": { |
| 39 | + sc: nil, |
| 40 | + want: nil, |
| 41 | + }, |
| 42 | + "default secured pod security": { |
| 43 | + sc: nil, |
| 44 | + secured: true, |
| 45 | + want: &core.PodSecurityContext{ |
| 46 | + FSGroup: util.NewType[int64](defaultFSGroup), |
| 47 | + }, |
| 48 | + }, |
| 49 | + "user secured pod security takes precedence": { |
| 50 | + sc: &ServerGroupSpecSecurityContext{ |
| 51 | + FSGroup: util.NewType[int64](3001), |
| 52 | + }, |
| 53 | + secured: true, |
| 54 | + want: &core.PodSecurityContext{ |
| 55 | + FSGroup: util.NewType[int64](3001), |
| 56 | + }, |
| 57 | + }, |
| 58 | + "user secured pod security with FSGroup==nil": { |
| 59 | + sc: &ServerGroupSpecSecurityContext{ |
| 60 | + SupplementalGroups: []int64{1}, |
| 61 | + }, |
| 62 | + secured: true, |
| 63 | + want: &core.PodSecurityContext{ |
| 64 | + FSGroup: util.NewType[int64](defaultFSGroup), |
| 65 | + SupplementalGroups: []int64{1}, |
| 66 | + }, |
| 67 | + }, |
| 68 | + "user unsecured pod security": { |
| 69 | + sc: &ServerGroupSpecSecurityContext{ |
| 70 | + FSGroup: util.NewType[int64](3001), |
| 71 | + SupplementalGroups: []int64{1}, |
| 72 | + }, |
| 73 | + secured: false, |
| 74 | + want: &core.PodSecurityContext{ |
| 75 | + FSGroup: util.NewType[int64](3001), |
| 76 | + SupplementalGroups: []int64{1}, |
| 77 | + }, |
| 78 | + }, |
| 79 | + } |
| 80 | + |
| 81 | + for testName, testCase := range testCases { |
| 82 | + t.Run(testName, func(t *testing.T) { |
| 83 | + actual := testCase.sc.NewPodSecurityContext(testCase.secured) |
| 84 | + assert.Equalf(t, testCase.want, actual, "NewPodSecurityContext(%v)", testCase.secured) |
| 85 | + }) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +func TestServerGroupSpecSecurityContext_NewSecurityContext(t *testing.T) { |
| 90 | + tests := map[string]struct { |
| 91 | + sc *ServerGroupSpecSecurityContext |
| 92 | + secured bool |
| 93 | + want *core.SecurityContext |
| 94 | + }{ |
| 95 | + "default unsecured context security": { |
| 96 | + sc: nil, |
| 97 | + secured: false, |
| 98 | + want: &core.SecurityContext{ |
| 99 | + Capabilities: &core.Capabilities{ |
| 100 | + Drop: []core.Capability{"ALL"}, |
| 101 | + }, |
| 102 | + }, |
| 103 | + }, |
| 104 | + "default secured context security": { |
| 105 | + sc: nil, |
| 106 | + secured: true, |
| 107 | + want: &core.SecurityContext{ |
| 108 | + Capabilities: &core.Capabilities{ |
| 109 | + Drop: []core.Capability{"ALL"}, |
| 110 | + }, |
| 111 | + ReadOnlyRootFilesystem: util.NewType(true), |
| 112 | + RunAsGroup: util.NewType[int64](defaultRunAsGroup), |
| 113 | + RunAsNonRoot: util.NewType(true), |
| 114 | + RunAsUser: util.NewType[int64](defaultRunAsUser), |
| 115 | + }, |
| 116 | + }, |
| 117 | + "user unsecured context security": { |
| 118 | + sc: &ServerGroupSpecSecurityContext{ |
| 119 | + RunAsUser: util.NewType[int64](3001), |
| 120 | + }, |
| 121 | + secured: false, |
| 122 | + want: &core.SecurityContext{ |
| 123 | + Capabilities: &core.Capabilities{ |
| 124 | + Drop: []core.Capability{"ALL"}, |
| 125 | + }, |
| 126 | + RunAsUser: util.NewType[int64](3001), |
| 127 | + }, |
| 128 | + }, |
| 129 | + "secured user setting RunAsUser takes precedence": { |
| 130 | + sc: &ServerGroupSpecSecurityContext{ |
| 131 | + RunAsUser: util.NewType[int64](3001), |
| 132 | + }, |
| 133 | + secured: true, |
| 134 | + want: &core.SecurityContext{ |
| 135 | + Capabilities: &core.Capabilities{ |
| 136 | + Drop: []core.Capability{"ALL"}, |
| 137 | + }, |
| 138 | + ReadOnlyRootFilesystem: util.NewType(true), |
| 139 | + RunAsGroup: util.NewType[int64](defaultRunAsGroup), |
| 140 | + RunAsNonRoot: util.NewType(true), |
| 141 | + RunAsUser: util.NewType[int64](3001), |
| 142 | + }, |
| 143 | + }, |
| 144 | + "secured mixed users' settings takes precedence": { |
| 145 | + sc: &ServerGroupSpecSecurityContext{ |
| 146 | + AddCapabilities: []core.Capability{"1"}, |
| 147 | + AllowPrivilegeEscalation: util.NewType(true), |
| 148 | + DropAllCapabilities: util.NewType(false), // secured will turn it on |
| 149 | + Privileged: util.NewType(false), |
| 150 | + RunAsNonRoot: util.NewType(false), |
| 151 | + RunAsUser: util.NewType[int64](3001), |
| 152 | + }, |
| 153 | + secured: true, |
| 154 | + want: &core.SecurityContext{ |
| 155 | + |
| 156 | + AllowPrivilegeEscalation: util.NewType(true), |
| 157 | + Capabilities: &core.Capabilities{ |
| 158 | + Add: []core.Capability{"1"}, |
| 159 | + Drop: []core.Capability{"ALL"}, |
| 160 | + }, |
| 161 | + Privileged: util.NewType(false), |
| 162 | + ReadOnlyRootFilesystem: util.NewType(true), |
| 163 | + RunAsGroup: util.NewType[int64](defaultRunAsGroup), |
| 164 | + RunAsNonRoot: util.NewType(false), |
| 165 | + RunAsUser: util.NewType[int64](3001), |
| 166 | + }, |
| 167 | + }, |
| 168 | + } |
| 169 | + |
| 170 | + for testName, testCase := range tests { |
| 171 | + t.Run(testName, func(t *testing.T) { |
| 172 | + actual := testCase.sc.NewSecurityContext(testCase.secured) |
| 173 | + assert.Equalf(t, testCase.want, actual, "NewSecurityContext(%v)", testCase.secured) |
| 174 | + }) |
| 175 | + } |
| 176 | +} |
0 commit comments