Skip to content

Commit

Permalink
Use HOME envvar for identity file location
Browse files Browse the repository at this point in the history
Allows simplier use with external tools like Argo
  • Loading branch information
george-angel committed Aug 5, 2024
1 parent c1499f8 commit 97dded2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
6 changes: 4 additions & 2 deletions age.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const (
defaultIdentityFilename = ".strongbox_identity"
)

var identityFilename string

func ageGenIdentity(desc string) {
identity, err := age.GenerateX25519Identity()
if err != nil {
Expand All @@ -27,7 +29,7 @@ func ageGenIdentity(desc string) {

fmt.Printf("public key: %s\n", identity.Recipient().String())

f, err := os.OpenFile(*flagIdentityFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
f, err := os.OpenFile(identityFilename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -82,7 +84,7 @@ func ageEncrypt(w io.Writer, r []age.Recipient, in []byte, f string) {
}

func ageDecrypt(w io.Writer, in []byte) {
identityFile, err := os.Open(*flagIdentityFile)
identityFile, err := os.Open(identityFilename)
if err != nil {
// identity file doesn't exist, copy as is and return
if _, err = io.Copy(w, bytes.NewReader(in)); err != nil {
Expand Down
19 changes: 12 additions & 7 deletions strongbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var (
flagGenIdentity = flag.String("gen-identity", "", "Generate a new identity and add it to your strongbox identity file")
flagGenKey = flag.String("gen-key", "", "Generate a new key and add it to your strongbox keyring")
flagGitConfig = flag.Bool("git-config", false, "Configure git for strongbox use")
flagIdentityFile = flag.String("identity-file", filepath.Join(os.Getenv("HOME"), defaultIdentityFilename), "strongbox identity file, if not set default '$HOME/.strongbox_identity' will be used")
flagIdentityFile = flag.String("identity-file", "", "strongbox identity file, if not set default '$HOME/.strongbox_identity' will be used")
flagKey = flag.String("key", "", "Private key to use to decrypt")
flagKeyRing = flag.String("keyring", "", "strongbox keyring file path, if not set default '$HOME/.strongbox_keyring' will be used")
flagRecursive = flag.Bool("recursive", false, "Recursively decrypt all files under given folder, must be used with -decrypt flag")
Expand Down Expand Up @@ -78,6 +78,12 @@ func main() {
home := deriveHome()
kr = &fileKeyRing{fileName: filepath.Join(home, ".strongbox_keyring")}

if *flagIdentityFile != "" {
identityFilename = *flagIdentityFile
} else {
identityFilename = filepath.Join(home, defaultIdentityFilename)
}

// if keyring flag is set replace default keyRing
if *flagKeyRing != "" {
kr = &fileKeyRing{fileName: *flagKeyRing}
Expand Down Expand Up @@ -149,17 +155,16 @@ func deriveHome() string {
if home := os.Getenv("STRONGBOX_HOME"); home != "" {
return home
}
// try HOME env var
if home := os.Getenv("HOME"); home != "" {
return home
}
// Try user.Current which works in most cases, but may not work with CGO disabled.
u, err := user.Current()
if err == nil && u.HomeDir != "" {
return u.HomeDir
}
// try HOME env var
if home := os.Getenv("HOME"); home != "" {
return home
}

log.Fatal("Could not call os/user.Current() or find $STRONGBOX_HOME or $HOME. Please recompile with CGO enabled or set $STRONGBOX_HOME or $HOME")
log.Fatal("Could not find $STRONGBOX_HOME, $HOME or call os/user.Current(). Please set $STRONGBOX_HOME, $HOME or recompile with CGO enabled")
// not reached
return ""
}
Expand Down

0 comments on commit 97dded2

Please sign in to comment.