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 (
|
2015-09-21 11:35:59 -04:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2015-09-18 05:14:15 -04:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
2015-09-21 11:35:59 -04:00
|
|
|
"io/ioutil"
|
2015-09-18 12:58:52 -04:00
|
|
|
"math/rand"
|
|
|
|
"os"
|
2015-09-21 11:35:59 -04:00
|
|
|
"path"
|
|
|
|
"strings"
|
2015-09-18 12:58:52 -04:00
|
|
|
"time"
|
2015-09-21 11:35:59 -04:00
|
|
|
|
|
|
|
"github.com/golang/glog"
|
2015-09-21 06:31:12 -04:00
|
|
|
"gopkg.in/yaml.v2"
|
2016-02-25 09:28:14 -05:00
|
|
|
"path/filepath"
|
2015-09-18 05:14:15 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rand.Seed(int64(time.Now().Nanosecond()))
|
|
|
|
}
|
|
|
|
|
2015-10-12 06:14:50 -04:00
|
|
|
// showUsage prints the command usage and exits
|
2015-09-18 05:14:15 -04:00
|
|
|
// message : an error message to display if exiting with an error
|
2015-09-18 12:58:52 -04:00
|
|
|
func showUsage(message string, args ...interface{}) {
|
2015-09-18 05:14:15 -04:00
|
|
|
flag.PrintDefaults()
|
|
|
|
if message != "" {
|
2015-09-18 12:58:52 -04:00
|
|
|
fmt.Printf("\n[error] "+message+"\n", args...)
|
2015-09-18 05:14:15 -04:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2015-10-12 06:14:50 -04:00
|
|
|
// hasKey checks to see if a key is present
|
2015-09-21 06:31:12 -04:00
|
|
|
// key : the key we are looking for
|
|
|
|
// data : a map of strings to something we are looking at
|
2015-09-21 11:35:59 -04:00
|
|
|
func hasKey(key string, data map[string]interface{}) bool {
|
2015-09-21 06:31:12 -04:00
|
|
|
_, found := data[key]
|
|
|
|
return found
|
|
|
|
}
|
|
|
|
|
2015-10-12 06:14:50 -04:00
|
|
|
// getKeys retrieves a list of keys from the map
|
2015-09-21 06:31:12 -04:00
|
|
|
// data : the map which you wish to extract the keys from
|
2015-09-18 05:14:15 -04:00
|
|
|
func getKeys(data map[string]interface{}) []string {
|
|
|
|
var list []string
|
2015-09-18 12:58:52 -04:00
|
|
|
for key := range data {
|
2015-09-18 05:14:15 -04:00
|
|
|
list = append(list, key)
|
|
|
|
}
|
|
|
|
return list
|
|
|
|
}
|
|
|
|
|
2015-10-12 06:14:50 -04:00
|
|
|
// readConfigFile read in a configuration file
|
2015-09-21 06:31:12 -04:00
|
|
|
// filename : the path to the file
|
|
|
|
func readConfigFile(filename string) (map[string]string, error) {
|
2015-09-23 17:39:50 -04:00
|
|
|
// step: check the file exists
|
|
|
|
if exists, err := fileExists(filename); !exists {
|
|
|
|
return nil, fmt.Errorf("the file: %s does not exist", filename)
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// step: we only read in json or yaml formats
|
2015-09-21 06:31:12 -04:00
|
|
|
suffix := path.Ext(filename)
|
|
|
|
switch suffix {
|
|
|
|
case ".yaml":
|
2016-02-25 09:28:14 -05:00
|
|
|
fallthrough
|
2015-09-21 06:31:12 -04:00
|
|
|
case ".yml":
|
2015-09-23 17:39:50 -04:00
|
|
|
return readYAMLFile(filename)
|
2015-10-14 09:17:17 -04:00
|
|
|
default:
|
|
|
|
return readJSONFile(filename)
|
2015-09-21 06:31:12 -04:00
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("unsupported config file format: %s", suffix)
|
|
|
|
}
|
|
|
|
|
2015-10-12 06:14:50 -04:00
|
|
|
// readJsonFile read in and unmarshall the data into a map
|
2015-09-21 06:31:12 -04:00
|
|
|
// filename : the path to the file container the json data
|
2015-09-21 11:35:59 -04:00
|
|
|
func readJSONFile(filename string) (map[string]string, error) {
|
2015-09-21 06:31:12 -04:00
|
|
|
data := make(map[string]string, 0)
|
|
|
|
|
|
|
|
content, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
return data, err
|
|
|
|
}
|
|
|
|
// unmarshall the data
|
2015-10-14 11:04:22 -04:00
|
|
|
err = json.Unmarshal(content, &data)
|
2015-09-21 06:31:12 -04:00
|
|
|
if err != nil {
|
|
|
|
return data, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
2015-10-12 06:14:50 -04:00
|
|
|
// readYAMLFile read in and unmarshall the data into a map
|
2015-09-21 06:31:12 -04:00
|
|
|
// filename : the path to the file container the yaml data
|
2015-09-23 17:39:50 -04:00
|
|
|
func readYAMLFile(filename string) (map[string]string, error) {
|
2015-09-21 06:31:12 -04:00
|
|
|
data := make(map[string]string, 0)
|
|
|
|
content, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
return data, err
|
|
|
|
}
|
|
|
|
err = yaml.Unmarshal(content, data)
|
|
|
|
if err != nil {
|
|
|
|
return data, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
2015-10-14 09:17:17 -04:00
|
|
|
// getDurationWithin generate a random integer between min and max
|
2015-09-18 05:14:15 -04:00
|
|
|
// min : the smallest number we can accept
|
|
|
|
// max : the largest number we can accept
|
2015-10-14 09:17:17 -04:00
|
|
|
func getDurationWithin(min, max int) time.Duration {
|
2016-03-12 13:49:57 -05:00
|
|
|
duration := rand.Intn(max-min) + min
|
|
|
|
return time.Duration(duration) * time.Second
|
2015-09-18 05:14:15 -04:00
|
|
|
}
|
|
|
|
|
2015-10-12 06:14:50 -04:00
|
|
|
// getEnv checks to see if an environment variable exists otherwise uses the default
|
2015-09-18 05:14:15 -04:00
|
|
|
// env : the name of the environment variable you are checking for
|
|
|
|
// value : the default value to return if the value is not there
|
|
|
|
func getEnv(env, value string) string {
|
|
|
|
if v := os.Getenv(env); v != "" {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
2015-10-12 06:14:50 -04:00
|
|
|
// fileExists checks to see if a file exists
|
2015-09-18 05:14:15 -04:00
|
|
|
// filename : the full path to the file you are checking for
|
|
|
|
func fileExists(filename string) (bool, error) {
|
|
|
|
if _, err := os.Stat(filename); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
2015-09-21 11:35:59 -04:00
|
|
|
|
2015-10-12 06:14:50 -04:00
|
|
|
// writeResourceContent is responsible for generating the specific content from the resource
|
2015-09-21 11:35:59 -04:00
|
|
|
// rn : a point to the vault resource
|
|
|
|
// data : a map of the related secret associated to the resource
|
2015-09-23 17:39:50 -04:00
|
|
|
func writeResource(rn *VaultResource, data map[string]interface{}) error {
|
2015-09-21 11:35:59 -04:00
|
|
|
// step: determine the resource path
|
2015-09-23 17:39:50 -04:00
|
|
|
resourcePath := rn.GetFilename()
|
2015-09-21 11:35:59 -04:00
|
|
|
if !strings.HasPrefix(resourcePath, "/") {
|
2016-02-25 09:28:14 -05:00
|
|
|
resourcePath = fmt.Sprintf("%s/%s", options.outputDir, filepath.Base(resourcePath))
|
2015-09-21 11:35:59 -04:00
|
|
|
}
|
|
|
|
|
2015-10-15 12:28:12 -04:00
|
|
|
glog.V(10).Infof("writing the resource: %s, format: %s", resourcePath, rn.format)
|
|
|
|
|
2015-09-21 11:35:59 -04:00
|
|
|
if rn.format == "yaml" {
|
|
|
|
// marshall the content to yaml
|
2016-03-12 13:49:57 -05:00
|
|
|
content, err := yaml.Marshal(data)
|
|
|
|
if err != nil {
|
2015-09-21 11:35:59 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return writeFile(resourcePath, content)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rn.format == "ini" {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
for key, val := range data {
|
2016-03-12 13:49:57 -05:00
|
|
|
buf.WriteString(fmt.Sprintf("%s = %v\n", key, val))
|
2015-09-21 11:35:59 -04:00
|
|
|
}
|
|
|
|
|
2016-03-12 13:49:57 -05:00
|
|
|
return writeFile(resourcePath, buf.Bytes())
|
2015-09-21 11:35:59 -04:00
|
|
|
}
|
|
|
|
|
2016-02-25 09:28:14 -05:00
|
|
|
if rn.format == "bundle" {
|
|
|
|
certificateFile := fmt.Sprintf("%s.crt", resourcePath)
|
|
|
|
caFile := fmt.Sprintf("%s.ca", resourcePath)
|
|
|
|
certificate := fmt.Sprintf("%s\n\n%s", data["certificate"], data["private_key"])
|
|
|
|
ca := fmt.Sprintf("%s", data["issuing_ca"])
|
|
|
|
|
|
|
|
if err := writeFile(certificateFile, []byte(certificate)); err != nil {
|
|
|
|
glog.Errorf("failed to write the bundled certificate file, error: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := writeFile(caFile, []byte(ca)); err != nil {
|
|
|
|
glog.Errorf("failed to write the ca certificate file, errro: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-12 13:49:57 -05:00
|
|
|
if rn.format == "env" {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
for key, val := range data {
|
|
|
|
buf.WriteString(fmt.Sprintf("%s=%v\n", strings.ToUpper(key), val))
|
|
|
|
}
|
|
|
|
|
|
|
|
return writeFile(resourcePath, buf.Bytes())
|
|
|
|
}
|
|
|
|
|
2015-09-21 11:35:59 -04:00
|
|
|
if rn.format == "cert" {
|
|
|
|
files := map[string]string{
|
|
|
|
"certificate": "crt",
|
|
|
|
"issuing_ca": "ca",
|
|
|
|
"private_key": "key",
|
|
|
|
}
|
|
|
|
for key, suffix := range files {
|
|
|
|
filename := fmt.Sprintf("%s.%s", resourcePath, suffix)
|
|
|
|
content, found := data[key]
|
|
|
|
if !found {
|
2015-10-21 09:40:06 -04:00
|
|
|
glog.Errorf("didn't find the certification option: %s in the resource: %s", key, rn.path)
|
2015-09-21 11:35:59 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// step: write the file
|
|
|
|
if err := writeFile(filename, []byte(fmt.Sprintf("%s", content))); err != nil {
|
|
|
|
glog.Errorf("failed to write resource: %s, elemment: %s, filename: %s, error: %s", rn, suffix, filename, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if rn.format == "csv" {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
for key, val := range data {
|
2016-03-12 13:49:57 -05:00
|
|
|
buf.WriteString(fmt.Sprintf("%s,%v\n", key, val))
|
2015-09-21 11:35:59 -04:00
|
|
|
}
|
|
|
|
|
2016-03-12 13:49:57 -05:00
|
|
|
return writeFile(resourcePath, buf.Bytes())
|
2015-09-21 11:35:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if rn.format == "txt" {
|
|
|
|
keys := getKeys(data)
|
|
|
|
if len(keys) > 1 {
|
|
|
|
// step: for plain formats we need to iterate the keys and produce a file per key
|
|
|
|
for suffix, content := range data {
|
|
|
|
filename := fmt.Sprintf("%s.%s", resourcePath, suffix)
|
|
|
|
if err := writeFile(filename, []byte(fmt.Sprintf("%s", content))); err != nil {
|
|
|
|
glog.Errorf("failed to write resource: %s, elemment: %s, filename: %s, error: %s",
|
|
|
|
rn, suffix, filename, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// step: we only have the one key, so will write plain
|
|
|
|
value, _ := data[keys[0]]
|
2016-03-12 13:49:57 -05:00
|
|
|
content := []byte(fmt.Sprintf("%s", value))
|
2015-09-21 11:35:59 -04:00
|
|
|
|
|
|
|
return writeFile(resourcePath, content)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if rn.format == "json" {
|
2016-03-12 13:49:57 -05:00
|
|
|
content, err := json.MarshalIndent(data, "", " ")
|
|
|
|
if err != nil {
|
2015-09-21 11:35:59 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return writeFile(resourcePath, content)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("unknown output format: %s", rn.format)
|
|
|
|
}
|
|
|
|
|
|
|
|
// writeFile ... writes the content to a file .. dah
|
|
|
|
// filename : the path to the file
|
|
|
|
// content : the content to be written
|
|
|
|
func writeFile(filename string, content []byte) error {
|
|
|
|
if options.dryRun {
|
|
|
|
glog.Infof("dry-run: filename: %s, content:", filename)
|
|
|
|
fmt.Printf("%s\n", string(content))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-23 17:39:50 -04:00
|
|
|
glog.V(3).Infof("saving the file: %s", filename)
|
|
|
|
|
2015-09-21 11:35:59 -04:00
|
|
|
return ioutil.WriteFile(filename, content, 0660)
|
|
|
|
}
|