Skip to content

Commit 7ded501

Browse files
Add go vet and staticcheck to CI (#183)
* Add go vet and staticcheck to CI Fix go vet bugs Make staticcheck happy Add to go.mod Revert "Add to go.mod" This reverts commit 292d57f. Specify staticcheck version Try without go.mod Attempt Attempt Attempt Fail early Fail early Attempt Attempt Attempt Attempt * Undo accidental upgrade * unused var * Use assert library * Remove special case for 1.15
1 parent b9d59f0 commit 7ded501

File tree

17 files changed

+58
-76
lines changed

17 files changed

+58
-76
lines changed

.github/workflows/build.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,9 @@ jobs:
3333
run: |
3434
GO111MODULE=off go get github.com/mattn/goveralls
3535
$(go env GOPATH)/bin/goveralls -coverprofile=profile.cov -service=github
36+
- name: Vet
37+
run: go vet ./...
38+
- name: Install staticcheck
39+
run: "go install honnef.co/go/tools/cmd/[email protected]"
40+
- name: Run staticcheck
41+
run: staticcheck ./...

client/client.go

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package client
33
import (
44
"bytes"
55
"encoding/json"
6-
"errors"
76
"io"
87
"io/ioutil"
98
"log"
@@ -560,56 +559,6 @@ func (c *Client) downloadMetaUnsafe(name string, maxMetaSize int64) ([]byte, err
560559
return ioutil.ReadAll(io.LimitReader(r, maxMetaSize))
561560
}
562561

563-
// getRootAndLocalVersionsUnsafe decodes the versions stored in the local
564-
// metadata without verifying signatures to protect against downgrade attacks
565-
// when the root is replaced and contains new keys. It also sets the local meta
566-
// cache to only contain the local root metadata.
567-
func (c *Client) getRootAndLocalVersionsUnsafe() error {
568-
type versionData struct {
569-
Signed struct {
570-
Version int
571-
}
572-
}
573-
574-
meta, err := c.local.GetMeta()
575-
if err != nil {
576-
return err
577-
}
578-
579-
getVersion := func(name string) (int, error) {
580-
m, ok := meta[name]
581-
if !ok {
582-
return 0, nil
583-
}
584-
var data versionData
585-
if err := json.Unmarshal(m, &data); err != nil {
586-
return 0, err
587-
}
588-
return data.Signed.Version, nil
589-
}
590-
591-
c.timestampVer, err = getVersion("timestamp.json")
592-
if err != nil {
593-
return err
594-
}
595-
c.snapshotVer, err = getVersion("snapshot.json")
596-
if err != nil {
597-
return err
598-
}
599-
c.targetsVer, err = getVersion("targets.json")
600-
if err != nil {
601-
return err
602-
}
603-
604-
root, ok := meta["root.json"]
605-
if !ok {
606-
return errors.New("tuf: missing local root after downloading, this should not be possible")
607-
}
608-
c.localMeta = map[string]json.RawMessage{"root.json": root}
609-
610-
return nil
611-
}
612-
613562
// remoteGetFunc is the type of function the download method uses to download
614563
// remote files
615564
type remoteGetFunc func(string) (io.ReadCloser, int64, error)
@@ -790,6 +739,7 @@ func (c *Client) localMetaFromSnapshot(name string, m data.SnapshotFileMeta) (js
790739
}
791740

792741
// hasTargetsMeta checks whether local metadata has the given snapshot meta
742+
//lint:ignore U1000 unused
793743
func (c *Client) hasTargetsMeta(m data.SnapshotFileMeta) bool {
794744
b, ok := c.localMeta["targets.json"]
795745
if !ok {
@@ -804,6 +754,7 @@ func (c *Client) hasTargetsMeta(m data.SnapshotFileMeta) bool {
804754
}
805755

806756
// hasSnapshotMeta checks whether local metadata has the given meta
757+
//lint:ignore U1000 unused
807758
func (c *Client) hasMetaFromTimestamp(name string, m data.TimestampFileMeta) bool {
808759
b, ok := c.localMeta[name]
809760
if !ok {

client/client_test.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func (f *fakeFile) Read(p []byte) (int, error) {
8484
}
8585

8686
func (f *fakeFile) Close() error {
87-
f.buf.Seek(0, os.SEEK_SET)
87+
f.buf.Seek(0, io.SeekStart)
8888
return nil
8989
}
9090

@@ -905,14 +905,21 @@ func (s *ClientSuite) TestUpdateReplayAttack(c *C) {
905905
c.Assert(s.repo.Timestamp(), IsNil)
906906
s.syncRemote(c)
907907
_, err := client.Update()
908+
c.Assert(err, IsNil)
908909
c.Assert(client.timestampVer > version, Equals, true)
909910

910911
// replace remote timestamp.json with the old one
911912
s.remote.meta["timestamp.json"] = oldTimestamp
912913

913914
// check update returns ErrLowVersion
914915
_, err = client.Update()
915-
c.Assert(err, DeepEquals, ErrDecodeFailed{"timestamp.json", verify.ErrLowVersion{version, client.timestampVer}})
916+
c.Assert(err, DeepEquals, ErrDecodeFailed{
917+
File: "timestamp.json",
918+
Err: verify.ErrLowVersion{
919+
Actual: version,
920+
Current: client.timestampVer,
921+
},
922+
})
916923
}
917924

918925
func (s *ClientSuite) TestUpdateTamperedTargets(c *C) {
@@ -1109,7 +1116,7 @@ func (s *ClientSuite) TestUnknownKeyIDs(c *C) {
11091116

11101117
var root struct {
11111118
Signed data.Root `json:"signed"`
1112-
Signatures []data.Signature `json:signatures"`
1119+
Signatures []data.Signature `json:"signatures"`
11131120
}
11141121
c.Assert(json.Unmarshal(rootJSON, &root), IsNil)
11151122

@@ -1137,6 +1144,7 @@ func (s *ClientSuite) TestUnknownKeyIDs(c *C) {
11371144
// the TUF-0.9 update workflow, where we decide to update the root
11381145
// metadata when we observe a new root through the snapshot.
11391146
repo, err := tuf.NewRepo(s.store)
1147+
c.Assert(err, IsNil)
11401148
c.Assert(repo.Snapshot(), IsNil)
11411149
c.Assert(repo.Timestamp(), IsNil)
11421150
c.Assert(repo.Commit(), IsNil)

client/delegations_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ func initTestDelegationClient(t *testing.T, dirPrefix string) (*Client, func() e
263263
TargetsPath: "targets",
264264
}
265265
remote, err := HTTPRemoteStore(fmt.Sprintf("http://%s/", addr), opts, nil)
266+
assert.Nil(t, err)
266267

267268
c := NewClient(MemoryLocalStore(), remote)
268269
rawFile, err := ioutil.ReadFile(initialStateDir + "/" + "root.json")

client/errors.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ func (e ErrMaxDelegations) Error() string {
4949
return fmt.Sprintf("tuf: max delegation of %d reached searching for %s with snapshot version %d", e.MaxDelegations, e.Target, e.SnapshotVersion)
5050
}
5151

52+
//lint:ignore U1000 unused
5253
func isDecodeFailedWithErrRoleThreshold(err error) bool {
5354
e, ok := err.(ErrDecodeFailed)
5455
if !ok {

client/interop_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ func computeHashes(c *C, dir string) map[string]string {
5353
hashes := make(map[string]string)
5454

5555
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
56+
if err != nil {
57+
return err
58+
}
59+
5660
if info.IsDir() {
5761
return nil
5862
}

cmd/tuf-client/get.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func cmdGet(args *docopt.Args, client *tuf.Client) error {
4444
return err
4545
}
4646
defer tmp.Delete()
47-
if _, err := tmp.Seek(0, os.SEEK_SET); err != nil {
47+
if _, err := tmp.Seek(0, io.SeekStart); err != nil {
4848
return err
4949
}
5050
_, err = io.Copy(os.Stdout, file)

cmd/tuf/main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
docopt "github.com/flynn/go-docopt"
1515
tuf "github.com/theupdateframework/go-tuf"
1616
"github.com/theupdateframework/go-tuf/util"
17-
"golang.org/x/crypto/ssh/terminal"
17+
"golang.org/x/term"
1818
)
1919

2020
func main() {
@@ -143,7 +143,7 @@ func getPassphrase(role string, confirm bool, change bool) ([]byte, error) {
143143
role = fmt.Sprintf("new %s", role)
144144
}
145145
fmt.Printf("Enter %s keys passphrase: ", role)
146-
passphrase, err := terminal.ReadPassword(int(syscall.Stdin))
146+
passphrase, err := term.ReadPassword(int(syscall.Stdin))
147147
fmt.Println()
148148
if err != nil {
149149
return nil, err
@@ -154,14 +154,14 @@ func getPassphrase(role string, confirm bool, change bool) ([]byte, error) {
154154
}
155155

156156
fmt.Printf("Repeat %s keys passphrase: ", role)
157-
confirmation, err := terminal.ReadPassword(int(syscall.Stdin))
157+
confirmation, err := term.ReadPassword(int(syscall.Stdin))
158158
fmt.Println()
159159
if err != nil {
160160
return nil, err
161161
}
162162

163163
if !bytes.Equal(passphrase, confirmation) {
164-
return nil, errors.New("The entered passphrases do not match")
164+
return nil, errors.New("the entered passphrases do not match")
165165
}
166166
return passphrase, nil
167167
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ require (
99
github.com/stretchr/testify v1.7.0
1010
github.com/syndtr/goleveldb v1.0.0
1111
golang.org/x/crypto v0.0.0-20211117183948-ae814b36b871
12+
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211
1213
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405
1314
)

go.sum

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,12 @@ golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5h
3535
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
3636
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
3737
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4=
38+
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4=
39+
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
3840
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
39-
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E=
4041
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
42+
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
43+
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
4144
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
4245
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
4346
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=

0 commit comments

Comments
 (0)