Skip to content

Commit

Permalink
🐛 Fix terraform platform information (#1805)
Browse files Browse the repository at this point in the history
This fixes the values for:
- platform name
- platform title
- platform ID

Fixes #1743

Signed-off-by: Christian Zunker <[email protected]>
  • Loading branch information
czunker authored Sep 20, 2023
1 parent 0d23ac3 commit c345e42
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 41 deletions.
20 changes: 0 additions & 20 deletions providers/terraform/connection/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
52 changes: 48 additions & 4 deletions providers/terraform/provider/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,36 @@ 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"
)

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",
Family: []string{"terraform"},
Kind: "code",
Runtime: "terraform",
}
case "plan":
case PlanConnectionType:
p = &inventory.Platform{
Name: "terraform-plan",
Title: "Terraform Plan",
Family: []string{"terraform"},
Kind: "code",
Runtime: "terraform",
}
case "hcl":
case HclConnectionType:
fallthrough
default:
p = &inventory.Platform{
Expand All @@ -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
}
20 changes: 3 additions & 17 deletions providers/terraform/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 <path>', 'plan <path>' or 'hcl <path>'")
}
conf.Type = "hcl"
conf.Type = HclConnectionType
conf.Options["path"] = req.Args[0]
}

Expand Down

0 comments on commit c345e42

Please sign in to comment.