github.com/khulnasoft-lab/defsec@v1.0.5-0.20230827010352-5e9f46893d95/rules/cloud/policies/google/compute/no_default_service_account.go (about)

     1  package compute
     2  
     3  import (
     4  	"github.com/khulnasoft-lab/defsec/internal/rules"
     5  	"github.com/khulnasoft-lab/defsec/pkg/providers"
     6  	"github.com/khulnasoft-lab/defsec/pkg/scan"
     7  	"github.com/khulnasoft-lab/defsec/pkg/severity"
     8  	"github.com/khulnasoft-lab/defsec/pkg/state"
     9  )
    10  
    11  var CheckNoDefaultServiceAccount = rules.Register(
    12  	scan.Rule{
    13  		AVDID:       "AVD-GCP-0044",
    14  		Provider:    providers.GoogleProvider,
    15  		Service:     "compute",
    16  		ShortCode:   "no-default-service-account",
    17  		Summary:     "Instances should not use the default service account",
    18  		Impact:      "Instance has full access to the project",
    19  		Resolution:  "Remove use of default service account",
    20  		Explanation: `The default service account has full project access. Instances should instead be assigned the minimal access they need.`,
    21  		Links:       []string{},
    22  		Terraform: &scan.EngineMetadata{
    23  			GoodExamples:        terraformNoDefaultServiceAccountGoodExamples,
    24  			BadExamples:         terraformNoDefaultServiceAccountBadExamples,
    25  			Links:               terraformNoDefaultServiceAccountLinks,
    26  			RemediationMarkdown: terraformNoDefaultServiceAccountRemediationMarkdown,
    27  		},
    28  		Severity: severity.Critical,
    29  	},
    30  	func(s *state.State) (results scan.Results) {
    31  		for _, instance := range s.Google.Compute.Instances {
    32  			if instance.Metadata.IsUnmanaged() {
    33  				continue
    34  			}
    35  			if instance.ServiceAccount.IsDefault.IsTrue() {
    36  				results.Add(
    37  					"Instance uses the default service account.",
    38  					instance.ServiceAccount.Email,
    39  				)
    40  			} else {
    41  				results.AddPassed(&instance)
    42  			}
    43  		}
    44  		return
    45  	},
    46  )