Skip to content

Commit 7155955

Browse files
committed
validate policy paths
1 parent 303c474 commit 7155955

3 files changed

Lines changed: 29 additions & 36 deletions

File tree

internal/command/init.go

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,7 @@ func (c *InitCommand) Run(args []string) int {
5656

5757
view := views.NewInit(initArgs.ViewType, c.View)
5858

59-
loader, err := c.initConfigLoader()
60-
if err != nil {
61-
diags = diags.Append(err)
62-
view.Diagnostics(diags)
63-
return 1
64-
}
65-
66-
var varDiags tfdiags.Diagnostics
67-
c.VariableValues, varDiags = initArgs.Vars.CollectValues(func(filename string, src []byte) {
68-
loader.Parser().ForceFileSource(filename, src)
69-
})
70-
diags = diags.Append(varDiags)
71-
59+
diags = diags.Append(c.Validate(initArgs))
7260
if diags.HasErrors() {
7361
view.Diagnostics(diags)
7462
return 1
@@ -294,6 +282,23 @@ func (c *InitCommand) initBackend(ctx context.Context, root *configs.Module, ini
294282
return back, true, diags
295283
}
296284

285+
func (c *InitCommand) Validate(args *arguments.Init) (diags tfdiags.Diagnostics) {
286+
loader, err := c.initConfigLoader()
287+
if err != nil {
288+
diags = diags.Append(err)
289+
return diags
290+
}
291+
292+
var varDiags tfdiags.Diagnostics
293+
c.VariableValues, varDiags = args.Vars.CollectValues(func(filename string, src []byte) {
294+
loader.Parser().ForceFileSource(filename, src)
295+
})
296+
diags = diags.Append(varDiags)
297+
298+
diags = diags.Append(validatePolicyPaths(args.PolicyPaths, c.AllowExperimentalFeatures))
299+
return diags
300+
}
301+
297302
func (c *InitCommand) earlyValidateBackend(root *configs.Module, initArgs *arguments.Init) (diags tfdiags.Diagnostics) {
298303
switch {
299304
case root.StateStore != nil && root.Backend != nil:
@@ -517,18 +522,18 @@ func (c *InitCommand) getProvidersFromConfig(ctx context.Context, config *config
517522

518523
// Determine which required providers are already downloaded, and download any
519524
// new providers or newer versions of providers
520-
configLocks, installErr := inst.EnsureProviderVersions(ctx, previousLocks, reqs, mode, installerHook)
525+
configLocks, err := inst.EnsureProviderVersions(ctx, previousLocks, reqs, mode, installerHook)
521526
if ctx.Err() == context.Canceled {
522527
diags = diags.Append(fmt.Errorf("Provider installation was canceled by an interrupt signal."))
523-
view.Diagnostics(diags) // TODO: Why is the output viewed here?
528+
view.Diagnostics(diags)
524529
return true, nil, SafeInitActionInvalid, nil, diags
525530
}
526-
if installErr != nil {
527-
// The errors captured in "installErr" should be redundant with what we
531+
if err != nil {
532+
// The errors captured in "err" should be redundant with what we
528533
// received via the InstallerEvents callbacks above, so we'll
529534
// just return those as long as we have some.
530535
if !diags.HasErrors() {
531-
diags = diags.Append(installErr)
536+
diags = diags.Append(err)
532537
}
533538

534539
return true, nil, SafeInitActionInvalid, nil, diags

internal/command/init_run.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ func (c *InitCommand) run(initArgs *arguments.Init, view views.Init) int {
4040
c.Meta.input = initArgs.InputEnabled
4141
c.Meta.targetFlags = initArgs.TargetFlags
4242
c.Meta.compactWarnings = initArgs.CompactWarnings
43-
c.Meta.policyPaths = initArgs.PolicyPaths
4443

4544
// Copying the state only happens during backend migration, so setting
4645
// -force-copy implies -migrate-state

internal/providercache/installer.go

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,6 @@ type Installer struct {
6464
// from the users machine via CLI configuration, so Terraform does
6565
// not need to worry about installing them.
6666
devOverrideTypes map[addrs.Provider]struct{}
67-
68-
// hook is an optional hook whose methods may be called for each provider
69-
// version that is being installed or upgraded.
70-
hook InstallerHook
7167
}
7268

7369
// NewInstaller constructs and returns a new installer with the given target
@@ -180,10 +176,6 @@ func (i *Installer) SetDevOverrideTypes(types map[addrs.Provider]struct{}) {
180176
i.devOverrideTypes = types
181177
}
182178

183-
func (i *Installer) SetHook(hook InstallerHook) {
184-
i.hook = hook
185-
}
186-
187179
// EnsureProviderVersions compares the given provider requirements with what
188180
// is already available in the installer's target directory and then takes
189181
// appropriate installation actions to ensure that suitable packages
@@ -371,16 +363,13 @@ NeedProvider:
371363
}
372364

373365
for _, hook := range hooks {
374-
// For each needed provider, we will send the version
375-
// and provider to the hook for policy evaluation.
376-
// If the hook returns an error, we'll abort the installation.
377-
// We do this before checking the lock file, so that we also
378-
// evaluate policy for providers that are already installed.
366+
// For each needed provider, we will report the selected version
367+
// to the hooks. If a hook returns an error, we'll abort the installation.
368+
// We do this for all providers, including already installed ones.
369+
// Their installation cannot be prevented, but the hook can still
370+
// return an error to indicate that the provider version is not
371+
// acceptable.
379372
err := hook.ProviderVersionSelected(ctx, provider, version.String())
380-
381-
// return a generic error here that the init command returns to the CLI.
382-
// The detailed policy diagnostics are included in the policy results
383-
// and will be formatted in the CLI output.
384373
if err != nil {
385374
return nil, err
386375
}

0 commit comments

Comments
 (0)