Skip to content

Commit

Permalink
Adding Logic and Tests for Authorization token to only be used if its…
Browse files Browse the repository at this point in the history
… provided
  • Loading branch information
davidcollom authored Jul 8, 2024
1 parent c052ef3 commit c027c97
Show file tree
Hide file tree
Showing 2 changed files with 176 additions and 9 deletions.
18 changes: 9 additions & 9 deletions pkg/client/ghcr/ghcr.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import (
"strings"

"github.com/gofri/go-github-ratelimit/github_ratelimit"
"github.com/google/go-github/v58/github"
"github.com/google/go-github/v62/github"
"github.com/jetstack/version-checker/pkg/api"
"github.com/jetstack/version-checker/pkg/client/util"
)

type Options struct {
Expand All @@ -31,7 +32,11 @@ func New(opts Options) *Client {
if err != nil {
panic(err)
}
client := github.NewClient(ghRateLimiter).WithAuthToken(opts.Token)
client := github.NewClient(ghRateLimiter)
// Only add Auth Token if it is provided.
if len(opts.Token) > 0 {
client = client.WithAuthToken(opts.Token)
}

return &Client{
client: client,
Expand All @@ -47,6 +52,7 @@ func (c *Client) Name() string {
func (c *Client) Tags(ctx context.Context, host, owner, repo string) ([]api.ImageTag, error) {
// Choose the correct list packages function based on whether the owner
// is a user or an organization
// getReleases := c.Client.Repositories.ListReleases(ctx, owner, repo)
getAllVersions := c.client.Organizations.PackageGetAllVersions
ownerType, err := c.ownerType(ctx, owner)
if err != nil {
Expand Down Expand Up @@ -88,13 +94,7 @@ func (c *Client) Tags(ctx context.Context, host, owner, repo string) ([]api.Imag

for _, tag := range ver.Metadata.Container.Tags {
// Exclude attestations, signatures and sboms
if strings.HasSuffix(tag, ".att") {
continue
}
if strings.HasSuffix(tag, ".sig") {
continue
}
if strings.HasSuffix(tag, ".sbom") {
if util.FilterSbomAttestationSigs(tag) {
continue
}

Expand Down
167 changes: 167 additions & 0 deletions pkg/client/ghcr/ghcr_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
package ghcr

import (
"context"
"net/http"
"testing"

"github.com/google/go-github/v62/github"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
)

func setup() {
httpmock.Activate()
}

func teardown() {
httpmock.DeactivateAndReset()
}

func registerCommonResponders() {
httpmock.RegisterResponder("GET", "https://api.github.com/users/test-user-owner",
func(req *http.Request) (*http.Response, error) {
return httpmock.NewStringResponse(200, `{"type":"User"}`), nil
})
httpmock.RegisterResponder("GET", "https://api.github.com/users/test-org-owner",
func(req *http.Request) (*http.Response, error) {
return httpmock.NewStringResponse(200, `{"type":"Organization"}`), nil
})
}

func registerTagResponders() {
httpmock.RegisterResponder("GET", "https://api.github.com/users/test-user-owner/packages/container/test-repo/versions",
func(req *http.Request) (*http.Response, error) {
return httpmock.NewStringResponse(200, `[
{
"name": "sha123",
"metadata": {
"container": {
"tags": ["tag1", "tag2"]
}
},
"created_at": "2023-07-08T12:34:56Z"
}
]`), nil
})
httpmock.RegisterResponder("GET", "https://api.github.com/orgs/test-org-owner/packages/container/test-repo/versions",
func(req *http.Request) (*http.Response, error) {
return httpmock.NewStringResponse(200, `[
{
"name": "sha123",
"metadata": {
"container": {
"tags": ["tag1", "tag2"]
}
},
"created_at": "2023-07-08T12:34:56Z"
}
]`), nil
})
}

func TestClient_Tags(t *testing.T) {
setup()
defer teardown()

ctx := context.Background()
host := "ghcr.io"

t.Run("successful tags fetch", func(t *testing.T) {
httpmock.Reset()
registerCommonResponders()
registerTagResponders()

client := New(Options{})
client.client = github.NewClient(nil) // Use the default HTTP client

tags, err := client.Tags(ctx, host, "test-user-owner", "test-repo")
assert.NoError(t, err)
assert.Len(t, tags, 2)
assert.Equal(t, "tag1", tags[0].Tag)
assert.Equal(t, "tag2", tags[1].Tag)
})

t.Run("failed to fetch owner type", func(t *testing.T) {
httpmock.Reset()
httpmock.RegisterResponder("GET", "https://api.github.com/users/test-user-owner",
func(req *http.Request) (*http.Response, error) {
return httpmock.NewStringResponse(404, `{"message": "Not Found"}`), nil
})

client := New(Options{})
client.client = github.NewClient(nil) // Use the default HTTP client

_, err := client.Tags(ctx, host, "test-user-owner", "test-repo")
assert.Error(t, err)
})

t.Run("token not set, no authorization header", func(t *testing.T) {
httpmock.Reset()
httpmock.RegisterResponder("GET", "https://api.github.com/users/test-user-owner",
func(req *http.Request) (*http.Response, error) {
if req.Header.Get("Authorization") != "" {
t.Errorf("expected no Authorization header, got %s", req.Header.Get("Authorization"))
}
return httpmock.NewStringResponse(200, `{"type":"User"}`), nil
})
registerTagResponders()

client := New(Options{}) // No token provided
client.client = github.NewClient(nil)

_, err := client.Tags(ctx, host, "test-user-owner", "test-repo")
assert.NoError(t, err)
})

t.Run("token set, authorization header sent", func(t *testing.T) {
token := "test-token"
httpmock.Reset()
httpmock.RegisterResponder("GET", "https://api.github.com/users/test-user-owner",
func(req *http.Request) (*http.Response, error) {
authHeader := req.Header.Get("Authorization")
expectedAuthHeader := "Bearer " + token
if authHeader != expectedAuthHeader {
t.Errorf("expected Authorization header %s, got %s", expectedAuthHeader, authHeader)
}
return httpmock.NewStringResponse(200, `{"type":"User"}`), nil
})

registerTagResponders()

client := New(Options{Token: token})

_, err := client.Tags(ctx, host, "test-user-owner", "test-repo")
assert.NoError(t, err)
})

t.Run("ownerType returns user", func(t *testing.T) {
httpmock.Reset()
registerCommonResponders()
registerTagResponders()

client := New(Options{})
client.client = github.NewClient(nil) // Use the default HTTP client

tags, err := client.Tags(ctx, host, "test-user-owner", "test-repo")
assert.NoError(t, err)
assert.Len(t, tags, 2)
assert.Equal(t, "tag1", tags[0].Tag)
assert.Equal(t, "tag2", tags[1].Tag)
})

t.Run("ownerType returns org", func(t *testing.T) {
httpmock.Reset()
registerCommonResponders()
registerTagResponders()

client := New(Options{})
client.client = github.NewClient(nil) // Use the default HTTP client

tags, err := client.Tags(ctx, host, "test-org-owner", "test-repo")
assert.NoError(t, err)
assert.Len(t, tags, 2)
assert.Equal(t, "tag1", tags[0].Tag)
assert.Equal(t, "tag2", tags[1].Tag)
})
}

0 comments on commit c027c97

Please sign in to comment.