Debian Codegraph @

launcher/spec/launch_spec_test.go

raw
1 package spec
2
3 import (
4 "regexp"
5 "testing"
6
7 "github.com/google/go-cmp/cmp"
8 "github.com/google/go-tpm-tools/launcher/internal/experiments"
9 "github.com/google/go-tpm-tools/launcher/internal/launchermount"
10 "github.com/google/go-tpm-tools/verifier"
11 )
12
13 func TestLaunchSpecUnmarshalJSONHappyCases(t *testing.T) {
14 baseWant := LaunchSpec{
15 ImageRef: "docker.io/library/hello-world:latest",
16 SignedImageRepos: []string{"docker.io/library/hello-world", "gcr.io/cloudrun/hello"},
17 RestartPolicy: Always,
18 Cmd: []string{"--foo", "--bar", "--baz"},
19 Envs: []EnvVar{{"foo", "bar"}},
20 ImpersonateServiceAccounts: []string{"[email protected]", "[email protected]"},
21 LogRedirect: Everywhere,
22 MonitoringEnabled: MemoryOnly,
23 DevShmSize: 234234,
24 Mounts: []launchermount.Mount{launchermount.TmpfsMount{Destination: "/tmpmount", Size: 0},
25 launchermount.TmpfsMount{Destination: "/sized", Size: 222}},
26 ITAConfig: verifier.ITAConfig{
27 ITARegion: "US",
28 ITAKey: "test-api-key",
29 },
30 Experiments: experiments.Experiments{
31 EnableItaVerifier: true,
32 },
33 GcaAddress: "https://confidentialcomputing.googleapis.com",
34 InstallGpuDriver: true,
35 DisableGcaRefresh: false,
36 }
37
38 var testCases = []struct {
39 testName string
40 mdsJSON string
41 modifyWant func(LaunchSpec) LaunchSpec
42 }{
43 {
44 testName: "HappyCase",
45 mdsJSON: `{
46 "tee-cmd":"[\"--foo\",\"--bar\",\"--baz\"]",
47 "tee-env-foo":"bar",
48 "tee-image-reference":"docker.io/library/hello-world:latest",
49 "tee-signed-image-repos":"docker.io/library/hello-world,gcr.io/cloudrun/hello",
50 "tee-restart-policy":"Always",
51 "tee-impersonate-service-accounts":"[email protected],[email protected]",
52 "tee-container-log-redirect":"true",
53 "tee-monitoring-memory-enable":"true",
54 "tee-dev-shm-size-kb":"234234",
55 "tee-mount":"type=tmpfs,source=tmpfs,destination=/tmpmount;type=tmpfs,source=tmpfs,destination=/sized,size=222",
56 "ita-region":"US",
57 "ita-api-key":"test-api-key",
58 "gca-service-env":"STAGING",
59 "tee-install-gpu-driver":"true"
60 }`,
61 modifyWant: func(ls LaunchSpec) LaunchSpec {
62 ls.GcaAddress = "https://staging-confidentialcomputing.sandbox.googleapis.com"
63 return ls
64 },
65 },
66 {
67 testName: "HappyCaseWithExtraUnknownFields",
68 mdsJSON: `{
69 "tee-cmd":"[\"--foo\",\"--bar\",\"--baz\"]",
70 "tee-env-foo":"bar",
71 "tee-unknown":"unknown",
72 "unknown":"unknown",
73 "tee-image-reference":"docker.io/library/hello-world:latest",
74 "tee-signed-image-repos":"docker.io/library/hello-world,gcr.io/cloudrun/hello",
75 "tee-restart-policy":"Always",
76 "tee-impersonate-service-accounts":"[email protected],[email protected]",
77 "tee-container-log-redirect":"true",
78 "tee-monitoring-memory-enable":"TRUE",
79 "tee-dev-shm-size-kb":"234234",
80 "tee-mount":"type=tmpfs,source=tmpfs,destination=/tmpmount;type=tmpfs,source=tmpfs,destination=/sized,size=222",
81 "ita-region":"US",
82 "ita-api-key":"test-api-key",
83 "tee-install-gpu-driver":"true"
84 }`,
85 modifyWant: func(ls LaunchSpec) LaunchSpec {
86 ls.GcaAddress = ""
87 return ls
88 },
89 },
90 {
91 testName: "GcaServiceSetToProd",
92 mdsJSON: `{
93 "tee-cmd":"[\"--foo\",\"--bar\",\"--baz\"]",
94 "tee-env-foo":"bar",
95 "tee-unknown":"unknown",
96 "unknown":"unknown",
97 "tee-image-reference":"docker.io/library/hello-world:latest",
98 "tee-signed-image-repos":"docker.io/library/hello-world,gcr.io/cloudrun/hello",
99 "tee-restart-policy":"Always",
100 "tee-impersonate-service-accounts":"[email protected],[email protected]",
101 "tee-container-log-redirect":"true",
102 "tee-monitoring-memory-enable":"TRUE",
103 "tee-dev-shm-size-kb":"234234",
104 "tee-mount":"type=tmpfs,source=tmpfs,destination=/tmpmount;type=tmpfs,source=tmpfs,destination=/sized,size=222",
105 "ita-region":"US",
106 "ita-api-key":"test-api-key",
107 "gca-service-env":"prod",
108 "tee-install-gpu-driver":"true"
109 }`,
110 modifyWant: func(ls LaunchSpec) LaunchSpec {
111 return ls
112 },
113 },
114 {
115 testName: "GcaServiceEnvTramplesTeeAttestationServiceEndpoint",
116 mdsJSON: `{
117 "tee-cmd":"[\"--foo\",\"--bar\",\"--baz\"]",
118 "tee-env-foo":"bar",
119 "tee-unknown":"unknown",
120 "unknown":"unknown",
121 "tee-image-reference":"docker.io/library/hello-world:latest",
122 "tee-signed-image-repos":"docker.io/library/hello-world,gcr.io/cloudrun/hello",
123 "tee-restart-policy":"Always",
124 "tee-impersonate-service-accounts":"[email protected],[email protected]",
125 "tee-container-log-redirect":"true",
126 "tee-monitoring-memory-enable":"TRUE",
127 "tee-dev-shm-size-kb":"234234",
128 "tee-mount":"type=tmpfs,source=tmpfs,destination=/tmpmount;type=tmpfs,source=tmpfs,destination=/sized,size=222",
129 "ita-region":"US",
130 "ita-api-key":"test-api-key",
131 "gca-service-env":"staging",
132 "tee-install-gpu-driver":"true"
133 }`,
134 modifyWant: func(ls LaunchSpec) LaunchSpec {
135 ls.GcaAddress = "https://staging-confidentialcomputing.sandbox.googleapis.com"
136 return ls
137 },
138 },
139 {
140 testName: "DisableGcaRefreshSetToTrue",
141 mdsJSON: `{
142 "tee-cmd":"[\"--foo\",\"--bar\",\"--baz\"]",
143 "tee-env-foo":"bar",
144 "tee-image-reference":"docker.io/library/hello-world:latest",
145 "tee-signed-image-repos":"docker.io/library/hello-world,gcr.io/cloudrun/hello",
146 "tee-restart-policy":"Always",
147 "tee-impersonate-service-accounts":"[email protected],[email protected]",
148 "tee-container-log-redirect":"true",
149 "tee-monitoring-memory-enable":"true",
150 "tee-dev-shm-size-kb":"234234",
151 "tee-mount":"type=tmpfs,source=tmpfs,destination=/tmpmount;type=tmpfs,source=tmpfs,destination=/sized,size=222",
152 "ita-region":"US",
153 "ita-api-key":"test-api-key",
154 "tee-install-gpu-driver":"true",
155 "tee-disable-gca-refresh":"true"
156 }`,
157 modifyWant: func(ls LaunchSpec) LaunchSpec {
158 ls.GcaAddress = ""
159 ls.DisableGcaRefresh = true
160 return ls
161 },
162 },
163 }
164
165 for _, testcase := range testCases {
166 t.Run(testcase.testName, func(t *testing.T) {
167 spec := &LaunchSpec{}
168 spec.Experiments = experiments.Experiments{
169 EnableItaVerifier: true,
170 }
171 if err := spec.UnmarshalJSON([]byte(testcase.mdsJSON)); err != nil {
172 t.Fatal(err)
173 }
174 want := testcase.modifyWant(baseWant)
175 if !cmp.Equal(spec, &want) {
176 t.Errorf("LaunchSpec UnmarshalJSON got %+v, want %+v", spec, want)
177 }
178 })
179 }
180 }
181
182 func TestLaunchSpecUnmarshalJSONBadInput(t *testing.T) {
183 var testCases = []struct {
184 testName string
185 mdsJSON string
186 }{
187 // not likely to happen for MDS
188 {
189 "BadJSON",
190 `{
191 BadJSONFormat
192 }`,
193 },
194 // when there is no MDS values
195 {
196 "EmptyJSON",
197 `{}`,
198 },
199 // not likely to happen, since MDS will always use string as the value
200 {
201 "JSONWithPrimitives",
202 `{
203 "tee-env-bool":true,
204 "tee-image-reference":"docker.io/library/hello-world:latest"
205 }`,
206 },
207 {
208 "WrongRestartPolicy",
209 `{
210 "tee-image-reference":"docker.io/library/hello-world:latest",
211 "tee-restart-policy":"noway",
212 }`,
213 },
214 {
215 "WrongLogRedirectLocation",
216 `{
217 "tee-image-reference":"docker.io/library/hello-world:latest",
218 "tee-container-log-redirect":"badideas",
219 }`,
220 },
221 {
222 "Memory and Health Monitoring both specified",
223 `{
224 "tee-monitoring-memory-enable":"false",
225 "tee-monitoring-health-enable":"false",
226 }`,
227 },
228 {
229 "GCA endpoint not within map",
230 `{
231 "tee-cmd":"[\"--foo\",\"--bar\",\"--baz\"]",
232 "tee-env-foo":"bar",
233 "tee-image-reference":"docker.io/library/hello-world:latest",
234 "tee-signed-image-repos":"docker.io/library/hello-world,gcr.io/cloudrun/hello",
235 "tee-restart-policy":"Always",
236 "tee-impersonate-service-accounts":"[email protected],[email protected]",
237 "tee-container-log-redirect":"true",
238 "tee-monitoring-memory-enable":"true",
239 "tee-dev-shm-size-kb":"234234",
240 "tee-mount":"type=tmpfs,source=tmpfs,destination=/tmpmount;type=tmpfs,source=tmpfs,destination=/sized,size=222",
241 "ita-region":"US",
242 "ita-api-key":"test-api-key",
243 "gca-service-env":"https://testhost.com"
244 }`,
245 },
246 {
247 "EmptyStringAsGcaEndpoint",
248 `{
249 "tee-cmd":"[\"--foo\",\"--bar\",\"--baz\"]",
250 "tee-env-foo":"bar",
251 "tee-image-reference":"docker.io/library/hello-world:latest",
252 "tee-signed-image-repos":"docker.io/library/hello-world,gcr.io/cloudrun/hello",
253 "tee-restart-policy":"Always",
254 "tee-impersonate-service-accounts":"[email protected],[email protected]",
255 "tee-container-log-redirect":"true",
256 "tee-monitoring-memory-enable":"true",
257 "tee-dev-shm-size-kb":"234234",
258 "tee-mount":"type=tmpfs,source=tmpfs,destination=/tmpmount;type=tmpfs,source=tmpfs,destination=/sized,size=222",
259 "ita-region":"US",
260 "ita-api-key":"test-api-key",
261 "gca-service-env":""
262 }`,
263 },
264 {
265 "EmptyStringAsDisableGcaRefresh",
266 `{
267 "tee-image-reference":"docker.io/library/hello-world:latest",
268 "tee-disable-gca-refresh":"badvalue"
269 }`,
270 },
271 }
272
273 for _, testcase := range testCases {
274 t.Run(testcase.testName, func(t *testing.T) {
275 spec := &LaunchSpec{}
276 if err := spec.UnmarshalJSON([]byte(testcase.mdsJSON)); err == nil {
277 t.Fatal("expected JSON parsing err")
278 }
279 })
280 }
281 }
282
283 func TestLaunchSpecUnmarshalJSONWithDefaultValue(t *testing.T) {
284 mdsJSON := `{
285 "tee-image-reference":"docker.io/library/hello-world:latest",
286 "tee-impersonate-service-accounts":"",
287 "tee-signed-image-repos":"",
288 "tee-container-log-redirect":"",
289 "tee-restart-policy":"",
290 "tee-monitoring-memory-enable":"",
291 "tee-mount":""
292 }`
293
294 spec := &LaunchSpec{}
295 if err := spec.UnmarshalJSON([]byte(mdsJSON)); err != nil {
296 t.Fatal(err)
297 }
298
299 want := &LaunchSpec{
300 ImageRef: "docker.io/library/hello-world:latest",
301 RestartPolicy: Never,
302 LogRedirect: Nowhere,
303 MonitoringEnabled: None,
304 GcaAddress: "",
305 InstallGpuDriver: false,
306 DisableGcaRefresh: false,
307 }
308
309 if !cmp.Equal(spec, want) {
310 t.Errorf("LaunchSpec UnmarshalJSON got %+v, want %+v", spec, want)
311 }
312 }
313
314 func TestLaunchSpecUnmarshalJSONWithoutImageReference(t *testing.T) {
315 mdsJSON := `{
316 "tee-cmd":"[\"--foo\",\"--bar\",\"--baz\"]",
317 "tee-env-foo":"bar",
318 "tee-restart-policy":"Never"
319 }`
320
321 spec := &LaunchSpec{}
322 if err := spec.UnmarshalJSON([]byte(mdsJSON)); err == nil || err != errImageRefNotSpecified {
323 t.Errorf("got %v error, but expected %v error", err, errImageRefNotSpecified)
324 }
325 }
326
327 func TestLaunchSpecUnmarshalJSONWithTmpfsMounts(t *testing.T) {
328 var testCases = []struct {
329 testName string
330 mdsJSON string
331 wantDst string
332 wantSz uint64
333 }{
334 {
335 "Empty Mounts",
336 `{
337 "tee-image-reference":"docker.io/library/hello-world:latest",
338 "tee-mount":""
339 }`,
340 "",
341 0,
342 },
343 {
344 "Tmpfs",
345 `{
346 "tee-image-reference":"docker.io/library/hello-world:latest",
347 "tee-mount":"type=tmpfs,source=tmpfs,destination=/tmpmount"
348 }`,
349 "/tmpmount",
350 0,
351 },
352 {
353 "Tmpfs Sized",
354 `{
355 "tee-image-reference":"docker.io/library/hello-world:latest",
356 "tee-mount":"type=tmpfs,source=tmpfs,destination=/tmpmount,size=78987"
357 }`,
358 "/tmpmount",
359 78987,
360 },
361 }
362 for _, testcase := range testCases {
363 t.Run(testcase.testName, func(t *testing.T) {
364 spec := &LaunchSpec{}
365 if err := spec.UnmarshalJSON([]byte(testcase.mdsJSON)); err != nil {
366 t.Errorf("got %v error, but expected nil error", err)
367 }
368 })
369 }
370 }
371
372 func TestLaunchSpecUnmarshalJSONWithBadMounts(t *testing.T) {
373 var testCases = []struct {
374 testName string
375 mdsJSON string
376 errMatch string
377 }{
378 {
379 "Unknown Type",
380 `{
381 "tee-image-reference":"docker.io/library/hello-world:latest",
382 "tee-mount":"type=hallo"
383 }`,
384 "found unknown or unspecified mount type",
385 },
386 {
387 "Not k=v",
388 `{
389 "tee-image-reference":"docker.io/library/hello-world:latest",
390 "tee-mount":"type=tmpfs,source"
391 }`,
392 "failed to parse mount option",
393 },
394 {
395 "Unknown Option",
396 `{
397 "tee-image-reference":"docker.io/library/hello-world:latest",
398 "tee-mount":"type=tmpfs,source=tmpfs,destination=/tmpmount,size=123,foo=bar"
399 }`,
400 "found unknown mount option",
401 },
402 {
403 "Tmpfs Bad Source",
404 `{
405 "tee-image-reference":"docker.io/library/hello-world:latest",
406 "tee-mount":"type=tmpfs,source=src,destination=/tmpmount"
407 }`,
408 "received wrong mount source",
409 },
410 {
411 "Tmpfs No Destination",
412 `{
413 "tee-image-reference":"docker.io/library/hello-world:latest",
414 "tee-mount":"type=tmpfs,source=tmpfs"
415 }`,
416 "mount type \"tmpfs\" must have destination specified",
417 },
418 {
419 "Tmpfs Size Not Int",
420 `{
421 "tee-image-reference":"docker.io/library/hello-world:latest",
422 "tee-mount":"type=tmpfs,source=tmpfs,destination=/tmpmount,size=foo"
423 }`,
424 "failed to convert size option",
425 },
426 }
427 for _, testcase := range testCases {
428 t.Run(testcase.testName, func(t *testing.T) {
429 spec := &LaunchSpec{}
430 err := spec.UnmarshalJSON([]byte(testcase.mdsJSON))
431 if match, _ := regexp.MatchString(testcase.errMatch, err.Error()); !match {
432 t.Errorf("got %v error, but expected %v error", err, testcase.errMatch)
433 }
434 })
435 }
436 }