-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
⭐ support terraform discovery over gitlab
```bash cnquery run gitlab --group mondoolabs --project example-gitlab --discover terraform -c 'terraform.blocks' ``` Unlike v8, this also cleans up the temporary git repos on provider shutdown. Follow-up: We have a general issue that calls connect twice on all providers right now, leading to double-clones of the git repo. This will be solved via connection re-use. Signed-off-by: Dominik Richter <[email protected]>
- Loading branch information
1 parent
c5c8d7e
commit 3ac831f
Showing
8 changed files
with
245 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,15 +7,18 @@ import ( | |
"encoding/json" | ||
"fmt" | ||
"io/fs" | ||
"net/url" | ||
"os" | ||
"path/filepath" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/go-git/go-git/v5" | ||
"github.com/hashicorp/hcl/v2" | ||
"github.com/pkg/errors" | ||
"github.com/rs/zerolog/log" | ||
"go.mondoo.com/cnquery/providers-sdk/v1/inventory" | ||
"go.mondoo.com/cnquery/providers-sdk/v1/vault" | ||
) | ||
|
||
func ParseTerraformModuleManifest(manifestPath string) (*ModuleManifest, error) { | ||
|
@@ -42,7 +45,11 @@ var MODULE_EXAMPLES = regexp.MustCompile(`^.*/modules/.+/examples/.+`) | |
|
||
func NewHclConnection(id uint32, asset *inventory.Asset) (*Connection, error) { | ||
cc := asset.Connections[0] | ||
path := cc.Options["path"] | ||
return newHclConnection(path, asset) | ||
} | ||
|
||
func newHclConnection(path string, asset *inventory.Asset) (*Connection, error) { | ||
// NOTE: right now we are only supporting to load either state, plan or hcl files but not at the same time | ||
|
||
var assetType terraformAssetType | ||
|
@@ -52,7 +59,6 @@ func NewHclConnection(id uint32, asset *inventory.Asset) (*Connection, error) { | |
var modulesManifest *ModuleManifest | ||
|
||
assetType = configurationfiles | ||
path := cc.Options["path"] | ||
// FIXME: cannot handle relative paths | ||
stat, err := os.Stat(path) | ||
if os.IsNotExist(err) { | ||
|
@@ -121,3 +127,94 @@ func NewHclConnection(id uint32, asset *inventory.Asset) (*Connection, error) { | |
modulesManifest: modulesManifest, | ||
}, nil | ||
} | ||
|
||
func NewHclGitConnection(id uint32, asset *inventory.Asset) (*Connection, error) { | ||
cc := asset.Connections[0] | ||
|
||
if len(cc.Options) == 0 { | ||
return nil, errors.New("missing URLs in options for HCL over Git connection") | ||
} | ||
|
||
user := "" | ||
token := "" | ||
for i := range cc.Credentials { | ||
cred := cc.Credentials[i] | ||
if cred.Type == vault.CredentialType_password { | ||
user = cred.User | ||
token = string(cred.Secret) | ||
if token == "" && cred.Password != "" { | ||
token = string(cred.Password) | ||
} | ||
} | ||
} | ||
|
||
gitUrl := "" | ||
|
||
// If a token is provided, it will be used to clone the repo | ||
// gitlab: git clone https://oauth2:[email protected]/vendor/package.git | ||
// if sshUrl := cc.Options["ssh-url"]; sshUrl != "" { ... not doing ssh url right now | ||
if httpUrl := cc.Options["http-url"]; httpUrl != "" { | ||
u, err := url.Parse(httpUrl) | ||
if err != nil { | ||
return nil, errors.New("failed to parse url for git repo: " + httpUrl) | ||
} | ||
|
||
if user != "" && token != "" { | ||
u.User = url.UserPassword(user, token) | ||
} else if token != "" { | ||
u.User = url.User(token) | ||
} | ||
|
||
gitUrl = u.String() | ||
} | ||
|
||
if gitUrl == "" { | ||
return nil, errors.New("missing url for git repo " + asset.Name) | ||
} | ||
|
||
path, closer, err := gitClone(gitUrl) | ||
if err != nil { | ||
return nil, err | ||
} | ||
conn, err := newHclConnection(path, asset) | ||
if err != nil { | ||
return nil, err | ||
} | ||
conn.closer = closer | ||
return conn, nil | ||
} | ||
|
||
func gitClone(url string) (string, func(), error) { | ||
cloneDir, err := os.MkdirTemp(os.TempDir(), "gitClone") | ||
if err != nil { | ||
return "", nil, errors.Wrap(err, "failed to create temporary dir for git processing") | ||
} | ||
|
||
closer := func() { | ||
log.Info().Str("path", cloneDir).Msg("cleaning up git clone") | ||
if err = os.RemoveAll(cloneDir); err != nil { | ||
log.Error().Err(err).Msg("failed to remove temporary dir for git processing") | ||
} | ||
} | ||
|
||
log.Info().Str("url", url).Str("path", cloneDir).Msg("git clone") | ||
repo, err := git.PlainClone(cloneDir, false, &git.CloneOptions{ | ||
URL: url, | ||
Progress: os.Stderr, | ||
Depth: 1, | ||
RecurseSubmodules: git.DefaultSubmoduleRecursionDepth, | ||
}) | ||
if err != nil { | ||
closer() | ||
return "", nil, errors.Wrap(err, "failed to clone git repo "+url) | ||
} | ||
|
||
ref, err := repo.Head() | ||
if err != nil { | ||
closer() | ||
return "", nil, errors.Wrap(err, "failed to get head of git repo "+url) | ||
} | ||
log.Info().Str("url", url).Str("path", cloneDir).Str("head", ref.Hash().String()).Msg("finshed git clone") | ||
|
||
return cloneDir, closer, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.