Skip to content

Commit

Permalink
fix: terraform source url handling (#3142)
Browse files Browse the repository at this point in the history
  • Loading branch information
levkohimins authored May 17, 2024
1 parent 96426f4 commit a549c3b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
32 changes: 30 additions & 2 deletions terraform/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,10 @@ func NewSource(source string, downloadDir string, workingDir string, logger *log
// Convert the given source into a URL struct. This method should be able to handle all source URLs that the terraform
// init command can handle, parsing local file paths, Git paths, and HTTP URLs correctly.
func ToSourceUrl(source string, workingDir string) (*url.URL, error) {
// we need to remove the http(s) scheme to allow `getter.Detect` to add the source type
source = httpSchemeRegexp.ReplaceAllString(source, "")
source, err := normalizeSourceURL(source, workingDir)
if err != nil {
return nil, err
}

// The go-getter library is what Terraform's init command uses to download source URLs. Use that library to
// parse the URL.
Expand All @@ -204,6 +206,32 @@ func ToSourceUrl(source string, workingDir string) (*url.URL, error) {
return parseSourceUrl(rawSourceUrlWithGetter)
}

// We have to remove the http(s) scheme from the source URL to allow `getter.Detect` to add the source type, but only if the `getter` has a detector for that host.
func normalizeSourceURL(source string, workingDir string) (string, error) {
newSource := httpSchemeRegexp.ReplaceAllString(source, "")

// We can't use `the getter.Detectors` global variable because we need to exclude `getter.FileDetector` from checking since it is not a URL detector.
detectors := []getter.Detector{
new(getter.GitHubDetector),
new(getter.GitLabDetector),
new(getter.GitDetector),
new(getter.BitBucketDetector),
new(getter.S3Detector),
new(getter.GCSDetector),
}

for _, detector := range detectors {
_, ok, err := detector.Detect(newSource, workingDir)
if err != nil {
return source, errors.WithStackTrace(err)
}
if ok {
return newSource, nil
}
}
return source, nil
}

// Parse the given source URL into a URL struct. This method can handle source URLs that include go-getter's "forced
// getter" prefixes, such as git::.
func parseSourceUrl(source string) (*url.URL, error) {
Expand Down
4 changes: 3 additions & 1 deletion terraform/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package terraform
import (
"fmt"
"net/url"
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -78,6 +79,7 @@ func TestToSourceUrl(t *testing.T) {
{"https://www.googleapis.com/storage/v1/modules/foomodule.zip", "gcs::https://www.googleapis.com/storage/v1/modules/foomodule.zip"},
{"https://www.googleapis.com/storage/v1/modules/foomodule.zip", "gcs::https://www.googleapis.com/storage/v1/modules/foomodule.zip"},
{"git::https://[email protected]/name/project-name/_git/repo-name", "git::https://[email protected]/name/project-name/_git/repo-name"},
{"https://repositry.rnd.net/artifactory/generic-production-iac/tf-auto-azr-iam.2.6.0.zip", "https://repositry.rnd.net/artifactory/generic-production-iac/tf-auto-azr-iam.2.6.0.zip"},
}

for i, testCase := range testCases {
Expand All @@ -86,7 +88,7 @@ func TestToSourceUrl(t *testing.T) {
t.Run(fmt.Sprintf("testCase-%d", i), func(t *testing.T) {
t.Parallel()

actualSourceURL, err := ToSourceUrl(testCase.sourceURL, "")
actualSourceURL, err := ToSourceUrl(testCase.sourceURL, os.TempDir())
require.NoError(t, err)
assert.Equal(t, testCase.expectedSourceURL, actualSourceURL.String())
})
Expand Down

0 comments on commit a549c3b

Please sign in to comment.