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

updatekeys subcommand: show changes in shamir_threshold #1609

Merged
merged 1 commit into from
Sep 25, 2024
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
14 changes: 14 additions & 0 deletions cmd/sops/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,3 +447,17 @@ func PrettyPrintDiffs(diffs []Diff) {
}
}
}

// PrettyPrintShamirDiff prints changes in shamir_threshold to stdout
func PrettyPrintShamirDiff(oldValue, newValue int) {
if oldValue > 0 && oldValue == newValue {
fmt.Printf("shamir_threshold: %d\n", newValue)
} else {
if newValue > 0 {
color.New(color.FgGreen).Printf("+++ shamir_threshold: %d\n", newValue)
}
if oldValue > 0 {
color.New(color.FgRed).Printf("--- shamir_threshold: %d\n", oldValue)
}
}
}
18 changes: 13 additions & 5 deletions cmd/sops/subcommand/updatekeys/updatekeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,22 @@ func updateFile(opts Opts) error {
keysWillChange = true
}
}
if !keysWillChange {

// TODO: use conf.ShamirThreshold instead of tree.Metadata.ShamirThreshold in the next line?
// Or make this configurable?
var shamirThreshold = tree.Metadata.ShamirThreshold
Fixed Show fixed Hide fixed
if opts.GroupQuorum != 0 {
felixfontein marked this conversation as resolved.
Show resolved Hide resolved
felixfontein marked this conversation as resolved.
Show resolved Hide resolved
shamirThreshold = opts.GroupQuorum
Fixed Show fixed Hide fixed
}
shamirThreshold = min(shamirThreshold, len(conf.KeyGroups))
var shamirThresholdWillChange = tree.Metadata.ShamirThreshold != shamirThreshold

if !keysWillChange && !shamirThresholdWillChange {
log.Printf("File %s already up to date", opts.InputPath)
return nil
}
fmt.Printf("The following changes will be made to the file's groups:\n")
common.PrettyPrintShamirDiff(tree.Metadata.ShamirThreshold, shamirThreshold)
common.PrettyPrintDiffs(diffs)

if opts.Interactive {
Expand All @@ -92,10 +103,7 @@ func updateFile(opts Opts) error {
return common.NewExitError(err, codes.CouldNotRetrieveKey)
}
tree.Metadata.KeyGroups = conf.KeyGroups
if opts.GroupQuorum != 0 {
tree.Metadata.ShamirThreshold = opts.GroupQuorum
}
tree.Metadata.ShamirThreshold = min(tree.Metadata.ShamirThreshold, len(tree.Metadata.KeyGroups))
tree.Metadata.ShamirThreshold = shamirThreshold
errs := tree.Metadata.UpdateMasterKeysWithKeyServices(key, opts.KeyServices)
if len(errs) > 0 {
return fmt.Errorf("error updating one or more master keys: %s", errs)
Expand Down
Loading