From 633e932b60c78891ec0ad47861b39758b2b5c3b7 Mon Sep 17 00:00:00 2001 From: Brian Nuszkowski Date: Fri, 7 Apr 2023 14:40:46 -0400 Subject: [PATCH] Enforce linting, fix suggestions (#102) * Capture gofmt errors, add golint * Resolve linting violations --- Makefile | 4 ++++ pkg/auth/auth.go | 12 ++++++------ pkg/auth/azure.go | 2 ++ pkg/auth/gcp.go | 1 + pkg/auth/k8s.go | 2 ++ pkg/daytona/options.go | 1 + pkg/daytona/unmarshal.go | 14 +++++++------- pkg/helpers/file.go | 1 + pkg/helpers/testhelpers/testing.go | 2 ++ pkg/logging/logging.go | 3 +++ pkg/pki/pki_test.go | 6 +++--- pkg/secrets/secrets.go | 3 +-- 12 files changed, 33 insertions(+), 18 deletions(-) diff --git a/Makefile b/Makefile index 7efce280..3d8f2ed4 100644 --- a/Makefile +++ b/Makefile @@ -39,6 +39,10 @@ coverage: lint: go vet ${PACKAGES} gofmt -d -l ${GOFILES} + test -z $(shell gofmt -d -l ${GOFILES}) + GO111MODULE=off \ + go get -u golang.org/x/lint/golint + golint -set_exit_status ${PACKAGES} build: CGO_ENABLED=0 go build ${GO_LDFLAGS} -o daytona cmd/daytona/main.go diff --git a/pkg/auth/auth.go b/pkg/auth/auth.go index 2f856614..0ed7a10f 100644 --- a/pkg/auth/auth.go +++ b/pkg/auth/auth.go @@ -80,19 +80,19 @@ func EnsureAuthenticated(client *api.Client, config cfg.Config) bool { // If it didn't find one, attempt to read token from disk. log.Info().Msg("Checking for an existing, valid vault token") - if err := checkToken(client); err == nil { + err := checkToken(client) + if err == nil { log.Info().Msg("Found an existing, valid token via VAULT_TOKEN") return true - } else { - log.Info().Msgf("Couldn't use VAULT_TOKEN, attempting file token instead: %s", err) } + log.Info().Msgf("Couldn't use VAULT_TOKEN, attempting file token instead: %s", err) - if err := checkFileToken(client, config.TokenPath); err == nil { + err = checkFileToken(client, config.TokenPath) + if err == nil { log.Info().Str("tokenPath", config.TokenPath).Msg("Found an existing token at token path, setting as client token") return true - } else { - log.Info().Err(err).Str("tokenPath", config.TokenPath).Msg("File token failed, trying to re-authenticate") } + log.Info().Err(err).Str("tokenPath", config.TokenPath).Msg("File token failed, trying to re-authenticate") bo := backoff.NewExponentialBackOff() bo.MaxInterval = time.Second * 15 diff --git a/pkg/auth/azure.go b/pkg/auth/azure.go index a8e1930e..c836f9c7 100644 --- a/pkg/auth/azure.go +++ b/pkg/auth/azure.go @@ -13,6 +13,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ + package auth import ( @@ -27,6 +28,7 @@ import ( // AzureService is an external service that vault can authenticate request against type AzureService struct{} +// Auth is used to authenticate to the service func (a *AzureService) Auth(client *api.Client, config cfg.Config) (string, error) { metadata, err := a.getMetadata() if err != nil { diff --git a/pkg/auth/gcp.go b/pkg/auth/gcp.go index a00a8165..ae1ce5fb 100644 --- a/pkg/auth/gcp.go +++ b/pkg/auth/gcp.go @@ -13,6 +13,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ + package auth import ( diff --git a/pkg/auth/k8s.go b/pkg/auth/k8s.go index 096c6e84..0bbcd18f 100644 --- a/pkg/auth/k8s.go +++ b/pkg/auth/k8s.go @@ -13,6 +13,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ + package auth import ( @@ -29,6 +30,7 @@ import ( ) var ( + // ErrInferRoleClaims is returns when the authenticator fails to infer a role name ErrInferRoleClaims = errors.New("could not parse service-account name / role name from claims") ) diff --git a/pkg/daytona/options.go b/pkg/daytona/options.go index 7292fc5f..7f25d3d1 100644 --- a/pkg/daytona/options.go +++ b/pkg/daytona/options.go @@ -4,6 +4,7 @@ import ( "github.com/hashicorp/vault/api" ) +// Option defines how an option should be applied type Option interface { Apply(s *SecretUnmarshler) } diff --git a/pkg/daytona/unmarshal.go b/pkg/daytona/unmarshal.go index 5eee5d00..900db392 100644 --- a/pkg/daytona/unmarshal.go +++ b/pkg/daytona/unmarshal.go @@ -78,14 +78,14 @@ func NewSecretUnmarshler(opts ...Option) (*SecretUnmarshler, error) { // (DATA EXAMPLE #1) Consider the design of the following secret path: secret/application, that contains // several sub-keys: // -// API_KEY - the data being stored in the data key 'value' -// DB_PASSWORD - the data being stored in the data key 'value' +// API_KEY - the data being stored in the data key 'value' +// DB_PASSWORD - the data being stored in the data key 'value' // // (DATA EXAMPLE #2) Consider the design of the following secret path: secret/application/configs, that contains // several data keys // -// api_key -// db_password +// api_key +// db_password // // A field tagged with 'vault_path_key' implies that the apex is a top-level secret path, // and the value provided by 'vault_path_key' is the suffix key in the path. The full final path will @@ -93,15 +93,15 @@ func NewSecretUnmarshler(opts ...Option) (*SecretUnmarshler, error) { // with a 'vault_path_key' of DB_PASSWORD, will attempt to read the data stored in secret/application/DB_PASSSWORD. // By default a data key of 'value' is used. The data key can be customized via the tag `vault_path_data_key` // -// Field string `vault_path_key:"DB_PASSWORD"` -// Field string `vault_path_key:"DB_PASSWORD" vault_path_data_key:"password"` // data key override +// Field string `vault_path_key:"DB_PASSWORD"` +// Field string `vault_path_key:"DB_PASSWORD" vault_path_data_key:"password"` // data key override // // A field tagged with 'vault_data_key' implies that the apex is a full, final secret path // and the value provided by 'vault_data_key' is the name of the data key. e.g. an apex of secret/application/configs // with a 'vault_data_key' of db_password, will attempt to read the data stored in secret/application/configs, referncing // the db_password data key. // -// Field string `vault_data_key:"db_password"` +// Field string `vault_data_key:"db_password"` func (su SecretUnmarshler) Unmarshal(ctx context.Context, apex string, v interface{}) error { val := reflect.ValueOf(v) if val.Kind() != reflect.Ptr { diff --git a/pkg/helpers/file.go b/pkg/helpers/file.go index ab921cb8..94294071 100644 --- a/pkg/helpers/file.go +++ b/pkg/helpers/file.go @@ -6,6 +6,7 @@ import ( "path/filepath" ) +// WriteFile is a convenience method for writing data to a filesystem func WriteFile(path string, data []byte, perm fs.FileMode) error { dir, _ := filepath.Split(path) err := os.MkdirAll(dir, os.ModePerm) diff --git a/pkg/helpers/testhelpers/testing.go b/pkg/helpers/testhelpers/testing.go index 6622bcb8..3451350c 100644 --- a/pkg/helpers/testhelpers/testing.go +++ b/pkg/helpers/testhelpers/testing.go @@ -2,6 +2,8 @@ package testhelpers import "github.com/hashicorp/vault/api" +// GetTestClient returns a vault api client configured to the +// supplied url. This is intented to be used in tests func GetTestClient(url string) (*api.Client, error) { vaultConfig := api.DefaultConfig() vaultConfig.Address = url diff --git a/pkg/logging/logging.go b/pkg/logging/logging.go index 2bdbb071..2737d143 100644 --- a/pkg/logging/logging.go +++ b/pkg/logging/logging.go @@ -8,8 +8,11 @@ import ( "github.com/rs/zerolog/log" ) +// EnvLevel defines the envionrment variable name to use +// to determine the log level const EnvLevel = "LOG_LEVEL" +// Config holds configuration items for the logger type Config struct { Structured bool Level string diff --git a/pkg/pki/pki_test.go b/pkg/pki/pki_test.go index a0b4f04c..bc04c679 100644 --- a/pkg/pki/pki_test.go +++ b/pkg/pki/pki_test.go @@ -447,11 +447,11 @@ func testHandler() http.Handler { if strings.HasSuffix(r.URL.Path, "correct-role") { decoder := json.NewDecoder(r.Body) tstr := struct { - Alt_names string `json:"alt_names"` - Common_name string `json:"common_name"` + AltNames string `json:"alt_names"` + CommonName string `json:"common_name"` }{} _ = decoder.Decode(&tstr) - if len(strings.Split(tstr.Alt_names, ",")) > 1 { + if len(strings.Split(tstr.AltNames, ",")) > 1 { fmt.Fprint(w, testPkiIssueResponseMultipleDomain) } else { fmt.Fprint(w, testPkiIssueResponseSingleDomain) diff --git a/pkg/secrets/secrets.go b/pkg/secrets/secrets.go index 5d1e8652..4edefb84 100644 --- a/pkg/secrets/secrets.go +++ b/pkg/secrets/secrets.go @@ -333,9 +333,8 @@ func (sd *SecretDefinition) copyValue(secretData map[string]interface{}, key str sd.secrets[key] = secretValue sd.Unlock() return nil - } else { - return err } + return err } return nil }