This commit is contained in:
Damien Nozay 2019-10-22 11:52:35 -07:00
Родитель a03712f086
Коммит 559b27c01f
2 изменённых файлов: 13 добавлений и 78 удалений

Просмотреть файл

@ -5,13 +5,13 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
"github.com/fatih/color"
wordwrap "github.com/mitchellh/go-wordwrap"
"go.mozilla.org/sops"
"go.mozilla.org/sops/cmd/sops/codes"
. "go.mozilla.org/sops/cmd/sops/formats"
"go.mozilla.org/sops/keys"
"go.mozilla.org/sops/keyservice"
"go.mozilla.org/sops/kms"
@ -24,24 +24,6 @@ import (
"gopkg.in/urfave/cli.v1"
)
type Format int
const (
Binary Format = iota
Dotenv
Ini
Json
Yaml
)
var stringToFormat = map[string]Format{
"binary": Binary,
"dotenv": Dotenv,
"ini": Ini,
"json": Json,
"yaml": Yaml,
}
// ExampleFileEmitter emits example files. This is used by the `sops` binary
// whenever a new file is created, in order to present the user with a non-empty file
type ExampleFileEmitter interface {
@ -167,51 +149,6 @@ func NewExitError(i interface{}, exitCode int) *cli.ExitError {
return cli.NewExitError(i, exitCode)
}
// IsYAMLFile returns true if a given file path corresponds to a YAML file
func IsYAMLFile(path string) bool {
return strings.HasSuffix(path, ".yaml") || strings.HasSuffix(path, ".yml")
}
// IsJSONFile returns true if a given file path corresponds to a JSON file
func IsJSONFile(path string) bool {
return strings.HasSuffix(path, ".json")
}
// IsEnvFile returns true if a given file path corresponds to a .env file
func IsEnvFile(path string) bool {
return strings.HasSuffix(path, ".env")
}
// IsIniFile returns true if a given file path corresponds to a INI file
func IsIniFile(path string) bool {
return strings.HasSuffix(path, ".ini")
}
// FormatFromString returns a Format from a string.
// This is used for converting string cli options.
func FormatFromString(formatString string) Format {
format, found := stringToFormat[formatString]
if !found {
return Binary
}
return format
}
// FormatForPath returns the correct format given the path to a file
func FormatForPath(path string) Format {
format := Binary // default
if IsYAMLFile(path) {
format = Yaml
} else if IsJSONFile(path) {
format = Json
} else if IsEnvFile(path) {
format = Dotenv
} else if IsIniFile(path) {
format = Ini
}
return format
}
// StoreForFormat returns the correct format-specific implementation
// of the Store interface given the format.
func StoreForFormat(format Format) Store {
@ -232,12 +169,9 @@ func DefaultStoreForPath(path string) Store {
// DefaultStoreForPathOrFormat returns the correct format-specific implementation
// of the Store interface given the formatString if specified, or the path to a file.
// This is to support the cli, where both are provided.
func DefaultStoreForPathOrFormat(path, formatString string) Store {
format, found := stringToFormat[formatString]
if !found {
format = FormatForPath(path)
}
return StoreForFormat(format)
func DefaultStoreForPathOrFormat(path, format string) Store {
formatFmt := FormatForPathOrString(path, format)
return StoreForFormat(formatFmt)
}
// KMS_ENC_CTX_BUG_FIXED_VERSION represents the SOPS version in which the

Просмотреть файл

@ -9,9 +9,9 @@ import (
"io/ioutil"
"time"
"go.mozilla.org/sops"
"go.mozilla.org/sops/aes"
"go.mozilla.org/sops/cmd/sops/common"
. "go.mozilla.org/sops/cmd/sops/formats" // Re-export
)
// File is a wrapper around Data that reads a local encrypted
@ -24,14 +24,15 @@ func File(path, format string) (cleartext []byte, err error) {
}
// uses same logic as cli.
store := common.DefaultStoreForPathOrFormat(path, format)
return DataWithStore(encryptedData, store)
format := FormatForPathOrString(path, format)
return DataWithFormat(encryptedData, format)
}
// DataWithStore is a helper that takes encrypted data, and a store,
// DataWithFormat is a helper that takes encrypted data, and a format enum value,
// decrypts the data and returns its cleartext in an []byte.
func DataWithStore(data []byte, store sops.Store) (cleartext []byte, err error) {
func DataWithFormat(data []byte, format Format) (cleartext []byte, err error) {
store := common.StoreForFormat(format)
// Load SOPS file and access the data key
tree, err := store.LoadEncryptedFile(data)
@ -70,6 +71,6 @@ func DataWithStore(data []byte, store sops.Store) (cleartext []byte, err error)
// The format string can be `json`, `yaml`, `ini`, `dotenv` or `binary`.
// If the format string is empty, binary format is assumed.
func Data(data []byte, format string) (cleartext []byte, err error) {
store := common.StoreForFormat(common.FormatFromString(format))
return DataWithStore(data, store)
format := FormatFromString(format)
return DataWithFormat(data, format)
}