Skip to content

Commit

Permalink
Resolves #522 set Created date to time of execution (#2108)
Browse files Browse the repository at this point in the history
* #522 set Created date to time of execution

Signed-off-by: Tobias Trabelsi <lerentis@uploadfilter24.eu>

* #522 added check to only sign if previous date was zero and added date change to files as well

Signed-off-by: Tobias Trabelsi <tobias.trabelsi@dbschenker.com>

* added unit tests and removed log entry

Signed-off-by: Tobias Trabelsi <tobias.trabelsi@dbschenker.com>

* #522 review feedback

Signed-off-by: Tobias Trabelsi <tobias.trabelsi@dbschenker.com>

* #522 Apply suggestions from code review

Co-authored-by: Jason Hall <jason@chainguard.dev>
Signed-off-by: Tobias Trabelsi <tobias.trabelsi@dbschenker.com>

* #522 additional nil check

Signed-off-by: Tobias Trabelsi <tobias.trabelsi@dbschenker.com>

Co-authored-by: Tobias Trabelsi <tobias.trabelsi@dbschenker.com>
Co-authored-by: Jason Hall <jason@chainguard.dev>
  • Loading branch information
3 people authored Jul 29, 2022
1 parent 98396dd commit ae99f7b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
10 changes: 10 additions & 0 deletions pkg/oci/mutate/signatures.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package mutate

import (
"time"

v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/empty"
"github.com/google/go-containerregistry/pkg/v1/mutate"
Expand All @@ -36,10 +38,18 @@ func AppendSignatures(base oci.Signatures, sigs ...oci.Signature) (oci.Signature
Annotations: ann,
})
}

img, err := mutate.Append(base, adds...)
if err != nil {
return nil, err
}

// Set the Created date to time of execution
img, err = mutate.CreatedAt(img, v1.Time{Time: time.Now()})
if err != nil {
return nil, err
}

return &sigAppender{
Image: img,
base: base,
Expand Down
6 changes: 6 additions & 0 deletions pkg/oci/mutate/signatures_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,10 @@ func TestAppendSignatures(t *testing.T) {
} else if got, want := len(sl), 3; got != want {
t.Errorf("len(Get()) = %d, wanted %d", got, want)
}

if testCfg, err := threeSig.ConfigFile(); err != nil {
t.Fatalf("ConfigFile() = %v", err)
} else if testCfg.Created.Time.IsZero() {
t.Errorf("Date of Signature was Zero")
}
}
7 changes: 7 additions & 0 deletions pkg/oci/static/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package static

import (
"io"
"time"

v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/empty"
Expand Down Expand Up @@ -44,6 +45,12 @@ func NewFile(payload []byte, opts ...Option) (oci.File, error) {
if err != nil {
return nil, err
}

// Set the Created date to time of execution
img, err = mutate.CreatedAt(img, v1.Time{Time: time.Now()})
if err != nil {
return nil, err
}
return &file{
SignedImage: signed.Image(img),
layer: layer,
Expand Down
10 changes: 10 additions & 0 deletions pkg/oci/static/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,14 @@ func TestNewFile(t *testing.T) {
t.Errorf("Payload() = %s, wanted %s", got, want)
}
})

t.Run("check date", func(t *testing.T) {
fileCfg, err := file.ConfigFile()
if err != nil {
t.Fatalf("ConfigFile() = %v", err)
}
if fileCfg.Created.Time.IsZero() {
t.Errorf("Date of Signature was Zero")
}
})
}

0 comments on commit ae99f7b

Please sign in to comment.