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 (
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
2015-09-18 12:18:17 -04:00
|
|
|
"strconv"
|
2015-09-18 12:58:52 -04:00
|
|
|
"time"
|
2015-09-18 05:14:15 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// OptionFilename ... option to set the filename of the resource
|
|
|
|
OptionFilename = "fn"
|
2015-09-18 12:58:52 -04:00
|
|
|
// OptionFormat ... option to set the output format (yaml, xml, json)
|
|
|
|
OptionFormat = "fmt"
|
|
|
|
// OptionCommonName ... use by the PKI resource
|
2015-09-18 05:14:15 -04:00
|
|
|
OptionCommonName = "cn"
|
|
|
|
// OptionTemplatePath ... the full path to a template
|
2015-09-18 12:58:52 -04:00
|
|
|
OptionTemplatePath = "tpl"
|
|
|
|
// OptionRenewal ... a duration to renew the resource
|
2015-09-18 12:18:17 -04:00
|
|
|
OptionRenewal = "rn"
|
|
|
|
// OptionRevoke ... revoke an old lease when retrieving a new one
|
|
|
|
OptionRevoke = "rv"
|
|
|
|
// OptionUpdate ... override the lease of the resource
|
|
|
|
OptionUpdate = "up"
|
2015-09-18 05:14:15 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2015-09-18 12:45:34 -04:00
|
|
|
resourceFormatRegex = regexp.MustCompile("^(yaml|json|ini|txt|cert)$")
|
2015-09-18 05:14:15 -04:00
|
|
|
|
|
|
|
// a map of valid resource to retrieve from vault
|
|
|
|
validResources = map[string]bool{
|
2015-09-18 12:58:52 -04:00
|
|
|
"pki": true,
|
|
|
|
"aws": true,
|
|
|
|
"secret": true,
|
|
|
|
"mysql": true,
|
|
|
|
"tpl": true,
|
|
|
|
"postgres": true,
|
|
|
|
"cassandra": true,
|
2015-09-18 05:14:15 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2015-09-18 12:18:17 -04:00
|
|
|
func defaultVaultResource() *vaultResource {
|
2015-09-18 05:14:15 -04:00
|
|
|
return &vaultResource{
|
2015-09-18 12:58:52 -04:00
|
|
|
format: "yaml",
|
2015-09-18 12:18:17 -04:00
|
|
|
renewable: false,
|
2015-09-18 12:58:52 -04:00
|
|
|
revoked: false,
|
|
|
|
options: make(map[string]string, 0),
|
2015-09-18 05:14:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// resource ... the structure which defined a resource set from vault
|
|
|
|
type vaultResource struct {
|
|
|
|
// the namespace of the resource
|
|
|
|
resource string
|
|
|
|
// the name of the resource
|
|
|
|
name string
|
2015-09-18 12:18:17 -04:00
|
|
|
// the format of the resource
|
|
|
|
format string
|
|
|
|
// whether the resource should be renewed?
|
|
|
|
renewable bool
|
|
|
|
// whether the resource should be revoked?
|
|
|
|
revoked bool
|
|
|
|
// the lease duration
|
|
|
|
update time.Duration
|
2015-09-18 05:14:15 -04:00
|
|
|
// additional options to the resource
|
|
|
|
options map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
// isValid ... checks to see if the resource is valid
|
2015-09-18 12:18:17 -04:00
|
|
|
func (r *vaultResource) isValid() error {
|
2015-09-18 05:14:15 -04:00
|
|
|
// step: check the resource type
|
|
|
|
if _, found := validResources[r.resource]; !found {
|
|
|
|
return fmt.Errorf("unsupported resource type: %s", r.resource)
|
|
|
|
}
|
|
|
|
|
|
|
|
// step: check the options
|
|
|
|
if err := r.isValidOptions(); err != nil {
|
2015-09-18 12:18:17 -04:00
|
|
|
return fmt.Errorf("invalid resource options, %s", err)
|
2015-09-18 05:14:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// step: check is have all the required options to this resource type
|
|
|
|
if err := r.isValidResource(); err != nil {
|
|
|
|
return fmt.Errorf("invalid resource: %s, %s", r, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// isValidResource ... validate the resource meets the requirements
|
2015-09-18 12:18:17 -04:00
|
|
|
func (r *vaultResource) isValidResource() error {
|
2015-09-18 05:14:15 -04:00
|
|
|
switch r.resource {
|
|
|
|
case "pki":
|
|
|
|
if _, found := r.options[OptionCommonName]; !found {
|
|
|
|
return fmt.Errorf("pki resource requires a common name specified")
|
|
|
|
}
|
|
|
|
case "tpl":
|
2015-09-18 12:58:52 -04:00
|
|
|
if _, found := r.options[OptionTemplatePath]; !found {
|
2015-09-18 05:14:15 -04:00
|
|
|
return fmt.Errorf("template resource requires a template path option")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-18 12:18:17 -04:00
|
|
|
// isValidOptions ... iterates through the options, converts the options and so forth
|
|
|
|
func (r *vaultResource) isValidOptions() error {
|
2015-09-18 05:14:15 -04:00
|
|
|
// check the filename directive
|
|
|
|
for opt, val := range r.options {
|
|
|
|
switch opt {
|
|
|
|
case OptionFormat:
|
|
|
|
if matched := resourceFormatRegex.MatchString(r.options[OptionFormat]); !matched {
|
|
|
|
return fmt.Errorf("unsupported output format: %s", r.options[OptionFormat])
|
|
|
|
}
|
2015-09-18 12:18:17 -04:00
|
|
|
r.format = val
|
|
|
|
case OptionUpdate:
|
|
|
|
duration, err := time.ParseDuration(val)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("the update option: %s is not value, should be a duration format", val)
|
|
|
|
}
|
|
|
|
r.update = duration
|
|
|
|
case OptionRevoke:
|
|
|
|
choice, err := strconv.ParseBool(val)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("the revoke option: %s is invalid, should be a boolean", val)
|
|
|
|
}
|
|
|
|
r.revoked = choice
|
|
|
|
case OptionRenewal:
|
|
|
|
choice, err := strconv.ParseBool(val)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("the renewal option: %s is invalid, should be a boolean", val)
|
2015-09-18 05:14:15 -04:00
|
|
|
}
|
2015-09-18 12:18:17 -04:00
|
|
|
r.renewable = choice
|
2015-09-18 05:14:15 -04:00
|
|
|
case OptionFilename:
|
2015-09-18 12:18:17 -04:00
|
|
|
// @TODO need to check it's valid filename / path
|
2015-09-18 05:14:15 -04:00
|
|
|
case OptionCommonName:
|
2015-09-18 12:18:17 -04:00
|
|
|
// @TODO need to check it's a valid hostname
|
2015-09-18 12:58:52 -04:00
|
|
|
case OptionTemplatePath:
|
2015-09-18 05:14:15 -04:00
|
|
|
if exists, _ := fileExists(val); !exists {
|
|
|
|
return fmt.Errorf("the template file: %s does not exist", val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// resourceFilename ... generates a resource filename by default the resource name and resource type, which
|
|
|
|
// can override by the OPTION_FILENAME option
|
|
|
|
func (r vaultResource) filename() string {
|
|
|
|
if path, found := r.options[OptionFilename]; found {
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%s.%s", r.name, r.resource)
|
|
|
|
}
|
|
|
|
|
|
|
|
// String ... a string representation of the struct
|
|
|
|
func (r vaultResource) String() string {
|
2015-09-21 06:31:12 -04:00
|
|
|
return fmt.Sprintf("%s/%s", r.resource, r.name)
|
2015-09-18 05:14:15 -04:00
|
|
|
}
|