Skip to content

Commit

Permalink
Add restic methods for managing restic keys (#211)
Browse files Browse the repository at this point in the history
Signed-off-by: hmsayem <[email protected]>
  • Loading branch information
hmsayem authored Oct 4, 2023
1 parent 3eb3dc0 commit 741d014
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 3 deletions.
7 changes: 4 additions & 3 deletions apis/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,15 @@ const (
PrefixStashVolumeSnapshot = "stash-vs"
PrefixStashTrigger = "stash-trigger"

OperatorContainer = "operator"
StashContainer = "stash"
StashInitContainer = "stash-init"
StashCronJobContainer = "stash-trigger"
LocalVolumeName = "stash-local"
ScratchDirVolumeName = "stash-scratchdir"
TmpDirVolumeName = "stash-tmp-dir"
TmpDirMountPath = "/stash-tmp"
SnapshotDownloadDir = "/stash-tmp/snapshots"
ScratchDirMountPath = "/tmp"
PodinfoVolumeName = "stash-podinfo"

RecoveryJobPrefix = "stash-recovery-"
Expand Down Expand Up @@ -251,6 +252,6 @@ const (
)

const (
ESMetaFile = "meta.txt"
TempDir = "/tmp"
ESMetaFile = "meta.txt"
SnapshotDownloadDir = "snapshots"
)
70 changes: 70 additions & 0 deletions pkg/restic/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ type restoreParams struct {
args []string
}

type keyParams struct {
id string
user string
host string
file string
}

func (w *ResticWrapper) listSnapshots(snapshotIDs []string) ([]Snapshot, error) {
result := make([]Snapshot, 0)
args := w.appendCacheDirFlag([]interface{}{"snapshots", "--json", "--quiet", "--no-lock"})
Expand Down Expand Up @@ -488,3 +495,66 @@ func (w *ResticWrapper) applyNiceSettings(oldCommand Command) (Command, error) {
newCommand.Args = append(newCommand.Args, oldCommand.Args...)
return newCommand, nil
}

func (w *ResticWrapper) addKey(params keyParams) ([]byte, error) {
klog.Infoln("Adding new key to restic repository")

args := []interface{}{"key", "add", "--no-lock"}
if params.host != "" {
args = append(args, "--host", params.host)
}

if params.user != "" {
args = append(args, "--user", params.user)
}

if params.file != "" {
args = append(args, "--new-password-file", params.file)
}

args = w.appendCacheDirFlag(args)
args = w.appendMaxConnectionsFlag(args)
args = w.appendCaCertFlag(args)

return w.run(Command{Name: ResticCMD, Args: args})
}

func (w *ResticWrapper) listKey() ([]byte, error) {
klog.Infoln("Listing restic keys")

args := []interface{}{"key", "list", "--no-lock"}

args = w.appendCacheDirFlag(args)
args = w.appendMaxConnectionsFlag(args)
args = w.appendCaCertFlag(args)

return w.run(Command{Name: ResticCMD, Args: args})
}

func (w *ResticWrapper) updateKey(params keyParams) ([]byte, error) {
klog.Infoln("Updating restic key")

args := []interface{}{"key", "passwd", "--no-lock"}

if params.file != "" {
args = append(args, "--new-password-file", params.file)
}

args = w.appendCacheDirFlag(args)
args = w.appendMaxConnectionsFlag(args)
args = w.appendCaCertFlag(args)

return w.run(Command{Name: ResticCMD, Args: args})
}

func (w *ResticWrapper) removeKey(params keyParams) ([]byte, error) {
klog.Infoln("Removing restic key")

args := []interface{}{"key", "remove", params.id, "--no-lock"}

args = w.appendCacheDirFlag(args)
args = w.appendMaxConnectionsFlag(args)
args = w.appendCaCertFlag(args)

return w.run(Command{Name: ResticCMD, Args: args})
}
7 changes: 7 additions & 0 deletions pkg/restic/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ type SetupOptions struct {
IONice *ofst.IONiceSettings
}

type KeyOptions struct {
ID string
User string
Host string
File string
}

func NewResticWrapper(options SetupOptions) (*ResticWrapper, error) {
wrapper := &ResticWrapper{
sh: shell.NewSession(),
Expand Down
56 changes: 56 additions & 0 deletions pkg/restic/key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
Copyright AppsCode Inc. and Contributors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package restic

import "os"

func (w *ResticWrapper) AddKey(opt KeyOptions) error {
params := keyParams{
user: opt.User,
host: opt.Host,
file: opt.File,
}
_, err := w.addKey(params)
return err
}

func (w *ResticWrapper) ListKey() error {
out, err := w.listKey()
if err != nil {
return err
}
// Write the output to stdout
_, err = os.Stdout.Write(out)

return err
}

func (w *ResticWrapper) UpdateKey(opt KeyOptions) error {
params := keyParams{
file: opt.File,
}
_, err := w.updateKey(params)
return err
}

func (w *ResticWrapper) RemoveKey(opt KeyOptions) error {
params := keyParams{
id: opt.ID,
}
_, err := w.removeKey(params)
return err
}

0 comments on commit 741d014

Please sign in to comment.