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

Add updatekeys command #49

Merged
merged 1 commit into from
Sep 6, 2022
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
48 changes: 48 additions & 0 deletions cmd/updatekeys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright © 2017 Ibotta

package cmd

import (
"github.com/Ibotta/sopstool/fileutil"
"github.com/spf13/cobra"
)

// updatekeysCmd represents the updatekeysCmd command
var updatekeysCmd = &cobra.Command{
Use: "updatekeys [files ...]",
Short: "update recipients keys",
Long: "update recipients keys",
RunE: UpdateKeysCommand,
}

var nonInteractiveMode bool

func init() {
RootCmd.AddCommand(updatekeysCmd)
updatekeysCmd.Flags().BoolVar(&nonInteractiveMode, "non-interactive", false, "pre-approve all changes and run non-interactively")
}

// UpdateKeysCommand updates recipients keys
func UpdateKeysCommand(cmd *cobra.Command, args []string) error {
initConfig()
var extraArgs []string

filesToUpdate, err := fileutil.SomeOrAllFiles(args, sopsConfig.EncryptedFiles)
if err != nil {
return err
}

if nonInteractiveMode {
extraArgs = append(extraArgs, "--yes")
}

//Update recipients keys on files
for _, f := range filesToUpdate {
err := encrypter.UpdateKeysFile(f, extraArgs)
if err != nil {
return err
}
}

return nil
}
1 change: 1 addition & 0 deletions filecrypt/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ type FileCrypt interface {
RemoveCryptFile(fn string) error
RotateFile(fn string) error
EditFile(fn string) error
UpdateKeysFile(fn string, extraArgs []string) error
}
7 changes: 7 additions & 0 deletions filecrypt/sops.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,10 @@ func (sops sopsCrypt) EditFile(fn string) error {

return sops.execWrap.RunCommandDirect([]string{"sops", cryptfile})
}

// UpdateKeysFile update the keys of a SOPS file
func (sops sopsCrypt) UpdateKeysFile(fn string, extraArgs []string) error {
cryptfile := fileutil.NormalizeToSopsFile(fn)
cmd := append([]string{"sops", "updatekeys", cryptfile}, extraArgs...)
return sops.execWrap.RunCommandDirect(cmd)
}
19 changes: 19 additions & 0 deletions filecrypt/sops_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,25 @@ func TestRemoveCryptFile(t *testing.T) {
})
}

func TestUpdateKeysFile(t *testing.T) {
origEw := sops.execWrap
t.Run("run updatekeys", func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mock := mock_oswrap.NewMockExecWrap(ctrl)

mock.EXPECT().RunCommandDirect(gomock.Eq([]string{"sops", "updatekeys", "myfile.sops.yaml"})).Return(nil)
sops.execWrap = mock

err := sops.UpdateKeysFile("myfile.yaml", []string{})
if err != nil {
t.Errorf("TestUpdateKeysFile() unexpected error %v", err)
}

sops.execWrap = origEw
})
}

func TestRotateFile(t *testing.T) {
origEw := sops.execWrap
t.Run("run rotate", func(t *testing.T) {
Expand Down