Skip to content

Commit d73926c

Browse files
committed
Remove empty ca_certs from cloud-config.yaml
Since it is a pointer, it is always true (non-nil). And structs are always true, so they can't be tested. Signed-off-by: Anders F Björklund <[email protected]>
1 parent ad2ec08 commit d73926c

File tree

5 files changed

+30
-3
lines changed

5 files changed

+30
-3
lines changed

pkg/cidata/cidata.TEMPLATE.d/user-data

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,20 @@ resolv_conf:
6666
{{- end }}
6767
{{- end }}
6868

69+
{{- if or .CACerts.RemoveDefaults .CACerts.Trusted }}
6970
{{ with .CACerts }}
7071
ca_certs:
72+
{{- if .RemoveDefaults }}
7173
remove_defaults: {{ .RemoveDefaults }}
74+
{{- end }}
7275
{{- if .Trusted}}
7376
trusted:
7477
{{- range $cert := .Trusted }}
7578
- |
7679
{{- range $line := $cert.Lines }}
7780
{{ $line }}
7881
{{- end }}
82+
{{- end }}
7983
{{- end }}
8084
{{- end }}
8185
{{- end }}

pkg/cidata/cidata.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,12 @@ func templateArgs(instDir, name string, instConfig *limayaml.LimaYAML, udpDNSLoc
311311
args.CACerts.Trusted = append(args.CACerts.Trusted, cert)
312312
}
313313

314+
// Remove empty caCerts (default values) from configuration yaml
315+
if !*args.CACerts.RemoveDefaults && len(args.CACerts.Trusted) == 0 {
316+
args.CACerts.RemoveDefaults = nil
317+
args.CACerts.Trusted = nil
318+
}
319+
314320
args.BootCmds = getBootCmds(instConfig.Provision)
315321

316322
for _, f := range instConfig.Provision {

pkg/cidata/cloud-config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ users:
3737
# resolv_conf is not included here
3838
{{- end }}
3939

40+
{{- if .CACerts.RemoveDefaults }}
4041
{{ with .CACerts }}
4142
ca_certs:
4243
remove_defaults: {{ .RemoveDefaults }}
@@ -50,6 +51,7 @@ ca_certs:
5051
{{- end }}
5152
{{- end }}
5253
{{- end }}
54+
{{- end }}
5355

5456
{{- if .BootCmds }}
5557
bootcmd:

pkg/cidata/template.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,6 @@ func ValidateTemplateArgs(args *TemplateArgs) error {
117117
return fmt.Errorf("field mounts[%d] must be absolute, got %q", i, f)
118118
}
119119
}
120-
if args.CACerts.RemoveDefaults == nil {
121-
return errors.New("field CACerts.RemoveDefaults must be set")
122-
}
123120
return nil
124121
}
125122

pkg/cidata/template_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,23 @@ import (
1111
var defaultRemoveDefaults = false
1212

1313
func TestConfig(t *testing.T) {
14+
args := &TemplateArgs{
15+
Name: "default",
16+
User: "foo",
17+
UID: 501,
18+
Home: "/home/foo.linux",
19+
SSHPubKeys: []string{
20+
"ssh-rsa dummy [email protected]",
21+
},
22+
MountType: "reverse-sshfs",
23+
}
24+
config, err := ExecuteTemplateCloudConfig(args)
25+
assert.NilError(t, err)
26+
t.Log(string(config))
27+
assert.Assert(t, !strings.Contains(string(config), "ca_certs:"))
28+
}
29+
30+
func TestConfigCACerts(t *testing.T) {
1431
args := &TemplateArgs{
1532
Name: "default",
1633
User: "foo",
@@ -27,6 +44,7 @@ func TestConfig(t *testing.T) {
2744
config, err := ExecuteTemplateCloudConfig(args)
2845
assert.NilError(t, err)
2946
t.Log(string(config))
47+
assert.Assert(t, strings.Contains(string(config), "ca_certs:"))
3048
}
3149

3250
func TestTemplate(t *testing.T) {

0 commit comments

Comments
 (0)