2015-09-18 05:14:15 -04:00
/ *
Copyright 2015 Home Office All rights reserved .
Licensed under the Apache License , Version 2.0 ( the "License" ) ;
you may not use this file except in compliance with the License .
You may obtain a copy of the License at
http : //www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing , software
distributed under the License is distributed on an "AS IS" BASIS ,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
See the License for the specific language governing permissions and
limitations under the License .
* /
package main
import (
"flag"
"fmt"
"net/url"
2017-07-25 03:08:15 -04:00
"os"
2015-09-18 05:14:15 -04:00
"time"
)
2017-07-25 03:08:15 -04:00
type vaultAuthOptions struct {
ClientToken string
Token string
LeaseDuration int
Renewable bool
Method string
VaultURL string ` json:"vaultAddr" `
RoleID string ` json:"role_id" yaml:"role_id" `
SecretID string ` json:"secret_id" yaml:"secret_id" `
FileName string
FileFormat string
Username string
Password string
}
2015-09-18 05:14:15 -04:00
type config struct {
// the url for th vault server
vaultURL string
2015-09-21 06:31:12 -04:00
// a file containing the authenticate options
vaultAuthFile string
2017-05-07 23:05:03 -04:00
// whether or not the auth file format is default
vaultAuthFileFormat string
2015-09-21 06:31:12 -04:00
// the authentication options
2017-07-25 03:08:15 -04:00
vaultAuthOptions * vaultAuthOptions
2015-10-09 12:42:24 -04:00
// the vault ca file
vaultCaFile string
2015-09-18 05:14:15 -04:00
// the place to write the resources
2015-09-21 06:31:12 -04:00
outputDir string
2015-09-18 05:14:15 -04:00
// switch on dry run
dryRun bool
2015-09-23 09:21:54 -04:00
// skip tls verify
2017-05-24 10:52:54 -04:00
skipTLSVerify bool
2015-09-18 05:14:15 -04:00
// the resource items to retrieve
2015-09-23 17:39:50 -04:00
resources * VaultResources
2015-09-18 05:14:15 -04:00
// the interval for producing statistics
statsInterval time . Duration
2016-03-17 10:04:21 -04:00
// the timeout for a exec command
execTimeout time . Duration
2017-05-24 07:41:16 -04:00
// version flag
showVersion bool
2017-06-21 13:33:49 -04:00
// one-shot mode
oneShot bool
2015-09-18 05:14:15 -04:00
}
var (
options config
)
func init ( ) {
2015-09-23 17:39:50 -04:00
// step: setup some defaults
options . resources = new ( VaultResources )
2017-07-25 03:08:15 -04:00
options . vaultAuthOptions = & vaultAuthOptions {
Method : "token" ,
}
2015-09-23 17:39:50 -04:00
2015-10-09 12:42:24 -04:00
flag . StringVar ( & options . vaultURL , "vault" , getEnv ( "VAULT_ADDR" , "https://127.0.0.1:8200" ) , "url the vault service or VAULT_ADDR" )
2017-05-07 23:05:03 -04:00
flag . StringVar ( & options . vaultAuthFile , "auth" , getEnv ( "AUTH_FILE" , "" ) , "a configuration file in json or yaml containing authentication arguments" )
flag . StringVar ( & options . vaultAuthFileFormat , "format" , getEnv ( "AUTH_FORMAT" , "default" ) , "the auth file format" )
2017-11-13 14:12:05 -05:00
flag . StringVar ( & options . vaultAuthOptions . Method , "method" , getEnv ( "AUTH_METHOD" , "token" ) , "the authentication method to use (use of an auth file will override this setting)" )
2015-10-09 12:42:24 -04:00
flag . StringVar ( & options . outputDir , "output" , getEnv ( "VAULT_OUTPUT" , "/etc/secrets" ) , "the full path to write resources or VAULT_OUTPUT" )
2015-09-21 06:31:12 -04:00
flag . BoolVar ( & options . dryRun , "dryrun" , false , "perform a dry run, printing the content to screen" )
2017-05-24 10:52:54 -04:00
flag . BoolVar ( & options . skipTLSVerify , "tls-skip-verify" , false , "whether to check and verify the vault service certificate" )
2015-10-09 12:42:24 -04:00
flag . StringVar ( & options . vaultCaFile , "ca-cert" , "" , "the path to the file container the CA used to verify the vault service" )
2016-02-25 09:28:14 -05:00
flag . DurationVar ( & options . statsInterval , "stats" , time . Duration ( 1 ) * time . Hour , "the interval to produce statistics on the accessed resources" )
2016-04-27 19:29:03 -04:00
flag . DurationVar ( & options . execTimeout , "exec-timeout" , time . Duration ( 60 ) * time . Second , "the timeout applied to commands on the exec option" )
2017-05-24 07:41:16 -04:00
flag . BoolVar ( & options . showVersion , "version" , false , "show the vault-sidekick version" )
2015-10-09 12:42:24 -04:00
flag . Var ( options . resources , "cn" , "a resource to retrieve and monitor from vault" )
2017-06-21 13:33:49 -04:00
flag . BoolVar ( & options . oneShot , "one-shot" , false , "retrieve resources from vault once and then exit" )
2015-09-18 05:14:15 -04:00
}
2015-10-12 06:14:50 -04:00
// parseOptions validate the command line options and validates them
2015-09-18 05:14:15 -04:00
func parseOptions ( ) error {
flag . Parse ( )
return validateOptions ( & options )
}
2015-10-12 06:14:50 -04:00
// validateOptions parses and validates the command line options
2015-10-15 12:28:12 -04:00
func validateOptions ( cfg * config ) ( err error ) {
2015-09-18 05:14:15 -04:00
// step: read in the token if required
2017-07-25 03:08:15 -04:00
2015-09-21 06:31:12 -04:00
if cfg . vaultAuthFile != "" {
if exists , _ := fileExists ( cfg . vaultAuthFile ) ; ! exists {
return fmt . Errorf ( "the token file: %s does not exists, please check" , cfg . vaultAuthFile )
2015-09-18 05:14:15 -04:00
}
2017-07-25 03:08:15 -04:00
cfg . vaultAuthOptions , err = readConfigFile ( cfg . vaultAuthFile , cfg . vaultAuthFileFormat )
2015-10-15 12:28:12 -04:00
if err != nil {
2015-09-21 06:31:12 -04:00
return fmt . Errorf ( "unable to read in authentication options from: %s, error: %s" , cfg . vaultAuthFile , err )
2015-09-18 05:14:15 -04:00
}
2017-07-25 03:08:15 -04:00
if cfg . vaultAuthOptions . VaultURL != "" {
cfg . vaultURL = cfg . vaultAuthOptions . VaultURL
}
}
if cfg . vaultURL == "" {
cfg . vaultURL = os . Getenv ( "VAULT_ADDR" )
}
if cfg . vaultURL == "" {
return fmt . Errorf ( "VAULT_ADDR is unset" )
}
// step: validate the vault url
if _ , err = url . Parse ( cfg . vaultURL ) ; err != nil {
return fmt . Errorf ( "invalid vault url: '%s' specified" , cfg . vaultURL )
2015-09-18 05:14:15 -04:00
}
2015-10-09 12:42:24 -04:00
if cfg . vaultCaFile != "" {
if exists , _ := fileExists ( cfg . vaultCaFile ) ; ! exists {
return fmt . Errorf ( "the ca certificate file: %s does not exist" , cfg . vaultCaFile )
}
}
2017-05-24 10:52:54 -04:00
if cfg . skipTLSVerify == true && cfg . vaultCaFile != "" {
2015-10-09 12:42:24 -04:00
return fmt . Errorf ( "you are skipping the tls but supplying a CA, doesn't make sense" )
}
2015-09-18 05:14:15 -04:00
return nil
}