Migrate ParseAddress to shared-secure-libs (#1313)
This commit is contained in:
1 parent
8c63ede9cc
commit
9c945b7021
6 files changed
+24
-56
No files matched your search
@@ -535,8 +535,8 @@ func (b *Server) SetupControllerPublicClusterAddress(conf *config.Config, flagVa
|
||||
}
|
||||
} else {
|
||||
var err error
|
||||
conf.Controller.PublicClusterAddr, err = config.ParseAddress(conf.Controller.PublicClusterAddr)
|
||||
if err != nil && err != config.ErrNotAUrl {
|
||||
conf.Controller.PublicClusterAddr, err = configutil.ParsePath(conf.Controller.PublicClusterAddr)
|
||||
if err != nil && !errors.Is(err, configutil.ErrNotAUrl) {
|
||||
return fmt.Errorf("Error parsing public cluster addr: %w", err)
|
||||
}
|
||||
}
|
||||
@@ -572,8 +572,8 @@ func (b *Server) SetupWorkerPublicAddress(conf *config.Config, flagValue string)
|
||||
}
|
||||
} else {
|
||||
var err error
|
||||
conf.Worker.PublicAddr, err = config.ParseAddress(conf.Worker.PublicAddr)
|
||||
if err != nil && err != config.ErrNotAUrl {
|
||||
conf.Worker.PublicAddr, err = configutil.ParsePath(conf.Worker.PublicAddr)
|
||||
if err != nil && !errors.Is(err, configutil.ErrNotAUrl) {
|
||||
return fmt.Errorf("Error parsing public addr: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"github.com/hashicorp/boundary/internal/types/scope"
|
||||
"github.com/hashicorp/boundary/sdk/wrapper"
|
||||
wrapping "github.com/hashicorp/go-kms-wrapping"
|
||||
"github.com/hashicorp/shared-secure-libs/configutil"
|
||||
"github.com/hashicorp/vault/sdk/helper/mlock"
|
||||
"github.com/mitchellh/cli"
|
||||
"github.com/posener/complete"
|
||||
@@ -233,8 +234,8 @@ func (c *InitCommand) Run(args []string) (retCode int) {
|
||||
return base.CommandUserError
|
||||
}
|
||||
|
||||
migrationUrl, err := config.ParseAddress(migrationUrlToParse)
|
||||
if err != nil && err != config.ErrNotAUrl {
|
||||
migrationUrl, err := configutil.ParsePath(migrationUrlToParse)
|
||||
if err != nil && !errors.Is(err, configutil.ErrNotAUrl) {
|
||||
c.UI.Error(fmt.Errorf("Error parsing migration url: %w", err).Error())
|
||||
return base.CommandUserError
|
||||
}
|
||||
@@ -254,8 +255,8 @@ func (c *InitCommand) Run(args []string) (retCode int) {
|
||||
c.UI.Error(`"url" not specified in "database" config block`)
|
||||
return base.CommandUserError
|
||||
}
|
||||
c.srv.DatabaseUrl, err = config.ParseAddress(urlToParse)
|
||||
if err != nil && err != config.ErrNotAUrl {
|
||||
c.srv.DatabaseUrl, err = configutil.ParsePath(urlToParse)
|
||||
if err != nil && !errors.Is(err, configutil.ErrNotAUrl) {
|
||||
c.UI.Error(fmt.Errorf("Error parsing database url: %w", err).Error())
|
||||
return base.CommandUserError
|
||||
}
|
||||
|
||||
@@ -5,8 +5,10 @@ import (
|
||||
|
||||
"github.com/hashicorp/boundary/internal/cmd/base"
|
||||
"github.com/hashicorp/boundary/internal/cmd/config"
|
||||
"github.com/hashicorp/boundary/internal/errors"
|
||||
"github.com/hashicorp/boundary/sdk/wrapper"
|
||||
wrapping "github.com/hashicorp/go-kms-wrapping"
|
||||
"github.com/hashicorp/shared-secure-libs/configutil"
|
||||
"github.com/hashicorp/vault/sdk/helper/mlock"
|
||||
"github.com/mitchellh/cli"
|
||||
"github.com/posener/complete"
|
||||
@@ -174,8 +176,8 @@ func (c *MigrateCommand) Run(args []string) (retCode int) {
|
||||
return base.CommandUserError
|
||||
}
|
||||
|
||||
migrationUrl, err := config.ParseAddress(migrationUrlToParse)
|
||||
if err != nil && err != config.ErrNotAUrl {
|
||||
migrationUrl, err := configutil.ParsePath(migrationUrlToParse)
|
||||
if err != nil && !errors.Is(err, configutil.ErrNotAUrl) {
|
||||
c.UI.Error(fmt.Errorf("Error parsing migration url: %w", err).Error())
|
||||
return base.CommandUserError
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package dev
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"runtime"
|
||||
@@ -19,6 +20,7 @@ import (
|
||||
"github.com/hashicorp/boundary/internal/target"
|
||||
"github.com/hashicorp/boundary/internal/types/scope"
|
||||
"github.com/hashicorp/boundary/sdk/strutil"
|
||||
"github.com/hashicorp/shared-secure-libs/configutil"
|
||||
"github.com/mitchellh/cli"
|
||||
"github.com/posener/complete"
|
||||
)
|
||||
@@ -483,8 +485,8 @@ func (c *Command) Run(args []string) int {
|
||||
c.ShutdownFuncs = append(c.ShutdownFuncs, c.DestroyDevDatabase)
|
||||
}
|
||||
default:
|
||||
c.DatabaseUrl, err = config.ParseAddress(c.flagDatabaseUrl)
|
||||
if err != nil && err != config.ErrNotAUrl {
|
||||
c.DatabaseUrl, err = configutil.ParsePath(c.flagDatabaseUrl)
|
||||
if err != nil && !errors.Is(err, configutil.ErrNotAUrl) {
|
||||
c.UI.Error(fmt.Errorf("Error parsing database url: %w", err).Error())
|
||||
return base.CommandUserError
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ import (
|
||||
"github.com/hashicorp/go-hclog"
|
||||
wrapping "github.com/hashicorp/go-kms-wrapping"
|
||||
"github.com/hashicorp/go-multierror"
|
||||
"github.com/hashicorp/shared-secure-libs/configutil"
|
||||
"github.com/hashicorp/vault/sdk/helper/mlock"
|
||||
"github.com/mitchellh/cli"
|
||||
"github.com/posener/complete"
|
||||
@@ -361,8 +362,8 @@ func (c *Command) Run(args []string) int {
|
||||
return base.CommandUserError
|
||||
}
|
||||
var err error
|
||||
c.DatabaseUrl, err = config.ParseAddress(c.Config.Controller.Database.Url)
|
||||
if err != nil && err != config.ErrNotAUrl {
|
||||
c.DatabaseUrl, err = configutil.ParsePath(c.Config.Controller.Database.Url)
|
||||
if err != nil && !errors.Is(err, configutil.ErrNotAUrl) {
|
||||
c.UI.Error(fmt.Errorf("Error parsing database url: %w", err).Error())
|
||||
return base.CommandUserError
|
||||
}
|
||||
|
||||
@@ -8,8 +8,6 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -238,8 +236,8 @@ func Parse(d string) (*Config, error) {
|
||||
|
||||
// Perform controller configuration overrides for auth token settings
|
||||
if result.Controller != nil {
|
||||
result.Controller.Name, err = ParseAddress(result.Controller.Name)
|
||||
if err != nil && err != ErrNotAUrl {
|
||||
result.Controller.Name, err = configutil.ParsePath(result.Controller.Name)
|
||||
if err != nil && !errors.Is(err, configutil.ErrNotAUrl) {
|
||||
return nil, fmt.Errorf("Error parsing controller name: %w", err)
|
||||
}
|
||||
if result.Controller.Name != strings.ToLower(result.Controller.Name) {
|
||||
@@ -267,8 +265,8 @@ func Parse(d string) (*Config, error) {
|
||||
|
||||
// Parse worker tags
|
||||
if result.Worker != nil {
|
||||
result.Worker.Name, err = ParseAddress(result.Worker.Name)
|
||||
if err != nil && err != ErrNotAUrl {
|
||||
result.Worker.Name, err = configutil.ParsePath(result.Worker.Name)
|
||||
if err != nil && !errors.Is(err, configutil.ErrNotAUrl) {
|
||||
return nil, fmt.Errorf("Error parsing worker name: %w", err)
|
||||
}
|
||||
if result.Worker.Name != strings.ToLower(result.Worker.Name) {
|
||||
@@ -385,39 +383,3 @@ func (c *Config) Sanitized() map[string]interface{} {
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
var ErrNotAUrl = errors.New("not a url")
|
||||
|
||||
// ParseAddress parses a URL with schemes file://, env://, or any other.
|
||||
// Depending on the scheme it will return specific types of data:
|
||||
//
|
||||
// * file:// will return a string with the file's contents
|
||||
//
|
||||
// * env:// will return a string with the env var's contents
|
||||
//
|
||||
// * Anything else will return the string as it was
|
||||
//
|
||||
// On error, we return the original string along with the error. The caller can
|
||||
// switch on ErrNotAUrl to understand whether it was the parsing step that
|
||||
// errored or something else. This is useful to attempt to read a non-URL string
|
||||
// from some resource, but where the original input may simply be a valid string
|
||||
// of that type.
|
||||
func ParseAddress(addr string) (string, error) {
|
||||
addr = strings.TrimSpace(addr)
|
||||
parsed, err := url.Parse(addr)
|
||||
if err != nil {
|
||||
return addr, ErrNotAUrl
|
||||
}
|
||||
switch parsed.Scheme {
|
||||
case "file":
|
||||
contents, err := ioutil.ReadFile(strings.TrimPrefix(addr, "file://"))
|
||||
if err != nil {
|
||||
return addr, fmt.Errorf("error reading file at %s: %w", addr, err)
|
||||
}
|
||||
return strings.TrimSpace(string(contents)), nil
|
||||
case "env":
|
||||
return strings.TrimSpace(os.Getenv(strings.TrimPrefix(addr, "env://"))), nil
|
||||
}
|
||||
|
||||
return addr, nil
|
||||
}
|
||||
Reference in new issue
Block a user