1
0
Fork 0
mirror of https://github.com/Luzifer/vault-openvpn.git synced 2024-12-26 06:41:20 +00:00

Add version command

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2018-05-27 12:10:14 +02:00
parent 0c61a521c1
commit b5935b315f
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E
8 changed files with 57 additions and 27 deletions

View file

@ -10,8 +10,9 @@ import (
// clientCmd represents the client command // clientCmd represents the client command
var clientCmd = &cobra.Command{ var clientCmd = &cobra.Command{
Use: "client", Use: "client <fqdn>",
Short: "Generate certificate and output client config", 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 { RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 || !validateFQDN(args[0]) { if len(args) != 1 || !validateFQDN(args[0]) {
return errors.New("You need to provide a valid FQDN") return errors.New("You need to provide a valid FQDN")

View file

@ -13,6 +13,7 @@ import (
"text/template" "text/template"
"time" "time"
"github.com/hashicorp/vault/api"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@ -182,6 +183,27 @@ func getCAChain() (string, error) {
return cert.(string), nil 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 { func renderTemplate(tplName string, tplv *templateVars) error {
raw, err := ioutil.ReadFile(path.Join(viper.GetString("template-path"), tplName)) raw, err := ioutil.ReadFile(path.Join(viper.GetString("template-path"), tplName))
if err != nil { if err != nil {

View file

@ -12,8 +12,9 @@ import (
// listCmd represents the list command // listCmd represents the list command
var listCmd = &cobra.Command{ var listCmd = &cobra.Command{
Use: "list", Use: "list",
Short: "List all valid (not expired, not revoked) certificates", 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 { RunE: func(cmd *cobra.Command, args []string) error {
return listCertificates() return listCertificates()
}, },

View file

@ -12,8 +12,9 @@ import (
// revokeSerialCmd represents the revoke-serial command // revokeSerialCmd represents the revoke-serial command
var revokeSerialCmd = &cobra.Command{ var revokeSerialCmd = &cobra.Command{
Use: "revoke-serial <serial>", Use: "revoke-serial <serial>",
Short: "Revoke certificate by serial number", Short: "Revoke certificate by serial number",
PreRunE: func(cmd *cobra.Command, args []string) error { return initVaultClient() },
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 || !validateSerial(args[0]) { if len(args) != 1 || !validateSerial(args[0]) {
return errors.New("You need to provide a valid serial") return errors.New("You need to provide a valid serial")

View file

@ -9,8 +9,9 @@ import (
// revokeCmd represents the revoke command // revokeCmd represents the revoke command
var revokeCmd = &cobra.Command{ var revokeCmd = &cobra.Command{
Use: "revoke <fqdn>", Use: "revoke <fqdn>",
Short: "Revoke all certificates matching to 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 { RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 || !validateFQDN(args[0]) { if len(args) != 1 || !validateFQDN(args[0]) {
return errors.New("You need to provide a valid FQDN") return errors.New("You need to provide a valid FQDN")

View file

@ -52,23 +52,6 @@ var RootCmd = &cobra.Command{
return fmt.Errorf("Unable to interprete log level: %s", err) 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 return nil
}, },
} }

View file

@ -10,8 +10,9 @@ import (
// serverCmd represents the server command // serverCmd represents the server command
var serverCmd = &cobra.Command{ var serverCmd = &cobra.Command{
Use: "server", Use: "server <fqdn>",
Short: "Generate certificate and output server config", 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 { RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 || !validateFQDN(args[0]) { if len(args) != 1 || !validateFQDN(args[0]) {
return errors.New("You need to provide a valid FQDN") return errors.New("You need to provide a valid FQDN")

20
cmd/version.go Normal file
View 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)
}