The Wayback Machine - https://web.archive.org/web/20201216020240/https://github.com/mozilla/sops/pull/602
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Publishing improvements: directory walking; prevent Vault unneeded version increment #602

Merged
merged 13 commits into from Jan 14, 2020

Conversation

@mmorev
Copy link
Contributor

@mmorev mmorev commented Jan 9, 2020

Hello.
This PR contains 3 changes in 3 corresponding commits:

  • implement -recurse cmd option for directory walking for publish command, so you can publish a bunch of files, preserving structure;
  • implement -omit-extensions cmd option to omit file extension in destination secret path. Can be specified in .sops.yaml;
  • implement pre-check for already existing Vault secret, to avoid version increment if data not changed.
@codecov-io
Copy link

@codecov-io codecov-io commented Jan 9, 2020

Codecov Report

Merging #602 into develop will increase coverage by 0.02%.
The diff coverage is 100%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop     #602      +/-   ##
===========================================
+ Coverage    37.11%   37.13%   +0.02%     
===========================================
  Files           21       21              
  Lines         2891     2892       +1     
===========================================
+ Hits          1073     1074       +1     
  Misses        1724     1724              
  Partials        94       94
Impacted Files Coverage Δ
config/config.go 71.64% <100%> (+0.21%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 53c6470...0c26330. Read the comment docs.

MOREV Mikhail MOREV Mikhail
@ajvb ajvb requested review from ajvb and autrilla Jan 9, 2020
Copy link
Collaborator

@autrilla autrilla left a comment

Looks good overall, just a few comments

README.rst Outdated Show resolved Hide resolved
README.rst Outdated Show resolved Hide resolved
README.rst Outdated Show resolved Hide resolved
@@ -968,6 +968,7 @@ This command requires a ``.sops.yaml`` configuration file. Below is an example:
vault_kv_mount_name: "secret/" # default
vault_kv_version: 2 # default
path_regex: vault/*
omit_extensions: true

This comment has been minimized.

@autrilla

autrilla Jan 9, 2020
Collaborator

Out of curiosity, what's your use case for this?

This comment has been minimized.

@mmorev

mmorev Jan 10, 2020
Author Contributor

Thanks for this question!
The goal is to use Vault as secrets source for several applications in different environments. As soon as it is a team work, we want to view diffs and approve/reject changes just like code changes in any Git platform (github, bitbucket) we do.
Vault has no option to stage changes like this (the only option in Enterprise edition is to approve the fact of write access, without seeing what data will be modified) and no diffs between versions or something. So we decided to store some info in Git/Sops and publish it to Vault in a batch triggered by repository change, and then lock down Vault to read-only mode by policies.
So in Vault we have some secrets schema, extensions are not needed there.

This comment has been minimized.

@autrilla

autrilla Jan 10, 2020
Collaborator

Thanks for explaining, it's always good to see how people use sops :)

Usage: "Omit file extensions in destination path when publishing sops file to configured destinations",
},
cli.BoolFlag{
Name: "recurse",

This comment has been minimized.

@autrilla

autrilla Jan 9, 2020
Collaborator

Please change all mentions of this to recursive

This comment has been minimized.

@mmorev

mmorev Jan 10, 2020
Author Contributor

fixed in 3ab2d41

@@ -46,10 +49,27 @@ func Run(opts Opts) error {
if err != nil {
return err
}
if info.IsDir() {
if info.IsDir() && !opts.Recurse {

This comment has been minimized.

@autrilla

autrilla Jan 9, 2020
Collaborator

I'd nest this,

if info.IsDir() {
  if !opts.Recursive {
    return fmt.Errorf("can't operate on a directory")
  } 
   err = filepath.Walk(opts.InputPat...
}

This comment has been minimized.

@mmorev

mmorev Jan 10, 2020
Author Contributor

Made some rework in 3ab2d41. See below.

return fmt.Errorf("can't operate on a directory")
} else if info.IsDir() && opts.Recurse {
err = filepath.Walk(opts.InputPath, func(subPath string, info os.FileInfo, err error) error {

This comment has been minimized.

@autrilla

autrilla Jan 9, 2020
Collaborator

You should handle the error passed into the function

This comment has been minimized.

@mmorev

mmorev Jan 10, 2020
Author Contributor

fixed in 3ab2d41

} else if info.IsDir() && opts.Recurse {
err = filepath.Walk(opts.InputPath, func(subPath string, info os.FileInfo, err error) error {
subAbsPath, _ := filepath.Abs(subPath)
if !info.IsDir() && subAbsPath != path {

This comment has been minimized.

@autrilla

autrilla Jan 9, 2020
Collaborator

Could subAbsPath != path ever be false? path is the original input path, and by the mere fact that we got to this code path, we've already established it's a directory, so !info.,IsDir() would be false anyway and it would short-circuit. Am I missing something?

This comment has been minimized.

@mmorev

mmorev Jan 10, 2020
Author Contributor

Thanks for this comment. I just found out I had abused filepath.Walk function by using additional recursion inside it. So i moved out Walk call, IsDir check and store type detection to main.go:

err = filepath.Walk(path, func(subPath string, info os.FileInfo, err error) error {

cmd/sops/subcommand/publish/publish.go Outdated Show resolved Hide resolved
MOREV Mikhail and others added 2 commits Jan 10, 2020
fix filepath.Walk abuse; rename recursive flag; minor fixes
Copy link
Collaborator

@autrilla autrilla left a comment

LGTM, but I have one comment because this might break some people's use case.

@@ -65,6 +66,17 @@ func (vaultd *VaultDestination) UploadUnencrypted(data map[string]interface{}, f
}
}

existingSecret, err := client.Logical().Read(vaultd.secretsPath(fileName))
if err != nil {
return err

This comment has been minimized.

@autrilla

autrilla Jan 10, 2020
Collaborator

I think it might be good to keep going regardless (after printing a warning) if there's an error reading the secret. I'm imagining a situation where someone has given SOPS permission to only write to Vault.

This comment has been minimized.

@mmorev

mmorev Jan 11, 2020
Author Contributor

Good idea. Fixed this behavior in 01b5fb6: log warn when no read access, log info when data not changed

MOREV Mikhail and others added 4 commits Jan 11, 2020
Dont fail Vault publish with write-only access; improve vault publish logging
Fix destination path on single file publish
@ajvb
ajvb approved these changes Jan 13, 2020
Copy link
Member

@ajvb ajvb left a comment

LGTM! Just left one small grammar tweak comment.

cmd/sops/main.go Outdated Show resolved Hide resolved
mmorev and others added 3 commits Jan 13, 2020
Co-Authored-By: AJ Bahnken <1144310+ajvb@users.noreply.github.com>
Recursive publish - use relative paths
@mmorev
Copy link
Contributor Author

@mmorev mmorev commented Jan 14, 2020

Added another one improvement in the last commit - make use of relative paths for recursive publish, so the destination path is: vault_kv_mount_name + vault_path + path under the directory passed to sops publish --recursive

@ajvb ajvb merged commit df39dca into mozilla:develop Jan 14, 2020
1 check passed
1 check passed
continuous-integration/travis-ci/pr The Travis CI build passed
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
4 participants
You can’t perform that action at this time.