Skip to content

Commit

Permalink
Add config option to disable MIME (#1546)
Browse files Browse the repository at this point in the history
RELEASE_NOTES=n/a

Signed-off-by: Dominik Schulz <[email protected]>
  • Loading branch information
dominikschulz authored Aug 22, 2020
1 parent 95d1d10 commit 922cb1a
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 51 deletions.
8 changes: 1 addition & 7 deletions internal/action/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,9 @@ func (s *Action) Config(c *cli.Context) error {
}

func (s *Action) printConfigValues(ctx context.Context, needles ...string) {
prefix := ""
if len(needles) < 1 {
out.Print(ctx, "root store config:")
prefix = " "
}

m := s.cfg.ConfigMap()
for _, k := range filterMap(m, needles) {
out.Print(ctx, "%s%s: %s", prefix, k, m[k])
out.Print(ctx, "%s: %s", k, m[k])
}
for alias, path := range s.cfg.Mounts {
if len(needles) < 1 {
Expand Down
41 changes: 21 additions & 20 deletions internal/action/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ func TestConfig(t *testing.T) {

c := gptest.CliCtx(ctx, t)
assert.NoError(t, act.Config(c))
want := `root store config:
autoclip: true
autoimport: true
cliptimeout: 45
exportkeys: true
nocolor: false
nopager: false
notifications: true
want := `autoclip: true
autoimport: true
cliptimeout: 45
exportkeys: true
mime: true
nocolor: false
nopager: false
notifications: true
`
want += " path: " + u.StoreDir("") + "\n"
want += ` safecontent: false
want += "path: " + u.StoreDir("") + "\n"
want += `safecontent: false
`
assert.Equal(t, want, buf.String())
})
Expand Down Expand Up @@ -77,17 +77,17 @@ func TestConfig(t *testing.T) {
defer buf.Reset()

act.printConfigValues(ctx)
want := `root store config:
autoclip: true
autoimport: true
cliptimeout: 45
exportkeys: true
nocolor: false
nopager: true
notifications: true
want := `autoclip: true
autoimport: true
cliptimeout: 45
exportkeys: true
mime: true
nocolor: false
nopager: true
notifications: true
`
want += " path: " + u.StoreDir("") + "\n"
want += ` safecontent: false`
want += "path: " + u.StoreDir("") + "\n"
want += `safecontent: false`
assert.Equal(t, want, strings.TrimSpace(buf.String()), "action.printConfigValues")

delete(act.cfg.Mounts, "foo")
Expand Down Expand Up @@ -117,6 +117,7 @@ func TestConfig(t *testing.T) {
autoimport
cliptimeout
exportkeys
mime
nocolor
nopager
notifications
Expand Down
2 changes: 2 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Config struct {
AutoImport bool `yaml:"autoimport"` // import missing public keys w/o asking
ClipTimeout int `yaml:"cliptimeout"` // clear clipboard after seconds
ExportKeys bool `yaml:"exportkeys"` // automatically export public keys of all recipients
MIME bool `yaml:"mime"` // enable gopass native MIME secrets
NoColor bool `yaml:"nocolor"` // do not use color when outputing text
NoPager bool `yaml:"nopager"` // do not invoke a pager to display long lists
Notifications bool `yaml:"notifications"` // enable desktop notifications
Expand All @@ -42,6 +43,7 @@ func New() *Config {
AutoImport: true,
ClipTimeout: 45,
ExportKeys: true,
MIME: true,
Mounts: make(map[string]string),
Notifications: true,
Path: PwStoreDir(""),
Expand Down
4 changes: 2 additions & 2 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestNewConfig(t *testing.T) {

cfg := config.New()
cs := cfg.String()
assert.Contains(t, cs, `&config.Config{AutoClip:false, AutoImport:true, ClipTimeout:45, ExportKeys:true, NoColor:false, NoPager:false, Notifications:true,`)
assert.Contains(t, cs, `&config.Config{AutoClip:false, AutoImport:true, ClipTimeout:45, ExportKeys:true, MIME:true, NoColor:false, NoPager:false, Notifications:true,`)
assert.Contains(t, cs, `SafeContent:false, Mounts:map[string]string{},`)

cfg = &config.Config{
Expand All @@ -30,7 +30,7 @@ func TestNewConfig(t *testing.T) {
cfg.Mounts["foo"] = ""
cfg.Mounts["bar"] = ""
cs = cfg.String()
assert.Contains(t, cs, `&config.Config{AutoClip:false, AutoImport:false, ClipTimeout:0, ExportKeys:false, NoColor:false, NoPager:false, Notifications:false,`)
assert.Contains(t, cs, `&config.Config{AutoClip:false, AutoImport:false, ClipTimeout:0, ExportKeys:false, MIME:false, NoColor:false, NoPager:false, Notifications:false,`)
assert.Contains(t, cs, `SafeContent:false, Mounts:map[string]string{"bar":"", "foo":""},`)
}

Expand Down
4 changes: 4 additions & 0 deletions internal/config/legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func (c *Pre193) Config() *Config {
AutoClip: c.Root.AutoClip,
AutoImport: c.Root.AutoImport,
ClipTimeout: c.Root.ClipTimeout,
MIME: true,
NoColor: c.Root.NoColor,
NoPager: c.Root.NoPager,
Notifications: c.Root.Notifications,
Expand Down Expand Up @@ -108,6 +109,7 @@ func (c *Pre182) Config() *Config {
AutoClip: c.Root.AutoClip,
AutoImport: c.Root.AutoImport,
ClipTimeout: c.Root.ClipTimeout,
MIME: true,
NoColor: c.Root.NoColor,
NoPager: c.Root.NoPager,
Notifications: c.Root.Notifications,
Expand Down Expand Up @@ -156,6 +158,7 @@ func (c *Pre140) Config() *Config {
ClipTimeout: c.ClipTimeout,
Path: c.Path,
SafeContent: c.SafeContent,
MIME: true,
Mounts: make(map[string]string, len(c.Mounts)),
}
return cfg
Expand Down Expand Up @@ -196,6 +199,7 @@ func (c *Pre130) Config() *Config {
ClipTimeout: c.ClipTimeout,
Path: c.Path,
SafeContent: c.SafeContent,
MIME: true,
Mounts: make(map[string]string, len(c.Mounts)),
}
return cfg
Expand Down
1 change: 1 addition & 0 deletions internal/gptest/unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const (
autoimport: true
cliptimeout: 45
exportkeys: true
mime: true
notifications: true`
)

Expand Down
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
_ "github.com/gopasspw/gopass/internal/backend/storage"
"github.com/gopasspw/gopass/internal/queue"
"github.com/gopasspw/gopass/pkg/ctxutil"
"github.com/gopasspw/gopass/pkg/gopass/secret"
"github.com/gopasspw/gopass/pkg/protect"
"golang.org/x/crypto/ssh/terminal"

Expand Down Expand Up @@ -301,5 +302,8 @@ func initContext(ctx context.Context, cfg *config.Config) context.Context {
ctx = ctxutil.WithColor(ctx, false)
}

// secrets compat mode
secret.WriteMIME = cfg.MIME

return ctx
}
10 changes: 8 additions & 2 deletions pkg/gopass/secret/mime.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"io"
"net/textproto"
"os"
"sort"
"strings"
)
Expand All @@ -16,6 +15,13 @@ const (
Ident = "GOPASS-SECRET-1.0"
)

var (
// WriteMIME can be disabled to disable writing the new secrets format.
// Use this to ensure secrets written by gopass can be correctly consumed
// by other Password Store implementations, too.
WriteMIME = true
)

// PermanentError signal that parsing should not attempt other formats.
type PermanentError struct {
Err error
Expand Down Expand Up @@ -114,7 +120,7 @@ func (s *MIME) bytesCompat() []byte {

// Bytes serializes the secret
func (s *MIME) Bytes() []byte {
if compat := os.Getenv("GOPASS_DISABLE_MIME"); compat != "" {
if !WriteMIME {
return s.bytesCompat()
}
buf := &bytes.Buffer{}
Expand Down
41 changes: 21 additions & 20 deletions tests/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,23 @@ func TestBaseConfig(t *testing.T) {

out, err := ts.run("config")
assert.NoError(t, err)
wanted := `root store config:
autoclip: false
autoimport: true
cliptimeout: 45
exportkeys: false
nocolor: false
nopager: false
notifications: false
wanted := `autoclip: false
autoimport: true
cliptimeout: 45
exportkeys: false
mime: true
nocolor: false
nopager: false
notifications: false
`
wanted += " path: " + ts.storeDir("root") + "\n"
wanted += " safecontent: false"
wanted += "path: " + ts.storeDir("root") + "\n"
wanted += "safecontent: false"

assert.Equal(t, wanted, out)

invertables := []string{
"autoimport",
"mime",
"safecontent",
}

Expand Down Expand Up @@ -68,17 +69,17 @@ func TestMountConfig(t *testing.T) {
_, err = ts.run("config")
assert.NoError(t, err)

wanted := `root store config:
autoclip: false
autoimport: true
cliptimeout: 45
exportkeys: false
nocolor: false
nopager: false
notifications: false
path: `
wanted := `autoclip: false
autoimport: true
cliptimeout: 45
exportkeys: false
mime: true
nocolor: false
nopager: false
notifications: false
path: `
wanted += ts.storeDir("root") + "\n"
wanted += ` safecontent: false
wanted += `safecontent: false
mount 'mnt/m1' => '`
wanted += ts.storeDir("m1") + "'\n"

Expand Down
1 change: 1 addition & 0 deletions tests/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (
autoimport: true
cliptimeout: 45
exportkeys: false
mime: true
`
keyID = "BE73F104"
)
Expand Down

0 comments on commit 922cb1a

Please sign in to comment.