-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
118 lines (101 loc) · 3.94 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright (c) 2016 Pulcy.
//
// 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 (
"fmt"
"os"
"strings"
"github.com/juju/errgo"
"github.com/op/go-logging"
"github.com/spf13/cobra"
"github.com/pulcy/vault-monkey/service"
)
var (
projectVersion = "dev"
projectBuild = "dev"
maskAny = errgo.MaskFunc(errgo.Any)
)
const (
projectName = "vault-monkey"
defaultLogLevel = "debug"
)
type globalOptions struct {
logLevel string
service.VaultServiceConfig
ghToken string
}
var (
cmdMain = &cobra.Command{
Use: projectName,
Run: showUsage,
PersistentPreRun: func(*cobra.Command, []string) { setLogLevel(globalFlags.logLevel) },
}
globalFlags globalOptions
log = logging.MustGetLogger(cmdMain.Use)
)
func init() {
logging.SetFormatter(logging.MustStringFormatter("[%{level:-5s}] %{message}"))
globalFlags.VaultAddr = os.Getenv("VAULT_ADDR")
globalFlags.VaultCACert = os.Getenv("VAULT_CACERT")
globalFlags.VaultCAPath = os.Getenv("VAULT_CAPATH")
globalFlags.IPv4Only = boolFromEnv("VAULT_IPV4_ONLY", false)
globalFlags.IPv6Only = boolFromEnv("VAULT_IPV6_ONLY", false)
cmdMain.PersistentFlags().StringVar(&globalFlags.logLevel, "log-level", defaultLogLevel, "Log level (debug|info|warning|error)")
cmdMain.PersistentFlags().StringVar(&globalFlags.VaultAddr, "vault-addr", globalFlags.VaultAddr, "URL of the vault (defaults to VAULT_ADDR environment variable)")
cmdMain.PersistentFlags().StringVar(&globalFlags.VaultCACert, "vault-cacert", globalFlags.VaultCACert, "Path to a PEM-encoded CA cert file to use to verify the Vault server SSL certificate")
cmdMain.PersistentFlags().StringVar(&globalFlags.VaultCAPath, "vault-capath", globalFlags.VaultCAPath, "Path to a directory of PEM-encoded CA cert files to verify the Vault server SSL certificate")
cmdMain.PersistentFlags().StringVar(&globalFlags.TokenPath, "token-path", "", "Path of a file containing your vault token (token defaults to VAULT_TOKEN environment variable)")
cmdMain.PersistentFlags().StringVarP(&globalFlags.ghToken, "github-token", "G", "", "Personal github token for administrator logins")
cmdMain.PersistentFlags().BoolVar(&globalFlags.IPv4Only, "vault-ipv4-only", globalFlags.IPv4Only, "If set, only use IPv4 addresses")
cmdMain.PersistentFlags().BoolVar(&globalFlags.IPv6Only, "vault-ipv6-only", globalFlags.IPv6Only, "If set, only use IPv6 addresses")
cmdMain.PersistentFlags().BoolVar(&globalFlags.DisableAppID, "vault-disable-app-id", globalFlags.DisableAppID, "If set, do not use app-id authentication")
cmdMain.PersistentFlags().BoolVar(&globalFlags.DisableAppRole, "vault-disable-approle", globalFlags.DisableAppRole, "If set, do not use approle authentication")
}
func main() {
cmdMain.Execute()
}
func showUsage(cmd *cobra.Command, args []string) {
cmd.Usage()
}
func Exitf(format string, args ...interface{}) {
if !strings.HasSuffix(format, "\n") {
format = format + "\n"
}
fmt.Printf(format, args...)
fmt.Println()
os.Exit(1)
}
func assert(err error) {
if err != nil {
Exitf("Assertion failed: %#v", err)
}
}
func assertArgIsSet(arg, argKey string) {
if arg == "" {
Exitf("%s must be set\n", argKey)
}
}
func setLogLevel(logLevel string) {
level, err := logging.LogLevel(logLevel)
if err != nil {
Exitf("Invalid log-level '%s': %#v", logLevel, err)
}
logging.SetLevel(level, projectName)
}
func (opt globalOptions) GithubToken() string {
if opt.ghToken != "" {
return opt.ghToken
}
return defaultGithubToken()
}