Skip to content

Commit e8f9a34

Browse files
authored
CR-13257-git-token-validation (#532)
1 parent 45fbf4f commit e8f9a34

16 files changed

+801
-81
lines changed

Makefile

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION=v0.0.478
1+
VERSION=v0.0.479
22

33
OUT_DIR=dist
44
YEAR?=$(shell date +"%Y")
@@ -129,7 +129,7 @@ test:
129129
@./hack/test.sh
130130

131131
.PHONY: codegen
132-
codegen: $(GOBIN)/mockery
132+
codegen: $(GOBIN)/mockgen
133133
rm -f ./docs/commands/*
134134
go generate ./...
135135
go run ./hack/license.go --license ./hack/boilerplate.txt --year $(YEAR) .
@@ -165,16 +165,9 @@ tidy:
165165
check-worktree:
166166
@./hack/check_worktree.sh
167167

168-
$(GOBIN)/mockery:
169-
@mkdir dist || true
170-
@echo installing: mockery
171-
@curl -L -o dist/mockery.tar.gz -- https://github.com/vektra/mockery/releases/download/v1.1.1/mockery_1.1.1_$(shell uname -s)_$(shell uname -m).tar.gz
172-
@tar zxvf dist/mockery.tar.gz mockery
173-
@rm dist/mockery.tar.gz
174-
@chmod +x mockery
175-
@mkdir -p $(GOBIN)
176-
@mv mockery $(GOBIN)/mockery
177-
@mockery -version
168+
$(GOBIN)/mockgen:
169+
@go install github.com/golang/mock/mockgen@v1.6.0
170+
@mockgen -version
178171

179172
$(GOBIN)/golangci-lint:
180173
@mkdir dist || true

cmd/commands/common.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,8 @@ func getIngressClassFromUserSelect(ingressClassNames []string) (string, error) {
281281
return result, nil
282282
}
283283

284-
// ensureGitToken gets the runtime token from the user (if !silent), and verifys it witht he provider (if available)
285-
func ensureGitToken(cmd *cobra.Command, gitProvider cfgit.Provider, cloneOpts *apgit.CloneOptions) error {
284+
// ensureGitRuntimeToken gets the runtime token from the user (if !silent), and verifys it with he provider (if available)
285+
func ensureGitRuntimeToken(cmd *cobra.Command, gitProvider cfgit.Provider, cloneOpts *apgit.CloneOptions) error {
286286
ctx := cmd.Context()
287287
errMessage := "Value stored in environment variable GIT_TOKEN is invalid; enter a valid runtime token: %w"
288288
if cloneOpts.Auth.Password == "" && !store.Get().Silent {
@@ -294,7 +294,7 @@ func ensureGitToken(cmd *cobra.Command, gitProvider cfgit.Provider, cloneOpts *a
294294
}
295295

296296
if gitProvider != nil {
297-
err := gitProvider.VerifyToken(ctx, cfgit.RuntimeToken, cloneOpts.Auth.Password)
297+
err := gitProvider.VerifyRuntimeToken(ctx, cloneOpts.Auth.Password)
298298
if err != nil {
299299
// in case when we get invalid value from env variable TOKEN we clean
300300
cloneOpts.Auth.Password = ""
@@ -307,8 +307,8 @@ func ensureGitToken(cmd *cobra.Command, gitProvider cfgit.Provider, cloneOpts *a
307307
return nil
308308
}
309309

310-
// ensureGitPAT verifys the user's Personal Access Token (if it is different from the Runtime Token)
311-
func ensureGitPAT(ctx context.Context, opts *RuntimeInstallOptions) error {
310+
// ensureGitUserToken verifys the user's Personal Access Token (if it is different from the Runtime Token)
311+
func ensureGitUserToken(ctx context.Context, opts *RuntimeInstallOptions) error {
312312
if opts.GitIntegrationRegistrationOpts.Token == "" {
313313
opts.GitIntegrationRegistrationOpts.Token = opts.InsCloneOpts.Auth.Password
314314
currentUser, err := cfConfig.NewClient().Users().GetCurrent(ctx)
@@ -321,7 +321,7 @@ func ensureGitPAT(ctx context.Context, opts *RuntimeInstallOptions) error {
321321
}
322322

323323
if opts.gitProvider != nil {
324-
return opts.gitProvider.VerifyToken(ctx, cfgit.PersonalToken, opts.InsCloneOpts.Auth.Password)
324+
return opts.gitProvider.VerifyUserToken(ctx, opts.GitIntegrationRegistrationOpts.Token)
325325
}
326326

327327
return nil
@@ -566,7 +566,7 @@ func checkIngressHostWithInsecure(ingress string) bool {
566566
customTransport := http.DefaultTransport.(*http.Transport).Clone()
567567
customTransport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
568568
httpClient.Transport = customTransport
569-
req, err := http.NewRequest("GET", ingress, nil)
569+
req, err := http.NewRequest(http.MethodGet, ingress, nil)
570570
if err != nil {
571571
return false
572572
}

cmd/commands/runtime.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ func runtimeUninstallCommandPreRunHandler(cmd *cobra.Command, args []string, opt
179179
}
180180

181181
if !opts.Managed {
182-
err = ensureGitToken(cmd, nil, opts.CloneOpts)
182+
err = ensureGitRuntimeToken(cmd, nil, opts.CloneOpts)
183183
}
184184
handleCliStep(reporter.UninstallStepPreCheckEnsureGitToken, "Getting git token", err, true, false)
185185
if err != nil {
@@ -217,7 +217,7 @@ func runtimeUpgradeCommandPreRunHandler(cmd *cobra.Command, args []string, opts
217217
return err
218218
}
219219

220-
err = ensureGitToken(cmd, nil, opts.CloneOpts)
220+
err = ensureGitRuntimeToken(cmd, nil, opts.CloneOpts)
221221
handleCliStep(reporter.UpgradeStepPreCheckEnsureGitToken, "Getting git token", err, true, false)
222222
if err != nil {
223223
return err

cmd/commands/runtime_install.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ func ensureGitData(cmd *cobra.Command, opts *RuntimeInstallOptions) error {
340340
return err
341341
}
342342

343-
err = ensureGitPAT(ctx, opts)
343+
err = ensureGitUserToken(ctx, opts)
344344
handleCliStep(reporter.InstallStepPreCheckEnsureGitPAT, "Getting git personal access token", err, true, false)
345345
if err != nil {
346346
return err
@@ -371,10 +371,10 @@ func getGitToken(cmd *cobra.Command, opts *RuntimeInstallOptions) error {
371371
var err error
372372

373373
if store.Get().Silent {
374-
err = ensureGitToken(cmd, opts.gitProvider, opts.InsCloneOpts)
374+
err = ensureGitRuntimeToken(cmd, opts.gitProvider, opts.InsCloneOpts)
375375
} else {
376376
handleValidationFailsWithRepeat(func() error {
377-
err = ensureGitToken(cmd, opts.gitProvider, opts.InsCloneOpts)
377+
err = ensureGitRuntimeToken(cmd, opts.gitProvider, opts.InsCloneOpts)
378378
if isValidationError(err) {
379379
fmt.Println(err)
380380
return err

docs/releases/release_notes.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ cf version
2323

2424
```bash
2525
# download and extract the binary
26-
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.478/cf-linux-amd64.tar.gz | tar zx
26+
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.479/cf-linux-amd64.tar.gz | tar zx
2727

2828
# move the binary to your $PATH
2929
mv ./cf-linux-amd64 /usr/local/bin/cf
@@ -36,7 +36,7 @@ cf version
3636

3737
```bash
3838
# download and extract the binary
39-
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.478/cf-darwin-amd64.tar.gz | tar zx
39+
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.479/cf-darwin-amd64.tar.gz | tar zx
4040

4141
# move the binary to your $PATH
4242
mv ./cf-darwin-amd64 /usr/local/bin/cf

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ require (
1616
github.com/go-git/go-billy/v5 v5.3.1
1717
github.com/go-git/go-git/v5 v5.4.2
1818
github.com/gobuffalo/packr v1.30.1
19+
github.com/golang/mock v1.6.0
1920
github.com/google/uuid v1.3.0
2021
github.com/juju/ansiterm v0.0.0-20210929141451-8b71cc96ebdc
2122
github.com/manifoldco/promptui v0.8.0

manifests/runtime.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ metadata:
55
namespace: "{{ namespace }}"
66
spec:
77
defVersion: 1.0.1
8-
version: 0.0.478
8+
version: 0.0.479
99
bootstrapSpecifier: github.com/codefresh-io/cli-v2/manifests/argo-cd
1010
components:
1111
- name: events

pkg/git/mocks/roundTripper.go

Lines changed: 64 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/git/provider.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,50 +17,49 @@ package git
1717
import (
1818
"context"
1919
"fmt"
20+
"net/http"
2021
"strings"
2122
)
2223

24+
//go:generate mockgen -destination=./mocks/roundTripper.go -package=mocks net/http RoundTripper
25+
2326
type (
24-
TokenType string
2527
ProviderType string
2628

2729
// Provider represents a git provider
2830
Provider interface {
29-
Type() ProviderType
3031
BaseURL() string
31-
VerifyToken(ctx context.Context, tokenType TokenType, token string) error
3232
SupportsMarketplace() bool
33+
Type() ProviderType
34+
VerifyRuntimeToken(ctx context.Context, token string) error
35+
VerifyUserToken(ctx context.Context, token string) error
3336
}
3437
)
3538

36-
const (
37-
RuntimeToken TokenType = "runtime token"
38-
PersonalToken TokenType = "personal token"
39-
)
40-
41-
var providers = map[ProviderType]func(string) (Provider, error){
39+
var providers = map[ProviderType]func(string, *http.Client) (Provider, error){
4240
BITBUCKET_SERVER: NewBitbucketServerProvider,
4341
GITHUB: NewGithubProvider,
4442
GITHUB_ENT: NewGithubProvider, // for backward compatability
4543
GITLAB: NewGitlabProvider,
4644
}
4745

4846
func GetProvider(providerType ProviderType, baseURL string) (Provider, error) {
47+
client := &http.Client{}
4948
if providerType != "" {
5049
fn := providers[providerType]
5150
if fn == nil {
5251
return nil, fmt.Errorf("invalid git provider %s", providerType)
5352
}
5453

55-
return fn(baseURL)
54+
return fn(baseURL, client)
5655
}
5756

5857
if strings.Contains(baseURL, GITHUB_CLOUD_DOMAIN) {
59-
return NewGithubProvider(baseURL)
58+
return NewGithubProvider(baseURL, client)
6059
}
6160

6261
if strings.Contains(baseURL, GITLAB_CLOUD_DOMAIN) {
63-
return NewGitlabProvider(baseURL)
62+
return NewGitlabProvider(baseURL, client)
6463
}
6564

6665
return nil, fmt.Errorf("failed getting provider for clone url %s", baseURL)

0 commit comments

Comments
 (0)