Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 8 additions & 13 deletions pkg/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ import (
"github.com/spf13/cobra"
)

var defaultFallbackDir string

const defaultFallbackFileMaxAge = 14 * 24 * time.Hour // 14 days
const defaultLivenessPingIntervalSeconds = 60 * 5 * time.Second

Expand Down Expand Up @@ -445,9 +443,9 @@ var runCleanCmd = &cobra.Command{
dryRun := utils.GetBoolFlag(cmd, "dry-run")
all := utils.GetBoolFlag(cmd, "all")

utils.LogDebug(fmt.Sprintf("Using fallback directory %s", defaultFallbackDir))
utils.LogDebug(fmt.Sprintf("Using fallback directory %s", configuration.UserFallbackDir))

if _, err := os.Stat(defaultFallbackDir); err != nil {
if _, err := os.Stat(configuration.UserFallbackDir); err != nil {
if os.IsNotExist(err) {
utils.LogDebug("Fallback directory does not exist")
utils.Print("Nothing to clean")
Expand All @@ -457,7 +455,7 @@ var runCleanCmd = &cobra.Command{
utils.HandleError(err, "Unable to read fallback directory")
}

entries, err := ioutil.ReadDir(defaultFallbackDir)
entries, err := ioutil.ReadDir(configuration.UserFallbackDir)
if err != nil {
utils.HandleError(err, "Unable to read fallback directory")
}
Expand Down Expand Up @@ -486,7 +484,7 @@ var runCleanCmd = &cobra.Command{
}

if delete {
file := filepath.Join(defaultFallbackDir, entry.Name())
file := filepath.Join(configuration.UserFallbackDir, entry.Name())
utils.LogDebug(fmt.Sprintf("%s %s", action, file))

if dryRun {
Expand Down Expand Up @@ -517,7 +515,7 @@ var runCleanCmd = &cobra.Command{
func legacyFallbackFile(project string, config string) string {
name := fmt.Sprintf("%s:%s", project, config)
fileName := fmt.Sprintf(".run-%s.json", crypto.Hash(name))
return filepath.Join(defaultFallbackDir, fileName)
return filepath.Join(configuration.UserFallbackDir, fileName)
}

// generate the passphrase used for encrypting a secrets file
Expand Down Expand Up @@ -552,15 +550,15 @@ func initFallbackDir(cmd *cobra.Command, config models.ScopedOptions, format mod
}
} else {
fallbackFileName := fmt.Sprintf(".secrets-%s.json", controllers.GenerateFallbackFileHash(config.Token.Value, config.EnclaveProject.Value, config.EnclaveConfig.Value, format, nameTransformer, secretNames))
fallbackPath = filepath.Join(defaultFallbackDir, fallbackFileName)
fallbackPath = filepath.Join(configuration.UserFallbackDir, fallbackFileName)
// TODO remove this when releasing CLI v4 (DPLR-435)
if config.EnclaveProject.Value != "" && config.EnclaveConfig.Value != "" {
// save to old path to maintain backwards compatibility
legacyFallbackPath = legacyFallbackFile(config.EnclaveProject.Value, config.EnclaveConfig.Value)
}

if !utils.Exists(defaultFallbackDir) {
err := os.Mkdir(defaultFallbackDir, 0700)
if !utils.Exists(configuration.UserFallbackDir) {
err := os.Mkdir(configuration.UserFallbackDir, 0700)
if err != nil {
utils.LogDebug("Unable to create directory for fallback file")
if exitOnWriteFailure {
Expand All @@ -584,9 +582,6 @@ func initFallbackDir(cmd *cobra.Command, config models.ScopedOptions, format mod
}

func init() {
defaultFallbackDir = filepath.Join(configuration.UserConfigDir, "fallback")
controllers.DefaultMetadataDir = defaultFallbackDir

forwardSignals := !isatty.IsTerminal(os.Stdout.Fd())

runCmd.Flags().StringP("project", "p", "", "project (e.g. backend)")
Expand Down
8 changes: 8 additions & 0 deletions pkg/configuration/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ var UserConfigDir string
// UserConfigFile (e.g. /home/user/doppler/.doppler.yaml)
var UserConfigFile string

// UserFallbackDir (e.g. /home/user/doppler/.doppler.yaml)
var UserFallbackDir string
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would these be better named as FallbackDir and MetadataDir since they're still used even if the user hasn't configured them? Or am I misunderstanding that part?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question -- SetConfigDir is called in the init function with the default $HOME/.doppler directory, which is invoked automatically when the module loads. SetConfigDir already updates UserConfigDir and UserConfigFile so I'm mostly just following the existing naming pattern

func init() {
SetConfigDir(filepath.Join(utils.HomeDir(), ".doppler"))
}
func SetConfigDir(dir string) {
UserConfigDir = dir
UserConfigFile = filepath.Join(UserConfigDir, configFileName)
}


// UserMetadataDir the directory containing metadata files
var UserMetadataDir string

// Scope to use for config file
var Scope = "."

Expand All @@ -54,6 +60,8 @@ func init() {

func SetConfigDir(dir string) {
UserConfigDir = dir
UserFallbackDir = filepath.Join(dir, "fallback")
UserMetadataDir = UserFallbackDir
UserConfigFile = filepath.Join(UserConfigDir, configFileName)
}

Expand Down
6 changes: 2 additions & 4 deletions pkg/controllers/fallback.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,13 @@ import (
"sort"
"strings"

"github.com/DopplerHQ/cli/pkg/configuration"
"github.com/DopplerHQ/cli/pkg/crypto"
"github.com/DopplerHQ/cli/pkg/models"
"github.com/DopplerHQ/cli/pkg/utils"
"gopkg.in/yaml.v3"
)

// DefaultMetadataDir the directory containing metadata files
var DefaultMetadataDir string

func GenerateFallbackFileHash(token string, project string, config string, format models.SecretsFormat, nameTransformer *models.SecretsNameTransformer, secretNames []string) string {
parts := []string{token}
if project != "" && config != "" {
Expand Down Expand Up @@ -63,7 +61,7 @@ func GenerateFallbackFileHash(token string, project string, config string, forma
// MetadataFilePath calculates the name of the metadata file
func MetadataFilePath(token string, project string, config string, format models.SecretsFormat, nameTransformer *models.SecretsNameTransformer, secretNames []string) string {
fileName := fmt.Sprintf(".metadata-%s.json", GenerateFallbackFileHash(token, project, config, format, nameTransformer, secretNames))
path := filepath.Join(DefaultMetadataDir, fileName)
path := filepath.Join(configuration.UserMetadataDir, fileName)
if absPath, err := filepath.Abs(path); err == nil {
return absPath
}
Expand Down
14 changes: 14 additions & 0 deletions tests/e2e/secrets-download-fallback.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ beforeEach

beforeEach

# test fallback file and metadata file respect DOPPLER_CONFIG_DIR
test_config_dir='/tmp/doppler-fallback-config-test'
test_fallback_dir="$test_config_dir/fallback"
mkdir -p "$test_fallback_dir"
DOPPLER_CONFIG_DIR="$test_config_dir" "$DOPPLER_BINARY" secrets download --no-file > /dev/null

fallback_file_count="$(find "$test_fallback_dir" -name '.secrets-*' | wc -l)"
[[ "$fallback_file_count" == "1" ]] || (echo "ERROR: 'run' did not create fallback file in DOPPLER_CONFIG_DIR/fallback" && exit 1)

metadata_file_count="$(find "$test_fallback_dir" -name '.metadata-*' | wc -l)"
[[ "$metadata_file_count" == "1" ]] || (echo "ERROR: 'run' did not create metadata file in DOPPLER_CONFIG_DIR/fallback" && exit 1)

beforeEach

# test fallback-readonly doesn't write a fallback file
"$DOPPLER_BINARY" secrets download --no-file --fallback-readonly > /dev/null
"$DOPPLER_BINARY" secrets download --no-file --fallback-only > /dev/null 2>&1 && (echo "ERROR: --fallback-readonly flag is not respected" && exit 1)
Expand Down
Loading