Skip to content

Commit

Permalink
Enable staticcheck Go linter. (#23487)
Browse files Browse the repository at this point in the history
  • Loading branch information
getvictor authored Nov 5, 2024
1 parent 3bfbc56 commit 3cf99f8
Show file tree
Hide file tree
Showing 27 changed files with 70 additions and 70 deletions.
3 changes: 3 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ linters:
- unused
- errcheck
- gosimple
- staticcheck

linters-settings:
depguard:
Expand Down Expand Up @@ -69,6 +70,8 @@ linters-settings:
- name: superfluous-else
- name: unreachable-code
- name: redefines-builtin-id
staticcheck:
checks: ["all", "-SA9003", "-ST1000", "-ST1003", "-ST1016", "-ST1020", "-ST1021", "-ST1022", "-ST1023", "-SA1019"]

# Before excluding files from gosec linter - an issue must be created and referenced in a comment.
issues:
Expand Down
2 changes: 1 addition & 1 deletion cmd/fleetctl/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ func TestConvertFileOutput(t *testing.T) {

// setup a file for the convert command to write to
file, err := os.CreateTemp(t.TempDir(), "convert_output.yml")
defer file.Close()
require.NoError(t, err)
defer file.Close()

// get the program name
args := os.Args[0:1]
Expand Down
2 changes: 1 addition & 1 deletion ee/fleetctl/updates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ func TestRollback(t *testing.T) {
assert.True(t, os.IsNotExist(err))

// Roots should NOT have changed.
repo, err = openRepo(tmpDir)
_, err = openRepo(tmpDir)
require.NoError(t, err)
roots := getRoots(t, tmpDir)
assert.Equal(t, initialRoots, roots)
Expand Down
12 changes: 6 additions & 6 deletions orbit/pkg/cryptoinfo/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,26 @@ type kiDataNames string

const (
kiCaCertificate kiDataNames = "certificate"
kiCertificate = "certificate"
kiKey = "key"
kiCertificate kiDataNames = "certificate"
kiKey kiDataNames = "key"
)

// kiType is an internal type to denote what an indentified blob is. It is ultimately presented as a string
type kiType string

const (
kiCACERTIFICATE kiType = "CA-CERTIFICATE" // Not totally sure what the correct string is here
kiCERTIFICATE = "CERTIFICATE"
kiKEY = "KEY"
kiCERTIFICATE kiType = "CERTIFICATE"
kiKEY kiType = "KEY"
)

// kiType is an internal type to denote what encoding was used. It is ultimately presented as a string
type kiEncoding string

const (
kiPEM kiEncoding = "PEM"
kiDER = "DER"
kiP12 = "P12"
kiDER kiEncoding = "DER"
kiP12 kiEncoding = "P12"
)

func NewKey(encoding kiEncoding) *KeyInfo {
Expand Down
3 changes: 2 additions & 1 deletion orbit/pkg/installer/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,8 @@ func (r *Runner) runInstallerScript(ctx context.Context, scriptContents string,
return "", -1, fmt.Errorf("writing script: %w", err)
}

ctx, cancel := context.WithTimeout(context.Background(), r.installerExecutionTimeout)
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, r.installerExecutionTimeout)
defer cancel()

execFn := r.execCmdFn
Expand Down
4 changes: 1 addition & 3 deletions pkg/spec/gitops.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func (g *GitOps) IsNoTeam() bool {
}

func isNoTeam(teamName string) bool {
return strings.ToLower(teamName) == strings.ToLower(noTeam)
return strings.EqualFold(teamName, noTeam)
}

const noTeam = "No team"
Expand Down Expand Up @@ -588,9 +588,7 @@ func parsePolicyRunScript(baseDir string, teamName *string, policy *Policy, scri
}

scriptOnTeamFound := false
var foundScriptPaths []string
for _, script := range scripts {
foundScriptPaths = append(foundScriptPaths, *script.Path)
if scriptPath == *script.Path {
scriptOnTeamFound = true
break
Expand Down
2 changes: 0 additions & 2 deletions server/datastore/cached_mysql/cached_mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,6 @@ func (ds *cachedMysql) GetAllMDMConfigAssetsByName(ctx context.Context, assetNam

cachedAssets := make(map[fleet.MDMAssetName]fleet.MDMConfigAsset)
var missingAssets []fleet.MDMAssetName
var missingKeys []string

for _, name := range assetNames {
key := fmt.Sprintf(mdmConfigAssetKey, name, latestHashes[name])
Expand All @@ -428,7 +427,6 @@ func (ds *cachedMysql) GetAllMDMConfigAssetsByName(ctx context.Context, assetNam
}

missingAssets = append(missingAssets, name)
missingKeys = append(missingKeys, key)
}

if len(missingAssets) == 0 {
Expand Down
2 changes: 1 addition & 1 deletion server/datastore/mysql/apple_mdm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1112,7 +1112,7 @@ func expectAppleDeclarations(
// if an expected uploaded_at timestamp is provided for this declaration, keep
// its value, otherwise clear it as we don't care about asserting its
// value.
if wantD == nil || wantD.UploadedAt.IsZero() {
if wantD.UploadedAt.IsZero() {
gotD.UploadedAt = time.Time{}
}

Expand Down
2 changes: 1 addition & 1 deletion server/datastore/mysql/calendar_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (ds *Datastore) CreateOrUpdateCalendarEvent(
webhook_status = VALUES(webhook_status),
calendar_event_id = VALUES(calendar_event_id);
`
result, err = tx.ExecContext(
_, err = tx.ExecContext(
ctx,
hostCalendarEventsQuery,
hostID,
Expand Down
14 changes: 7 additions & 7 deletions server/datastore/mysql/hosts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1242,7 +1242,7 @@ func testHostsUnenrollFromMDM(t *testing.T, ds *Datastore) {
require.NoError(t, err)

// host_mdm entry should not exist anymore.
hmdm, err = ds.GetHostMDM(ctx, h2.ID)
_, err = ds.GetHostMDM(ctx, h2.ID)
require.NoError(t, err)

// host_mdm entry should still exist with empty values.
Expand Down Expand Up @@ -3528,7 +3528,7 @@ func testHostsListFailingPolicies(t *testing.T, ds *Datastore) {
require.NoError(t, ds.RecordPolicyQueryExecutions(context.Background(), h1, map[uint]*bool{p.ID: ptr.Bool(false)}, time.Now(), false))
checkHostIssues(t, ds, hosts, filter, h1.ID, 1)

checkHostIssuesWithOpts(t, ds, hosts, filter, h1.ID, fleet.HostListOptions{DisableIssues: true}, 0)
checkHostIssuesWithOpts(t, ds, filter, h1.ID, fleet.HostListOptions{DisableIssues: true}, 0)
}

// This doesn't work when running the whole test suite, but helps inspect individual tests
Expand Down Expand Up @@ -3619,13 +3619,13 @@ func testHostsReadsLessRows(t *testing.T, ds *Datastore) {
}

func checkHostIssues(t *testing.T, ds *Datastore, hosts []*fleet.Host, filter fleet.TeamFilter, hid uint, expected uint64) {
checkHostIssuesWithOpts(t, ds, hosts, filter, hid, fleet.HostListOptions{}, expected)
checkHostIssuesWithOpts(t, ds, filter, hid, fleet.HostListOptions{}, expected)
}

func checkHostIssuesWithOpts(
t *testing.T, ds *Datastore, hosts []*fleet.Host, filter fleet.TeamFilter, hid uint, opts fleet.HostListOptions, expected uint64,
t *testing.T, ds *Datastore, filter fleet.TeamFilter, hid uint, opts fleet.HostListOptions, expected uint64,
) {
hosts = listHostsCheckCount(t, ds, filter, opts, 10)
hosts := listHostsCheckCount(t, ds, filter, opts, 10)
foundH2 := false
var foundHost *fleet.Host
for _, host := range hosts {
Expand Down Expand Up @@ -7786,7 +7786,7 @@ func testHostsLoadHostByOrbitNodeKey(t *testing.T, ds *Datastore) {
// simulate the device being assigned to Fleet in ABM
err = ds.UpsertMDMAppleHostDEPAssignments(ctx, []fleet.Host{*hFleet}, abmToken.ID)
require.NoError(t, err)
loadFleet, err = ds.LoadHostByOrbitNodeKey(ctx, *hFleet.OrbitNodeKey)
_, err = ds.LoadHostByOrbitNodeKey(ctx, *hFleet.OrbitNodeKey)
require.NoError(t, err)

// simulate a failed JSON profile assignment
Expand All @@ -7795,7 +7795,7 @@ func testHostsLoadHostByOrbitNodeKey(t *testing.T, ds *Datastore) {
"foo", []string{hFleet.HardwareSerial}, string(fleet.DEPAssignProfileResponseFailed), &abmToken.ID,
)
require.NoError(t, err)
loadFleet, err = ds.LoadHostByOrbitNodeKey(ctx, *hFleet.OrbitNodeKey)
_, err = ds.LoadHostByOrbitNodeKey(ctx, *hFleet.OrbitNodeKey)
require.NoError(t, err)
}

Expand Down
18 changes: 9 additions & 9 deletions server/datastore/mysql/labels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1057,7 +1057,7 @@ func testListHostsInLabelIssues(t *testing.T, ds *Datastore) {
require.NoError(t, ds.RecordPolicyQueryExecutions(context.Background(), h1, map[uint]*bool{p.ID: ptr.Bool(true)}, time.Now(), false))

require.NoError(t, ds.RecordPolicyQueryExecutions(context.Background(), h2, map[uint]*bool{p.ID: ptr.Bool(false), p2.ID: ptr.Bool(false)}, time.Now(), false))
checkLabelHostIssues(t, ds, hosts, l1.ID, filter, h2.ID, fleet.HostListOptions{}, 2, 0)
checkLabelHostIssues(t, ds, l1.ID, filter, h2.ID, fleet.HostListOptions{}, 2, 0)

// Add a critical vulnerability
// seed software
Expand Down Expand Up @@ -1124,26 +1124,26 @@ func testListHostsInLabelIssues(t *testing.T, ds *Datastore) {
// Populate critical vulnerabilities, which can be done with premium license.
ctx = license.NewContext(ctx, &fleet.LicenseInfo{Tier: fleet.TierPremium})
assert.NoError(t, ds.UpdateHostIssuesVulnerabilities(ctx))
checkLabelHostIssues(t, ds, hosts, l1.ID, filter, hosts[6].ID, fleet.HostListOptions{}, 0, 4)
checkLabelHostIssues(t, ds, l1.ID, filter, hosts[6].ID, fleet.HostListOptions{}, 0, 4)

require.NoError(t, ds.RecordPolicyQueryExecutions(context.Background(), h2, map[uint]*bool{p.ID: ptr.Bool(true), p2.ID: ptr.Bool(false)}, time.Now(), false))
checkLabelHostIssues(t, ds, hosts, l1.ID, filter, h2.ID, fleet.HostListOptions{}, 1, 1)
checkLabelHostIssues(t, ds, l1.ID, filter, h2.ID, fleet.HostListOptions{}, 1, 1)

require.NoError(t, ds.RecordPolicyQueryExecutions(context.Background(), h2, map[uint]*bool{p.ID: ptr.Bool(true), p2.ID: ptr.Bool(true)}, time.Now(), false))
checkLabelHostIssues(t, ds, hosts, l1.ID, filter, h2.ID, fleet.HostListOptions{}, 0, 1)
checkLabelHostIssues(t, ds, l1.ID, filter, h2.ID, fleet.HostListOptions{}, 0, 1)

require.NoError(t, ds.RecordPolicyQueryExecutions(context.Background(), h1, map[uint]*bool{p.ID: ptr.Bool(false)}, time.Now(), false))
checkLabelHostIssues(t, ds, hosts, l1.ID, filter, h1.ID, fleet.HostListOptions{}, 1, 1)
checkLabelHostIssues(t, ds, l1.ID, filter, h1.ID, fleet.HostListOptions{}, 1, 1)

checkLabelHostIssues(t, ds, hosts, l1.ID, filter, h1.ID, fleet.HostListOptions{DisableIssues: true}, 0, 0)
checkLabelHostIssues(t, ds, hosts, l1.ID, filter, hosts[6].ID, fleet.HostListOptions{DisableIssues: true}, 0, 0)
checkLabelHostIssues(t, ds, l1.ID, filter, h1.ID, fleet.HostListOptions{DisableIssues: true}, 0, 0)
checkLabelHostIssues(t, ds, l1.ID, filter, hosts[6].ID, fleet.HostListOptions{DisableIssues: true}, 0, 0)
}

func checkLabelHostIssues(
t *testing.T, ds *Datastore, hosts []*fleet.Host, lid uint, filter fleet.TeamFilter, hid uint, opts fleet.HostListOptions,
t *testing.T, ds *Datastore, lid uint, filter fleet.TeamFilter, hid uint, opts fleet.HostListOptions,
failingPoliciesExpected uint64, criticalVulnerabilitiesExpected uint64,
) {
hosts = listHostsInLabelCheckCount(t, ds, filter, lid, opts, 10)
hosts := listHostsInLabelCheckCount(t, ds, filter, lid, opts, 10)
foundH2 := false
var foundHost *fleet.Host
for _, host := range hosts {
Expand Down
2 changes: 1 addition & 1 deletion server/datastore/mysql/packs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func testPacksDelete(t *testing.T, ds *Datastore) {
assert.Nil(t, err)

assert.NotEqual(t, uint(0), pack.ID)
pack, err = ds.Pack(context.Background(), pack.ID)
_, err = ds.Pack(context.Background(), pack.ID)
assert.NotNil(t, err)
}

Expand Down
4 changes: 2 additions & 2 deletions server/datastore/mysql/queries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func testQueriesGetByName(t *testing.T, ds *Datastore) {
require.Equal(t, "q1", actual.Name)
require.Equal(t, "select * from time", actual.Query)

actual, err = ds.QueryByName(context.Background(), nil, "xxx")
_, err = ds.QueryByName(context.Background(), nil, "xxx")
require.Error(t, err)
require.True(t, fleet.IsNotFound(err))

Expand All @@ -262,7 +262,7 @@ func testQueriesGetByName(t *testing.T, ds *Datastore) {
require.Equal(t, teamRocket.ID, *actual.TeamID)
require.Equal(t, "select * from time", actual.Query)

actual, err = ds.QueryByName(context.Background(), &teamRocket.ID, "xxx")
_, err = ds.QueryByName(context.Background(), &teamRocket.ID, "xxx")
require.Error(t, err)
require.True(t, fleet.IsNotFound(err))
}
Expand Down
4 changes: 2 additions & 2 deletions server/datastore/mysql/sessions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func testSessionsGetters(t *testing.T, ds *Datastore) {
assert.Equal(t, newSession.ID, gotByKey.ID)
require.Nil(t, gotByKey.APIOnly)

gotByID, err = ds.SessionByID(context.Background(), newSession.ID)
_, err = ds.SessionByID(context.Background(), newSession.ID)
require.NoError(t, err)
assert.Equal(t, newSession.ID, gotByKey.ID)
require.Nil(t, gotByKey.APIOnly)
Expand All @@ -108,7 +108,7 @@ func testSessionsGetters(t *testing.T, ds *Datastore) {
require.NotNil(t, gotByKey.APIOnly)
assert.True(t, *gotByKey.APIOnly)

gotByID, err = ds.SessionByID(context.Background(), apiSession.ID)
_, err = ds.SessionByID(context.Background(), apiSession.ID)
require.NoError(t, err)
assert.Equal(t, apiSession.ID, gotByKey.ID)
require.NotNil(t, gotByKey.APIOnly)
Expand Down
4 changes: 2 additions & 2 deletions server/datastore/mysql/teams_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,13 @@ func testTeamsList(t *testing.T, ds *Datastore) {
{User: user1, Role: "maintainer"},
{User: user2, Role: "observer"},
}
team1, err = ds.SaveTeam(context.Background(), team1)
_, err = ds.SaveTeam(context.Background(), team1)
require.NoError(t, err)

team2.Users = []fleet.TeamUser{
{User: user1, Role: "maintainer"},
}
team1, err = ds.SaveTeam(context.Background(), team2)
_, err = ds.SaveTeam(context.Background(), team2)
require.NoError(t, err)

teams, err = ds.ListTeams(context.Background(), fleet.TeamFilter{User: &user1}, fleet.ListOptions{})
Expand Down
2 changes: 1 addition & 1 deletion server/datastore/mysql/vpp.go
Original file line number Diff line number Diff line change
Expand Up @@ -1087,7 +1087,7 @@ func (ds *Datastore) GetVPPTokenByTeamID(ctx context.Context, teamID *uint) (*fl
err = sqlx.GetContext(ctx, ds.reader(ctx), &tokEnc, stmtNullTeam, fleet.NullTeamNoTeam)
}
if err != nil {
if errors.Is(sql.ErrNoRows, err) {
if errors.Is(err, sql.ErrNoRows) {
if err := sqlx.GetContext(ctx, ds.reader(ctx), &tokEnc, stmtNullTeam, fleet.NullTeamAllTeams); err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, ctxerr.Wrap(ctx, notFound("VPPToken"), "retrieving vpp token by team")
Expand Down
20 changes: 10 additions & 10 deletions server/datastore/mysql/vpp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ func testGetVPPAppByTeamAndTitleID(t *testing.T, ds *Datastore) {
require.Equal(t, "foo", gotVPPApp.AdamID)
require.Equal(t, fooTitleID, gotVPPApp.TitleID)
// title that doesn't exist
gotVPPApp, err = ds.GetVPPAppByTeamAndTitleID(ctx, &team.ID, 999)
_, err = ds.GetVPPAppByTeamAndTitleID(ctx, &team.ID, 999)
require.ErrorAs(t, err, &nfe)

// create an entry for the global team
Expand All @@ -676,7 +676,7 @@ func testGetVPPAppByTeamAndTitleID(t *testing.T, ds *Datastore) {
require.NoError(t, err)
barTitleID := barApp.TitleID
// not found providing the team id
gotVPPApp, err = ds.GetVPPAppByTeamAndTitleID(ctx, &team.ID, barTitleID)
_, err = ds.GetVPPAppByTeamAndTitleID(ctx, &team.ID, barTitleID)
require.ErrorAs(t, err, &nfe)
// found for the global team
gotVPPApp, err = ds.GetVPPAppByTeamAndTitleID(ctx, nil, barTitleID)
Expand Down Expand Up @@ -852,7 +852,7 @@ func testVPPTokensCRUD(t *testing.T, ds *Datastore) {
assert.Equal(t, uint(0), teamTok.Teams[0].ID)
assert.Equal(t, fleet.TeamNameNoTeam, teamTok.Teams[0].Name)

teamTok, err = ds.GetVPPTokenByTeamID(ctx, &team.ID)
_, err = ds.GetVPPTokenByTeamID(ctx, &team.ID)
assert.Error(t, err)
assert.True(t, fleet.IsNotFound(err))

Expand All @@ -876,7 +876,7 @@ func testVPPTokensCRUD(t *testing.T, ds *Datastore) {
assert.Equal(t, team.ID, toks[0].Teams[0].ID)
assert.Equal(t, team.Name, toks[0].Teams[0].Name)

teamTok, err = ds.GetVPPTokenByTeamID(ctx, nil)
_, err = ds.GetVPPTokenByTeamID(ctx, nil)
assert.Error(t, err)
assert.True(t, fleet.IsNotFound(err))

Expand Down Expand Up @@ -926,11 +926,11 @@ func testVPPTokensCRUD(t *testing.T, ds *Datastore) {
assert.NoError(t, err)
assert.Len(t, toks, 1)

teamTok, err = ds.GetVPPTokenByTeamID(ctx, &team.ID)
_, err = ds.GetVPPTokenByTeamID(ctx, &team.ID)
assert.Error(t, err)
assert.True(t, fleet.IsNotFound(err))

teamTok, err = ds.GetVPPTokenByTeamID(ctx, nil)
_, err = ds.GetVPPTokenByTeamID(ctx, nil)
assert.Error(t, err)
assert.True(t, fleet.IsNotFound(err))

Expand Down Expand Up @@ -1059,7 +1059,7 @@ func testVPPTokensCRUD(t *testing.T, ds *Datastore) {
assert.NoError(t, err)
assert.Equal(t, tokTeams.ID, tokNil.ID)

tokTeam1, err = ds.GetVPPTokenByTeamID(ctx, &team.ID)
_, err = ds.GetVPPTokenByTeamID(ctx, &team.ID)
assert.Error(t, err)

tokTeam2, err = ds.GetVPPTokenByTeamID(ctx, &team2.ID)
Expand All @@ -1070,15 +1070,15 @@ func testVPPTokensCRUD(t *testing.T, ds *Datastore) {
err = ds.DeleteVPPToken(ctx, tokTeams.ID)
assert.NoError(t, err)

tokNil, err = ds.GetVPPTokenByTeamID(ctx, nil)
_, err = ds.GetVPPTokenByTeamID(ctx, nil)
assert.Error(t, err)
assert.True(t, fleet.IsNotFound(err))

tokTeam1, err = ds.GetVPPTokenByTeamID(ctx, &team.ID)
_, err = ds.GetVPPTokenByTeamID(ctx, &team.ID)
assert.Error(t, err)
assert.True(t, fleet.IsNotFound(err))

tokTeam2, err = ds.GetVPPTokenByTeamID(ctx, &team2.ID)
_, err = ds.GetVPPTokenByTeamID(ctx, &team2.ID)
assert.Error(t, err)
assert.True(t, fleet.IsNotFound(err))

Expand Down
2 changes: 1 addition & 1 deletion server/mail/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type PasswordResetMailer struct {
}

func (r PasswordResetMailer) Message() ([]byte, error) {
r.CurrentYear = time.Now().Year()
r.CurrentYear = time.Now().Year() // nolint:staticcheck // SA4005 false positive for Go templates
t, err := server.GetTemplate("server/mail/templates/password_reset.html", "email_template")
if err != nil {
return nil, err
Expand Down
1 change: 0 additions & 1 deletion server/mdm/nanomdm/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,5 @@ func (s slowReader) Read(p []byte) (n int, err error) {

time.Sleep(200 * time.Millisecond)
n = copy(p, s.b[:len(s.b)/2])
s.b = s.b[n:]
return n, nil
}
Loading

0 comments on commit 3cf99f8

Please sign in to comment.