config: factor Obscure and Reveal into its own package

This commit is contained in:
Nick Craig-Wood 2018-01-18 20:19:55 +00:00
parent 59a8108fc3
commit 71722b5b95
19 changed files with 43 additions and 29 deletions

View File

@ -25,6 +25,7 @@ import (
"github.com/ncw/rclone/fs" "github.com/ncw/rclone/fs"
"github.com/ncw/rclone/fs/config" "github.com/ncw/rclone/fs/config"
"github.com/ncw/rclone/fs/config/flags" "github.com/ncw/rclone/fs/config/flags"
"github.com/ncw/rclone/fs/config/obscure"
"github.com/ncw/rclone/fs/fserrors" "github.com/ncw/rclone/fs/fserrors"
"github.com/ncw/rclone/fs/hash" "github.com/ncw/rclone/fs/hash"
"github.com/ncw/rclone/lib/dircache" "github.com/ncw/rclone/lib/dircache"
@ -58,7 +59,7 @@ var (
TokenURL: "https://app.box.com/api/oauth2/token", TokenURL: "https://app.box.com/api/oauth2/token",
}, },
ClientID: rcloneClientID, ClientID: rcloneClientID,
ClientSecret: config.MustReveal(rcloneEncryptedClientSecret), ClientSecret: obscure.MustReveal(rcloneEncryptedClientSecret),
RedirectURL: oauthutil.RedirectURL, RedirectURL: oauthutil.RedirectURL,
} }
uploadCutoff = fs.SizeSuffix(50 * 1024 * 1024) uploadCutoff = fs.SizeSuffix(50 * 1024 * 1024)

View File

@ -20,6 +20,7 @@ import (
"github.com/ncw/rclone/fs" "github.com/ncw/rclone/fs"
"github.com/ncw/rclone/fs/config" "github.com/ncw/rclone/fs/config"
"github.com/ncw/rclone/fs/config/flags" "github.com/ncw/rclone/fs/config/flags"
"github.com/ncw/rclone/fs/config/obscure"
"github.com/ncw/rclone/fs/hash" "github.com/ncw/rclone/fs/hash"
"github.com/ncw/rclone/fs/walk" "github.com/ncw/rclone/fs/walk"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -308,7 +309,7 @@ func NewFs(name, rpath string) (fs.Fs, error) {
plexUsername := config.FileGet(name, "plex_username") plexUsername := config.FileGet(name, "plex_username")
plexPassword := config.FileGet(name, "plex_password") plexPassword := config.FileGet(name, "plex_password")
if plexPassword != "" && plexUsername != "" { if plexPassword != "" && plexUsername != "" {
decPass, err := config.Reveal(plexPassword) decPass, err := obscure.Reveal(plexPassword)
if err != nil { if err != nil {
decPass = plexPassword decPass = plexPassword
} }

View File

@ -12,6 +12,7 @@ import (
"github.com/ncw/rclone/fs" "github.com/ncw/rclone/fs"
"github.com/ncw/rclone/fs/config" "github.com/ncw/rclone/fs/config"
"github.com/ncw/rclone/fs/config/flags" "github.com/ncw/rclone/fs/config/flags"
"github.com/ncw/rclone/fs/config/obscure"
"github.com/ncw/rclone/fs/hash" "github.com/ncw/rclone/fs/hash"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
@ -86,13 +87,13 @@ func NewFs(name, rpath string) (fs.Fs, error) {
if password == "" { if password == "" {
return nil, errors.New("password not set in config file") return nil, errors.New("password not set in config file")
} }
password, err = config.Reveal(password) password, err = obscure.Reveal(password)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "failed to decrypt password") return nil, errors.Wrap(err, "failed to decrypt password")
} }
salt := config.FileGet(name, "password2", "") salt := config.FileGet(name, "password2", "")
if salt != "" { if salt != "" {
salt, err = config.Reveal(salt) salt, err = obscure.Reveal(salt)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "failed to decrypt password2") return nil, errors.Wrap(err, "failed to decrypt password2")
} }

View File

@ -4,7 +4,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"github.com/ncw/rclone/fs/config" "github.com/ncw/rclone/fs/config/obscure"
"github.com/ncw/rclone/fstest/fstests" "github.com/ncw/rclone/fstest/fstests"
) )
@ -19,15 +19,15 @@ func init() {
fstests.ExtraConfig = []fstests.ExtraConfigItem{ fstests.ExtraConfig = []fstests.ExtraConfigItem{
{Name: name, Key: "type", Value: "crypt"}, {Name: name, Key: "type", Value: "crypt"},
{Name: name, Key: "remote", Value: tempdir}, {Name: name, Key: "remote", Value: tempdir},
{Name: name, Key: "password", Value: config.MustObscure("potato")}, {Name: name, Key: "password", Value: obscure.MustObscure("potato")},
{Name: name, Key: "filename_encryption", Value: "standard"}, {Name: name, Key: "filename_encryption", Value: "standard"},
{Name: name2, Key: "type", Value: "crypt"}, {Name: name2, Key: "type", Value: "crypt"},
{Name: name2, Key: "remote", Value: tempdir2}, {Name: name2, Key: "remote", Value: tempdir2},
{Name: name2, Key: "password", Value: config.MustObscure("potato2")}, {Name: name2, Key: "password", Value: obscure.MustObscure("potato2")},
{Name: name2, Key: "filename_encryption", Value: "off"}, {Name: name2, Key: "filename_encryption", Value: "off"},
{Name: name3, Key: "type", Value: "crypt"}, {Name: name3, Key: "type", Value: "crypt"},
{Name: name3, Key: "remote", Value: tempdir3}, {Name: name3, Key: "remote", Value: tempdir3},
{Name: name3, Key: "password", Value: config.MustObscure("potato2")}, {Name: name3, Key: "password", Value: obscure.MustObscure("potato2")},
{Name: name3, Key: "filename_encryption", Value: "obfuscate"}, {Name: name3, Key: "filename_encryption", Value: "obfuscate"},
} }
fstests.SkipBadWindowsCharacters[name3+":"] = true fstests.SkipBadWindowsCharacters[name3+":"] = true

View File

@ -23,6 +23,7 @@ import (
"github.com/ncw/rclone/fs" "github.com/ncw/rclone/fs"
"github.com/ncw/rclone/fs/config" "github.com/ncw/rclone/fs/config"
"github.com/ncw/rclone/fs/config/flags" "github.com/ncw/rclone/fs/config/flags"
"github.com/ncw/rclone/fs/config/obscure"
"github.com/ncw/rclone/fs/fserrors" "github.com/ncw/rclone/fs/fserrors"
"github.com/ncw/rclone/fs/fshttp" "github.com/ncw/rclone/fs/fshttp"
"github.com/ncw/rclone/fs/hash" "github.com/ncw/rclone/fs/hash"
@ -66,7 +67,7 @@ var (
Scopes: []string{"https://www.googleapis.com/auth/drive"}, Scopes: []string{"https://www.googleapis.com/auth/drive"},
Endpoint: google.Endpoint, Endpoint: google.Endpoint,
ClientID: rcloneClientID, ClientID: rcloneClientID,
ClientSecret: config.MustReveal(rcloneEncryptedClientSecret), ClientSecret: obscure.MustReveal(rcloneEncryptedClientSecret),
RedirectURL: oauthutil.TitleBarRedirectURL, RedirectURL: oauthutil.TitleBarRedirectURL,
} }
mimeTypeToExtension = map[string]string{ mimeTypeToExtension = map[string]string{

View File

@ -39,6 +39,7 @@ import (
"github.com/ncw/rclone/fs" "github.com/ncw/rclone/fs"
"github.com/ncw/rclone/fs/config" "github.com/ncw/rclone/fs/config"
"github.com/ncw/rclone/fs/config/flags" "github.com/ncw/rclone/fs/config/flags"
"github.com/ncw/rclone/fs/config/obscure"
"github.com/ncw/rclone/fs/fserrors" "github.com/ncw/rclone/fs/fserrors"
"github.com/ncw/rclone/fs/hash" "github.com/ncw/rclone/fs/hash"
"github.com/ncw/rclone/lib/oauthutil" "github.com/ncw/rclone/lib/oauthutil"
@ -67,7 +68,7 @@ var (
// }, // },
Endpoint: dropbox.OAuthEndpoint(""), Endpoint: dropbox.OAuthEndpoint(""),
ClientID: rcloneClientID, ClientID: rcloneClientID,
ClientSecret: config.MustReveal(rcloneEncryptedClientSecret), ClientSecret: obscure.MustReveal(rcloneEncryptedClientSecret),
RedirectURL: oauthutil.RedirectLocalhostURL, RedirectURL: oauthutil.RedirectLocalhostURL,
} }
// A regexp matching path names for files Dropbox ignores // A regexp matching path names for files Dropbox ignores

View File

@ -14,6 +14,7 @@ import (
"github.com/jlaffaye/ftp" "github.com/jlaffaye/ftp"
"github.com/ncw/rclone/fs" "github.com/ncw/rclone/fs"
"github.com/ncw/rclone/fs/config" "github.com/ncw/rclone/fs/config"
"github.com/ncw/rclone/fs/config/obscure"
"github.com/ncw/rclone/fs/hash" "github.com/ncw/rclone/fs/hash"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
@ -188,7 +189,7 @@ func NewFs(name, root string) (ff fs.Fs, err error) {
user := config.FileGet(name, "user") user := config.FileGet(name, "user")
pass := config.FileGet(name, "pass") pass := config.FileGet(name, "pass")
port := config.FileGet(name, "port") port := config.FileGet(name, "port")
pass, err = config.Reveal(pass) pass, err = obscure.Reveal(pass)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "NewFS decrypt password") return nil, errors.Wrap(err, "NewFS decrypt password")
} }

View File

@ -30,6 +30,7 @@ import (
"github.com/ncw/rclone/fs" "github.com/ncw/rclone/fs"
"github.com/ncw/rclone/fs/config" "github.com/ncw/rclone/fs/config"
"github.com/ncw/rclone/fs/config/flags" "github.com/ncw/rclone/fs/config/flags"
"github.com/ncw/rclone/fs/config/obscure"
"github.com/ncw/rclone/fs/fshttp" "github.com/ncw/rclone/fs/fshttp"
"github.com/ncw/rclone/fs/hash" "github.com/ncw/rclone/fs/hash"
"github.com/ncw/rclone/fs/walk" "github.com/ncw/rclone/fs/walk"
@ -58,7 +59,7 @@ var (
Scopes: []string{storage.DevstorageFullControlScope}, Scopes: []string{storage.DevstorageFullControlScope},
Endpoint: google.Endpoint, Endpoint: google.Endpoint,
ClientID: rcloneClientID, ClientID: rcloneClientID,
ClientSecret: config.MustReveal(rcloneEncryptedClientSecret), ClientSecret: obscure.MustReveal(rcloneEncryptedClientSecret),
RedirectURL: oauthutil.TitleBarRedirectURL, RedirectURL: oauthutil.TitleBarRedirectURL,
} }
) )

View File

@ -16,6 +16,7 @@ import (
"github.com/ncw/rclone/backend/swift" "github.com/ncw/rclone/backend/swift"
"github.com/ncw/rclone/fs" "github.com/ncw/rclone/fs"
"github.com/ncw/rclone/fs/config" "github.com/ncw/rclone/fs/config"
"github.com/ncw/rclone/fs/config/obscure"
"github.com/ncw/rclone/fs/fshttp" "github.com/ncw/rclone/fs/fshttp"
"github.com/ncw/rclone/lib/oauthutil" "github.com/ncw/rclone/lib/oauthutil"
swiftLib "github.com/ncw/swift" swiftLib "github.com/ncw/swift"
@ -40,7 +41,7 @@ var (
TokenURL: "https://api.hubic.com/oauth/token/", TokenURL: "https://api.hubic.com/oauth/token/",
}, },
ClientID: rcloneClientID, ClientID: rcloneClientID,
ClientSecret: config.MustReveal(rcloneEncryptedClientSecret), ClientSecret: obscure.MustReveal(rcloneEncryptedClientSecret),
RedirectURL: oauthutil.RedirectLocalhostURL, RedirectURL: oauthutil.RedirectLocalhostURL,
} }
) )

View File

@ -18,6 +18,7 @@ import (
"github.com/ncw/rclone/fs" "github.com/ncw/rclone/fs"
"github.com/ncw/rclone/fs/config" "github.com/ncw/rclone/fs/config"
"github.com/ncw/rclone/fs/config/flags" "github.com/ncw/rclone/fs/config/flags"
"github.com/ncw/rclone/fs/config/obscure"
"github.com/ncw/rclone/fs/fserrors" "github.com/ncw/rclone/fs/fserrors"
"github.com/ncw/rclone/fs/hash" "github.com/ncw/rclone/fs/hash"
"github.com/ncw/rclone/lib/dircache" "github.com/ncw/rclone/lib/dircache"
@ -56,7 +57,7 @@ var (
TokenURL: "https://login.live.com/oauth20_token.srf", TokenURL: "https://login.live.com/oauth20_token.srf",
}, },
ClientID: rclonePersonalClientID, ClientID: rclonePersonalClientID,
ClientSecret: config.MustReveal(rclonePersonalEncryptedClientSecret), ClientSecret: obscure.MustReveal(rclonePersonalEncryptedClientSecret),
RedirectURL: oauthutil.RedirectLocalhostURL, RedirectURL: oauthutil.RedirectLocalhostURL,
} }
@ -67,7 +68,7 @@ var (
TokenURL: "https://login.microsoftonline.com/common/oauth2/token", TokenURL: "https://login.microsoftonline.com/common/oauth2/token",
}, },
ClientID: rcloneBusinessClientID, ClientID: rcloneBusinessClientID,
ClientSecret: config.MustReveal(rcloneBusinessEncryptedClientSecret), ClientSecret: obscure.MustReveal(rcloneBusinessEncryptedClientSecret),
RedirectURL: oauthutil.RedirectLocalhostURL, RedirectURL: oauthutil.RedirectLocalhostURL,
} }
oauthBusinessResource = oauth2.SetAuthURLParam("resource", discoveryServiceURL) oauthBusinessResource = oauth2.SetAuthURLParam("resource", discoveryServiceURL)

View File

@ -25,6 +25,7 @@ import (
"github.com/ncw/rclone/fs" "github.com/ncw/rclone/fs"
"github.com/ncw/rclone/fs/config" "github.com/ncw/rclone/fs/config"
"github.com/ncw/rclone/fs/config/flags" "github.com/ncw/rclone/fs/config/flags"
"github.com/ncw/rclone/fs/config/obscure"
"github.com/ncw/rclone/fs/fserrors" "github.com/ncw/rclone/fs/fserrors"
"github.com/ncw/rclone/fs/hash" "github.com/ncw/rclone/fs/hash"
"github.com/ncw/rclone/lib/dircache" "github.com/ncw/rclone/lib/dircache"
@ -55,7 +56,7 @@ var (
TokenURL: "https://api.pcloud.com/oauth2_token", TokenURL: "https://api.pcloud.com/oauth2_token",
}, },
ClientID: rcloneClientID, ClientID: rcloneClientID,
ClientSecret: config.MustReveal(rcloneEncryptedClientSecret), ClientSecret: obscure.MustReveal(rcloneEncryptedClientSecret),
RedirectURL: oauthutil.RedirectLocalhostURL, RedirectURL: oauthutil.RedirectLocalhostURL,
} }
uploadCutoff = fs.SizeSuffix(50 * 1024 * 1024) uploadCutoff = fs.SizeSuffix(50 * 1024 * 1024)

View File

@ -17,6 +17,7 @@ import (
"github.com/ncw/rclone/fs" "github.com/ncw/rclone/fs"
"github.com/ncw/rclone/fs/config" "github.com/ncw/rclone/fs/config"
"github.com/ncw/rclone/fs/config/obscure"
"github.com/ncw/rclone/fs/fshttp" "github.com/ncw/rclone/fs/fshttp"
"github.com/ncw/rclone/fs/hash" "github.com/ncw/rclone/fs/hash"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -318,7 +319,7 @@ func NewFs(name, root string) (fs.Fs, error) {
// Auth from password if specified // Auth from password if specified
if pass != "" { if pass != "" {
clearpass, err := config.Reveal(pass) clearpass, err := obscure.Reveal(pass)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -31,6 +31,7 @@ import (
"github.com/ncw/rclone/backend/webdav/api" "github.com/ncw/rclone/backend/webdav/api"
"github.com/ncw/rclone/fs" "github.com/ncw/rclone/fs"
"github.com/ncw/rclone/fs/config" "github.com/ncw/rclone/fs/config"
"github.com/ncw/rclone/fs/config/obscure"
"github.com/ncw/rclone/fs/fserrors" "github.com/ncw/rclone/fs/fserrors"
"github.com/ncw/rclone/fs/fshttp" "github.com/ncw/rclone/fs/fshttp"
"github.com/ncw/rclone/fs/hash" "github.com/ncw/rclone/fs/hash"
@ -260,7 +261,7 @@ func NewFs(name, root string) (fs.Fs, error) {
pass := config.FileGet(name, "pass") pass := config.FileGet(name, "pass")
if pass != "" { if pass != "" {
var err error var err error
pass, err = config.Reveal(pass) pass, err = obscure.Reveal(pass)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "couldn't decrypt password") return nil, errors.Wrap(err, "couldn't decrypt password")
} }

View File

@ -16,6 +16,7 @@ import (
yandex "github.com/ncw/rclone/backend/yandex/api" yandex "github.com/ncw/rclone/backend/yandex/api"
"github.com/ncw/rclone/fs" "github.com/ncw/rclone/fs"
"github.com/ncw/rclone/fs/config" "github.com/ncw/rclone/fs/config"
"github.com/ncw/rclone/fs/config/obscure"
"github.com/ncw/rclone/fs/fshttp" "github.com/ncw/rclone/fs/fshttp"
"github.com/ncw/rclone/fs/hash" "github.com/ncw/rclone/fs/hash"
"github.com/ncw/rclone/lib/oauthutil" "github.com/ncw/rclone/lib/oauthutil"
@ -39,7 +40,7 @@ var (
TokenURL: "https://oauth.yandex.com/token", //same as https://oauth.yandex.ru/token TokenURL: "https://oauth.yandex.com/token", //same as https://oauth.yandex.ru/token
}, },
ClientID: rcloneClientID, ClientID: rcloneClientID,
ClientSecret: config.MustReveal(rcloneEncryptedClientSecret), ClientSecret: obscure.MustReveal(rcloneEncryptedClientSecret),
RedirectURL: oauthutil.RedirectURL, RedirectURL: oauthutil.RedirectURL,
} }
) )

View File

@ -4,7 +4,7 @@ import (
"fmt" "fmt"
"github.com/ncw/rclone/cmd" "github.com/ncw/rclone/cmd"
"github.com/ncw/rclone/fs/config" "github.com/ncw/rclone/fs/config/obscure"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -18,8 +18,8 @@ var commandDefintion = &cobra.Command{
Run: func(command *cobra.Command, args []string) { Run: func(command *cobra.Command, args []string) {
cmd.CheckArgs(1, 1, command, args) cmd.CheckArgs(1, 1, command, args)
cmd.Run(false, false, command, func() error { cmd.Run(false, false, command, func() error {
obscure := config.MustObscure(args[0]) obscured := obscure.MustObscure(args[0])
fmt.Println(obscure) fmt.Println(obscured)
return nil return nil
}) })
}, },

View File

@ -25,6 +25,7 @@ import (
"github.com/Unknwon/goconfig" "github.com/Unknwon/goconfig"
"github.com/ncw/rclone/fs" "github.com/ncw/rclone/fs"
"github.com/ncw/rclone/fs/accounting" "github.com/ncw/rclone/fs/accounting"
"github.com/ncw/rclone/fs/config/obscure"
"github.com/ncw/rclone/fs/driveletter" "github.com/ncw/rclone/fs/driveletter"
"github.com/ncw/rclone/fs/fshttp" "github.com/ncw/rclone/fs/fshttp"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -694,7 +695,7 @@ func ChooseOption(o *fs.Option) string {
default: default:
fs.Errorf(nil, "Bad choice %c", i) fs.Errorf(nil, "Bad choice %c", i)
} }
return MustObscure(password) return obscure.MustObscure(password)
} }
if len(o.Examples) > 0 { if len(o.Examples) > 0 {
var values []string var values []string
@ -749,7 +750,7 @@ func PasswordRemote(name string, keyValues []string) error {
} }
// Suppress Confirm // Suppress Confirm
fs.Config.AutoConfirm = true fs.Config.AutoConfirm = true
passwd := MustObscure(keyValues[1]) passwd := obscure.MustObscure(keyValues[1])
if passwd != "" { if passwd != "" {
configData.SetValue(name, keyValues[0], passwd) configData.SetValue(name, keyValues[0], passwd)
RemoteConfig(name) RemoteConfig(name)

View File

@ -6,6 +6,7 @@ import (
"testing" "testing"
"github.com/ncw/rclone/fs" "github.com/ncw/rclone/fs"
"github.com/ncw/rclone/fs/config/obscure"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -89,7 +90,7 @@ func TestReveal(t *testing.T) {
{"aGVsbG8", "input too short when revealing password - is it obscured?"}, {"aGVsbG8", "input too short when revealing password - is it obscured?"},
{"", "input too short when revealing password - is it obscured?"}, {"", "input too short when revealing password - is it obscured?"},
} { } {
gotString, gotErr := Reveal(test.in) gotString, gotErr := obscure.Reveal(test.in)
assert.Equal(t, "", gotString) assert.Equal(t, "", gotString)
assert.Equal(t, test.wantErr, gotErr.Error()) assert.Equal(t, test.wantErr, gotErr.Error())
} }

View File

@ -1,6 +1,5 @@
// Obscure and Reveal config values // Package obscure contains the Obscure and Reveal commands
package obscure
package config
import ( import (
"crypto/aes" "crypto/aes"

View File

@ -1,4 +1,4 @@
package config package obscure
import ( import (
"bytes" "bytes"