Merge pull request #3 from gambol99/fix/formatting
- - fixed up the formatting
This commit is contained in:
commit
096e365598
1
Makefile
1
Makefile
|
@ -14,6 +14,7 @@ build:
|
||||||
|
|
||||||
docker: build
|
docker: build
|
||||||
sudo docker build -t ${AUTHOR}/${NAME} .
|
sudo docker build -t ${AUTHOR}/${NAME} .
|
||||||
|
sudo docker build -t ${AUTHOR}/${NAME} .
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -rf ./build 2>/dev/null
|
rm -rf ./build 2>/dev/null
|
||||||
|
|
|
@ -21,8 +21,9 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/url"
|
"net/url"
|
||||||
"github.com/golang/glog"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/golang/glog"
|
||||||
)
|
)
|
||||||
|
|
||||||
// config ... the command line configuration
|
// config ... the command line configuration
|
||||||
|
|
8
utils.go
8
utils.go
|
@ -17,11 +17,11 @@ limitations under the License.
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
|
||||||
"math/rand"
|
|
||||||
"time"
|
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math/rand"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -48,7 +48,7 @@ func randomWait(min, max int ) <-chan time.Time {
|
||||||
// getKeys ... retrieve a list of keys from the map
|
// getKeys ... retrieve a list of keys from the map
|
||||||
func getKeys(data map[string]interface{}) []string {
|
func getKeys(data map[string]interface{}) []string {
|
||||||
var list []string
|
var list []string
|
||||||
for key, _ := range data {
|
for key := range data {
|
||||||
list = append(list, key)
|
list = append(list, key)
|
||||||
}
|
}
|
||||||
return list
|
return list
|
||||||
|
|
17
vault.go
17
vault.go
|
@ -17,11 +17,11 @@ limitations under the License.
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/hashicorp/vault/api"
|
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
"fmt"
|
"github.com/hashicorp/vault/api"
|
||||||
)
|
)
|
||||||
|
|
||||||
// a channel to send resource
|
// a channel to send resource
|
||||||
|
@ -122,7 +122,8 @@ func newVaultService(url, token string) (*vaultService, error) {
|
||||||
func (r vaultService) vaultServiceProcessor() {
|
func (r vaultService) vaultServiceProcessor() {
|
||||||
go func() {
|
go func() {
|
||||||
// a list of resource being watched
|
// a list of resource being watched
|
||||||
items := make([]*watchedResource, 0)
|
var items []*watchedResource
|
||||||
|
|
||||||
// the channel to receive renewal notifications on
|
// the channel to receive renewal notifications on
|
||||||
renewChannel := make(chan *watchedResource, 10)
|
renewChannel := make(chan *watchedResource, 10)
|
||||||
retrieveChannel := make(chan *watchedResource, 10)
|
retrieveChannel := make(chan *watchedResource, 10)
|
||||||
|
@ -144,10 +145,10 @@ func (r vaultService) vaultServiceProcessor() {
|
||||||
|
|
||||||
case x := <-retrieveChannel:
|
case x := <-retrieveChannel:
|
||||||
// step: save the current lease if we have one
|
// step: save the current lease if we have one
|
||||||
leaseId := ""
|
leaseID := ""
|
||||||
if x.secret != nil && x.secret.LeaseID != "" {
|
if x.secret != nil && x.secret.LeaseID != "" {
|
||||||
leaseId = x.secret.LeaseID
|
leaseID = x.secret.LeaseID
|
||||||
glog.V(10).Infof("resource: %s has a previous lease: %s", x.resource, leaseId)
|
glog.V(10).Infof("resource: %s has a previous lease: %s", x.resource, leaseID)
|
||||||
}
|
}
|
||||||
|
|
||||||
// step: retrieve the resource from vault
|
// step: retrieve the resource from vault
|
||||||
|
@ -163,8 +164,8 @@ func (r vaultService) vaultServiceProcessor() {
|
||||||
glog.Infof("succesfully retrieved resournce: %s, leaseID: %s", x.resource, x.secret.LeaseID)
|
glog.Infof("succesfully retrieved resournce: %s, leaseID: %s", x.resource, x.secret.LeaseID)
|
||||||
|
|
||||||
// step: if we had a previous lease and the option is to revoke, lets throw into the revoke channel
|
// step: if we had a previous lease and the option is to revoke, lets throw into the revoke channel
|
||||||
if leaseId != "" && x.resource.revoked {
|
if leaseID != "" && x.resource.revoked {
|
||||||
revokeChannel <- leaseId
|
revokeChannel <- leaseID
|
||||||
}
|
}
|
||||||
|
|
||||||
// step: setup a timer for renewal
|
// step: setup a timer for renewal
|
||||||
|
|
|
@ -19,28 +19,25 @@ package main
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"regexp"
|
"regexp"
|
||||||
"time"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"github.com/golang/glog"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// OptionFilename ... option to set the filename of the resource
|
// OptionFilename ... option to set the filename of the resource
|
||||||
OptionFilename = "fn"
|
OptionFilename = "fn"
|
||||||
// OptionsFormat ... option to set the output format (yaml, xml, json)
|
// OptionFormat ... option to set the output format (yaml, xml, json)
|
||||||
OptionFormat = "fmt"
|
OptionFormat = "fmt"
|
||||||
// OptionsCommonName ... use by the PKI resource
|
// OptionCommonName ... use by the PKI resource
|
||||||
OptionCommonName = "cn"
|
OptionCommonName = "cn"
|
||||||
// OptionTemplatePath ... the full path to a template
|
// OptionTemplatePath ... the full path to a template
|
||||||
OptionsTemplatePath = "tpl"
|
OptionTemplatePath = "tpl"
|
||||||
// OptionRenew ... a duration to renew the resource
|
// OptionRenewal ... a duration to renew the resource
|
||||||
OptionRenewal = "rn"
|
OptionRenewal = "rn"
|
||||||
// OptionRevoke ... revoke an old lease when retrieving a new one
|
// OptionRevoke ... revoke an old lease when retrieving a new one
|
||||||
OptionRevoke = "rv"
|
OptionRevoke = "rv"
|
||||||
// OptionUpdate ... override the lease of the resource
|
// OptionUpdate ... override the lease of the resource
|
||||||
OptionUpdate = "up"
|
OptionUpdate = "up"
|
||||||
|
|
||||||
DefaultRenewable = "false"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -53,6 +50,8 @@ var (
|
||||||
"secret": true,
|
"secret": true,
|
||||||
"mysql": true,
|
"mysql": true,
|
||||||
"tpl": true,
|
"tpl": true,
|
||||||
|
"postgres": true,
|
||||||
|
"cassandra": true,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -111,7 +110,7 @@ func (r *vaultResource) isValidResource() error {
|
||||||
return fmt.Errorf("pki resource requires a common name specified")
|
return fmt.Errorf("pki resource requires a common name specified")
|
||||||
}
|
}
|
||||||
case "tpl":
|
case "tpl":
|
||||||
if _, found := r.options[OptionsTemplatePath]; !found {
|
if _, found := r.options[OptionTemplatePath]; !found {
|
||||||
return fmt.Errorf("template resource requires a template path option")
|
return fmt.Errorf("template resource requires a template path option")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -128,34 +127,30 @@ func (r *vaultResource) isValidOptions() error {
|
||||||
if matched := resourceFormatRegex.MatchString(r.options[OptionFormat]); !matched {
|
if matched := resourceFormatRegex.MatchString(r.options[OptionFormat]); !matched {
|
||||||
return fmt.Errorf("unsupported output format: %s", r.options[OptionFormat])
|
return fmt.Errorf("unsupported output format: %s", r.options[OptionFormat])
|
||||||
}
|
}
|
||||||
glog.V(20).Infof("setting the format: %s on resource: %s", val, r)
|
|
||||||
r.format = val
|
r.format = val
|
||||||
case OptionUpdate:
|
case OptionUpdate:
|
||||||
duration, err := time.ParseDuration(val)
|
duration, err := time.ParseDuration(val)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("the update option: %s is not value, should be a duration format", val)
|
return fmt.Errorf("the update option: %s is not value, should be a duration format", val)
|
||||||
}
|
}
|
||||||
glog.V(20).Infof("setting the update time: %s on resource: %s", duration, r)
|
|
||||||
r.update = duration
|
r.update = duration
|
||||||
case OptionRevoke:
|
case OptionRevoke:
|
||||||
choice, err := strconv.ParseBool(val)
|
choice, err := strconv.ParseBool(val)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("the revoke option: %s is invalid, should be a boolean", val)
|
return fmt.Errorf("the revoke option: %s is invalid, should be a boolean", val)
|
||||||
}
|
}
|
||||||
glog.V(20).Infof("setting the revoked: %t on resource: %s", choice, r)
|
|
||||||
r.revoked = choice
|
r.revoked = choice
|
||||||
case OptionRenewal:
|
case OptionRenewal:
|
||||||
choice, err := strconv.ParseBool(val)
|
choice, err := strconv.ParseBool(val)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("the renewal option: %s is invalid, should be a boolean", val)
|
return fmt.Errorf("the renewal option: %s is invalid, should be a boolean", val)
|
||||||
}
|
}
|
||||||
glog.V(20).Infof("setting the renewable: %t on resource: %s", choice, r)
|
|
||||||
r.renewable = choice
|
r.renewable = choice
|
||||||
case OptionFilename:
|
case OptionFilename:
|
||||||
// @TODO need to check it's valid filename / path
|
// @TODO need to check it's valid filename / path
|
||||||
case OptionCommonName:
|
case OptionCommonName:
|
||||||
// @TODO need to check it's a valid hostname
|
// @TODO need to check it's a valid hostname
|
||||||
case OptionsTemplatePath:
|
case OptionTemplatePath:
|
||||||
if exists, _ := fileExists(val); !exists {
|
if exists, _ := fileExists(val); !exists {
|
||||||
return fmt.Errorf("the template file: %s does not exist", val)
|
return fmt.Errorf("the template file: %s does not exist", val)
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,7 +33,6 @@ func TestResourceFilename(t *testing.T) {
|
||||||
assert.Equal(t, "credentials", rn.filename())
|
assert.Equal(t, "credentials", rn.filename())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func TestIsValid(t *testing.T) {
|
func TestIsValid(t *testing.T) {
|
||||||
resource := defaultVaultResource()
|
resource := defaultVaultResource()
|
||||||
resource.name = "/test/name"
|
resource.name = "/test/name"
|
||||||
|
|
|
@ -33,10 +33,6 @@ type vaultResources struct {
|
||||||
items []*vaultResource
|
items []*vaultResource
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r vaultResources) size() int {
|
|
||||||
return len(r.items)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set ... implementation for the parser
|
// Set ... implementation for the parser
|
||||||
func (r *vaultResources) Set(value string) error {
|
func (r *vaultResources) Set(value string) error {
|
||||||
rn := defaultVaultResource()
|
rn := defaultVaultResource()
|
||||||
|
|
|
@ -22,7 +22,6 @@ import (
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
func TestSetResources(t *testing.T) {
|
func TestSetResources(t *testing.T) {
|
||||||
var items vaultResources
|
var items vaultResources
|
||||||
|
|
||||||
|
@ -41,13 +40,6 @@ func TestSetResources(t *testing.T) {
|
||||||
assert.NotNil(t, items.Set("fn=filename.test,fmt=yaml"))
|
assert.NotNil(t, items.Set("fn=filename.test,fmt=yaml"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestResourceSize(t *testing.T) {
|
|
||||||
var items vaultResources
|
|
||||||
items.Set("secret:test:fn=filename.test,fmt=yaml")
|
|
||||||
items.Set("secret:test:fn=fileame.test")
|
|
||||||
assert.Equal(t, 2, items.size())
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestResources(t *testing.T) {
|
func TestResources(t *testing.T) {
|
||||||
var items vaultResources
|
var items vaultResources
|
||||||
items.Set("secret:test:fn=filename.test,fmt=yaml")
|
items.Set("secret:test:fn=filename.test,fmt=yaml")
|
||||||
|
|
|
@ -15,4 +15,3 @@ limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
|
|
Reference in a new issue