Skip to content

Commit 0449e99

Browse files
committed
Add Flag to Read Privatekey From File
1 parent f373792 commit 0449e99

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,21 @@ Note: this will install the binary as go-passbolt-cli, also tab completion and m
2222
# Getting Started
2323
First you need to Setup basic information: the Server Address, your Private Key and your Password.
2424
You have these options:
25-
- Save it in the config file using `passbolt configure --serverAddress https://passbolt.example.org --userPrivateKey 'private' --userPassword '1234'`
25+
- Save it in the config file using
26+
```
27+
passbolt configure --serverAddress https://passbolt.example.org --userPassword '1234' --userPrivateKeyFile 'keys/privatekey.asc'
28+
```
29+
or
30+
```
31+
passbolt configure --serverAddress https://passbolt.example.org --userPassword '1234' --userPrivateKey '-----BEGIN PGP PRIVATE KEY BLOCK-----'
32+
```
2633
- Setup Enviroment Variables
2734
- Provide the Flags manually every time
2835

29-
Note: userPrivateKey is the actual Private Key and not a path to a file. You can also just store the serverAddress and your Private Key, if your Password is not set it will prompt you for it every time. MFA settings can also be save permenantly this ways
36+
Notes:
37+
- You can set the Private Key using the flags `--userPrivateKey` or `--userPrivateKeyFile` where `--userPrivateKey` takes the actual private key and `--userPrivateKeyFile` loads the content of a file as the PrivateKey, `--userPrivateKeyFile` overwrites the value of `--userPrivateKey`.
38+
- You can also just store the serverAddress and your Private Key, if your Password is not set it will prompt you for it every time.
39+
- MFA settings can also be save permenantly this ways
3040

3141
# Usage
3242

cmd/root.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
"fmt"
5+
"io/ioutil"
56
"os"
67
"path/filepath"
78
"time"
@@ -16,9 +17,10 @@ var cfgFile string
1617

1718
// rootCmd represents the base command when called without any subcommands
1819
var rootCmd = &cobra.Command{
19-
Use: "passbolt",
20-
Short: "A CLI tool to interact with Passbolt.",
21-
Long: `A CLI tool to interact with Passbolt.`,
20+
Use: "passbolt",
21+
Short: "A CLI tool to interact with Passbolt.",
22+
Long: `A CLI tool to interact with Passbolt.`,
23+
SilenceUsage: true,
2224
}
2325

2426
// Execute adds all child commands to the root command and sets flags appropriately.
@@ -43,6 +45,7 @@ func init() {
4345
rootCmd.PersistentFlags().Duration("timeout", time.Minute, "Timeout for the Context")
4446
rootCmd.PersistentFlags().String("serverAddress", "", "Passbolt Server Address (https://passbolt.example.com)")
4547
rootCmd.PersistentFlags().String("userPrivateKey", "", "Passbolt User Private Key")
48+
rootCmd.PersistentFlags().String("userPrivateKeyFile", "", "Passbolt User Private Key File, if set then the userPrivateKey will be Overwritten with the File Content")
4649
rootCmd.PersistentFlags().String("userPassword", "", "Passbolt User Password")
4750
rootCmd.PersistentFlags().String("mfaMode", "interactive-totp", "How to Handle MFA, the following Modes exist: none, interactive-totp and noninteractive-totp")
4851
rootCmd.PersistentFlags().String("totpToken", "", "Token to generate TOTP's, only used in nointeractive-totp mode")
@@ -91,4 +94,18 @@ func initConfig() {
9194
// update Config file Permissions
9295
os.Chmod(viper.ConfigFileUsed(), 0600)
9396
}
97+
98+
// Read in Private Key from File if userprivatekeyfile is set
99+
userprivatekeyfile, err := rootCmd.PersistentFlags().GetString("userPrivateKeyFile")
100+
if err != nil && userprivatekeyfile != "" {
101+
if viper.GetBool("debug") {
102+
fmt.Fprintln(os.Stderr, "Loading Private Key from File:", userprivatekeyfile)
103+
}
104+
content, err := ioutil.ReadFile(userprivatekeyfile)
105+
if err != nil {
106+
fmt.Fprintln(os.Stderr, "Error Loading Private Key from File: ", err)
107+
os.Exit(1)
108+
}
109+
viper.Set("userprivatekey", string(content))
110+
}
94111
}

0 commit comments

Comments
 (0)