mirror of
https://github.com/Luzifer/vault-openvpn.git
synced 2024-11-09 08:40:04 +00:00
Add version command
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
0c61a521c1
commit
b5935b315f
8 changed files with 57 additions and 27 deletions
|
@ -10,8 +10,9 @@ import (
|
|||
|
||||
// clientCmd represents the client command
|
||||
var clientCmd = &cobra.Command{
|
||||
Use: "client",
|
||||
Use: "client <fqdn>",
|
||||
Short: "Generate certificate and output client config",
|
||||
PreRunE: func(cmd *cobra.Command, args []string) error { return initVaultClient() },
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if len(args) != 1 || !validateFQDN(args[0]) {
|
||||
return errors.New("You need to provide a valid FQDN")
|
||||
|
|
|
@ -13,6 +13,7 @@ import (
|
|||
"text/template"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/vault/api"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
@ -182,6 +183,27 @@ func getCAChain() (string, error) {
|
|||
return cert.(string), nil
|
||||
}
|
||||
|
||||
func initVaultClient() error {
|
||||
// Ensure token is present
|
||||
if viper.GetString("vault-token") == "" {
|
||||
return fmt.Errorf("You need to set vault-token")
|
||||
}
|
||||
|
||||
clientConfig := api.DefaultConfig()
|
||||
clientConfig.ReadEnvironment()
|
||||
clientConfig.Address = viper.GetString("vault-addr")
|
||||
|
||||
var err error
|
||||
client, err = api.NewClient(clientConfig)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Could not create Vault client: %s", err)
|
||||
}
|
||||
|
||||
client.SetToken(viper.GetString("vault-token"))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func renderTemplate(tplName string, tplv *templateVars) error {
|
||||
raw, err := ioutil.ReadFile(path.Join(viper.GetString("template-path"), tplName))
|
||||
if err != nil {
|
||||
|
|
|
@ -14,6 +14,7 @@ import (
|
|||
var listCmd = &cobra.Command{
|
||||
Use: "list",
|
||||
Short: "List all valid (not expired, not revoked) certificates",
|
||||
PreRunE: func(cmd *cobra.Command, args []string) error { return initVaultClient() },
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return listCertificates()
|
||||
},
|
||||
|
|
|
@ -14,6 +14,7 @@ import (
|
|||
var revokeSerialCmd = &cobra.Command{
|
||||
Use: "revoke-serial <serial>",
|
||||
Short: "Revoke certificate by serial number",
|
||||
PreRunE: func(cmd *cobra.Command, args []string) error { return initVaultClient() },
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if len(args) != 1 || !validateSerial(args[0]) {
|
||||
return errors.New("You need to provide a valid serial")
|
||||
|
|
|
@ -11,6 +11,7 @@ import (
|
|||
var revokeCmd = &cobra.Command{
|
||||
Use: "revoke <fqdn>",
|
||||
Short: "Revoke all certificates matching to FQDN",
|
||||
PreRunE: func(cmd *cobra.Command, args []string) error { return initVaultClient() },
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if len(args) != 1 || !validateFQDN(args[0]) {
|
||||
return errors.New("You need to provide a valid FQDN")
|
||||
|
|
17
cmd/root.go
17
cmd/root.go
|
@ -52,23 +52,6 @@ var RootCmd = &cobra.Command{
|
|||
return fmt.Errorf("Unable to interprete log level: %s", err)
|
||||
}
|
||||
|
||||
// Ensure token is present
|
||||
if viper.GetString("vault-token") == "" {
|
||||
return fmt.Errorf("You need to set vault-token")
|
||||
}
|
||||
|
||||
clientConfig := api.DefaultConfig()
|
||||
clientConfig.ReadEnvironment()
|
||||
clientConfig.Address = viper.GetString("vault-addr")
|
||||
|
||||
var err error
|
||||
client, err = api.NewClient(clientConfig)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Could not create Vault client: %s", err)
|
||||
}
|
||||
|
||||
client.SetToken(viper.GetString("vault-token"))
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
|
|
@ -10,8 +10,9 @@ import (
|
|||
|
||||
// serverCmd represents the server command
|
||||
var serverCmd = &cobra.Command{
|
||||
Use: "server",
|
||||
Use: "server <fqdn>",
|
||||
Short: "Generate certificate and output server config",
|
||||
PreRunE: func(cmd *cobra.Command, args []string) error { return initVaultClient() },
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if len(args) != 1 || !validateFQDN(args[0]) {
|
||||
return errors.New("You need to provide a valid FQDN")
|
||||
|
|
20
cmd/version.go
Normal file
20
cmd/version.go
Normal file
|
@ -0,0 +1,20 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// versionCmd represents the version command
|
||||
var versionCmd = &cobra.Command{
|
||||
Use: "version",
|
||||
Short: "Displays the version of the utility",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
fmt.Printf("vault-openvpn %s\n", version)
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
RootCmd.AddCommand(versionCmd)
|
||||
}
|
Loading…
Reference in a new issue