Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions internal/provider/cloudinit_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,15 @@ func (c *configModel) update(ctx context.Context) diag.Diagnostics {
return diags
}

func is7bit(s string) bool {
for i := 0; i < len(s); i++ {
if s[i] > 0x7F {
return false
}
}
return true
}

func renderPartsToWriter(ctx context.Context, mimeBoundary string, parts []configPartModel, writer io.Writer) error {
mimeWriter := multipart.NewWriter(writer)
defer func() {
Expand All @@ -173,11 +182,19 @@ func renderPartsToWriter(ctx context.Context, mimeBoundary string, parts []confi
}

for _, part := range parts {
encode_to_base64 := !is7bit(part.Content.ValueString())
var cte string
if encode_to_base64 {
cte = "base64"
} else {
cte = "7bit"
}

header := textproto.MIMEHeader{}

header.Set("Content-Type", part.ContentType.ValueString())
header.Set("MIME-Version", "1.0")
header.Set("Content-Transfer-Encoding", "7bit")
header.Set("Content-Transfer-Encoding", cte)

if part.FileName.ValueString() != "" {
header.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, part.FileName.ValueString()))
Expand All @@ -187,12 +204,20 @@ func renderPartsToWriter(ctx context.Context, mimeBoundary string, parts []confi
header.Set("X-Merge-Type", part.MergeType.ValueString())
}

var err error

partWriter, err := mimeWriter.CreatePart(header)
if err != nil {
return err
}

_, err = partWriter.Write([]byte(part.Content.ValueString()))
if encode_to_base64 {
base64Writer := base64.NewEncoder(base64.StdEncoding, partWriter)
defer base64Writer.Close()
_, err = base64Writer.Write([]byte(part.Content.ValueString()))
} else {
_, err = partWriter.Write([]byte(part.Content.ValueString()))
}
if err != nil {
return err
}
Expand Down
13 changes: 13 additions & 0 deletions internal/provider/data_source_cloudinit_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ func TestConfigDataSourceRender(t *testing.T) {
}`,
"Content-Type: multipart/mixed; boundary=\"MIMEBOUNDARY\"\nMIME-Version: 1.0\r\n\r\n--MIMEBOUNDARY\r\nContent-Transfer-Encoding: 7bit\r\nContent-Type: text/x-shellscript\r\nMime-Version: 1.0\r\n\r\nbaz\r\n--MIMEBOUNDARY--\r\n",
},
{
"no gzip or b64 - non-ASCII content",
`data "cloudinit_config" "foo" {
gzip = false
base64_encode = false

part {
content_type = "text/cloud-config"
content = "¡Hola mundo!"
}
}`,
"Content-Type: multipart/mixed; boundary=\"MIMEBOUNDARY\"\nMIME-Version: 1.0\r\n\r\n--MIMEBOUNDARY\r\nContent-Transfer-Encoding: base64\r\nContent-Type: text/cloud-config\r\nMime-Version: 1.0\r\n\r\nwqFIb2xhIG11bmRvIQ==\r\n--MIMEBOUNDARY--\r\n",
},
{
"no gzip or b64 - basic content - default to text plain",
`data "cloudinit_config" "foo" {
Expand Down
13 changes: 13 additions & 0 deletions internal/provider/resource_cloudinit_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ func TestConfigResourceRender(t *testing.T) {
}`,
"Content-Type: multipart/mixed; boundary=\"MIMEBOUNDARY\"\nMIME-Version: 1.0\r\n\r\n--MIMEBOUNDARY\r\nContent-Transfer-Encoding: 7bit\r\nContent-Type: text/x-shellscript\r\nMime-Version: 1.0\r\n\r\nbaz\r\n--MIMEBOUNDARY--\r\n",
},
{
"no gzip or b64 - non-ASCII content",
`resource "cloudinit_config" "foo" {
gzip = false
base64_encode = false

part {
content_type = "text/cloud-config"
content = "¡Hola mundo!"
}
}`,
"Content-Type: multipart/mixed; boundary=\"MIMEBOUNDARY\"\nMIME-Version: 1.0\r\n\r\n--MIMEBOUNDARY\r\nContent-Transfer-Encoding: base64\r\nContent-Type: text/cloud-config\r\nMime-Version: 1.0\r\n\r\nwqFIb2xhIG11bmRvIQ==\r\n--MIMEBOUNDARY--\r\n",
},
{
"no gzip or b64 - basic content - default to text plain",
`resource "cloudinit_config" "foo" {
Expand Down