diff --git a/providers/terraform/connection/connection.go b/providers/terraform/connection/connection.go index 89d7a236d8..ff3a82bfe0 100644 --- a/providers/terraform/connection/connection.go +++ b/providers/terraform/connection/connection.go @@ -11,28 +11,12 @@ import ( type ConnectionType string -/* -type Connection interface { - ID() uint32 - Name() string - Type() ConnectionType - Asset() *inventory.Asset - State() (*State, error) - Identifier() (string, error) - TfVars() map[string]*hcl.Attribute - Parser() *hclparse.Parser - ModulesManifest() *ModuleManifest - Plan() (*Plan, error) -} -*/ - // References: // - https://www.terraform.io/docs/language/syntax/configuration.html // - https://github.com/hashicorp/hcl/blob/main/hclsyntax/spec.md type Connection struct { id uint32 name string - connectionType ConnectionType asset *inventory.Asset platformID string assetType terraformAssetType @@ -65,10 +49,6 @@ func (c *Connection) Name() string { return c.name } -func (c *Connection) Type() ConnectionType { - return c.connectionType -} - func (c *Connection) Parser() *hclparse.Parser { return c.parsed } diff --git a/providers/terraform/provider/detector.go b/providers/terraform/provider/detector.go index 4f07142bb9..7c6e8effdc 100644 --- a/providers/terraform/provider/detector.go +++ b/providers/terraform/provider/detector.go @@ -6,7 +6,10 @@ package provider import ( "crypto/sha256" "encoding/hex" + "os" + "path" "path/filepath" + "strings" "go.mondoo.com/cnquery/providers-sdk/v1/inventory" "go.mondoo.com/cnquery/providers/terraform/connection" @@ -14,8 +17,9 @@ import ( func (s *Service) detect(asset *inventory.Asset, conn *connection.Connection) error { var p *inventory.Platform - switch conn.Type() { - case "state": + connType := asset.Connections[0].Type + switch connType { + case StateConnectionType: p = &inventory.Platform{ Name: "terraform-state", Title: "Terraform State", @@ -23,7 +27,7 @@ func (s *Service) detect(asset *inventory.Asset, conn *connection.Connection) er Kind: "code", Runtime: "terraform", } - case "plan": + case PlanConnectionType: p = &inventory.Platform{ Name: "terraform-plan", Title: "Terraform Plan", @@ -31,7 +35,7 @@ func (s *Service) detect(asset *inventory.Asset, conn *connection.Connection) er Kind: "code", Runtime: "terraform", } - case "hcl": + case HclConnectionType: fallthrough default: p = &inventory.Platform{ @@ -51,6 +55,46 @@ func (s *Service) detect(asset *inventory.Asset, conn *connection.Connection) er hash := hex.EncodeToString(h.Sum(nil)) platformID := "//platformid.api.mondoo.app/runtime/terraform/hash/" + hash asset.Connections[0].PlatformId = platformID + asset.PlatformIds = []string{platformID} + + name := "" + if projectPath != "" { + // manifest parent directory name + name = projectNameFromPath(projectPath) + } + asset.Name = "Terraform Static Analysis " + name return nil } + +func projectNameFromPath(file string) string { + // if it is a local file (which may not be true) + name := "" + fi, err := os.Stat(file) + if err == nil { + if fi.IsDir() && fi.Name() != "." { + name = "directory " + fi.Name() + } else if fi.IsDir() { + name = fi.Name() + } else { + name = filepath.Base(fi.Name()) + extension := filepath.Ext(name) + name = strings.TrimSuffix(name, extension) + } + } else { + // it is not a local file, so we try to be a bit smart + name = path.Base(file) + extension := path.Ext(name) + name = strings.TrimSuffix(name, extension) + } + + // if the path is . we read the current directory + if name == "." { + abspath, err := filepath.Abs(name) + if err == nil { + name = projectNameFromPath(abspath) + } + } + + return name +} diff --git a/providers/terraform/provider/provider.go b/providers/terraform/provider/provider.go index e3e52c4bf3..c10334cfa3 100644 --- a/providers/terraform/provider/provider.go +++ b/providers/terraform/provider/provider.go @@ -51,22 +51,8 @@ func (s *Service) ParseCLI(req *plugin.ParseCLIReq) (*plugin.ParseCLIRes, error) // below in this file we already have something similar: // tc.Options["asset-type"] == "state" switch req.Args[0] { - case "state": - conf.Type = "state" - if len(req.Args) > 1 { - conf.Options["path"] = req.Args[1] - } else { - return nil, errors.New("no path provided") - } - case "plan": - conf.Type = "plan" - if len(req.Args) > 1 { - conf.Options["path"] = req.Args[1] - } else { - return nil, errors.New("no path provided") - } - case "hcl": - conf.Type = "hcl" + case StateConnectionType, PlanConnectionType, HclConnectionType: + conf.Type = req.Args[0] if len(req.Args) > 1 { conf.Options["path"] = req.Args[1] } else { @@ -76,7 +62,7 @@ func (s *Service) ParseCLI(req *plugin.ParseCLIReq) (*plugin.ParseCLIRes, error) if len(req.Args) > 1 { return nil, errors.New("unknown set of arguments, use 'state ', 'plan ' or 'hcl '") } - conf.Type = "hcl" + conf.Type = HclConnectionType conf.Options["path"] = req.Args[0] }