/
config.go
186 lines (162 loc) · 4.99 KB
/
config.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright © 2017 Sam Elliott <me@sam-e.co.uk>
//
// 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 cmd
import (
"fmt"
"log"
"encoding/base64"
"os"
"github.com/elliottsam/vault-config/crypto"
"github.com/elliottsam/vault-config/template"
"github.com/elliottsam/vault-config/vault"
"github.com/hashicorp/hcl"
"github.com/hashicorp/vault/api"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// configCmd configures Vault server with configuration provided
var configCmd = &cobra.Command{
Use: "config",
Short: "Executes configuration of Vault",
Long: `vault-config configure will read through all
configuration files with the .vc extension and
apply changes to Vault
Vault configuration is retrieved through the
same environment variables the main Vault
client uses
If the -encrypted flag is set, the tool will
also cycle through all .vc.enc files and decrypt
these a key will be requested if not passed
via the -key flag
e.g.
vault-config config -e -k mSskqBC85rA65lofPOaQcVtjjnHJ16rI+/rqZfkBwqs=
This will cycle through all .vc and .vc.enc files
decrypting those that require it
`,
Run: func(cmd *cobra.Command, args []string) {
var err error
cmdInit()
e := crypto.EncryptionObject{}
e.PlainText = e.ReadConfigFiles(filename)
if encrypted {
if key == "" {
e.Key, err = crypto.GetPassword()
if err != nil {
log.Fatal(err)
}
} else {
e.Key, err = base64.StdEncoding.DecodeString(key)
if err != nil {
log.Fatalf("Error base64 decoding key: %v", err)
}
}
e.PlainText = crypto.JoinBytes(e.ReadEncryptedConfigFiles(filename), e.PlainText)
}
g := template.InitGenerator(varFile, e.PlainText)
e.PlainText = g.GenerateConfig()
c := api.DefaultConfig()
c.Address = vcVaultAddr
if vcVaultSkipVerify == true {
c.ConfigureTLS(&api.TLSConfig{Insecure: true})
}
client, err := vault.NewClient(c)
if err != nil {
log.Fatalf("Error creating Vault client: %v", err)
}
client.SetToken(vcVaultToken)
var vconf vault.Config
err = hcl.Unmarshal(e.PlainText, &vconf)
if err != nil {
log.Fatal(fmt.Errorf("Error reading HCL: %v", err))
}
if vault.SecretsEncrypted(vconf) {
if e.Key != nil {
err = vconf.DecryptSecrets(e.Key)
} else if key != "" {
decodedKey, err = base64.StdEncoding.DecodeString(key)
if err != nil {
log.Fatalf("Error base64 decoding key: %v", err)
}
err = vconf.DecryptSecrets(decodedKey)
} else {
decodedKey, err = crypto.GetPassword()
if err != nil {
log.Fatalf("Error getting encryption key: %v", err)
}
err = vconf.DecryptSecrets(decodedKey)
}
if err != nil {
log.Fatalf("Error decrypting secrets: %v", err)
}
}
for _, m := range vconf.Mounts {
if ok := client.MountExist(m.Path); !ok {
err := client.Mount(m.Path, vault.ConvertMapStringInterface(m.Config))
if err != nil {
log.Fatalf("Error creating mount: %v", err)
}
}
if err := client.TuneMount(m.Path, vault.ConvertMapStringInterface(m.Config.MountConfig)); err != nil {
log.Fatal(err)
}
}
for _, p := range vconf.Policies {
if err := client.PolicyAdd(p); err != nil {
log.Fatal(err)
}
}
if vconf.Auth.Ldap != nil {
if err := vault.EnableAndConfigure(vconf.Auth.Ldap, client); err != nil {
log.Fatal(fmt.Errorf("Error creating Ldap auth:\n%s", err))
}
}
if vconf.Auth.Github != nil {
if err := vault.EnableAndConfigure(vconf.Auth.Github, client); err != nil {
log.Fatal(fmt.Errorf("Error creating Github auth:\n%s", err))
}
}
for _, v := range vconf.TokenRoles {
if err := client.WriteTokenRole(v); err != nil {
log.Fatalf("Error writing token role: %v", err)
}
}
for _, v := range vconf.Secrets {
if err := client.WriteSecret(v); err != nil {
log.Fatalf("Error: %v", err)
}
}
},
}
func cmdInit() {
if !viper.IsSet("vault_addr") {
RootCmd.Help()
os.Exit(1)
}
if !viper.IsSet("vault_token") {
RootCmd.Help()
os.Exit(1)
}
if viper.IsSet("vault_skip_verify") {
vcVaultSkipVerify = viper.GetBool("vault_skip_verify")
} else {
vcVaultSkipVerify = false
}
}
func init() {
RootCmd.AddCommand(configCmd)
configCmd.Flags().StringVarP(&filename, "filename", "f", "", "Filename of configuration file")
configCmd.Flags().StringVarP(&varFile, "varFile", "v", "vault-config.vars", "Filename of vars to be used in templates")
configCmd.Flags().BoolVarP(&encrypted, "encrypted", "e", false, "Is this file encrypted")
configCmd.Flags().StringVarP(&key, "key", "k", "", "Encryption key this must be 32 bytes")
}