github.com/khulnasoft-lab/defsec@v1.0.5-0.20230827010352-5e9f46893d95/rules/cloud/policies/google/sql/no_public_access.go (about) 1 package sql 2 3 import ( 4 "github.com/khulnasoft-lab/defsec/internal/cidr" 5 "github.com/khulnasoft-lab/defsec/internal/rules" 6 "github.com/khulnasoft-lab/defsec/pkg/providers" 7 "github.com/khulnasoft-lab/defsec/pkg/scan" 8 "github.com/khulnasoft-lab/defsec/pkg/severity" 9 "github.com/khulnasoft-lab/defsec/pkg/state" 10 ) 11 12 var CheckNoPublicAccess = rules.Register( 13 scan.Rule{ 14 AVDID: "AVD-GCP-0017", 15 Provider: providers.GoogleProvider, 16 Service: "sql", 17 ShortCode: "no-public-access", 18 Summary: "Ensure that Cloud SQL Database Instances are not publicly exposed", 19 Impact: "Public exposure of sensitive data", 20 Resolution: "Remove public access from database instances", 21 Explanation: `Database instances should be configured so that they are not available over the public internet, but to internal compute resources which access them.`, 22 Links: []string{ 23 "https://www.cloudconformity.com/knowledge-base/gcp/CloudSQL/publicly-accessible-cloud-sql-instances.html", 24 }, 25 Terraform: &scan.EngineMetadata{ 26 GoodExamples: terraformNoPublicAccessGoodExamples, 27 BadExamples: terraformNoPublicAccessBadExamples, 28 Links: terraformNoPublicAccessLinks, 29 RemediationMarkdown: terraformNoPublicAccessRemediationMarkdown, 30 }, 31 Severity: severity.High, 32 }, 33 func(s *state.State) (results scan.Results) { 34 for _, instance := range s.Google.SQL.Instances { 35 if instance.Metadata.IsUnmanaged() { 36 continue 37 } 38 if instance.Settings.IPConfiguration.EnableIPv4.IsTrue() { 39 results.Add( 40 "Database instance is granted a public internet address.", 41 instance.Settings.IPConfiguration.EnableIPv4, 42 ) 43 } 44 for _, network := range instance.Settings.IPConfiguration.AuthorizedNetworks { 45 if cidr.IsPublic(network.CIDR.Value()) { 46 results.Add( 47 "Database instance allows access from the public internet.", 48 network.CIDR, 49 ) 50 } else { 51 results.AddPassed(&instance) 52 } 53 } 54 } 55 return 56 }, 57 )