Skip to content

Commit

Permalink
Use env vars containing nfpm ids for deb pgp passphrase
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanlloyd committed Oct 25, 2020
1 parent b422197 commit 322c5d5
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 23 deletions.
22 changes: 21 additions & 1 deletion internal/pipe/nfpm/nfpm.go
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/apex/log"
"github.com/goreleaser/nfpm"
Expand Down Expand Up @@ -189,7 +190,7 @@ func create(ctx *context.Context, fpm config.NFPM, format, arch string, binaries
VersionMetadata: overridden.Deb.VersionMetadata,
Signature: nfpm.DebSignature{
KeyFile: overridden.Deb.Signature.KeyFile,
KeyPassphrase: overridden.Deb.Signature.KeyPassphrase,
KeyPassphrase: getPassphraseFromEnv(ctx, "DEB", fpm.ID),
Type: overridden.Deb.Signature.Type,
},
},
Expand Down Expand Up @@ -234,3 +235,22 @@ func create(ctx *context.Context, fpm config.NFPM, format, arch string, binaries
})
return nil
}

func getPassphraseFromEnv(ctx *context.Context, packager string, nfpmID string) string {
var passphrase string

nfpmID = strings.ToUpper(nfpmID)
packagerSpecificPassphrase := ctx.Env[fmt.Sprintf(
"NFPM_%s_%s_PASSPHRASE",
nfpmID,
packager,
)]
if packagerSpecificPassphrase != "" {
passphrase = packagerSpecificPassphrase
} else {
generalPassphrase := ctx.Env[fmt.Sprintf("NFPM_%s_PASSPHRASE", nfpmID)]
passphrase = generalPassphrase
}

return passphrase
}
71 changes: 71 additions & 0 deletions internal/pipe/nfpm/nfpm_test.go
Expand Up @@ -347,6 +347,77 @@ func TestOverrides(t *testing.T) {
require.Equal(t, "bar", merged.FileNameTemplate)
}

func TestDebSpecificConfig(t *testing.T) {
folder, err := ioutil.TempDir("", "archivetest")
require.NoError(t, err)
var dist = filepath.Join(folder, "dist")
require.NoError(t, os.Mkdir(dist, 0755))
require.NoError(t, os.Mkdir(filepath.Join(dist, "mybin"), 0755))
var binPath = filepath.Join(dist, "mybin", "mybin")
_, err = os.Create(binPath)
require.NoError(t, err)
var ctx = context.New(config.Project{
ProjectName: "mybin",
Dist: dist,
NFPMs: []config.NFPM{
{
ID: "someid",
Builds: []string{"default"},
Formats: []string{"deb"},
NFPMOverridables: config.NFPMOverridables{
PackageName: "foo",
Files: map[string]string{
"./testdata/testfile.txt": "/usr/share/testfile.txt",
},
Deb: config.NFPMDeb{
Signature: config.NFPMDebSignature{
KeyFile: "./testdata/privkey.gpg",
},
},
},
},
},
})
ctx.Version = "1.0.0"
ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
for _, goos := range []string{"linux", "darwin"} {
for _, goarch := range []string{"amd64", "386"} {
ctx.Artifacts.Add(&artifact.Artifact{
Name: "mybin",
Path: binPath,
Goarch: goarch,
Goos: goos,
Type: artifact.Binary,
Extra: map[string]interface{}{
"ID": "default",
},
})
}
}

t.Run("no passphrase set", func(t *testing.T) {
require.Contains(
t,
Pipe{}.Run(ctx).Error(),
`key is encrypted but no passphrase was provided`,
)
})

t.Run("general passphrase set", func(t *testing.T) {
ctx.Env = map[string]string{
"NFPM_SOMEID_DEB_PASSPHRASE": "hunter2",
}
require.NoError(t, Pipe{}.Run(ctx))
})

t.Run("packager specific passphrase set", func(t *testing.T) {
ctx.Env = map[string]string{
"NFPM_SOMEID_PASSPHRASE": "hunter2",
}
require.NoError(t, Pipe{}.Run(ctx))
})
}

func TestSeveralNFPMsWithTheSameID(t *testing.T) {
var ctx = &context.Context{
Config: config.Project{
Expand Down
22 changes: 0 additions & 22 deletions pkg/config/config.go
Expand Up @@ -349,28 +349,6 @@ type NFPMDebSignature struct {
Type string `yaml:"type,omitempty"`
}

// type alias to prevent stack overflowing in the custom unmarshaler.
type nfpmDebSignature NFPMDebSignature

func (nds *NFPMDebSignature) UnmarshalYAML(unmarshal func(interface{}) error) error {
var sig nfpmDebSignature
if err := unmarshal(&sig); err != nil {
return err
}

debPassphrase := os.Getenv("NFPM_DEB_PASSPHRASE")
if debPassphrase != "" {
sig.KeyPassphrase = debPassphrase
} else {
generalPassphrase := os.Getenv("NFPM_PASSPHRASE")
sig.KeyPassphrase = generalPassphrase
}

*nds = NFPMDebSignature(sig)

return nil
}

// NFPMDeb is custom configs that are only available on deb packages.
type NFPMDeb struct {
Scripts NFPMDebScripts `yaml:"scripts,omitempty"`
Expand Down

0 comments on commit 322c5d5

Please sign in to comment.