Skip to content

Commit

Permalink
Merge pull request #24617 from hashicorp/alisdair/provider-installer-…
Browse files Browse the repository at this point in the history
…signature-verification

internal: Verify provider signatures on install
  • Loading branch information
alisdair committed Apr 20, 2020
2 parents 92d6a30 + a5b3d49 commit e32e7e2
Show file tree
Hide file tree
Showing 16 changed files with 1,222 additions and 66 deletions.
11 changes: 11 additions & 0 deletions command/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,17 @@ func (c *InitCommand) getProviders(earlyConfig *earlyconfig.Config, state *state
fmt.Sprintf("Error while installing %s v%s: %s.", provider.ForDisplay(), version, err),
))
},
FetchPackageSuccess: func(provider addrs.Provider, version getproviders.Version, localDir string, authResult *getproviders.PackageAuthenticationResult) {
var warning string
if authResult != nil {
warning = authResult.Warning
}
if warning != "" {
warning = c.Colorize().Color(fmt.Sprintf("\n [reset][yellow]Warning: %s[reset]", warning))
}

c.Ui.Info(fmt.Sprintf("- Installed %s v%s (%s)%s", provider.ForDisplay(), version, authResult, warning))
},
}

mode := providercache.InstallNewProvidersOnly
Expand Down
8 changes: 6 additions & 2 deletions command/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,10 @@ func TestInit_providerSource(t *testing.T) {
t.Errorf("wrong version selections after upgrade\n%s", diff)
}

outputStr := ui.OutputWriter.String()
if want := "Installed hashicorp/test v1.2.3 (verified checksum)"; !strings.Contains(outputStr, want) {
t.Fatalf("unexpected output: %s\nexpected to include %q", outputStr, want)
}
}

func TestInit_getUpgradePlugins(t *testing.T) {
Expand Down Expand Up @@ -1101,7 +1105,7 @@ func TestInit_getProviderMissing(t *testing.T) {

args := []string{}
if code := c.Run(args); code == 0 {
t.Fatalf("expceted error, got output: \n%s", ui.OutputWriter.String())
t.Fatalf("expected error, got output: \n%s", ui.OutputWriter.String())
}

if !strings.Contains(ui.ErrorWriter.String(), "no available releases match") {
Expand Down Expand Up @@ -1619,7 +1623,7 @@ func installFakeProviderPackagesElsewhere(t *testing.T, cacheDir *providercache.
if err != nil {
t.Fatalf("failed to prepare fake package for %s %s: %s", name, versionStr, err)
}
err = cacheDir.InstallPackage(context.Background(), meta)
_, err = cacheDir.InstallPackage(context.Background(), meta)
if err != nil {
t.Fatalf("failed to install fake package for %s %s: %s", name, versionStr, err)
}
Expand Down
12 changes: 12 additions & 0 deletions internal/getproviders/mock_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package getproviders

import (
"archive/zip"
"crypto/sha256"
"fmt"
"io"
"io/ioutil"
"os"

Expand Down Expand Up @@ -168,6 +170,14 @@ func FakeInstallablePackageMeta(provider addrs.Provider, version Version, target
return PackageMeta{}, close, fmt.Errorf("failed to close the mock zip file: %s", err)
}

// Compute the SHA256 checksum of the generated file, to allow package
// authentication code to be exercised.
f.Seek(0, io.SeekStart)
h := sha256.New()
io.Copy(h, f)
checksum := [32]byte{}
h.Sum(checksum[:0])

meta := PackageMeta{
Provider: provider,
Version: version,
Expand All @@ -181,6 +191,8 @@ func FakeInstallablePackageMeta(provider addrs.Provider, version Version, target
// (At the time of writing, no caller actually does that, but who
// knows what the future holds?)
Filename: fmt.Sprintf("terraform-provider-%s_%s_%s.zip", provider.Type, version.String(), target.String()),

Authentication: NewArchiveChecksumAuthentication(checksum),
}
return meta, close, nil
}
Loading

0 comments on commit e32e7e2

Please sign in to comment.