Skip to content

Commit dd1a900

Browse files
authored
oci_append: produce valid layer and validate (#120)
Signed-off-by: Jason Hall <[email protected]>
1 parent 75be17a commit dd1a900

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

internal/provider/append_resource.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ package provider
33
import (
44
"archive/tar"
55
"bytes"
6+
"compress/gzip"
67
"context"
78
"fmt"
89

910
"github.com/google/go-containerregistry/pkg/name"
1011
v1 "github.com/google/go-containerregistry/pkg/v1"
1112
"github.com/google/go-containerregistry/pkg/v1/mutate"
1213
"github.com/google/go-containerregistry/pkg/v1/remote"
13-
"github.com/google/go-containerregistry/pkg/v1/static"
14+
"github.com/google/go-containerregistry/pkg/v1/tarball"
1415
ggcrtypes "github.com/google/go-containerregistry/pkg/v1/types"
1516
"github.com/hashicorp/terraform-plugin-framework/diag"
1617
"github.com/hashicorp/terraform-plugin-framework/path"
@@ -214,11 +215,13 @@ func (r *AppendResource) doAppend(ctx context.Context, data *AppendResourceModel
214215
adds := []mutate.Addendum{}
215216
for _, l := range ls {
216217
var b bytes.Buffer
217-
tw := tar.NewWriter(&b)
218+
zw := gzip.NewWriter(&b)
219+
tw := tar.NewWriter(zw)
218220
for name, f := range l.Files {
219221
if err := tw.WriteHeader(&tar.Header{
220222
Name: name,
221223
Size: int64(len(f.Contents.ValueString())),
224+
Mode: 0644,
222225
}); err != nil {
223226
return nil, []diag.Diagnostic{diag.NewErrorDiagnostic("Unable to write tar header", fmt.Sprintf("Unable to write tar header for %q, got error: %s", name, err))}
224227
}
@@ -229,9 +232,17 @@ func (r *AppendResource) doAppend(ctx context.Context, data *AppendResourceModel
229232
if err := tw.Close(); err != nil {
230233
return nil, []diag.Diagnostic{diag.NewErrorDiagnostic("Unable to close tar writer", fmt.Sprintf("Unable to close tar writer, got error: %s", err))}
231234
}
235+
if err := zw.Close(); err != nil {
236+
return nil, []diag.Diagnostic{diag.NewErrorDiagnostic("Unable to close gzip writer", fmt.Sprintf("Unable to close gzip writer, got error: %s", err))}
237+
}
238+
239+
l, err := tarball.LayerFromReader(&b)
240+
if err != nil {
241+
return nil, []diag.Diagnostic{diag.NewErrorDiagnostic("Unable to create layer", fmt.Sprintf("Unable to create layer, got error: %s", err))}
242+
}
232243

233244
adds = append(adds, mutate.Addendum{
234-
Layer: static.NewLayer(b.Bytes(), ggcrtypes.OCILayer),
245+
Layer: l,
235246
History: v1.History{CreatedBy: "terraform-provider-oci: oci_append"},
236247
MediaType: ggcrtypes.OCILayer,
237248
})

internal/provider/append_resource_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ import (
66
"testing"
77

88
ocitesting "github.com/chainguard-dev/terraform-provider-oci/testing"
9+
"github.com/google/go-containerregistry/pkg/crane"
910
"github.com/google/go-containerregistry/pkg/v1/random"
1011
"github.com/google/go-containerregistry/pkg/v1/remote"
12+
"github.com/google/go-containerregistry/pkg/v1/validate"
1113
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
14+
"github.com/hashicorp/terraform-plugin-testing/terraform"
1215
)
1316

1417
func TestAccAppendResource(t *testing.T) {
@@ -55,6 +58,17 @@ func TestAccAppendResource(t *testing.T) {
5558
resource.TestCheckResourceAttr("oci_append.test", "base_image", ref1.String()),
5659
resource.TestMatchResourceAttr("oci_append.test", "image_ref", regexp.MustCompile(`/test@sha256:[0-9a-f]{64}$`)),
5760
resource.TestMatchResourceAttr("oci_append.test", "id", regexp.MustCompile(`/test@sha256:[0-9a-f]{64}$`)),
61+
resource.TestCheckFunc(func(s *terraform.State) error {
62+
rs := s.RootModule().Resources["oci_append.test"]
63+
img, err := crane.Pull(rs.Primary.Attributes["image_ref"])
64+
if err != nil {
65+
return fmt.Errorf("failed to pull image: %v", err)
66+
}
67+
if err := validate.Image(img); err != nil {
68+
return fmt.Errorf("failed to validate image: %v", err)
69+
}
70+
return nil
71+
}),
5872
),
5973
},
6074
// Update and Read testing

0 commit comments

Comments
 (0)