Repository: google/go-containerregistry
Pull request #693 (2020-03-12 08:20:00)

Canonical should keep OS/ARCH
These fields should be considered in canonical image. Upstream of a kaniko PR (tbd)

googlebot: 
Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

:memo: **Please visit <https://cla.developers.google.com/> to sign.**

Once you've signed (or fixed any issues), please reply here with `@googlebot I signed it!` and we'll verify it.

----

#### What to do if you already signed the CLA

##### Individual signers

*   It's possible we don't have your GitHub username or you're using a different email address on your commit. Check [your existing CLA data](https://cla.developers.google.com/clas) and verify that your [email is set on your git commits](https://help.github.com/articles/setting-your-email-in-git/).

##### Corporate signers

*   Your company has a Point of Contact who decides which employees are authorized to participate. Ask your POC to be added to the group of authorized contributors. If you don't know who your Point of Contact is, direct the Google project maintainer to [go/cla#troubleshoot](http://go/cla#troubleshoot) ([Public version](https://opensource.google/docs/cla/#troubleshoot)).
*   The email used to register you as an authorized contributor must be the email used for the Git commit. Check [your existing CLA data](https://cla.developers.google.com/clas) and verify that your [email is set on your git commits](https://help.github.com/articles/setting-your-email-in-git/).
*   The email used to register you as an authorized contributor must also be [attached to your GitHub account](https://github.com/settings/emails).
		

ℹ️ **Googlers: [Go here](https://goto.google.com/prinfo/https%3A%2F%2Fgithub.com%2Fgoogle%2Fgo-containerregistry%2Fpull%2F693) for more info**.

<!-- need_sender_cla -->
codecov-io: # [Codecov](https://codecov.io/gh/google/go-containerregistry/pull/693?src=pr&el=h1) Report
> Merging [#693](https://codecov.io/gh/google/go-containerregistry/pull/693?src=pr&el=desc) into [master](https://codecov.io/gh/google/go-containerregistry/commit/4fe717a9b4cb5f6f1ece33702a93c284e0f37ed4?src=pr&el=desc) will **increase** coverage by `0.01%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/google/go-containerregistry/pull/693/graphs/tree.svg?width=650&token=wG7iiDCdZ4&height=150&src=pr)](https://codecov.io/gh/google/go-containerregistry/pull/693?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #693      +/-   ##
==========================================
+ Coverage   79.72%   79.73%   +0.01%     
==========================================
  Files         102      102              
  Lines        4532     4535       +3     
==========================================
+ Hits         3613     3616       +3     
  Misses        505      505              
  Partials      414      414
```


| [Impacted Files](https://codecov.io/gh/google/go-containerregistry/pull/693?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pkg/v1/mutate/mutate.go](https://codecov.io/gh/google/go-containerregistry/pull/693/diff?src=pr&el=tree#diff-cGtnL3YxL211dGF0ZS9tdXRhdGUuZ28=) | `75% <100%> (+0.43%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/google/go-containerregistry/pull/693?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/google/go-containerregistry/pull/693?src=pr&el=footer). Last update [4fe717a...328ed56](https://codecov.io/gh/google/go-containerregistry/pull/693?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

googlebot: CLAs look good, thanks!

ℹ️ **Googlers: [Go here](https://goto.google.com/prinfo/https%3A%2F%2Fgithub.com%2Fgoogle%2Fgo-containerregistry%2Fpull%2F693) for more info**.

<!-- ok -->

Patch:
diff --git a/pkg/v1/mutate/mutate.go b/pkg/v1/mutate/mutate.go
index 106f5a65..cbb1bd80 100644
--- a/pkg/v1/mutate/mutate.go
+++ b/pkg/v1/mutate/mutate.go
@@ -279,6 +279,9 @@ func Time(img v1.Image, t time.Time) (v1.Image, error) {
 	cfg := cf.DeepCopy()
 
 	// Copy basic config over
+	cfg.Architecture = ocf.Architecture
+	cfg.OS = ocf.OS
+	cfg.OSVersion = ocf.OSVersion
 	cfg.Config = ocf.Config
 
 	// Strip away timestamps from the config file
diff --git a/pkg/v1/remote/transport/bearer.go b/pkg/v1/remote/transport/bearer.go
index c31a609c..f53eb178 100644
--- a/pkg/v1/remote/transport/bearer.go
+++ b/pkg/v1/remote/transport/bearer.go
@@ -92,6 +92,12 @@ func (bt *bearerTransport) refresh() error {
 	if err != nil {
 		return err
 	}
+
+	if auth.RegistryToken != "" {
+		bt.bearer.RegistryToken = auth.RegistryToken
+		return nil
+	}
+
 	var content []byte
 	if auth.IdentityToken != "" {
 		// If the secret being stored is an identity token,


Test patch:
diff --git a/pkg/v1/mutate/mutate_test.go b/pkg/v1/mutate/mutate_test.go
index 16a4fe85..ac050f51 100644
--- a/pkg/v1/mutate/mutate_test.go
+++ b/pkg/v1/mutate/mutate_test.go
@@ -440,11 +440,30 @@ func TestCanonical(t *testing.T) {
 	if err != nil {
 		t.Fatal(err)
 	}
+	sourceCf, err := source.ConfigFile()
+	if err != nil {
+		t.Fatal(err)
+	}
 	cf, err := img.ConfigFile()
 	if err != nil {
 		t.Fatal(err)
 	}
-
+	var want, got string
+	want = cf.Architecture
+	got = sourceCf.Architecture
+	if want != got {
+		t.Errorf("%q != %q", want, got)
+	}
+	want = cf.OS
+	got = sourceCf.OS
+	if want != got {
+		t.Errorf("%q != %q", want, got)
+	}
+	want = cf.OSVersion
+	got = sourceCf.OSVersion
+	if want != got {
+		t.Errorf("%q != %q", want, got)
+	}
 	for _, s := range []string{
 		cf.Container,
 		cf.Config.Hostname,
diff --git a/pkg/v1/remote/transport/bearer_test.go b/pkg/v1/remote/transport/bearer_test.go
index 3a3540b2..9a86f160 100644
--- a/pkg/v1/remote/transport/bearer_test.go
+++ b/pkg/v1/remote/transport/bearer_test.go
@@ -171,6 +171,7 @@ func TestBearerTransportTokenRefresh(t *testing.T) {
 		t.Fatalf("Unexpected error during NewRegistry: %v", err)
 	}
 
+	// Pass Username/Password
 	transport := &bearerTransport{
 		inner:    http.DefaultTransport,
 		bearer:   authn.AuthConfig{RegistryToken: initialToken},
@@ -184,6 +185,24 @@ func TestBearerTransportTokenRefresh(t *testing.T) {
 	res, err := client.Get(fmt.Sprintf("http://%s/v2/foo/bar/blobs/blah", u.Host))
 	if err != nil {
 		t.Errorf("Unexpected error during client.Get: %v", err)
+		return
+	}
+	if res.StatusCode != http.StatusOK {
+		t.Errorf("client.Get final StatusCode got %v, want: %v", res.StatusCode, http.StatusOK)
+	}
+	if got, want := transport.bearer.RegistryToken, refreshedToken; got != want {
+		t.Errorf("Expected Bearer token to be refreshed, got %v, want %v", got, want)
+	}
+
+	// Pass RegistryToken directly
+	transport.bearer = authn.AuthConfig{RegistryToken: initialToken}
+	transport.basic = &authn.Bearer{Token: refreshedToken}
+	client = http.Client{Transport: transport}
+
+	res, err = client.Get(fmt.Sprintf("http://%s/v2/foo/bar/blobs/blah", u.Host))
+	if err != nil {
+		t.Errorf("Unexpected error during client.Get: %v", err)
+		return
 	}
 	if res.StatusCode != http.StatusOK {
 		t.Errorf("client.Get final StatusCode got %v, want: %v", res.StatusCode, http.StatusOK)