Skip to content

Commit

Permalink
Address deprecations and dead code (#71)
Browse files Browse the repository at this point in the history
- `ioutil` has been deprecated since Go 1.16, so we can use the
replacements.
- `slices.Sort` is simpler and faster than using the `sort` package,
and now availabel with Go 1.21.
- Found an unused functions that can be dropped.

Change-Id: If44cceaa2bbba10535cd5c663de4615823d86ba5
  • Loading branch information
miguelbernadi authored Jan 18, 2024
1 parent f9a9737 commit d6f9a1b
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 45 deletions.
3 changes: 1 addition & 2 deletions pkg/arch/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"net/http"
"slices"
"sort"
"strings"
"time"

Expand Down Expand Up @@ -633,7 +632,7 @@ func keys(set map[string]struct{}) []string {
for k := range set {
r = append(r, k)
}
sort.Sort(sort.StringSlice(r))
slices.Sort(r)
return r
}

Expand Down
36 changes: 0 additions & 36 deletions pkg/registry/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -228,41 +227,6 @@ func (r RegistryAuthenticator) getAuthCandidates(ctx context.Context, cfg Docker
return candidates
}

// TODO: change to handle www-authenticate for HTTP/1.1 401 Unauthorized responses
// Www-Authenticate: Bearer realm="https://ghcr.io/token",service="ghcr.io",scope="repository:user/image:pull"
// Www-Authenticate: Bearer realm="https://auth.ipv6.docker.com/token",service="registry.docker.io"
func getDockerHubToken(ctx context.Context, registry, image, tag, token string) string {
authHost := registry
if authHost == "docker.io" {
authHost = "auth.docker.io"
registry = "registry." + registry
}

req, err := http.NewRequest("GET", fmt.Sprintf("https://%s/token?service=%s&scope=repository:%s:pull", authHost, registry, image), nil)
if err != nil {
return token
}
req = req.WithContext(ctx)
if token != "" {
req.Header.Set("Authorization", "Basic "+token)
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return ""
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return ""
}
authResponse := registryAuthResponse{}
err = json.NewDecoder(resp.Body).Decode(&authResponse)
if err != nil {
return ""
}
return authResponse.Token
}

func (r RegistryAuthenticator) tryAllCandidates(ctx context.Context, cfg DockerConfig, registry, image, tag string, candidates chan AuthenticationToken) {
for auth := range r.getAuthCandidates(ctx, cfg, registry, image) {
select {
Expand Down
14 changes: 7 additions & 7 deletions pkg/registry/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package registry
import (
"context"
"encoding/base64"
"io/ioutil"
"io"
"net/http"
"strings"
"testing"
Expand Down Expand Up @@ -174,7 +174,7 @@ func TestListArchsWithAuthenticationAndManifestListV2(t *testing.T) {
assert.Equal(t, "repository:my/image:pull", req.URL.Query().Get("scope"))
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(strings.NewReader(`{"token":"my-token"}`)),
Body: io.NopCloser(strings.NewReader(`{"token":"my-token"}`)),
}, nil
case "registry.company.corp":
switch req.URL.Path {
Expand All @@ -184,7 +184,7 @@ func TestListArchsWithAuthenticationAndManifestListV2(t *testing.T) {
return &http.Response{
StatusCode: http.StatusOK,
Header: headers,
Body: ioutil.NopCloser(strings.NewReader(`{
Body: io.NopCloser(strings.NewReader(`{
"manifests":[
{
"platform":{
Expand Down Expand Up @@ -249,7 +249,7 @@ func TestListArchsWithAuthenticationAndManifestIndexV1(t *testing.T) {
assert.Equal(t, "repository:my/image:pull", req.URL.Query().Get("scope"))
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(strings.NewReader(`{"token":"my-token"}`)),
Body: io.NopCloser(strings.NewReader(`{"token":"my-token"}`)),
}, nil
case "registry.company.corp":
switch req.URL.Path {
Expand All @@ -259,7 +259,7 @@ func TestListArchsWithAuthenticationAndManifestIndexV1(t *testing.T) {
return &http.Response{
StatusCode: http.StatusOK,
Header: headers,
Body: ioutil.NopCloser(strings.NewReader(`{
Body: io.NopCloser(strings.NewReader(`{
"manifests":[
{
"platform":{
Expand Down Expand Up @@ -329,7 +329,7 @@ func TestListArchsWithAuthenticationAndPlainManifest(t *testing.T) {
assert.Equal(t, "repository:my/image:pull", req.URL.Query().Get("scope"))
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(strings.NewReader(`{"token":"my-token"}`)),
Body: io.NopCloser(strings.NewReader(`{"token":"my-token"}`)),
}, nil
case "registry.company.corp":
switch req.URL.Path {
Expand All @@ -339,7 +339,7 @@ func TestListArchsWithAuthenticationAndPlainManifest(t *testing.T) {
return &http.Response{
StatusCode: http.StatusOK,
Header: headers,
Body: ioutil.NopCloser(strings.NewReader(`{
Body: io.NopCloser(strings.NewReader(`{
"architecture": "amd64"
}`)),
}, nil
Expand Down

0 comments on commit d6f9a1b

Please sign in to comment.