Skip to content

Commit

Permalink
Merge pull request #90 from ekristen/chore-golangci-lint
Browse files Browse the repository at this point in the history
chore: golangci-lint entire project
  • Loading branch information
ekristen authored Feb 23, 2024
2 parents 6b9ae0e + 7be03e7 commit da5ac7a
Show file tree
Hide file tree
Showing 209 changed files with 739 additions and 741 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: golangci-lint
on:
pull_request:
branches:
- main

permissions:
contents: read

jobs:
golangci-lint:
name: golangci-lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '1.21.x'
cache: false
- name: golangci-lint
uses: golangci/golangci-lint-action@v4
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ require (
golang.org/x/net v0.19.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/tools v0.1.12 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.6.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package main

import (
"os"

"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"os"

"github.com/ekristen/aws-nuke/pkg/common"

Expand Down
4 changes: 2 additions & 2 deletions pkg/awsutil/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
)

type Account struct {
Credentials
*Credentials

id string
arn string
Expand All @@ -26,7 +26,7 @@ type Account struct {
disabledRegions []string
}

func NewAccount(creds Credentials, endpoints config.CustomEndpoints) (*Account, error) {
func NewAccount(creds *Credentials, endpoints config.CustomEndpoints) (*Account, error) {
creds.CustomEndpoints = endpoints
account := Account{
Credentials: creds,
Expand Down
10 changes: 10 additions & 0 deletions pkg/awsutil/consts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package awsutil

// Default is a generic constant for the word default
const Default = "default"

// StateDeleted is a generic constant for the word deleted for state
const StateDeleted = "deleted"

// StateDeleting is a generic constant for the word deleting for state
const StateDeleting = "deleting"
4 changes: 4 additions & 0 deletions pkg/awsutil/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package awsutil

const ErrCodeInvalidAction = "InvalidAction"
const ErrCodeOperationNotPermitted = "OperationNotPermitted"
31 changes: 18 additions & 13 deletions pkg/awsutil/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type Credentials struct {
SecretAccessKey string
SessionToken string
AssumeRoleArn string
ExternalId string
ExternalID string
RoleSessionName string

Credentials *credentials.Credentials
Expand All @@ -65,9 +65,9 @@ func (c *Credentials) HasKeys() bool {

func (c *Credentials) Validate() error {
if c.HasProfile() && c.HasKeys() {
return fmt.Errorf("You have to specify either the --profile flag or " +
return fmt.Errorf("specify either the --profile flag or " +
"--access-key-id with --secret-access-key and optionally " +
"--session-token.\n")
"--session-token, but not both")
}

return nil
Expand Down Expand Up @@ -98,7 +98,7 @@ func (c *Credentials) rootSession() (*session.Session, error) {
}

case c.HasProfile():
fallthrough
fallthrough //nolint:gocritic

default:
opts = session.Options{
Expand All @@ -123,8 +123,8 @@ func (c *Credentials) rootSession() (*session.Session, error) {
p.RoleSessionName = c.RoleSessionName
}

if c.ExternalId != "" {
p.ExternalID = aws.String(c.ExternalId)
if c.ExternalID != "" {
p.ExternalID = aws.String(c.ExternalID)
}
})
}
Expand Down Expand Up @@ -172,16 +172,16 @@ func (c *Credentials) NewSession(region, serviceType string) (*session.Session,
}
if customService.TLSInsecureSkipVerify {
conf.HTTPClient = &http.Client{Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, //nolint:gosec
}}
}
// ll := aws.LogDebugWithEventStreamBody
// conf.LogLevel = &ll

var err error
sess, err = session.NewSession(conf)
if err != nil {
return nil, err
}

isCustom = true
}

Expand Down Expand Up @@ -246,25 +246,30 @@ func skipGlobalHandler(global bool) func(r *request.Request) {
if !ok {
// This means that the service does not exist in the endpoints list.
if global {
r.Error = liberrors.ErrSkipRequest(fmt.Sprintf("service '%s' is was not found in the endpoint list; assuming it is not global", service))
r.Error = liberrors.ErrSkipRequest(
fmt.Sprintf("service '%s' is was not found in the endpoint list; assuming it is not global",
service))
} else {
host := r.HTTPRequest.URL.Hostname()
_, err := net.LookupHost(host)
if err != nil {
log.Debug(err)
r.Error = liberrors.ErrUnknownEndpoint(fmt.Sprintf("DNS lookup failed for %s; assuming it does not exist in this region", host))
r.Error = liberrors.ErrUnknownEndpoint(
fmt.Sprintf("DNS lookup failed for %s; assuming it does not exist in this region", host))
}
}
return
}

if len(rs) == 0 && !global {
r.Error = liberrors.ErrSkipRequest(fmt.Sprintf("service '%s' is global, but the session is not", service))
r.Error = liberrors.ErrSkipRequest(
fmt.Sprintf("service '%s' is global, but the session is not", service))
return
}

if (len(rs) > 0 && global) && service != "sts" {
r.Error = liberrors.ErrSkipRequest(fmt.Sprintf("service '%s' is not global, but the session is", service))
r.Error = liberrors.ErrSkipRequest(
fmt.Sprintf("service '%s' is not global, but the session is", service))
return
}
}
Expand Down
16 changes: 9 additions & 7 deletions pkg/commands/account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ package account

import (
"fmt"

"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"

"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/ekristen/aws-nuke/pkg/awsutil"
"github.com/ekristen/aws-nuke/pkg/config"

libconfig "github.com/ekristen/libnuke/pkg/config"
"github.com/ekristen/libnuke/pkg/registry"
"github.com/sirupsen/logrus"

"github.com/urfave/cli/v2"

"github.com/ekristen/aws-nuke/pkg/awsutil"
"github.com/ekristen/aws-nuke/pkg/commands/global"
"github.com/ekristen/aws-nuke/pkg/commands/nuke"
"github.com/ekristen/aws-nuke/pkg/common"
"github.com/ekristen/aws-nuke/pkg/config"
)

func execute(c *cli.Context) error {
Expand Down Expand Up @@ -81,8 +83,8 @@ func execute(c *cli.Context) error {
if creds.RoleSessionName != "" {
fmt.Println("> Session Name: ", creds.RoleSessionName)
}
if creds.ExternalId != "" {
fmt.Println("> External ID: ", creds.ExternalId)
if creds.ExternalID != "" {
fmt.Println("> External ID: ", creds.ExternalID)
}
}

Expand Down
21 changes: 12 additions & 9 deletions pkg/commands/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@ package config

import (
"fmt"
"slices"

"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"

libconfig "github.com/ekristen/libnuke/pkg/config"
"github.com/ekristen/libnuke/pkg/registry"
"github.com/ekristen/libnuke/pkg/types"

"github.com/ekristen/aws-nuke/pkg/awsutil"
"github.com/ekristen/aws-nuke/pkg/commands/global"
"github.com/ekristen/aws-nuke/pkg/commands/nuke"
"github.com/ekristen/aws-nuke/pkg/common"
"github.com/ekristen/aws-nuke/pkg/config"
libconfig "github.com/ekristen/libnuke/pkg/config"
"github.com/ekristen/libnuke/pkg/registry"
"github.com/ekristen/libnuke/pkg/types"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"slices"
)

func execute(c *cli.Context) error {
Expand Down Expand Up @@ -54,17 +57,17 @@ func execute(c *cli.Context) error {
resourceTypes := types.ResolveResourceTypes(
registry.GetNames(),
[]types.Collection{
types.Collection{}, // note: empty collection since we are not capturing parameters
{}, // note: empty collection since we are not capturing parameters
parsedConfig.ResourceTypes.GetIncludes(),
accountConfig.ResourceTypes.GetIncludes(),
},
[]types.Collection{
types.Collection{}, // note: empty collection since we are not capturing parameters
{}, // note: empty collection since we are not capturing parameters
parsedConfig.ResourceTypes.Excludes,
accountConfig.ResourceTypes.Excludes,
},
[]types.Collection{
types.Collection{}, // note: empty collection since we are not capturing parameters
{}, // note: empty collection since we are not capturing parameters
parsedConfig.ResourceTypes.GetAlternatives(),
accountConfig.ResourceTypes.GetAlternatives(),
},
Expand Down
13 changes: 8 additions & 5 deletions pkg/commands/nuke/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package nuke
import (
"context"
"fmt"
"github.com/ekristen/libnuke/pkg/registry"
"slices"
"strings"
"time"
Expand All @@ -15,6 +14,7 @@ import (

libconfig "github.com/ekristen/libnuke/pkg/config"
libnuke "github.com/ekristen/libnuke/pkg/nuke"
"github.com/ekristen/libnuke/pkg/registry"
"github.com/ekristen/libnuke/pkg/scanner"
"github.com/ekristen/libnuke/pkg/types"

Expand All @@ -25,19 +25,22 @@ import (
"github.com/ekristen/aws-nuke/pkg/nuke"
)

func ConfigureCreds(c *cli.Context) (creds awsutil.Credentials) {
// ConfigureCreds is a helper function to configure the awsutil.Credentials object from the cli.Context
func ConfigureCreds(c *cli.Context) (creds *awsutil.Credentials) {
creds = &awsutil.Credentials{}

creds.Profile = c.String("profile")
creds.AccessKeyID = c.String("access-key-id")
creds.SecretAccessKey = c.String("secret-access-key")
creds.SessionToken = c.String("session-token")
creds.AssumeRoleArn = c.String("assume-role-arn")
creds.RoleSessionName = c.String("assume-role-session-name")
creds.ExternalId = c.String("assume-role-external-id")
creds.ExternalID = c.String("assume-role-external-id")

return creds
}

func execute(c *cli.Context) error {
func execute(c *cli.Context) error { //nolint:funlen,gocyclo
ctx, cancel := context.WithCancel(c.Context)
defer cancel()

Expand Down Expand Up @@ -198,7 +201,7 @@ func execute(c *cli.Context) error {
return n.Run(ctx)
}

func init() {
func init() { //nolint:funlen
flags := []cli.Flag{
&cli.PathFlag{
Name: "config",
Expand Down
8 changes: 4 additions & 4 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,15 @@ func (c *Config) ValidateAccount(accountID string, aliases []string, skipAliasCh
}

if len(aliases) == 0 {
return fmt.Errorf("The specified account doesn't have an alias. " +
return fmt.Errorf("specified account doesn't have an alias. " +
"For safety reasons you need to specify an account alias. " +
"Your production account should contain the term 'prod'.")
"Your production account should contain the term 'prod'")
}

for _, alias := range aliases {
if strings.Contains(strings.ToLower(alias), "prod") {
return fmt.Errorf("You are trying to nuke an account with the alias '%s', "+
"but it has the substring 'prod' in it. Aborting.", alias)
return fmt.Errorf("you are trying to nuke an account with the alias '%s', "+
"but it has the substring 'prod' in it. Aborting", alias)
}
}

Expand Down
1 change: 0 additions & 1 deletion resources/acmpca-certificateauthorities.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ type ACMPCACertificateAuthority struct {
}

func (f *ACMPCACertificateAuthority) Remove(_ context.Context) error {

_, err := f.svc.DeleteCertificateAuthority(&acmpca.DeleteCertificateAuthorityInput{
CertificateAuthorityArn: f.ARN,
})
Expand Down
3 changes: 0 additions & 3 deletions resources/acmpca-certificateauthoritystates.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ type ACMPCACertificateAuthorityState struct {
}

func (f *ACMPCACertificateAuthorityState) Remove(_ context.Context) error {

_, err := f.svc.UpdateCertificateAuthority(&acmpca.UpdateCertificateAuthorityInput{
CertificateAuthorityArn: f.ARN,
Status: aws.String("DISABLED"),
Expand All @@ -102,7 +101,6 @@ func (f *ACMPCACertificateAuthorityState) String() string {
}

func (f *ACMPCACertificateAuthorityState) Filter() error {

switch *f.status {
case "CREATING":
return fmt.Errorf("available for deletion")
Expand All @@ -115,7 +113,6 @@ func (f *ACMPCACertificateAuthorityState) Filter() error {
default:
return nil
}

}

func (f *ACMPCACertificateAuthorityState) Properties() types.Properties {
Expand Down
1 change: 0 additions & 1 deletion resources/apigateway-apikeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ type APIGatewayAPIKey struct {
}

func (f *APIGatewayAPIKey) Remove(_ context.Context) error {

_, err := f.svc.DeleteApiKey(&apigateway.DeleteApiKeyInput{
ApiKey: f.APIKey,
})
Expand Down
Loading

0 comments on commit da5ac7a

Please sign in to comment.