Change IsConfigured to OR instead of AND
(if the user specifies any option, this indicates intent)
Add and simplify validation for CredHub
@@ -23,7 +23,7 @@ type CredHubManager struct {
}
func (manager CredHubManager) IsConfigured() bool {
- return manager.URL != "" && manager.ClientId != "" && manager.ClientSecret != ""
+ return manager.URL != "" || manager.ClientId != "" || manager.ClientSecret != "" || len(manager.CACerts) != 0
}
func (manager CredHubManager) Validate() error {
@@ -31,6 +31,11 @@ func (manager CredHubManager) Validate() error {
if err != nil {
return fmt.Errorf("invalid URL: %s", err)
}
+ // "foo" will parse without error (as a Path, with an empty Host)
+ // so we'll do a few additional sanity checks that this is a valid URL
+ if parsedUrl.Host == "" || !(parsedUrl.Scheme == "http" || parsedUrl.Scheme == "https") {
+ return fmt.Errorf("invalid URL")
+ }
if parsedUrl.Scheme == "https" {
if len(manager.CACerts) < 1 && !manager.Insecure {
@@ -38,7 +43,10 @@ func (manager CredHubManager) Validate() error {
}
}
- if len(manager.CACerts) > 1 {
+ if manager.ClientId == "" || manager.ClientSecret == "" {
+ return fmt.Errorf("--credhub-client-id and --credhub-client-secret must be set to use CredHub")
+ }
+
for _, cert := range manager.CACerts {
contents, err := ioutil.ReadFile(cert)
if err != nil {
@@ -46,7 +54,6 @@ func (manager CredHubManager) Validate() error {
}
manager.caCerts = append(manager.caCerts, string(contents))
}
- }
return nil
}
