Skip to content

Commit

Permalink
happy linter
Browse files Browse the repository at this point in the history
  • Loading branch information
house-lee committed Sep 19, 2023
1 parent de5d00b commit 4d7bde7
Show file tree
Hide file tree
Showing 13 changed files with 35 additions and 43 deletions.
3 changes: 1 addition & 2 deletions cmd/agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,8 @@ func main() {
// launch the watcher
if err := metadataWatcher.Run(); err != nil {
log.Fatal("Failed to run watcher... %v", err)
} else {
log.Info("Watcher finished")
}
log.Info("Watcher finished")
}

func handleShutdown(bgJobsCancel context.CancelFunc, metadataWatcher watcher.MetadataWatcher, infoUpdater updater.AgentInfoUpdater, sshMgr *sysaccess.SSHManager) {
Expand Down
2 changes: 1 addition & 1 deletion internal/log/mutelog.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ func Mute() {

type muteLogger struct{}

func (*muteLogger) Output(calldepth int, s string) error {
func (*muteLogger) Output(_ int, _ string) error {
return nil
}
8 changes: 4 additions & 4 deletions internal/metadata/actioner/do_managed_keys_actioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,17 @@ func (da *doManagedKeysActioner) do(metadata *metadata.Metadata) {
da.sshMgr.DisableManagedDropletKeys()
}
// prepare ssh keys
for _, kRaw := range metadata.PublicKeys {
k, e := da.keyParser.FromPublicKey(kRaw)
for _, keyRaw := range metadata.PublicKeys {
k, e := da.keyParser.FromPublicKey(keyRaw)
if e != nil {
log.Error("[DO-Managed Keys Actioner] invalid public key object. %v", e)
continue
}
sshKeys = append(sshKeys, k)
}
// prepare dotty keys
for _, kRaw := range metadata.DOTTYKeys {
k, e := da.keyParser.FromDOTTYKey(kRaw)
for _, keyRaw := range metadata.DOTTYKeys {
k, e := da.keyParser.FromDOTTYKey(keyRaw)
if e != nil {
log.Error("[DO-Managed Keys Actioner] invalid ssh key object. %v", e)
continue
Expand Down
2 changes: 1 addition & 1 deletion internal/metadata/watcher/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const (
maxShutdownWaitTimeSeconds = 5
)

//Possible Errors
// Possible Errors
var (
ErrFetchMetadataFailed = errors.New("failed to fetch rmetadata")
ErrNoRegisteredActioner = errors.New("no registered actioners")
Expand Down
4 changes: 2 additions & 2 deletions internal/metadata/watcher/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package watcher
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"

"github.com/digitalocean/droplet-agent/internal/metadata"
Expand All @@ -32,7 +32,7 @@ func (m *metadataFetcherImpl) fetchMetadata() (*metadata.Metadata, error) {
_ = metaResp.Body.Close()
}()

metadataRaw, err := ioutil.ReadAll(metaResp.Body)
metadataRaw, err := io.ReadAll(metaResp.Body)
if err != nil {
return nil, fmt.Errorf("%w:%v", ErrFetchMetadataFailed, err)
}
Expand Down
5 changes: 3 additions & 2 deletions internal/metadata/watcher/web_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ func (w *webBasedWatcher) Run() error {
})

w.server = &http.Server{
Addr: webAddr,
Handler: r,
Addr: webAddr,
Handler: r,
ReadHeaderTimeout: 3 * time.Second,
}
if err := w.server.ListenAndServe(); err != nil {
if errors.Is(err, http.ErrServerClosed) {
Expand Down
6 changes: 3 additions & 3 deletions internal/mockutils/http_matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package mockutils
import (
"bytes"
"fmt"
"io/ioutil"
"io"
"net/http"

"github.com/golang/mock/gomock"
Expand Down Expand Up @@ -50,12 +50,12 @@ func (m *HTTPRequestMatcher) Matches(x interface{}) bool {
actualBodyReader := actual.Body
expectedBodyReader := m.ExpectedRequest.Body

actualBody, err := ioutil.ReadAll(actualBodyReader)
actualBody, err := io.ReadAll(actualBodyReader)
if err != nil {
return false
}

expectedBody, err := ioutil.ReadAll(expectedBodyReader)
expectedBody, err := io.ReadAll(expectedBodyReader)
if err != nil {
return false
}
Expand Down
5 changes: 1 addition & 4 deletions internal/sysaccess/authorized_keys_file_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@ func (u *updaterImpl) updateAuthorizedKeysFile(osUsername string, managedKeys []
localKeys = strings.Split(strings.TrimRight(string(localKeysRaw), "\n"), "\n")
}
updatedKeys := u.sshMgr.prepareAuthorizedKeys(localKeys, managedKeys)
if err = u.do(authorizedKeysFile, osUser, updatedKeys, fileExist); err != nil {
return err
}
return nil
return u.do(authorizedKeysFile, osUser, updatedKeys, fileExist)
}

func (u *updaterImpl) do(authorizedKeysFile string, user *sysutil.User, lines []string, srcFileExist bool) (retErr error) {
Expand Down
8 changes: 4 additions & 4 deletions internal/sysaccess/ssh_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ func (s *sshHelperImpl) authorizedKeysFile(user *sysutil.User) string {

// prepareAuthorizedKeys prepares the authorized keys that will be updated to filesystem
// NOTE: setting managedKeys to nil or empty slice will result in different behaviors
// - managedKeys = nil: will result in all temporary keys (keys with a TTL) being removed,
// but all permanent DO managed droplet keys will be preserved
// - managedKeys = []*SSHKey{}: means the droplet no longer has any DO managed keys (neither Droplet Keys nor DoTTY Keys),
// therefore, all DigitalOcean managed keys will be removed
// - managedKeys = nil: will result in all temporary keys (keys with a TTL) being removed,
// but all permanent DO managed droplet keys will be preserved
// - managedKeys = []*SSHKey{}: means the droplet no longer has any DO managed keys (neither Droplet Keys nor DoTTY Keys),
// therefore, all DigitalOcean managed keys will be removed
func (s *sshHelperImpl) prepareAuthorizedKeys(localKeys []string, managedKeys []*SSHKey) []string {
managedDropletKeysEnabled := atomic.LoadUint32(&s.mgr.manageDropletKeys) == manageDropletKeysEnabled
managedKeysQuickCheck := make(map[string]bool)
Expand Down
1 change: 1 addition & 0 deletions internal/sysaccess/ssh_helper_sshd_config_unix.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// SPDX-License-Identifier: Apache-2.0

//go:build !windows
// +build !windows

package sysaccess
Expand Down
27 changes: 11 additions & 16 deletions internal/sysaccess/sshmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,7 @@ func (s *SSHManager) RemoveExpiredKeys() (err error) {
return nil
})
}
if err := eg.Wait(); err != nil {
return err
}
return nil
return eg.Wait()
}

// UpdateKeys updates the given ssh keys to corresponding authorized_keys files.
Expand Down Expand Up @@ -218,10 +215,7 @@ func (s *SSHManager) RemoveDOTTYKeys() error {
return nil
})
}
if err := eg.Wait(); err != nil {
return err
}
return nil
return eg.Wait()
}

// SSHDPort returns the port sshd is binding to
Expand Down Expand Up @@ -286,15 +280,16 @@ func (s *SSHManager) Close() error {
}

// parseSSHDConfig parses the sshd_config file and retrieves configurations needed by the agent, which are:
// - AuthorizedKeysFile : to know how to locate the authorized_keys file
// - Port | ListenAddress : to know which port sshd is currently binding to
// - AuthorizedKeysFile : to know how to locate the authorized_keys file
// - Port | ListenAddress : to know which port sshd is currently binding to
//
// NOTES:
// - the port specified in the command line arguments (--sshd_port) when launching the agent has the highest priority,
// if given, parseSSHDConfig will skip parsing port numbers specified in the sshd_config
// - only 1 port is currently supported, if there are multiple ports presented, for example, multiple "Port" entries
// or more ports are found from `ListenAddress` entry/entries, the agent will only take the first one found, and this
// *MAY NOT* be the right one. If this happens to be the case, please explicit specify which port the agent should
// watch via the command line argument "--sshd_port"
// - the port specified in the command line arguments (--sshd_port) when launching the agent has the highest priority,
// if given, parseSSHDConfig will skip parsing port numbers specified in the sshd_config
// - only 1 port is currently supported, if there are multiple ports presented, for example, multiple "Port" entries
// or more ports are found from `ListenAddress` entry/entries, the agent will only take the first one found, and this
// *MAY NOT* be the right one. If this happens to be the case, please explicit specify which port the agent should
// watch via the command line argument "--sshd_port"
func (s *SSHManager) parseSSHDConfig() error {
defer func() {
if s.authorizedKeysFilePattern == "" {
Expand Down
4 changes: 2 additions & 2 deletions internal/sysutil/os_operations_unix.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
// SPDX-License-Identifier: Apache-2.0

//go:build !windows
// +build !windows

package sysutil

import (
"fmt"
"io"
"io/ioutil"
"os"
"strconv"
"strings"
)

func newOSOperator() osOperator {
return &osOperatorImpl{
readFileFn: ioutil.ReadFile,
readFileFn: os.ReadFile,
osStatFn: os.Stat,
osMkDir: os.MkdirAll,
osChown: os.Chown,
Expand Down
3 changes: 1 addition & 2 deletions internal/sysutil/sysmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"time"
Expand All @@ -27,7 +26,7 @@ type SysManager struct {

// ReadFile reads a file
func (s *SysManager) ReadFile(filename string) ([]byte, error) {
return ioutil.ReadFile(filename)
return os.ReadFile(filename)
}

// RenameFile renames a file
Expand Down

0 comments on commit 4d7bde7

Please sign in to comment.