Add Flag to Read Privatekey From File

This commit is contained in:
Samuel Lorch 2022-01-12 16:06:15 +01:00
parent f373792c06
commit 0449e9983c
2 changed files with 32 additions and 5 deletions

View file

@ -22,11 +22,21 @@ Note: this will install the binary as go-passbolt-cli, also tab completion and m
# Getting Started
First you need to Setup basic information: the Server Address, your Private Key and your Password.
You have these options:
- Save it in the config file using `passbolt configure --serverAddress https://passbolt.example.org --userPrivateKey 'private' --userPassword '1234'`
- Save it in the config file using
```
passbolt configure --serverAddress https://passbolt.example.org --userPassword '1234' --userPrivateKeyFile 'keys/privatekey.asc'
```
or
```
passbolt configure --serverAddress https://passbolt.example.org --userPassword '1234' --userPrivateKey '-----BEGIN PGP PRIVATE KEY BLOCK-----'
```
- Setup Enviroment Variables
- Provide the Flags manually every time
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
Notes:
- 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`.
- 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
# Usage

View file

@ -2,6 +2,7 @@ package cmd
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"time"
@ -16,9 +17,10 @@ var cfgFile string
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "passbolt",
Short: "A CLI tool to interact with Passbolt.",
Long: `A CLI tool to interact with Passbolt.`,
Use: "passbolt",
Short: "A CLI tool to interact with Passbolt.",
Long: `A CLI tool to interact with Passbolt.`,
SilenceUsage: true,
}
// Execute adds all child commands to the root command and sets flags appropriately.
@ -43,6 +45,7 @@ func init() {
rootCmd.PersistentFlags().Duration("timeout", time.Minute, "Timeout for the Context")
rootCmd.PersistentFlags().String("serverAddress", "", "Passbolt Server Address (https://passbolt.example.com)")
rootCmd.PersistentFlags().String("userPrivateKey", "", "Passbolt User Private Key")
rootCmd.PersistentFlags().String("userPrivateKeyFile", "", "Passbolt User Private Key File, if set then the userPrivateKey will be Overwritten with the File Content")
rootCmd.PersistentFlags().String("userPassword", "", "Passbolt User Password")
rootCmd.PersistentFlags().String("mfaMode", "interactive-totp", "How to Handle MFA, the following Modes exist: none, interactive-totp and noninteractive-totp")
rootCmd.PersistentFlags().String("totpToken", "", "Token to generate TOTP's, only used in nointeractive-totp mode")
@ -91,4 +94,18 @@ func initConfig() {
// update Config file Permissions
os.Chmod(viper.ConfigFileUsed(), 0600)
}
// Read in Private Key from File if userprivatekeyfile is set
userprivatekeyfile, err := rootCmd.PersistentFlags().GetString("userPrivateKeyFile")
if err != nil && userprivatekeyfile != "" {
if viper.GetBool("debug") {
fmt.Fprintln(os.Stderr, "Loading Private Key from File:", userprivatekeyfile)
}
content, err := ioutil.ReadFile(userprivatekeyfile)
if err != nil {
fmt.Fprintln(os.Stderr, "Error Loading Private Key from File: ", err)
os.Exit(1)
}
viper.Set("userprivatekey", string(content))
}
}