Skip to content

Commit

Permalink
Fix linter issues
Browse files Browse the repository at this point in the history
  • Loading branch information
hslatman committed Feb 17, 2025
1 parent 86c04f0 commit 0a97117
Show file tree
Hide file tree
Showing 33 changed files with 308 additions and 112 deletions.
6 changes: 3 additions & 3 deletions acme/api/revoke.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func isAccountAuthorized(_ context.Context, dbCert *acme.Certificate, certToBeRe
func wrapRevokeErr(err error) *acme.Error {
t := err.Error()
if strings.Contains(t, "is already revoked") {
return acme.NewError(acme.ErrorAlreadyRevokedType, t) //nolint:govet // allow non-constant error messages
return acme.NewError(acme.ErrorAlreadyRevokedType, t)
}
return acme.WrapErrorISE(err, "error when revoking certificate")
}
Expand All @@ -190,9 +190,9 @@ func wrapRevokeErr(err error) *acme.Error {
func wrapUnauthorizedError(cert *x509.Certificate, unauthorizedIdentifiers []acme.Identifier, msg string, err error) *acme.Error {
var acmeErr *acme.Error
if err == nil {
acmeErr = acme.NewError(acme.ErrorUnauthorizedType, msg) //nolint:govet // allow non-constant error messages
acmeErr = acme.NewError(acme.ErrorUnauthorizedType, msg)
} else {
acmeErr = acme.WrapError(acme.ErrorUnauthorizedType, err, msg) //nolint:govet // allow non-constant error messages
acmeErr = acme.WrapError(acme.ErrorUnauthorizedType, err, msg)
}
acmeErr.Status = http.StatusForbidden // RFC8555 7.6 shows example with 403

Expand Down
16 changes: 11 additions & 5 deletions acme/challenge.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"github.com/smallstep/certificates/acme/wire"
"github.com/smallstep/certificates/authority/provisioner"
wireprovisioner "github.com/smallstep/certificates/authority/provisioner/wire"
"github.com/smallstep/certificates/internal/cast"
)

type ChallengeType string
Expand Down Expand Up @@ -229,7 +230,7 @@ func tlsAlert(err error) uint8 {
if errors.As(err, &opErr) {
v := reflect.ValueOf(opErr.Err)
if v.Kind() == reflect.Uint8 {
return uint8(v.Uint())
return uint8(v.Uint()) //nolint:gosec // handled by checking its type
}
}
return 0
Expand Down Expand Up @@ -978,9 +979,9 @@ type tpmAttestationData struct {
type coseAlgorithmIdentifier int32

const (
coseAlgES256 coseAlgorithmIdentifier = -7
coseAlgRS256 coseAlgorithmIdentifier = -257
coseAlgRS1 coseAlgorithmIdentifier = -65535 // deprecated, but (still) often used in TPMs
coseAlgES256 = coseAlgorithmIdentifier(-7)
coseAlgRS256 = coseAlgorithmIdentifier(-257)
coseAlgRS1 = coseAlgorithmIdentifier(-65535) // deprecated, but (still) often used in TPMs
)

func doTPMAttestationFormat(_ context.Context, prov Provisioner, ch *Challenge, jwk *jose.JSONWebKey, att *attestationObject) (*tpmAttestationData, error) {
Expand Down Expand Up @@ -1105,8 +1106,13 @@ func doTPMAttestationFormat(_ context.Context, prov Provisioner, ch *Challenge,
return nil, NewDetailedError(ErrorBadAttestationStatementType, "invalid alg in attestation statement")
}

algI32, err := cast.SafeInt32(alg)
if err != nil {
return nil, WrapDetailedError(ErrorBadAttestationStatementType, err, "invalid alg %d in attestation statement", alg)
}

var hash crypto.Hash
switch coseAlgorithmIdentifier(alg) {
switch coseAlgorithmIdentifier(algI32) {
case coseAlgRS256, coseAlgES256:
hash = crypto.SHA256
case coseAlgRS1:
Expand Down
2 changes: 1 addition & 1 deletion acme/linker.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func GetUnescapedPathSuffix(typ LinkType, provisionerName string, inputs ...stri
case AccountLinkType, OrderLinkType, AuthzLinkType, CertificateLinkType:
return fmt.Sprintf("/%s/%s/%s", provisionerName, typ, inputs[0])
case ChallengeLinkType:
return fmt.Sprintf("/%s/%s/%s/%s", provisionerName, typ, inputs[0], inputs[1])
return fmt.Sprintf("/%s/%s/%s/%s", provisionerName, typ, inputs[0], inputs[1]) //nolint:gosec // operating on internally defined inputs
case OrdersByAccountLinkType:
return fmt.Sprintf("/%s/%s/%s/orders", provisionerName, AccountLinkType, inputs[0])
case FinalizeLinkType:
Expand Down
1 change: 0 additions & 1 deletion acme/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,6 @@ func (o *Order) Finalize(ctx context.Context, db DB, csr *x509.CertificateReques
// Add subproblem for webhook errors, others can be added later.
var webhookErr *webhook.Error
if errors.As(err, &webhookErr) {
//nolint:govet // ignore non-constant format string
acmeError := NewDetailedError(ErrorUnauthorizedType, webhookErr.Error())
acmeError.AddSubproblems(Subproblem{
Type: fmt.Sprintf("urn:smallstep:acme:error:%s", webhookErr.Code),
Expand Down
7 changes: 4 additions & 3 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"context"
"crypto"
"crypto/dsa" // support legacy algorithms
"crypto/dsa" //nolint:staticcheck // support legacy algorithms
"crypto/ecdsa"
"crypto/ed25519"
"crypto/rsa"
Expand All @@ -31,6 +31,7 @@ import (
"github.com/smallstep/certificates/authority/config"
"github.com/smallstep/certificates/authority/provisioner"
"github.com/smallstep/certificates/errs"
"github.com/smallstep/certificates/internal/cast"
"github.com/smallstep/certificates/logging"
)

Expand Down Expand Up @@ -595,8 +596,8 @@ func LogSSHCertificate(w http.ResponseWriter, cert *ssh.Certificate) {
m := map[string]interface{}{
"serial": cert.Serial,
"principals": cert.ValidPrincipals,
"valid-from": time.Unix(int64(cert.ValidAfter), 0).Format(time.RFC3339),
"valid-to": time.Unix(int64(cert.ValidBefore), 0).Format(time.RFC3339),
"valid-from": time.Unix(cast.Int64(cert.ValidAfter), 0).Format(time.RFC3339),
"valid-to": time.Unix(cast.Int64(cert.ValidBefore), 0).Format(time.RFC3339),
"certificate": certificate,
"certificate-type": certificateType,
}
Expand Down
5 changes: 3 additions & 2 deletions api/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/smallstep/certificates/authority/config"
"github.com/smallstep/certificates/authority/provisioner"
"github.com/smallstep/certificates/errs"
"github.com/smallstep/certificates/internal/cast"
"github.com/smallstep/certificates/templates"
)

Expand Down Expand Up @@ -331,8 +332,8 @@ func SSHSign(w http.ResponseWriter, r *http.Request) {
// Enforce the same duration as ssh certificate.
signOpts = append(signOpts, &identityModifier{
Identity: getIdentityURI(cr),
NotBefore: time.Unix(int64(cert.ValidAfter), 0),
NotAfter: time.Unix(int64(cert.ValidBefore), 0),
NotBefore: time.Unix(cast.Int64(cert.ValidAfter), 0),
NotAfter: time.Unix(cast.Int64(cert.ValidBefore), 0),
})

certChain, err := a.SignWithContext(ctx, cr, provisioner.SignOptions{}, signOpts...)
Expand Down
5 changes: 3 additions & 2 deletions api/sshRekey.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/smallstep/certificates/api/render"
"github.com/smallstep/certificates/authority/provisioner"
"github.com/smallstep/certificates/errs"
"github.com/smallstep/certificates/internal/cast"
)

// SSHRekeyRequest is the request body of an SSH certificate request.
Expand Down Expand Up @@ -80,8 +81,8 @@ func SSHRekey(w http.ResponseWriter, r *http.Request) {
}

// Match identity cert with the SSH cert
notBefore := time.Unix(int64(oldCert.ValidAfter), 0)
notAfter := time.Unix(int64(oldCert.ValidBefore), 0)
notBefore := time.Unix(cast.Int64(oldCert.ValidAfter), 0)
notAfter := time.Unix(cast.Int64(oldCert.ValidBefore), 0)

identity, err := renewIdentityCertificate(r, notBefore, notAfter)
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions api/sshRenew.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/smallstep/certificates/api/render"
"github.com/smallstep/certificates/authority/provisioner"
"github.com/smallstep/certificates/errs"
"github.com/smallstep/certificates/internal/cast"
)

// SSHRenewRequest is the request body of an SSH certificate request.
Expand Down Expand Up @@ -72,8 +73,8 @@ func SSHRenew(w http.ResponseWriter, r *http.Request) {
}

// Match identity cert with the SSH cert
notBefore := time.Unix(int64(oldCert.ValidAfter), 0)
notAfter := time.Unix(int64(oldCert.ValidBefore), 0)
notBefore := time.Unix(cast.Int64(oldCert.ValidAfter), 0)
notAfter := time.Unix(cast.Int64(oldCert.ValidBefore), 0)

identity, err := renewIdentityCertificate(r, notBefore, notAfter)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion authority/admin/api/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func (war *webhookAdminResponder) UpdateProvisionerWebhook(w http.ResponseWriter
}
if !found {
msg := fmt.Sprintf("provisioner %q has no webhook with the name %q", prov.Name, newWebhook.Name)
err := admin.NewError(admin.ErrorNotFoundType, msg) //nolint:govet // allow non-constant error messages
err := admin.NewError(admin.ErrorNotFoundType, msg)
render.Error(w, r, err)
return
}
Expand Down
7 changes: 4 additions & 3 deletions authority/linkedca.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/smallstep/certificates/authority/admin"
"github.com/smallstep/certificates/authority/provisioner"
"github.com/smallstep/certificates/db"
"github.com/smallstep/certificates/internal/cast"
)

const uuidPattern = "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$"
Expand Down Expand Up @@ -336,7 +337,7 @@ func (c *linkedCaClient) Revoke(crt *x509.Certificate, rci *db.RevokedCertificat
Serial: rci.Serial,
PemCertificate: serializeCertificate(crt),
Reason: rci.Reason,
ReasonCode: linkedca.RevocationReasonCode(rci.ReasonCode),
ReasonCode: linkedca.RevocationReasonCode(cast.Int32(rci.ReasonCode)),
Passive: true,
})

Expand All @@ -350,7 +351,7 @@ func (c *linkedCaClient) RevokeSSH(cert *ssh.Certificate, rci *db.RevokedCertifi
Serial: rci.Serial,
Certificate: serializeSSHCertificate(cert),
Reason: rci.Reason,
ReasonCode: linkedca.RevocationReasonCode(rci.ReasonCode),
ReasonCode: linkedca.RevocationReasonCode(cast.Int32(rci.ReasonCode)),
Passive: true,
})

Expand Down Expand Up @@ -403,7 +404,7 @@ func createProvisionerIdentity(p provisioner.Interface) *linkedca.ProvisionerIde
}
return &linkedca.ProvisionerIdentity{
Id: p.GetID(),
Type: linkedca.Provisioner_Type(p.GetType()),
Type: linkedca.Provisioner_Type(cast.Int32(int(p.GetType()))),
Name: p.GetName(),
}
}
Expand Down
6 changes: 4 additions & 2 deletions authority/provisioner/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import (
"strings"
"sync"

"github.com/smallstep/certificates/authority/admin"
"go.step.sm/crypto/jose"

"github.com/smallstep/certificates/authority/admin"
"github.com/smallstep/certificates/internal/cast"
)

// DefaultProvisionersLimit is the default limit for listing provisioners.
Expand Down Expand Up @@ -210,7 +212,7 @@ func (c *Collection) Store(p Interface) error {
// 0x00000000, 0x00000001, 0x00000002, ...
bi := make([]byte, 4)
sum := provisionerSum(p)
binary.BigEndian.PutUint32(bi, uint32(c.sorted.Len()))
binary.BigEndian.PutUint32(bi, cast.Uint32(c.sorted.Len()))
sum[0], sum[1], sum[2], sum[3] = bi[0], bi[1], bi[2], bi[3]
c.sorted = append(c.sorted, uidProvisioner{
provisioner: p,
Expand Down
11 changes: 7 additions & 4 deletions authority/provisioner/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ import (
"time"

"github.com/pkg/errors"
"golang.org/x/crypto/ssh"

"github.com/smallstep/linkedca"

"github.com/smallstep/certificates/errs"
"github.com/smallstep/certificates/internal/cast"
"github.com/smallstep/certificates/internal/httptransport"
"github.com/smallstep/certificates/webhook"
"github.com/smallstep/linkedca"
"golang.org/x/crypto/ssh"
)

// Controller wraps a provisioner with other attributes useful in callback
Expand Down Expand Up @@ -189,10 +192,10 @@ func DefaultAuthorizeSSHRenew(_ context.Context, p *Controller, cert *ssh.Certif
}

unixNow := time.Now().Unix()
if after := int64(cert.ValidAfter); after < 0 || unixNow < int64(cert.ValidAfter) {
if after := cast.Int64(cert.ValidAfter); after < 0 || unixNow < cast.Int64(cert.ValidAfter) {
return errs.Unauthorized("certificate is not yet valid")
}
if before := int64(cert.ValidBefore); cert.ValidBefore != uint64(ssh.CertTimeInfinity) && (unixNow >= before || before < 0) && !p.Claimer.AllowRenewalAfterExpiry() {
if before := cast.Int64(cert.ValidBefore); cert.ValidBefore != uint64(ssh.CertTimeInfinity) && (unixNow >= before || before < 0) && !p.Claimer.AllowRenewalAfterExpiry() {
return errs.Unauthorized("certificate has expired")
}

Expand Down
7 changes: 4 additions & 3 deletions authority/provisioner/jwk.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"go.step.sm/crypto/x509util"

"github.com/smallstep/certificates/errs"
"github.com/smallstep/certificates/internal/cast"
)

// jwtPayload extends jwt.Claims with step attributes.
Expand Down Expand Up @@ -249,7 +250,7 @@ func (p *JWK) AuthorizeSSHSign(_ context.Context, token string) ([]SignOption, e
// Use options in the token.
if opts.CertType != "" {
if certType, err = sshutil.CertTypeFromString(opts.CertType); err != nil {
return nil, errs.BadRequestErr(err, err.Error()) //nolint:govet // allow non-constant error messages
return nil, errs.BadRequestErr(err, err.Error())
}
}
if opts.KeyID != "" {
Expand All @@ -274,10 +275,10 @@ func (p *JWK) AuthorizeSSHSign(_ context.Context, token string) ([]SignOption, e
// Add modifiers from custom claims
t := now()
if !opts.ValidAfter.IsZero() {
signOptions = append(signOptions, sshCertValidAfterModifier(opts.ValidAfter.RelativeTime(t).Unix()))
signOptions = append(signOptions, sshCertValidAfterModifier(cast.Uint64(opts.ValidAfter.RelativeTime(t).Unix())))
}
if !opts.ValidBefore.IsZero() {
signOptions = append(signOptions, sshCertValidBeforeModifier(opts.ValidBefore.RelativeTime(t).Unix()))
signOptions = append(signOptions, sshCertValidBeforeModifier(cast.Uint64(opts.ValidBefore.RelativeTime(t).Unix())))
}

return append(signOptions,
Expand Down
7 changes: 4 additions & 3 deletions authority/provisioner/nebula.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@ import (

"github.com/pkg/errors"
nebula "github.com/slackhq/nebula/cert"
"golang.org/x/crypto/ssh"

"github.com/smallstep/linkedca"
"go.step.sm/crypto/jose"
"go.step.sm/crypto/sshutil"
"go.step.sm/crypto/x25519"
"go.step.sm/crypto/x509util"
"golang.org/x/crypto/ssh"

"github.com/smallstep/certificates/errs"
"github.com/smallstep/certificates/internal/cast"
)

const (
Expand Down Expand Up @@ -237,10 +238,10 @@ func (p *Nebula) AuthorizeSSHSign(_ context.Context, token string) ([]SignOption
// Add modifiers from custom claims
t := now()
if !opts.ValidAfter.IsZero() {
signOptions = append(signOptions, sshCertValidAfterModifier(opts.ValidAfter.RelativeTime(t).Unix()))
signOptions = append(signOptions, sshCertValidAfterModifier(cast.Uint64(opts.ValidAfter.RelativeTime(t).Unix())))
}
if !opts.ValidBefore.IsZero() {
signOptions = append(signOptions, sshCertValidBeforeModifier(opts.ValidBefore.RelativeTime(t).Unix()))
signOptions = append(signOptions, sshCertValidBeforeModifier(cast.Uint64(opts.ValidBefore.RelativeTime(t).Unix())))
}
}

Expand Down
Loading

0 comments on commit 0a97117

Please sign in to comment.