Skip to content

Commit

Permalink
Fix lint issues
Browse files Browse the repository at this point in the history
Signed-off-by: Jakub Hrozek <[email protected]>
  • Loading branch information
jhrozek committed Nov 6, 2022
1 parent 85a769a commit e06f940
Show file tree
Hide file tree
Showing 15 changed files with 79 additions and 55 deletions.
10 changes: 7 additions & 3 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
Expand Down Expand Up @@ -41,7 +41,7 @@ func getLogger() (logr.Logger, error) {
logIf := zapr.NewLogger(logger)
// NOTE(jaosorior): While this may return errors, they're mostly
// harmless and handling them is more work than its worth
// nolint:errcheck
//nolint:errcheck
defer logger.Sync() // flushes buffer, if any
return logIf, nil
}
Expand All @@ -50,7 +50,11 @@ func getHTTPClient(sockpath string) *http.Client {
return &http.Client{
Transport: &http.Transport{
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
return net.Dial("unix", sockpath)
conn, err := net.Dial("unix", sockpath)
if err != nil {
return nil, fmt.Errorf("error dialing unix socket: %w", err)
}
return conn, nil
},
},
}
Expand Down
7 changes: 3 additions & 4 deletions cmd/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
Expand All @@ -21,12 +21,11 @@ import (
"os/signal"
"syscall"

"github.com/spf13/cobra"

"github.com/containers/selinuxd/pkg/daemon"
"github.com/containers/selinuxd/pkg/datastore"
"github.com/containers/selinuxd/pkg/semodule"
"github.com/containers/selinuxd/pkg/version"
"github.com/spf13/cobra"
)

// daemonCmd represents the daemon command
Expand All @@ -42,7 +41,7 @@ to quickly create a Cobra application.`,
Run: daemonCmdFunc,
}

// nolint:gochecknoinits
//nolint:gochecknoinits
func init() {
rootCmd.AddCommand(daemonCmd)
defineFlags(daemonCmd)
Expand Down
7 changes: 3 additions & 4 deletions cmd/is-ready.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
Expand All @@ -23,9 +23,8 @@ import (
"os"
"syscall"

"github.com/spf13/cobra"

"github.com/containers/selinuxd/pkg/daemon"
"github.com/spf13/cobra"
)

// isreadyCmd represents the is-ready command
Expand All @@ -36,7 +35,7 @@ var isreadyCmd = &cobra.Command{
Run: isreadyCmdFunc,
}

// nolint:gochecknoinits
//nolint:gochecknoinits
func init() {
rootCmd.AddCommand(isreadyCmd)
defineIsReadyFlags(isreadyCmd)
Expand Down
9 changes: 4 additions & 5 deletions cmd/oneshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
Expand All @@ -20,14 +20,13 @@ import (
"os"
"syscall"

"github.com/go-logr/logr"
"github.com/spf13/cobra"

"github.com/containers/selinuxd/pkg/daemon"
"github.com/containers/selinuxd/pkg/datastore"
"github.com/containers/selinuxd/pkg/semodule"
seiface "github.com/containers/selinuxd/pkg/semodule/interface"
"github.com/containers/selinuxd/pkg/version"
"github.com/go-logr/logr"
"github.com/spf13/cobra"
)

// oneshotCmd represents the oneshot command
Expand All @@ -38,7 +37,7 @@ var oneshotCmd = &cobra.Command{
Run: oneshotCmdFunc,
}

// nolint:gochecknoinits
//nolint:gochecknoinits
func init() {
rootCmd.AddCommand(oneshotCmd)
defineOneShotFlags(oneshotCmd)
Expand Down
4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
Expand Down Expand Up @@ -50,7 +50,7 @@ func Execute() {
}
}

// nolint:gochecknoinits
//nolint:gochecknoinits
func init() {
cobra.OnInitialize(initConfig)

Expand Down
7 changes: 3 additions & 4 deletions cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
Expand All @@ -26,10 +26,9 @@ import (
"strings"
"syscall"

"github.com/containers/selinuxd/pkg/daemon"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"

"github.com/containers/selinuxd/pkg/daemon"
)

// statusCmd represents the status command
Expand All @@ -41,7 +40,7 @@ var statusCmd = &cobra.Command{
Run: statusCmdFunc,
}

// nolint:gochecknoinits
//nolint:gochecknoinits
func init() {
rootCmd.AddCommand(statusCmd)
defineStatusFlags(statusCmd)
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
Expand Down
24 changes: 16 additions & 8 deletions pkg/daemon/daemon.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package daemon

import (
"fmt"
"os"
"path/filepath"

"github.com/fsnotify/fsnotify"
"github.com/go-logr/logr"

"github.com/containers/selinuxd/pkg/datastore"
seiface "github.com/containers/selinuxd/pkg/semodule/interface"
"github.com/fsnotify/fsnotify"
"github.com/go-logr/logr"
)

type SelinuxdOptions struct {
Expand All @@ -23,7 +23,8 @@ type SelinuxdOptions struct {
// * `ds`: is the DataStore interface.
// * `l`: is a logger interface.
func Daemon(opts *SelinuxdOptions, mPath string, sh seiface.Handler, ds datastore.DataStore, done chan bool,
l logr.Logger) {
l logr.Logger,
) {
policyops := make(chan PolicyAction)
readychan := make(chan bool)

Expand Down Expand Up @@ -117,8 +118,7 @@ func watchFiles(watcher *fsnotify.Watcher, policyops chan PolicyAction, logger l
}

// InstallPolicies installs the policies found in the `modulePath` directory
// nolint: lll
func InstallPolicies(modulePath string, sh seiface.Handler, ds datastore.DataStore, policyops chan PolicyAction, logger logr.Logger) {
func InstallPolicies(modulePath string, sh seiface.Handler, ds datastore.DataStore, policyops chan PolicyAction, logger logr.Logger) { //nolint:lll
ilog := logger.WithName("policy-installer")
for action := range policyops {
if actionOut, err := action.do(modulePath, sh, ds); err != nil {
Expand All @@ -135,12 +135,16 @@ func InstallPolicies(modulePath string, sh seiface.Handler, ds datastore.DataSto
}

func InstallPoliciesInDir(mpath string, policyops chan PolicyAction, watcher *fsnotify.Watcher) error {
return filepath.Walk(mpath, func(path string, info os.FileInfo, err error) error {
err := filepath.Walk(mpath, func(path string, info os.FileInfo, err error) error {
if info == nil {
return nil
}
if watcher != nil && info.IsDir() {
return watcher.Add(path)
err := watcher.Add(path)
if err != nil {
return fmt.Errorf("unable to watch directory %s: %w", path, err)
}
return nil
} else if info.IsDir() {
// ignore directories
return nil
Expand All @@ -149,4 +153,8 @@ func InstallPoliciesInDir(mpath string, policyops chan PolicyAction, watcher *fs
policyops <- newInstallAction(path)
return nil
})
if err != nil {
return fmt.Errorf("unable to walk module directory: %w", err)
}
return nil
}
16 changes: 8 additions & 8 deletions pkg/daemon/daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ import (
"time"

backoff "github.com/cenkalti/backoff/v4"
"github.com/go-logr/zapr"
"go.uber.org/zap"

"github.com/containers/selinuxd/pkg/datastore"
"github.com/containers/selinuxd/pkg/semodule/test"
"github.com/go-logr/zapr"
"go.uber.org/zap"
)

const (
Expand All @@ -40,7 +39,7 @@ func getPolicyPath(module, path string) string {
func installPolicy(module, path string, t *testing.T) {
modPath := getPolicyPath(module, path)
message := []byte("Hello, Gophers!")
err := ioutil.WriteFile(modPath, message, 0600)
err := ioutil.WriteFile(modPath, message, 0o600)
if err != nil {
t.Fatal(err)
}
Expand All @@ -59,6 +58,7 @@ func getHTTPClient(sockpath string) *http.Client {
return &http.Client{
Transport: &http.Transport{
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
//nolint: wrapcheck // let's not complicate the test
return net.Dial("unix", sockpath)
},
},
Expand All @@ -73,7 +73,7 @@ func getReadyRequest(ctx context.Context, t *testing.T) *http.Request {
return req
}

// nolint:gocognit,gocyclo
//nolint:gocognit,gocyclo
func TestDaemon(t *testing.T) {
done := make(chan bool)
logger, err := zap.NewDevelopment()
Expand Down Expand Up @@ -294,7 +294,7 @@ func TestDaemon(t *testing.T) {
}

perms := fi.Mode().Perm()
if perms != 0660 {
if perms != 0o660 {
t.Fatalf("wrong perms, got %#o expected 0660", perms)
}

Expand All @@ -315,7 +315,7 @@ func TestDaemon(t *testing.T) {
subdirPolicy := "subdirpolicy"

t.Run("Module should track a policy in sub-directory", func(t *testing.T) {
if err := os.Mkdir(subdirPath, 0700); err != nil {
if err := os.Mkdir(subdirPath, 0o700); err != nil {
t.Fatalf("Unable to create sub-directory: %s", err)
}
installPolicy(subdirPolicy, subdirPath, t)
Expand Down Expand Up @@ -390,7 +390,7 @@ func TestDaemonWithSubdir(t *testing.T) {
subdirPolicy := "subdirpolicy"

t.Run("Install policy before daemon runs", func(t *testing.T) {
if err := os.Mkdir(subdirPath, 0700); err != nil {
if err := os.Mkdir(subdirPath, 0o700); err != nil {
t.Fatalf("Unable to create sub-directory: %s", err)
}
installPolicy(subdirPolicy, subdirPath, t)
Expand Down
10 changes: 6 additions & 4 deletions pkg/daemon/status_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ import (
"net/http"
"net/http/pprof"
"os"
"time"

"github.com/containers/selinuxd/pkg/datastore"
"github.com/go-logr/logr"
"github.com/gorilla/mux"

"github.com/containers/selinuxd/pkg/datastore"
)

const (
DefaultUnixSockAddr = "/var/run/selinuxd.sock"
unixSockMode = 0660
unixSockMode = 0o660
readTimeout = 5 * time.Second
)

type StatusServerConfig struct {
Expand Down Expand Up @@ -56,7 +57,8 @@ func (ss *statusServer) Serve(readychan <-chan bool) error {
ss.initializeRoutes(r)

server := &http.Server{
Handler: r,
Handler: r,
ReadTimeout: readTimeout,
}

go ss.waitForReady(readychan)
Expand Down
Loading

0 comments on commit e06f940

Please sign in to comment.