Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linter fixes #108

Merged
merged 2 commits into from
Apr 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,3 @@ jobs:

- name: Run golangci-lint
uses: actions-contrib/golangci-lint@v1
continue-on-error: true #TODO : fix linter errors
3 changes: 1 addition & 2 deletions certstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ func main() {
cmd.NewRevokeCommand(),
}
app.Before = func(c *cli.Context) error {
cmd.InitDepot(c.String("depot-path"))
return nil
return cmd.InitDepot(c.String("depot-path"))
}

if err := app.Run(os.Args); err != nil {
Expand Down
7 changes: 6 additions & 1 deletion cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ func initAction(c *cli.Context) {
var key *pkix.Key
if c.IsSet("key") {
keyBytes, err := ioutil.ReadFile(c.String("key"))
if err != nil {
fmt.Fprintln(os.Stderr, "Read Key error:", err)
os.Exit(1)
}

key, err = pkix.NewKeyFromPrivateKeyPEM(keyBytes)
if err != nil {
fmt.Fprintln(os.Stderr, "Read Key error:", err)
Expand Down Expand Up @@ -163,7 +168,7 @@ func initAction(c *cli.Context) {
fmt.Fprintln(os.Stderr, "Print CA certificate error:", err)
os.Exit(1)
} else {
fmt.Printf(string(crtBytes))
fmt.Println(string(crtBytes))
}
}

Expand Down
7 changes: 6 additions & 1 deletion cmd/request_cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ func newCertAction(c *cli.Context) {
keyFilepath := fileName(c, "key", depotDir, formattedName, "key")
if c.IsSet("key") && fileExists(c.String("key")) {
keyBytes, err := ioutil.ReadFile(c.String("key"))
if err != nil {
fmt.Fprintln(os.Stderr, "Read Key error:", err)
os.Exit(1)
}

key, err = pkix.NewKeyFromPrivateKeyPEM(keyBytes)
if err != nil {
fmt.Fprintln(os.Stderr, "Read Key error:", err)
Expand Down Expand Up @@ -191,7 +196,7 @@ func newCertAction(c *cli.Context) {
fmt.Fprintln(os.Stderr, "Print certificate request error:", err)
os.Exit(1)
} else {
fmt.Printf(string(csrBytes))
fmt.Println(string(csrBytes))
}
}

Expand Down
4 changes: 3 additions & 1 deletion cmd/revoke_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ func TestRevokeCmd(t *testing.T) {
fs := flag.NewFlagSet("test", flag.ContinueOnError)
fs.String("CA", "", "")
fs.String("CN", "", "")
fs.Parse([]string{"-CA", "ca", "-CN", "cn"})
if err := fs.Parse([]string{"-CA", "ca", "-CN", "cn"}); err != nil {
t.Fatal("could not parse flags")
}

new(revokeCommand).run(cli.NewContext(nil, fs, nil))

Expand Down
2 changes: 1 addition & 1 deletion cmd/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func newSignAction(c *cli.Context) {
fmt.Fprintln(os.Stderr, "Print certificate error:", err)
os.Exit(1)
} else {
fmt.Printf(string(crtBytes))
fmt.Println(string(crtBytes))
}
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func createPassPhrase() ([]byte, error) {
return nil, err
}

if bytes.Compare(pass1, pass2) != 0 {
if !bytes.Equal(pass1, pass2) {
return nil, errors.New("Passphrases do not match.")
}
return pass1, nil
Expand Down
1 change: 1 addition & 0 deletions depot/depot.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ func (d *FileDepot) Delete(tag *Tag) error {
func (d *FileDepot) List() []*Tag {
var tags = make([]*Tag, 0)

//nolint:errcheck
filepath.Walk(d.dirPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return nil
Expand Down
22 changes: 15 additions & 7 deletions depot/depot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,20 @@ func TestDepotCRUD(t *testing.T) {
if err != nil {
t.Fatal("Failed getting file from Depot:", err)
}
if bytes.Compare(dataRead, []byte(data)) != 0 {
if !bytes.Equal(dataRead, []byte(data)) {
t.Fatal("Failed getting the previous data")
}

if err = d.Put(tag, []byte(data)); err == nil || !os.IsExist(err) {
t.Fatal("Expect not to put file into Depot:", err)
}

d.Delete(tag)
if err := d.Delete(tag); err != nil {
t.Fatal("Failed to delete a tag:", err)
}

if d.Check(tag) {
t.Fatal("Failed deleting file from Depot:", err)
t.Fatal("Expected the tag to be deleted")
}
}

Expand All @@ -85,7 +87,9 @@ func TestDepotPutNil(t *testing.T) {
t.Fatal("Failed putting file into Depot:", err)
}

d.Delete(tag)
if err := d.Delete(tag); err != nil {
t.Fatal("Failed to delete a tag:", err)
}
}

func TestDepotCheckFailure(t *testing.T) {
Expand All @@ -104,7 +108,9 @@ func TestDepotCheckFailure(t *testing.T) {
t.Fatal("Expect not to checking out file with nonexist name")
}

d.Delete(tag)
if err := d.Delete(tag); err != nil {
t.Fatal("Failed to delete a tag:", err)
}
}

func TestDepotGetFailure(t *testing.T) {
Expand All @@ -123,7 +129,9 @@ func TestDepotGetFailure(t *testing.T) {
t.Fatal("Expect not to checking out file with nonexist name")
}

d.Delete(tag)
if err := d.Delete(tag); err != nil {
t.Fatal("Failed to delete a tag:", err)
}
}

func TestDepotList(t *testing.T) {
Expand Down Expand Up @@ -158,7 +166,7 @@ func TestDepotGetFile(t *testing.T) {
if err != nil {
t.Fatal("Failed getting file from Depot:", err)
}
if bytes.Compare(file.Data, []byte(data)) != 0 {
if !bytes.Equal(file.Data, []byte(data)) {
t.Fatal("Failed getting the previous data")
}

Expand Down
4 changes: 2 additions & 2 deletions pkix/cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ func (c *Certificate) GetRawCertificate() (*x509.Certificate, error) {
// GetExpirationDuration gets time duration before expiration
func (c *Certificate) GetExpirationDuration() time.Duration {
if err := c.buildX509Certificate(); err != nil {
return time.Unix(0, 0).Sub(time.Now())
return time.Until(time.Unix(0, 0))
}
return c.crt.NotAfter.Sub(time.Now())
return time.Until(c.crt.NotAfter)
}

// CheckAuthority checks the authority of certificate against itself.
Expand Down
4 changes: 2 additions & 2 deletions pkix/cert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func TestCertificateAuthority(t *testing.T) {
if err != nil {
t.Fatal("Failed exporting PEM-format bytes:", err)
}
if bytes.Compare(pemBytes, []byte(certAuthPEM)) != 0 {
if !bytes.Equal(pemBytes, []byte(certAuthPEM)) {
t.Fatal("Failed exporting the same PEM-format bytes")
}
}
Expand Down Expand Up @@ -153,7 +153,7 @@ func TestBadCertificate(t *testing.T) {
if err != nil {
t.Fatal("Failed exporting PEM-format bytes:", err)
}
if bytes.Compare(pemBytes, []byte(badCertAuthPEM)) != 0 {
if !bytes.Equal(pemBytes, []byte(badCertAuthPEM)) {
t.Fatal("Failed exporting the same PEM-format bytes")
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkix/crl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func TestCertificateRevocationList(t *testing.T) {
if err != nil {
t.Fatal("Failed exporting PEM-format bytes:", err)
}
if bytes.Compare(pemBytes, []byte(crlPEM)) != 0 {
if !bytes.Equal(pemBytes, []byte(crlPEM)) {
t.Fatal("Failed exporting the same PEM-format bytes")
}
}
2 changes: 2 additions & 0 deletions pkix/csr.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ func checkSignature(csr *x509.CertificateRequest, algo x509.SignatureAlgorithm,
return x509.ErrUnsupportedAlgorithm
}
h := hashType.New()

// nolint:errcheck
h.Write(signed)
digest := h.Sum(nil)
switch pub := csr.PublicKey.(type) {
Expand Down
4 changes: 2 additions & 2 deletions pkix/csr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func TestCertificateSigningRequest(t *testing.T) {
if err != nil {
t.Fatal("Failed exporting PEM-format bytes:", err)
}
if bytes.Compare(pemBytes, []byte(csrPEM)) != 0 {
if !bytes.Equal(pemBytes, []byte(csrPEM)) {
t.Fatal("Failed exporting the same PEM-format bytes")
}
}
Expand All @@ -134,7 +134,7 @@ func TestOldStyleCertificateSigningRequest(t *testing.T) {
if err != nil {
t.Fatal("Failed exporting PEM-format bytes:", err)
}
if bytes.Compare(pemBytes, []byte(csrPEM)) != 0 {
if !bytes.Equal(pemBytes, []byte(csrPEM)) {
t.Fatal("Failed exporting the same PEM-format bytes")
}
}
Expand Down
6 changes: 3 additions & 3 deletions pkix/key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func TestRSAKeyExport(t *testing.T) {
if err != nil {
t.Fatal("Failed exporting PEM-format bytes:", err)
}
if bytes.Compare(pemBytes, []byte(rsaPrivKeyAuthPEM)) != 0 {
if !bytes.Equal(pemBytes, []byte(rsaPrivKeyAuthPEM)) {
t.Fatal("Failed exporting the same PEM-format bytes")
}
}
Expand All @@ -164,7 +164,7 @@ func TestRSAKeyExportEncrypted(t *testing.T) {
if err != nil {
t.Fatal("Failed exporting PEM-format bytes:", err)
}
if bytes.Compare(pemBytes, []byte(rsaPrivKeyAuthPEM)) != 0 {
if !bytes.Equal(pemBytes, []byte(rsaPrivKeyAuthPEM)) {
t.Fatal("Failed exporting the same PEM-format bytes")
}

Expand All @@ -189,7 +189,7 @@ func TestRSAKeyGenerateSubjectKeyID(t *testing.T) {
t.Fatal("Failed generating SubjectKeyId:", err)
}
correctID, _ := base64.StdEncoding.DecodeString(subjectKeyIDOfRSAPubKeyAuthBASE64)
if bytes.Compare(id, correctID) != 0 {
if !bytes.Equal(id, correctID) {
t.Fatal("Failed generating correct SubjectKeyId")
}
}