@@ -14,6 +14,7 @@ import (
1414 "github.com/docker/docker/container"
1515 "github.com/docker/docker/daemon/config"
1616 "github.com/docker/docker/pkg/sysinfo"
17+ "github.com/opencontainers/selinux/go-selinux"
1718 "golang.org/x/sys/unix"
1819 "gotest.tools/v3/assert"
1920 is "gotest.tools/v3/assert/cmp"
@@ -138,115 +139,136 @@ func TestAdjustCPUSharesNoAdjustment(t *testing.T) {
138139
139140// Unix test as uses settings which are not available on Windows
140141func TestParseSecurityOptWithDeprecatedColon (t * testing.T ) {
141- ctr := & container.Container {}
142+ opts := & container.SecurityOptions {}
142143 cfg := & containertypes.HostConfig {}
143144
144145 // test apparmor
145146 cfg .SecurityOpt = []string {"apparmor=test_profile" }
146- if err := parseSecurityOpt (ctr , cfg ); err != nil {
147+ if err := parseSecurityOpt (opts , cfg ); err != nil {
147148 t .Fatalf ("Unexpected parseSecurityOpt error: %v" , err )
148149 }
149- if ctr .AppArmorProfile != "test_profile" {
150- t .Fatalf ("Unexpected AppArmorProfile, expected: \" test_profile\" , got %q" , ctr .AppArmorProfile )
150+ if opts .AppArmorProfile != "test_profile" {
151+ t .Fatalf ("Unexpected AppArmorProfile, expected: \" test_profile\" , got %q" , opts .AppArmorProfile )
151152 }
152153
153154 // test seccomp
154155 sp := "/path/to/seccomp_test.json"
155156 cfg .SecurityOpt = []string {"seccomp=" + sp }
156- if err := parseSecurityOpt (ctr , cfg ); err != nil {
157+ if err := parseSecurityOpt (opts , cfg ); err != nil {
157158 t .Fatalf ("Unexpected parseSecurityOpt error: %v" , err )
158159 }
159- if ctr .SeccompProfile != sp {
160- t .Fatalf ("Unexpected AppArmorProfile, expected: %q, got %q" , sp , ctr .SeccompProfile )
160+ if opts .SeccompProfile != sp {
161+ t .Fatalf ("Unexpected AppArmorProfile, expected: %q, got %q" , sp , opts .SeccompProfile )
161162 }
162163
163164 // test valid label
164165 cfg .SecurityOpt = []string {"label=user:USER" }
165- if err := parseSecurityOpt (ctr , cfg ); err != nil {
166+ if err := parseSecurityOpt (opts , cfg ); err != nil {
166167 t .Fatalf ("Unexpected parseSecurityOpt error: %v" , err )
167168 }
168169
169170 // test invalid label
170171 cfg .SecurityOpt = []string {"label" }
171- if err := parseSecurityOpt (ctr , cfg ); err == nil {
172+ if err := parseSecurityOpt (opts , cfg ); err == nil {
172173 t .Fatal ("Expected parseSecurityOpt error, got nil" )
173174 }
174175
175176 // test invalid opt
176177 cfg .SecurityOpt = []string {"test" }
177- if err := parseSecurityOpt (ctr , cfg ); err == nil {
178+ if err := parseSecurityOpt (opts , cfg ); err == nil {
178179 t .Fatal ("Expected parseSecurityOpt error, got nil" )
179180 }
180181}
181182
182183func TestParseSecurityOpt (t * testing.T ) {
183- ctr := & container.Container {}
184- cfg := & containertypes.HostConfig {}
185-
186- // test apparmor
187- cfg .SecurityOpt = []string {"apparmor=test_profile" }
188- if err := parseSecurityOpt (ctr , cfg ); err != nil {
189- t .Fatalf ("Unexpected parseSecurityOpt error: %v" , err )
190- }
191- if ctr .AppArmorProfile != "test_profile" {
192- t .Fatalf ("Unexpected AppArmorProfile, expected: \" test_profile\" , got %q" , ctr .AppArmorProfile )
193- }
194-
195- // test seccomp
196- sp := "/path/to/seccomp_test.json"
197- cfg .SecurityOpt = []string {"seccomp=" + sp }
198- if err := parseSecurityOpt (ctr , cfg ); err != nil {
199- t .Fatalf ("Unexpected parseSecurityOpt error: %v" , err )
200- }
201- if ctr .SeccompProfile != sp {
202- t .Fatalf ("Unexpected SeccompProfile, expected: %q, got %q" , sp , ctr .SeccompProfile )
203- }
204-
205- // test valid label
206- cfg .SecurityOpt = []string {"label=user:USER" }
207- if err := parseSecurityOpt (ctr , cfg ); err != nil {
208- t .Fatalf ("Unexpected parseSecurityOpt error: %v" , err )
209- }
210-
211- // test invalid label
212- cfg .SecurityOpt = []string {"label" }
213- if err := parseSecurityOpt (ctr , cfg ); err == nil {
214- t .Fatal ("Expected parseSecurityOpt error, got nil" )
215- }
216-
217- // test invalid opt
218- cfg .SecurityOpt = []string {"test" }
219- if err := parseSecurityOpt (ctr , cfg ); err == nil {
220- t .Fatal ("Expected parseSecurityOpt error, got nil" )
221- }
184+ t .Run ("apparmor" , func (t * testing.T ) {
185+ secOpts := & container.SecurityOptions {}
186+ err := parseSecurityOpt (secOpts , & containertypes.HostConfig {
187+ SecurityOpt : []string {"apparmor=test_profile" },
188+ })
189+ assert .Check (t , err )
190+ assert .Equal (t , secOpts .AppArmorProfile , "test_profile" )
191+ })
192+ t .Run ("apparmor using legacy separator" , func (t * testing.T ) {
193+ secOpts := & container.SecurityOptions {}
194+ err := parseSecurityOpt (secOpts , & containertypes.HostConfig {
195+ SecurityOpt : []string {"apparmor:test_profile" },
196+ })
197+ assert .Check (t , err )
198+ assert .Equal (t , secOpts .AppArmorProfile , "test_profile" )
199+ })
200+ t .Run ("seccomp" , func (t * testing.T ) {
201+ secOpts := & container.SecurityOptions {}
202+ err := parseSecurityOpt (secOpts , & containertypes.HostConfig {
203+ SecurityOpt : []string {"seccomp=/path/to/seccomp_test.json" },
204+ })
205+ assert .Check (t , err )
206+ assert .Equal (t , secOpts .SeccompProfile , "/path/to/seccomp_test.json" )
207+ })
208+ t .Run ("valid label" , func (t * testing.T ) {
209+ secOpts := & container.SecurityOptions {}
210+ err := parseSecurityOpt (secOpts , & containertypes.HostConfig {
211+ SecurityOpt : []string {"label=user:USER" },
212+ })
213+ assert .Check (t , err )
214+ if selinux .GetEnabled () {
215+ // TODO(thaJeztah): set expected labels here (or "partial" if depends on host)
216+ // assert.Check(t, is.Equal(secOpts.MountLabel, ""))
217+ // assert.Check(t, is.Equal(secOpts.ProcessLabel, ""))
218+ } else {
219+ assert .Check (t , is .Equal (secOpts .MountLabel , "" ))
220+ assert .Check (t , is .Equal (secOpts .ProcessLabel , "" ))
221+ }
222+ })
223+ t .Run ("invalid label" , func (t * testing.T ) {
224+ secOpts := & container.SecurityOptions {}
225+ err := parseSecurityOpt (secOpts , & containertypes.HostConfig {
226+ SecurityOpt : []string {"label" },
227+ })
228+ assert .Error (t , err , `invalid --security-opt 1: "label"` )
229+ })
230+ t .Run ("invalid option (no value)" , func (t * testing.T ) {
231+ secOpts := & container.SecurityOptions {}
232+ err := parseSecurityOpt (secOpts , & containertypes.HostConfig {
233+ SecurityOpt : []string {"unknown" },
234+ })
235+ assert .Error (t , err , `invalid --security-opt 1: "unknown"` )
236+ })
237+ t .Run ("unknown option" , func (t * testing.T ) {
238+ secOpts := & container.SecurityOptions {}
239+ err := parseSecurityOpt (secOpts , & containertypes.HostConfig {
240+ SecurityOpt : []string {"unknown=something" },
241+ })
242+ assert .Error (t , err , `invalid --security-opt 2: "unknown=something"` )
243+ })
222244}
223245
224246func TestParseNNPSecurityOptions (t * testing.T ) {
225247 daemon := & Daemon {
226248 configStore : & config.Config {NoNewPrivileges : true },
227249 }
228- ctr := & container.Container {}
250+ opts := & container.SecurityOptions {}
229251 cfg := & containertypes.HostConfig {}
230252
231253 // test NNP when "daemon:true" and "no-new-privileges=false""
232254 cfg .SecurityOpt = []string {"no-new-privileges=false" }
233255
234- if err := daemon .parseSecurityOpt (ctr , cfg ); err != nil {
256+ if err := daemon .parseSecurityOpt (opts , cfg ); err != nil {
235257 t .Fatalf ("Unexpected daemon.parseSecurityOpt error: %v" , err )
236258 }
237- if ctr .NoNewPrivileges {
238- t .Fatalf ("container.NoNewPrivileges should be FALSE: %v" , ctr .NoNewPrivileges )
259+ if opts .NoNewPrivileges {
260+ t .Fatalf ("container.NoNewPrivileges should be FALSE: %v" , opts .NoNewPrivileges )
239261 }
240262
241263 // test NNP when "daemon:false" and "no-new-privileges=true""
242264 daemon .configStore .NoNewPrivileges = false
243265 cfg .SecurityOpt = []string {"no-new-privileges=true" }
244266
245- if err := daemon .parseSecurityOpt (ctr , cfg ); err != nil {
267+ if err := daemon .parseSecurityOpt (opts , cfg ); err != nil {
246268 t .Fatalf ("Unexpected daemon.parseSecurityOpt error: %v" , err )
247269 }
248- if ! ctr .NoNewPrivileges {
249- t .Fatalf ("container.NoNewPrivileges should be TRUE: %v" , ctr .NoNewPrivileges )
270+ if ! opts .NoNewPrivileges {
271+ t .Fatalf ("container.NoNewPrivileges should be TRUE: %v" , opts .NoNewPrivileges )
250272 }
251273}
252274
0 commit comments