Skip to content

Commit 7cc7f51

Browse files
authored
Merge pull request #5 from garethjevans/fix-for-sha
chore: fix client when querying for sha
2 parents 02290cd + 548934c commit 7cc7f51

File tree

3 files changed

+73
-44
lines changed

3 files changed

+73
-44
lines changed

pkg/inspect/client.go

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ package inspect
22

33
import (
44
"encoding/json"
5+
"errors"
56
"fmt"
67
"io"
78
"io/ioutil"
89
"net/http"
9-
"strings"
1010

1111
"github.com/sirupsen/logrus"
1212
)
@@ -30,6 +30,7 @@ func (i *Client) token(repo string) (string, error) {
3030
if err != nil {
3131
return "", err
3232
}
33+
logrus.Debugf("got status code %d", resp.StatusCode)
3334

3435
tokenResponse := TokenResponse{}
3536

@@ -56,57 +57,63 @@ func (i *Client) Labels(repo string, version string) (map[string]string, error)
5657

5758
var digest string
5859

59-
if strings.HasPrefix(version, "sha256:") {
60-
digest = version
61-
} else {
62-
req, err := http.NewRequest("GET", fmt.Sprintf("https://registry-1.docker.io/v2/%s/manifests/%s", repo, version), nil)
63-
if err != nil {
64-
return nil, err
65-
}
66-
67-
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
68-
req.Header.Add("Accept", "application/vnd.docker.distribution.manifest.v2+json")
69-
70-
resp, err := i.Client.Do(req)
71-
if err != nil {
72-
return nil, err
73-
}
74-
75-
body, err := ioutil.ReadAll(io.LimitReader(resp.Body, 10000000))
76-
if err != nil {
77-
return nil, err
78-
}
79-
80-
logrus.Debugf("body> %s", string(body))
81-
82-
manifestResponse := ManifestResponse{}
83-
err = json.Unmarshal(body, &manifestResponse)
84-
if err != nil {
85-
return nil, err
86-
}
87-
88-
logrus.Debugf("got digest %s", manifestResponse.Config.Digest)
89-
digest = manifestResponse.Config.Digest
90-
}
91-
92-
req, err := http.NewRequest("GET", fmt.Sprintf("https://registry-1.docker.io/v2/%s/blobs/%s", repo, digest), nil)
60+
req, err := http.NewRequest("GET", fmt.Sprintf("https://registry-1.docker.io/v2/%s/manifests/%s", repo, version), nil)
9361
if err != nil {
9462
return nil, err
9563
}
64+
9665
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
66+
req.Header.Add("Accept", "application/vnd.docker.distribution.manifest.v2+json")
9767

9868
resp, err := i.Client.Do(req)
9969
if err != nil {
10070
return nil, err
10171
}
10272

73+
logrus.Debugf("got status code %d", resp.StatusCode)
74+
75+
if resp.StatusCode != 200 {
76+
return nil, errors.New("unable to find manifest " + version + ", " + resp.Status)
77+
}
78+
10379
body, err := ioutil.ReadAll(io.LimitReader(resp.Body, 10000000))
10480
if err != nil {
10581
return nil, err
10682
}
10783

10884
logrus.Debugf("body> %s", string(body))
10985

86+
manifestResponse := ManifestResponse{}
87+
err = json.Unmarshal(body, &manifestResponse)
88+
if err != nil {
89+
return nil, err
90+
}
91+
92+
logrus.Debugf("got digest %s", manifestResponse.Config.Digest)
93+
digest = manifestResponse.Config.Digest
94+
95+
req, err = http.NewRequest("GET", fmt.Sprintf("https://registry-1.docker.io/v2/%s/blobs/%s", repo, digest), nil)
96+
if err != nil {
97+
return nil, err
98+
}
99+
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
100+
101+
resp, err = i.Client.Do(req)
102+
if err != nil {
103+
return nil, err
104+
}
105+
logrus.Debugf("got status code %d", resp.StatusCode)
106+
if resp.StatusCode != 200 {
107+
return nil, errors.New("unable to find blob " + digest + ", " + resp.Status)
108+
}
109+
110+
body, err = ioutil.ReadAll(io.LimitReader(resp.Body, 10000000))
111+
if err != nil {
112+
return nil, err
113+
}
114+
115+
logrus.Debugf("body> %s", string(body))
116+
110117
blobResponse := BlobResponse{}
111118
err = json.Unmarshal(body, &blobResponse)
112119
if err != nil {

pkg/inspect/client_test.go

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ import (
1414
"github.com/stretchr/testify/assert"
1515
)
1616

17+
var (
18+
repo = "jenkinsciinfra/terraform"
19+
)
20+
1721
func TestCanQueryDockerManifest(t *testing.T) {
1822
// get token
19-
repo := "jenkinsciinfra/terraform"
2023
tag := "latest"
2124

2225
mock := &mocks.MockClient{}
@@ -25,9 +28,9 @@ func TestCanQueryDockerManifest(t *testing.T) {
2528
Client: mock,
2629
}
2730

28-
stubWithFixture(t, mock, "token.json")
29-
stubWithFixture(t, mock, "manifest.json")
30-
stubWithFixture(t, mock, "blobs.json")
31+
stubWithFixture(t, mock, 200, "token.json")
32+
stubWithFixture(t, mock, 200, "manifest.json")
33+
stubWithFixture(t, mock, 200, "blobs.json")
3134

3235
l, err := client.Labels(repo, tag)
3336
assert.NoError(t, err)
@@ -47,7 +50,6 @@ func TestCanQueryDockerManifest(t *testing.T) {
4750

4851
func TestCanQueryDockerManifest_UsingSha(t *testing.T) {
4952
// get token
50-
repo := "jenkinsciinfra/terraform"
5153
tag := "sha256:71e99d70bef50da077f0595044f3330b074e2d0417e486a12144d67cb5dd5603"
5254

5355
mock := &mocks.MockClient{}
@@ -56,8 +58,9 @@ func TestCanQueryDockerManifest_UsingSha(t *testing.T) {
5658
Client: mock,
5759
}
5860

59-
stubWithFixture(t, mock, "token.json")
60-
stubWithFixture(t, mock, "blobs.json")
61+
stubWithFixture(t, mock, 200, "token.json")
62+
stubWithFixture(t, mock, 200, "manifest.json")
63+
stubWithFixture(t, mock, 200, "blobs.json")
6164

6265
l, err := client.Labels(repo, tag)
6366
assert.NoError(t, err)
@@ -75,12 +78,30 @@ func TestCanQueryDockerManifest_UsingSha(t *testing.T) {
7578
assert.Equal(t, "https://github.com/jenkins-infra/docker-terraform/tree/d25f040", fullGitURL)
7679
}
7780

78-
func stubWithFixture(t *testing.T, mock *mocks.MockClient, file string) {
81+
func TestCanQueryDockerManifest_UsingUnknownSha(t *testing.T) {
82+
// get token
83+
tag := "sha256:71e99d70bef50da077f0595044f3330b074e2d0417e486a12144d67cb5dd5603XXX"
84+
85+
mock := &mocks.MockClient{}
86+
87+
client := inspect.Client{
88+
Client: mock,
89+
}
90+
91+
stubWithFixture(t, mock, 200, "token.json")
92+
stubWithFixture(t, mock, 200, "manifest.json")
93+
stubWithFixture(t, mock, 404, "unknown-blob.json")
94+
95+
_, err := client.Labels(repo, tag)
96+
assert.Error(t, err)
97+
}
98+
99+
func stubWithFixture(t *testing.T, mock *mocks.MockClient, responseCode int, file string) {
79100
data, err := ioutil.ReadFile(path.Join("testdata", file))
80101
assert.NoError(t, err)
81102

82103
// create a new reader with that JSON
83104
r := ioutil.NopCloser(bytes.NewReader(data))
84105

85-
mock.StubResponse(200, r)
106+
mock.StubResponse(responseCode, r)
86107
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"errors":[{"code":"BLOB_UNKNOWN","message":"blob unknown to registry","detail":"sha256:795a31b4c2e0884e469ee9fd1ff7c81bd59404bc95944fa0767cf912a80be79a"}]}

0 commit comments

Comments
 (0)