Skip to content

Commit

Permalink
chore: Add fmt/lint/vet/sec checks
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Bayer <[email protected]>
  • Loading branch information
abayer committed Oct 12, 2020
1 parent a1c44fa commit aa6b3d8
Show file tree
Hide file tree
Showing 43 changed files with 507 additions and 213 deletions.
46 changes: 45 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,50 @@
# Make does not offer a recursive wildcard function, so here's one:
rwildcard=$(wildcard $1$2) $(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2))

GO_DEPENDENCIES := $(call rwildcard,pkg/,*.go) $(call rwildcard,scm/,*.go)
GO := GO111MODULE=on go
GO_NOMOD := GO111MODULE=off go

build: test

test:
go test ./...

linux: build
linux: build

.PHONY: check
check: fmt lint sec ## Runs Go format check as well as security checks

get-fmt-deps:
$(GO_NOMOD) get golang.org/x/tools/cmd/goimports

.PHONY: importfmt
importfmt: get-fmt-deps ## Checks the import format of the Go source files
@echo "FORMATTING IMPORTS"
@goimports -w $(GO_DEPENDENCIES)

.PHONY: fmt ## Checks Go source files are formatted properly
fmt: importfmt
@echo "FORMATTING SOURCE"
FORMATTED=`$(GO) fmt ./...`
@([[ ! -z "$(FORMATTED)" ]] && printf "Fixed un-formatted files:\n$(FORMATTED)") || true

GOLINT := $(GOPATH)/bin/golint
$(GOLINT):
$(GO_NOMOD) get -u golang.org/x/lint/golint

.PHONY: lint
lint: $(GOLINT) ## Runs 'go vet' anf 'go lint'
@echo "VETTING"
$(GO) vet ./...
@echo "LINTING"
$(GOLINT) -set_exit_status ./...

GOSEC := $(GOPATH)/bin/gosec
$(GOSEC):
$(GO_NOMOD) get -u github.com/securego/gosec/cmd/gosec

.PHONY: sec
sec: $(GOSEC) ## Runs gosec to check for potential security issues in the Go source
@echo "SECURITY SCANNING"
$(GOSEC) -quiet -fmt=csv ./...
2 changes: 1 addition & 1 deletion jenkins-x.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ pipelineConfig:
- name: test
command: make
args:
- test
- check test
dir: /workspace/source
4 changes: 2 additions & 2 deletions pkg/hmac/hmac.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package hmac

import (
"crypto/hmac"
"crypto/sha1"
"crypto/sha1" // #nosec
"crypto/sha256"
"encoding/hex"
"hash"
Expand Down Expand Up @@ -42,7 +42,7 @@ func ValidatePrefix(message, key []byte, signature string) bool {

func validate(h func() hash.Hash, message, key, signature []byte) bool {
mac := hmac.New(h, key)
mac.Write(message)
mac.Write(message) // #nosec
sum := mac.Sum(nil)
return hmac.Equal(signature, sum)
}
1 change: 1 addition & 0 deletions scm/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
)

type (
// InstallationToken is the token used for interacting with the app
InstallationToken struct {
Token string
ExpiresAt *time.Time
Expand Down
4 changes: 2 additions & 2 deletions scm/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,9 @@ func (c *Client) Do(ctx context.Context, in *Request) (*Response, error) {

// dumps the response for debugging purposes.
if c.DumpResponse != nil {
c.DumpResponse(res, true)
_, err = c.DumpResponse(res, true)
}
return newResponse(res), nil
return newResponse(res), err
}

// newResponse creates a new Response for the provided
Expand Down
2 changes: 2 additions & 0 deletions scm/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,12 @@ func ToState(s string) State {
}
}

// MarshalJSON marshals State to JSON
func (s State) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`"%s"`, s.String())), nil
}

// UnmarshalJSON unmarshals JSON to State
func (s *State) UnmarshalJSON(b []byte) error {
*s = ToState(strings.Trim(string(b), `"`))
return nil
Expand Down
9 changes: 5 additions & 4 deletions scm/driver/bitbucket/bitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/jenkins-x/go-scm/scm"
)

// NewWebHookService creates a new instance of the webhook service without the rest of the client
func NewWebHookService() scm.WebhookService {
return &webhookService{nil}
}
Expand Down Expand Up @@ -70,7 +71,7 @@ func (c *wrapper) do(ctx context.Context, method, path string, in, out interface
// write it to the body of the request.
if in != nil {
buf := new(bytes.Buffer)
json.NewEncoder(buf).Encode(in)
json.NewEncoder(buf).Encode(in) // #nosec
req.Header = map[string][]string{
"Content-Type": {"application/json"},
}
Expand All @@ -90,7 +91,7 @@ func (c *wrapper) do(ctx context.Context, method, path string, in, out interface
return res, scm.ErrNotAuthorized
} else if res.Status > 300 {
err := new(Error)
json.NewDecoder(res.Body).Decode(err)
json.NewDecoder(res.Body).Decode(err) // #nosec
return res, err
}

Expand All @@ -101,8 +102,8 @@ func (c *wrapper) do(ctx context.Context, method, path string, in, out interface
// if raw output is expected, copy to the provided
// buffer and exit.
if w, ok := out.(io.Writer); ok {
io.Copy(w, res.Body)
return res, nil
_, err := io.Copy(w, res.Body)
return res, err
}

// if a json response is expected, parse and return
Expand Down
20 changes: 16 additions & 4 deletions scm/driver/bitbucket/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,31 +84,43 @@ func (s *gitService) ListBranches(ctx context.Context, repo string, opts scm.Lis
path := fmt.Sprintf("2.0/repositories/%s/refs/branches?%s", repo, encodeListOptions(opts))
out := new(branches)
res, err := s.client.do(ctx, "GET", path, nil, out)
copyPagination(out.pagination, res)
if err != nil {
return nil, res, err
}
err = copyPagination(out.pagination, res)
return convertBranchList(out), res, err
}

func (s *gitService) ListCommits(ctx context.Context, repo string, opts scm.CommitListOptions) ([]*scm.Commit, *scm.Response, error) {
path := fmt.Sprintf("2.0/repositories/%s/commits/%s?%s", repo, opts.Ref, encodeCommitListOptions(opts))
out := new(commits)
res, err := s.client.do(ctx, "GET", path, nil, out)
copyPagination(out.pagination, res)
if err != nil {
return nil, res, err
}
err = copyPagination(out.pagination, res)
return convertCommitList(out), res, err
}

func (s *gitService) ListTags(ctx context.Context, repo string, opts scm.ListOptions) ([]*scm.Reference, *scm.Response, error) {
path := fmt.Sprintf("2.0/repositories/%s/refs/tags?%s", repo, encodeListOptions(opts))
out := new(branches)
res, err := s.client.do(ctx, "GET", path, nil, &out)
copyPagination(out.pagination, res)
if err != nil {
return nil, res, err
}
err = copyPagination(out.pagination, res)
return convertTagList(out), res, err
}

func (s *gitService) ListChanges(ctx context.Context, repo, ref string, opts scm.ListOptions) ([]*scm.Change, *scm.Response, error) {
path := fmt.Sprintf("2.0/repositories/%s/diffstat/%s?%s", repo, ref, encodeListOptions(opts))
out := new(diffstats)
res, err := s.client.do(ctx, "GET", path, nil, &out)
copyPagination(out.pagination, res)
if err != nil {
return nil, res, err
}
err = copyPagination(out.pagination, res)
return convertDiffstats(out), res, err
}

Expand Down
5 changes: 4 additions & 1 deletion scm/driver/bitbucket/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ func (s *organizationService) List(ctx context.Context, opts scm.ListOptions) ([
path := fmt.Sprintf("2.0/teams?%s", encodeListRoleOptions(opts))
out := new(organizationList)
res, err := s.client.do(ctx, "GET", path, nil, out)
copyPagination(out.pagination, res)
if err != nil {
return nil, res, err
}
err = copyPagination(out.pagination, res)
return convertOrganizationList(out), res, err
}

Expand Down
10 changes: 8 additions & 2 deletions scm/driver/bitbucket/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,21 @@ func (s *pullService) List(ctx context.Context, repo string, opts scm.PullReques
return nil, res, err
}
res, err := s.client.do(ctx, "GET", path, nil, out)
copyPagination(out.pagination, res)
if err != nil {
return nil, res, err
}
err = copyPagination(out.pagination, res)
return convertPullRequests(out), res, err
}

func (s *pullService) ListChanges(ctx context.Context, repo string, number int, opts scm.ListOptions) ([]*scm.Change, *scm.Response, error) {
path := fmt.Sprintf("2.0/repositories/%s/pullrequests/%d/diffstat?%s", repo, number, encodeListOptions(opts))
out := new(diffstats)
res, err := s.client.do(ctx, "GET", path, nil, out)
copyPagination(out.pagination, res)
if err != nil {
return nil, res, err
}
err = copyPagination(out.pagination, res)
return convertDiffstats(out), res, err
}

Expand Down
15 changes: 12 additions & 3 deletions scm/driver/bitbucket/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,10 @@ func (s *repositoryService) List(ctx context.Context, opts scm.ListOptions) ([]*
}
out := new(repositories)
res, err := s.client.do(ctx, "GET", path, nil, &out)
copyPagination(out.pagination, res)
if err != nil {
return nil, res, err
}
err = copyPagination(out.pagination, res)
return convertRepositoryList(out), res, err
}

Expand All @@ -144,7 +147,10 @@ func (s *repositoryService) ListHooks(ctx context.Context, repo string, opts scm
path := fmt.Sprintf("2.0/repositories/%s/hooks?%s", repo, encodeListOptions(opts))
out := new(hooks)
res, err := s.client.do(ctx, "GET", path, nil, out)
copyPagination(out.pagination, res)
if err != nil {
return nil, res, err
}
err = copyPagination(out.pagination, res)
return convertHookList(out), res, err
}

Expand All @@ -153,7 +159,10 @@ func (s *repositoryService) ListStatus(ctx context.Context, repo, ref string, op
path := fmt.Sprintf("2.0/repositories/%s/commit/%s/statuses?%s", repo, ref, encodeListOptions(opts))
out := new(statuses)
res, err := s.client.do(ctx, "GET", path, nil, out)
copyPagination(out.pagination, res)
if err != nil {
return nil, res, err
}
err = copyPagination(out.pagination, res)
return convertStatusList(out), res, err
}

Expand Down
4 changes: 2 additions & 2 deletions scm/driver/fake/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (c contentService) Find(_ context.Context, repo, path, ref string) (*scm.Co
Status: 404,
}, errors.Wrapf(err, "file %s does not exist", f)
}
data, err := ioutil.ReadFile(f)
data, err := ioutil.ReadFile(f) // #nosec
if err != nil {
return nil, nil, errors.Wrapf(err, "failed to read file %s", f)
}
Expand Down Expand Up @@ -129,7 +129,7 @@ func (c contentService) path(repo string, path string, ref string) (string, erro
return filepath.Join(repoDir, path), nil
}

/// DirExists checks if path exists and is a directory
// DirExists checks if path exists and is a directory
func DirExists(path string) (bool, error) {
info, err := os.Stat(path)
if err == nil {
Expand Down
2 changes: 2 additions & 0 deletions scm/driver/fake/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fake

import "github.com/jenkins-x/go-scm/scm"

// Data is used to store/represent test data for the fake client
type Data struct {
Issues map[int][]*scm.Issue
OrgMembers map[string][]string
Expand Down Expand Up @@ -74,6 +75,7 @@ type Data struct {
ContentDir string
}

// DeletedRef represents a ref that has been deleted
type DeletedRef struct {
Org, Repo, Ref string
}
Expand Down
6 changes: 3 additions & 3 deletions scm/driver/fake/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ func (s *organizationService) List(context.Context, scm.ListOptions) ([]*scm.Org
Name: fmt.Sprintf("organisation%d", i),
Avatar: fmt.Sprintf("https://github.com/organisation%d.png", i),
Permissions: scm.Permissions{
true,
true,
true,
MembersCreatePrivate: true,
MembersCreatePublic: true,
MembersCreateInternal: true,
},
}
orgs = append(orgs, &org)
Expand Down
1 change: 1 addition & 0 deletions scm/driver/fake/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ func (s *repositoryService) ListHooks(ctx context.Context, fullName string, opts
}

func (s *repositoryService) CreateHook(ctx context.Context, fullName string, input *scm.HookInput) (*scm.Hook, *scm.Response, error) {
/* #nosec */
hook := &scm.Hook{
ID: fmt.Sprintf("%d", rand.Int()),
Name: input.Name,
Expand Down
12 changes: 6 additions & 6 deletions scm/driver/fake/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ func (s *userService) DeleteToken(context.Context, int64) (*scm.Response, error)
return nil, scm.ErrNotSupported
}

func (u *userService) Find(ctx context.Context) (*scm.User, *scm.Response, error) {
return &u.data.CurrentUser, nil, nil
func (s *userService) Find(ctx context.Context) (*scm.User, *scm.Response, error) {
return &s.data.CurrentUser, nil, nil
}

func (u *userService) FindEmail(ctx context.Context) (string, *scm.Response, error) {
return u.data.CurrentUser.Email, nil, nil
func (s *userService) FindEmail(ctx context.Context) (string, *scm.Response, error) {
return s.data.CurrentUser.Email, nil, nil
}

func (u *userService) FindLogin(ctx context.Context, login string) (*scm.User, *scm.Response, error) {
for _, user := range u.data.Users {
func (s *userService) FindLogin(ctx context.Context, login string) (*scm.User, *scm.Response, error) {
for _, user := range s.data.Users {
if user.Login == login {
return user, nil, nil
}
Expand Down
1 change: 1 addition & 0 deletions scm/driver/gitea/gitea.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/jenkins-x/go-scm/scm"
)

// NewWebHookService creates a new instance of the webhook service without the rest of the client
func NewWebHookService() scm.WebhookService {
return &webhookService{nil}
}
Expand Down
Loading

0 comments on commit aa6b3d8

Please sign in to comment.