Skip to content

Commit

Permalink
Add support for GPG signing
Browse files Browse the repository at this point in the history
Signed-off-by: Reinhard Nägele <unguiculus@gmail.com>
  • Loading branch information
unguiculus committed Oct 20, 2020
1 parent 4f86929 commit 364dcae
Show file tree
Hide file tree
Showing 8 changed files with 382 additions and 112 deletions.
12 changes: 12 additions & 0 deletions cr/cmd/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
package cmd

import (
"path/filepath"

"github.com/helm/chart-releaser/pkg/config"
"github.com/helm/chart-releaser/pkg/packager"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -52,6 +55,15 @@ func getRequiredPackageArgs() []string {
}

func init() {
dir, err := homedir.Dir()
if err != nil {
panic(err)
}

rootCmd.AddCommand(packageCmd)
packageCmd.Flags().StringP("package-path", "p", ".cr-release-packages", "Path to directory with chart packages")
packageCmd.Flags().Bool("sign", false, "Path to directory with chart packages")
packageCmd.Flags().String("key", "", "Name of the key to use when signing")
packageCmd.Flags().String("keyring", filepath.Join(dir, ".gnupg", "pubring.gpg"), "Location of a public keyring")
packageCmd.Flags().String("passphrase-file", "", "Location of a file which contains the passphrase for the signing key. Use '-' in order to read from stdin")
}
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ require (
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/onsi/ginkgo v1.13.0 // indirect
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.6.0 // indirect
github.com/spf13/cobra v1.0.0
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5
Expand All @@ -26,7 +25,7 @@ require (
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
golang.org/x/tools v0.0.0-20200724022722-7017fd6b1305
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
helm.sh/helm/v3 v3.1.2
helm.sh/helm/v3 v3.4.0-rc.1
honnef.co/go/tools v0.0.1-2020.1.4 // indirect
rsc.io/letsencrypt v0.0.3 // indirect
)
Expand Down
393 changes: 310 additions & 83 deletions go.sum

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ type Options struct {
ChartsRepo string `mapstructure:"charts-repo"`
IndexPath string `mapstructure:"index-path"`
PackagePath string `mapstructure:"package-path"`
Sign bool `mapstructure:"sign"`
Key string `mapstructure:"key"`
KeyRing string `mapstructure:"keyring"`
PassphraseFile string `mapstructure:"passphrase-file"`
Token string `mapstructure:"token"`
GitBaseURL string `mapstructure:"git-base-url"`
GitUploadURL string `mapstructure:"git-upload-url"`
Expand Down
6 changes: 6 additions & 0 deletions pkg/packager/packager.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ func NewPackager(config *config.Options, paths []string) *Packager {
func (p *Packager) CreatePackages() error {
helmClient := action.NewPackage()
helmClient.Destination = p.config.PackagePath
if p.config.Sign {
helmClient.Sign = true
helmClient.Key = p.config.Key
helmClient.Keyring = p.config.KeyRing
helmClient.PassphraseFile = p.config.PassphraseFile
}

for i := 0; i < len(p.paths); i++ {
path, err := filepath.Abs(p.paths[i])
Expand Down
75 changes: 48 additions & 27 deletions pkg/packager/packager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,58 +18,79 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/helm/chart-releaser/pkg/config"
)

func TestPackager_CreatePackages(t *testing.T) {
packagePath, _ := ioutil.TempDir(".", "packages")
invalidPackagePath := filepath.Join(packagePath, "bad")
file, _ := os.Create(invalidPackagePath)
defer file.Close()
defer os.RemoveAll(packagePath)
t.Cleanup(func() {
file.Close()
os.RemoveAll(packagePath)
})

tests := []struct {
name string
chartPath string
packagePath string
error bool
name string
chartPath string
options *config.Options
error bool
}{
{
"valid-chart-path",
"testdata/test-chart",
packagePath,
false,
name: "valid-chart-path",
chartPath: "testdata/test-chart",
options: &config.Options{PackagePath: packagePath},
error: false,
},
{
name: "invalid-package-path",
chartPath: "testdata/test-chart",
options: &config.Options{PackagePath: invalidPackagePath},
error: true,
},
{
"invalid-package-path",
"testdata/test-chart",
invalidPackagePath,
true,
name: "invalid-chart-path",
chartPath: "testdata/invalid-chart",
options: &config.Options{PackagePath: packagePath},
error: true,
},
{
"invalid-chart-path",
"testdata/invalid-chart",
packagePath,
true,
name: "valid-chart-path-with-provenance",
chartPath: "testdata/test-chart",
options: &config.Options{
PackagePath: packagePath,
Sign: true,
Key: "Chart Releaser Test Key <no-reply@example.com>",
KeyRing: "testdata/testkeyring.gpg",
PassphraseFile: "testdata/passphrase-file.txt",
},
error: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Cleanup(func() {
os.Remove(filepath.Join(packagePath, "test-chart-0.1.0.tgz"))
os.Remove(filepath.Join(packagePath, "test-chart-0.1.0.tgz.prov"))
})

p := &Packager{
paths: strings.Split(tt.chartPath, ","),
config: &config.Options{PackagePath: tt.packagePath},
paths: []string{tt.chartPath},
config: tt.options,
}
err := p.CreatePackages()

if tt.error {
if err == nil {
t.Error()
}
require.Error(t, err)
} else {
if err != nil {
t.Error()
require.NoError(t, err)
assert.FileExists(t, filepath.Join(tt.options.PackagePath, "test-chart-0.1.0.tgz"))
if tt.options.Sign {
assert.FileExists(t, filepath.Join(tt.options.PackagePath, "test-chart-0.1.0.tgz.prov"))
}
}
})
Expand Down
1 change: 1 addition & 0 deletions pkg/packager/testdata/passphrase-file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
secret
Binary file added pkg/packager/testdata/testkeyring.gpg
Binary file not shown.

0 comments on commit 364dcae

Please sign in to comment.