Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

terraform init: add link to documentation when a checksum is missing from the lock file #31408

Merged
merged 9 commits into from Jul 20, 2022
14 changes: 14 additions & 0 deletions internal/command/init.go
Expand Up @@ -761,6 +761,20 @@ func (c *InitCommand) getProviders(config *configs.Config, state *states.State,
// but rather just emit a single general message about it at
// the end, by checking ctx.Err().

case providercache.ErrProviderChecksumMiss:
// This is a special kind of error that can often be fixed using
// the `terraform providers lock` command. We're just going to
// amend the actual error message with some extra information
// about how to fix this.

diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
"Failed to install provider",
fmt.Sprintf("Error while installing %s v%s: %s\n\nYou can ensure the current platform, %s, is included in the dependency lock file by running: `terraform providers lock -provider=%s`.\n\nIf this does not fix the problem you may need to reset any provider caching present in your setup or make sure you are connecting to valid provider distributions.",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we might need to be careful about making this suggestion without some more context about the implications.

Specifically, when combining with this with the other change we recently made so that terraform providers lock will not honor existing checksums, I think this suggestion amounts to "The checksums didn't match the lock file, which you should fix by discarding the checksums in the lock file".

I must admit I'm not sure how to inject further subtlety here without making the message incredibly verbose. I think the main qualifier we want to get across here is "If you got into this situation by not having a complete set of checksums for all platforms you intend to use...", but I don't really know how to concisely explain to a user how they would determine if that predicate is true, and how to securely differentiate it from the bad case where someone has actually maliciously tampered with the plugin package. 🤔

I think we might need to ask the product security team for a second opinion on whatever we decide here too, since we're changing the characteristics of a pretty sensitive area for the threat models of some users.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've changed this so we just link directly to the relevant documentation, hopefully that avoids any potential security problems while still giving users a jump start on the potential fixes.

The relevant documentation does concretely suggest terraform providers lock, while providing context around how this could have happened.

provider.ForDisplay(), version, err.Msg, err.Meta.TargetPlatform.String(), err.Meta.TargetPlatform.String(),
),
))

default:
// We can potentially end up in here under cancellation too,
// in spite of our getproviders.ErrRequestCanceled case above,
Expand Down
2 changes: 1 addition & 1 deletion internal/getproviders/errors.go
Expand Up @@ -210,7 +210,7 @@ func (err ErrQueryFailed) Unwrap() error {
return err.Wrapped
}

// ErrRequestCancelled is an error type used to indicate that an operation
// ErrRequestCanceled is an error type used to indicate that an operation
// failed due to being cancelled via the given context.Context object.
//
// This error type doesn't include information about what was cancelled,
Expand Down
14 changes: 14 additions & 0 deletions internal/providercache/errors.go
@@ -0,0 +1,14 @@
package providercache

import "github.com/hashicorp/terraform/internal/getproviders"

// ErrProviderChecksumMiss is an error type used to indicate a provider
// installation failed due to a mismatch in the terraform provider lock file.
type ErrProviderChecksumMiss struct {
Meta getproviders.PackageMeta
Msg string
}

func (err ErrProviderChecksumMiss) Error() string {
return err.Msg
}
22 changes: 14 additions & 8 deletions internal/providercache/package_install.go
Expand Up @@ -116,10 +116,13 @@ func installFromLocalArchive(ctx context.Context, meta getproviders.PackageMeta,
meta.Provider, meta.Version, meta.Location, err,
)
} else if !matches {
return authResult, fmt.Errorf(
"the current package for %s %s doesn't match any of the checksums previously recorded in the dependency lock file",
meta.Provider, meta.Version,
)
return authResult, ErrProviderChecksumMiss{
Meta: meta,
Msg: fmt.Sprintf(
"the current package for %s %s doesn't match any of the checksums previously recorded in the dependency lock file",
meta.Provider, meta.Version,
),
}
}
}

Expand Down Expand Up @@ -198,10 +201,13 @@ func installFromLocalDir(ctx context.Context, meta getproviders.PackageMeta, tar
meta.Provider, meta.Version, meta.Location, err,
)
} else if !matches {
return authResult, fmt.Errorf(
"the local package for %s %s doesn't match any of the checksums previously recorded in the dependency lock file (this might be because the available checksums are for packages targeting different platforms)",
meta.Provider, meta.Version,
)
return authResult, ErrProviderChecksumMiss{
Meta: meta,
Msg: fmt.Sprintf(
"the local package for %s %s doesn't match any of the checksums previously recorded in the dependency lock file (this might be because the available checksums are for packages targeting different platforms)",
meta.Provider, meta.Version,
),
}
}
}

Expand Down