Skip to content

Commit

Permalink
Update to Golang 1.19 and add CSP token exchange testing
Browse files Browse the repository at this point in the history
Signed-off-by: Pete Wall <pwall@vmware.com>
  • Loading branch information
Pete Wall committed Aug 16, 2022
1 parent d08d143 commit fdceb86
Show file tree
Hide file tree
Showing 23 changed files with 205 additions and 60 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright 2022 VMware, Inc.
# SPDX-License-Identifier: BSD-2-Clause

FROM harbor-repo.vmware.com/dockerhub-proxy-cache/library/golang:1.17 as builder
FROM harbor-repo.vmware.com/dockerhub-proxy-cache/library/golang:1.19 as builder
ARG VERSION

COPY . /marketplace-cli/
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ default: build
.PHONY: deps-go-binary deps-counterfeiter deps-ginkgo deps-golangci-lint

GO_VERSION := $(shell go version)
GO_VERSION_REQUIRED = go1.18
GO_VERSION_REQUIRED = go1.19
GO_VERSION_MATCHED := $(shell go version | grep $(GO_VERSION_REQUIRED))

deps-go-binary:
Expand Down Expand Up @@ -45,7 +45,7 @@ ifeq ($(PLATFORM), Darwin)
brew install golangci-lint
endif
ifeq ($(PLATFORM), Linux)
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.46.2
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin
endif
endif

Expand Down
4 changes: 2 additions & 2 deletions ci/pipeline.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,7 @@ jobs:
type: registry-image
source:
repository: harbor-repo.vmware.com/dockerhub-proxy-cache/library/golang
tag: 1.17
tag: 1.19
username: ((harbor.username))
password: ((harbor.token))
inputs:
Expand Down Expand Up @@ -1011,7 +1011,7 @@ jobs:
type: registry-image
source:
repository: harbor-repo.vmware.com/dockerhub-proxy-cache/library/golang
tag: 1.17
tag: 1.19
username: ((harbor.username))
password: ((harbor.token))
inputs:
Expand Down
2 changes: 1 addition & 1 deletion ci/tasks/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ image_resource:
type: registry-image
source:
repository: harbor-repo.vmware.com/dockerhub-proxy-cache/library/golang
tag: 1.18
tag: 1.19
username: ((harbor.username))
password: ((harbor.token))

Expand Down
2 changes: 1 addition & 1 deletion ci/tasks/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ image_resource:
type: registry-image
source:
repository: harbor-repo.vmware.com/dockerhub-proxy-cache/library/golang
tag: 1.18
tag: 1.19
username: ((harbor.username))
password: ((harbor.token))

Expand Down
6 changes: 4 additions & 2 deletions cmd/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package cmd
import (
"fmt"

"github.com/golang-jwt/jwt"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/vmware-labs/marketplace-cli/v2/internal/csp"
Expand All @@ -21,8 +22,9 @@ type TokenServicesInitializer func(cspHost string) TokenServices

var InitializeTokenServices TokenServicesInitializer = func(cspHost string) TokenServices {
return &csp.TokenServices{
CSPHost: cspHost,
Client: Client,
CSPHost: cspHost,
Client: Client,
TokenParser: jwt.ParseWithClaims,
}
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/cmd_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package cmd_test
import (
"bytes"
"encoding/json"
"io/ioutil"
"io"
"net/http"
"testing"

Expand All @@ -25,6 +25,6 @@ func ResponseWithPayload(payload interface{}) *http.Response {

return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewReader(encoded)),
Body: io.NopCloser(bytes.NewReader(encoded)),
}
}
6 changes: 3 additions & 3 deletions cmd/curl.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/url"
"os"

"github.com/spf13/cobra"
"github.com/vmware-labs/marketplace-cli/v2/pkg"
Expand Down Expand Up @@ -55,7 +55,7 @@ var curlCmd = &cobra.Command{
var content io.Reader
headers := map[string]string{}
if payload != "" {
payloadBytes, err := ioutil.ReadFile(payload)
payloadBytes, err := os.ReadFile(payload)
if err != nil {
return fmt.Errorf("failed to read payload file: %w", err)
}
Expand All @@ -70,7 +70,7 @@ var curlCmd = &cobra.Command{

cmd.PrintErrf("Response status %d\n", resp.StatusCode)

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("failed to read response: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

module github.com/vmware-labs/marketplace-cli/v2

go 1.18
go 1.19

require (
github.com/aws/aws-sdk-go-v2 v1.16.5
Expand Down
120 changes: 120 additions & 0 deletions internal/csp/cspfakes/fake_token_parser_fn.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions internal/csp/token_services.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ import (
"github.com/vmware-labs/marketplace-cli/v2/pkg"
)

//go:generate counterfeiter . TokenParserFn
type TokenParserFn func(tokenString string, claims jwt.Claims, keyFunc jwt.Keyfunc) (*jwt.Token, error)

type TokenServices struct {
CSPHost string
Client pkg.HTTPClient
CSPHost string
Client pkg.HTTPClient
TokenParser TokenParserFn
}

type RedeemResponse struct {
Expand Down Expand Up @@ -57,7 +61,7 @@ func (csp *TokenServices) Redeem(refreshToken string) (*Claims, error) {
}

claims := &Claims{}
token, err := jwt.ParseWithClaims(body.AccessToken, claims, csp.GetPublicKey)
token, err := csp.TokenParser(body.AccessToken, claims, csp.GetPublicKey)
if err != nil {
return nil, fmt.Errorf("invalid token returned from CSP: %w", err)
}
Expand Down
37 changes: 31 additions & 6 deletions internal/csp/token_services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,48 @@ import (
"errors"
"net/http"

"github.com/golang-jwt/jwt"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/vmware-labs/marketplace-cli/v2/internal/csp"
"github.com/vmware-labs/marketplace-cli/v2/internal/csp/cspfakes"
"github.com/vmware-labs/marketplace-cli/v2/pkg/pkgfakes"
"github.com/vmware-labs/marketplace-cli/v2/test"
)

var _ = Describe("CSP Token Services", func() {
var (
client *pkgfakes.FakeHTTPClient
tokenParser *cspfakes.FakeTokenParserFn
tokenServices *csp.TokenServices
)

BeforeEach(func() {
client = &pkgfakes.FakeHTTPClient{}
tokenParser = &cspfakes.FakeTokenParserFn{}
tokenServices = &csp.TokenServices{
CSPHost: "csp.example.com",
Client: client,
CSPHost: "csp.example.com",
Client: client,
TokenParser: tokenParser.Spy,
}
})

Describe("Redeem", func() {
XIt("exchanges the token for the JWT token claims", func() {

BeforeEach(func() {
responseBody := csp.RedeemResponse{
AccessToken: "my-access-token",
StatusCode: http.StatusOK,
}
client.PostFormReturns(test.MakeJSONResponse(responseBody), nil)
token := &jwt.Token{
Raw: "my-jwt-token",
}
tokenParser.Returns(token, nil)
})
It("exchanges the token for the JWT token claims", func() {
token, err := tokenServices.Redeem("my-csp-api-token")
Expect(err).ToNot(HaveOccurred())
Expect(token.Token).To(Equal("my-jwt-token"))
})

When("sending the request fails", func() {
Expand Down Expand Up @@ -90,8 +108,15 @@ var _ = Describe("CSP Token Services", func() {
})
})

XWhen("the response is not a valid token", func() {

When("the response is not a valid token", func() {
BeforeEach(func() {
tokenParser.Returns(nil, errors.New("token parser failed"))
})
It("returns an error", func() {
_, err := tokenServices.Redeem("my-csp-api-token")
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(Equal("invalid token returned from CSP: token parser failed"))
})
})
})
})
8 changes: 4 additions & 4 deletions pkg/charts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package pkg_test
import (
"bytes"
"errors"
"io/ioutil"
"io"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -79,7 +79,7 @@ var _ = Describe("Charts", func() {
// This does not need to be cleaned up because chartDir will be removed
chartArchive, err := chartutil.Save(chart, chartDir)
Expect(err).ToNot(HaveOccurred())
chartBytes, err := ioutil.ReadFile(chartArchive)
chartBytes, err := os.ReadFile(chartArchive)
Expect(err).ToNot(HaveOccurred())
httpClient.DoReturns(test.MakeBytesResponse(chartBytes), nil)
})
Expand Down Expand Up @@ -249,11 +249,11 @@ var _ = Describe("Charts", func() {
// This does not need to be cleaned up because chartDir will be removed
chartArchive, err := chartutil.Save(chart, chartDir)
Expect(err).ToNot(HaveOccurred())
chartBytes, err = ioutil.ReadFile(chartArchive)
chartBytes, err = os.ReadFile(chartArchive)
Expect(err).ToNot(HaveOccurred())

httpClient.DoReturns(&http.Response{
Body: ioutil.NopCloser(bytes.NewReader(chartBytes)),
Body: io.NopCloser(bytes.NewReader(chartBytes)),
}, nil)
httpClient.PutStub = PutProductEchoResponse
})
Expand Down

0 comments on commit fdceb86

Please sign in to comment.