Skip to content

Commit fc62afb

Browse files
authored
fix: validate implied provider names in submodules (#31573)
1 parent fbda438 commit fc62afb

File tree

5 files changed

+63
-0
lines changed

5 files changed

+63
-0
lines changed

internal/configs/configload/loader_load_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,33 @@ func TestLoaderLoadConfig_loadDiags(t *testing.T) {
105105
}
106106
}
107107

108+
func TestLoaderLoadConfig_loadDiagsFromSubmodules(t *testing.T) {
109+
// building a config which didn't load correctly may cause configs to panic
110+
fixtureDir := filepath.Clean("testdata/invalid-names-in-submodules")
111+
loader, err := NewLoader(&Config{
112+
ModulesDir: filepath.Join(fixtureDir, ".terraform/modules"),
113+
})
114+
if err != nil {
115+
t.Fatalf("unexpected error from NewLoader: %s", err)
116+
}
117+
118+
cfg, diags := loader.LoadConfig(fixtureDir)
119+
if !diags.HasErrors() {
120+
t.Fatalf("loading succeeded; want an error")
121+
}
122+
if got, want := diags.Error(), " Invalid provider local name"; !strings.Contains(got, want) {
123+
t.Errorf("missing expected error\nwant substring: %s\ngot: %s", want, got)
124+
}
125+
126+
if cfg == nil {
127+
t.Fatal("partial config not returned with diagnostics")
128+
}
129+
130+
if cfg.Module == nil {
131+
t.Fatal("expected config module")
132+
}
133+
}
134+
108135
func TestLoaderLoadConfig_childProviderGrandchildCount(t *testing.T) {
109136
// This test is focused on the specific situation where:
110137
// - A child module contains a nested provider block, which is no longer
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"Modules": [
3+
{
4+
"Key": "test",
5+
"Source": "./sub",
6+
"Dir": "testdata/invalid-names-in-submodules/sub"
7+
},
8+
{
9+
"Key": "",
10+
"Source": "",
11+
"Dir": "."
12+
}
13+
]
14+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module "test" {
2+
source = "./sub"
3+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
resource "aws-_foo" "test" {
2+
3+
}
4+
5+
data "aws-_bar" "test" {
6+
7+
}

internal/configs/provider_validation.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,18 @@ func validateProviderConfigs(parentCall *ModuleCall, cfg *Config, noProviderConf
153153
}
154154

155155
localName := r.Addr().ImpliedProvider()
156+
157+
_, err := addrs.ParseProviderPart(localName)
158+
if err != nil {
159+
diags = append(diags, &hcl.Diagnostic{
160+
Severity: hcl.DiagError,
161+
Summary: "Invalid provider local name",
162+
Detail: fmt.Sprintf("%q is an invalid implied provider local name: %s", localName, err),
163+
Subject: r.DeclRange.Ptr(),
164+
})
165+
continue
166+
}
167+
156168
if _, ok := localNames[localName]; ok {
157169
// OK, this was listed directly in the required_providers
158170
continue

0 commit comments

Comments
 (0)