feat(cmd): Create target using address on boundary dev / database init

Creates a target using an address for boundary dev and when first
init-ing boundary (boundary database init).

It also makes this newly created target the default one
(ttcp_1234567890). The target using host sources is now a secondary
target (ttcp_0987654321).
This commit is contained in:
Hugo Vieira committed 2023-01-09 15:39:55 +00:00
1 parent 1d3930a711
commit f85da3ee2b
9 files changed
+236 -90

No files matched your search

+27 -11
View File
@@ -426,26 +426,42 @@ func (c *InitCommand) Run(args []string) (retCode int) {
}
c.DevTargetSessionConnectionLimit = -1
t, err := c.CreateInitialTarget(c.Context)
ta, err := c.CreateInitialTargetWithAddress(c.Context)
if err != nil {
c.UI.Error(fmt.Errorf("Error creating initial target: %w", err).Error())
return base.CommandCliError
}
taInfo := &TargetInfo{
TargetId: ta.GetPublicId(),
DefaultPort: ta.GetDefaultPort(),
SessionMaxSeconds: ta.GetSessionMaxSeconds(),
SessionConnectionLimit: ta.GetSessionConnectionLimit(),
Type: string(ta.GetType()),
ScopeId: ta.GetProjectId(),
Name: ta.GetName(),
}
targetInfo := &TargetInfo{
TargetId: c.DevTargetId,
DefaultPort: t.GetDefaultPort(),
SessionMaxSeconds: t.GetSessionMaxSeconds(),
SessionConnectionLimit: t.GetSessionConnectionLimit(),
Type: "tcp",
ScopeId: c.DevProjectId,
Name: t.GetName(),
ths, err := c.CreateInitialTargetWithHostSources(c.Context)
if err != nil {
c.UI.Error(fmt.Errorf("Error creating initial secondary target: %w", err).Error())
return base.CommandCliError
}
thsInfo := &TargetInfo{
TargetId: ths.GetPublicId(),
DefaultPort: ths.GetDefaultPort(),
SessionMaxSeconds: ths.GetSessionMaxSeconds(),
SessionConnectionLimit: ths.GetSessionConnectionLimit(),
Type: string(ths.GetType()),
ScopeId: ths.GetProjectId(),
Name: ths.GetName(),
}
switch base.Format(c.UI) {
case "table":
c.UI.Output(generateInitialTargetTableOutput(targetInfo))
c.UI.Output(generateInitialTargetTableOutput(taInfo))
c.UI.Output(generateInitialTargetTableOutput(thsInfo))
case "json":
jsonMap["target"] = targetInfo
jsonMap["target"] = taInfo
jsonMap["target_secondary"] = thsInfo
}
return base.CommandSuccess
+27 -1
View File
@@ -66,6 +66,7 @@ type Command struct {
flagUnprivilegedLoginName string
flagUnprivilegedPassword string
flagIdSuffix string
flagSecondaryIdSuffix string
flagHostAddress string
flagTargetDefaultPort int
flagTargetSessionMaxSeconds int
@@ -141,7 +142,15 @@ func (c *Command) Flags() *base.FlagSets {
Target: &c.flagIdSuffix,
Default: "1234567890",
EnvVar: "BOUNDARY_DEV_ID_SUFFIX",
Usage: `If set, auto-created resources will use this value for their identifier (along with their resource-specific prefix). Must be 10 alphanumeric characters. As an example, if this is set to "1234567890", the generated password auth method ID will be "ampw_1234567890", the generated TCP target ID will be "ttcp_1234567890", and so on.`,
Usage: `If set, auto-created resources will use this value for their identifier (along with their resource-specific prefix). Must be 10 alphanumeric characters. As an example, if this is set to "1234567890", the generated password auth method ID will be "ampw_1234567890", the generated TCP target ID will be "ttcp_1234567890", and so on. Must be different from -secondary-id-suffix (BOUNDARY_DEV_SECONDARY_ID_SUFFIX).`,
})
f.StringVar(&base.StringVar{
Name: "secondary-id-suffix",
Target: &c.flagSecondaryIdSuffix,
Default: "0987654321",
EnvVar: "BOUNDARY_DEV_SECONDARY_ID_SUFFIX",
Usage: `If set, secondary auto-created resources will use this value for their identifier (along with their resource-specific prefix). Must be 10 alphanumeric characters. Currently only used for the target resource. Must be different from -id-suffix (BOUNDARY_DEV_ID_SUFFIX).`,
})
f.StringVar(&base.StringVar{
@@ -450,6 +459,23 @@ func (c *Command) Run(args []string) int {
c.DevHostId = fmt.Sprintf("%s_%s", static.HostPrefix, c.flagIdSuffix)
c.DevTargetId = fmt.Sprintf("%s_%s", tcp.TargetPrefix, c.flagIdSuffix)
}
if c.flagSecondaryIdSuffix != "" {
if len(c.flagSecondaryIdSuffix) != 10 {
c.UI.Error("Invalid secondary ID suffix, must be exactly 10 characters")
return base.CommandUserError
}
if !handlers.ValidId(handlers.Id("abc_"+c.flagSecondaryIdSuffix), "abc") {
c.UI.Error("Invalid secondary ID suffix, must be in the set A-Za-z0-9")
return base.CommandUserError
}
c.DevSecondaryTargetId = fmt.Sprintf("%s_%s", tcp.TargetPrefix, c.flagSecondaryIdSuffix)
}
if c.flagIdSuffix != "" && c.flagSecondaryIdSuffix != "" &&
strings.EqualFold(c.flagIdSuffix, c.flagSecondaryIdSuffix) {
c.UI.Error("Primary and secondary ID suffixes are equal, must be distinct")
return base.CommandUserError
}
host, port, err := net.SplitHostPort(c.flagHostAddress)
if err != nil {
@@ -99,7 +99,7 @@ func TestServer_ReloadListener(t *testing.T) {
CreateDevDatabase: true,
ControllerKey: controllerKey,
UseDevAuthMethod: true,
UseDevTarget: true,
UseDevTargets: true,
})
// Unset auto-created KMSes that are overwritten by config on startup
cmd.RootKms = nil
+9 -5
View File
@@ -14,15 +14,18 @@ import (
// We try to pull these from TestController, but targets et al are
// computed off of a suffix instead of having constants. Just define
// for now here, this can be tweaked later if need be.
const defaultTestTargetId = "ttcp_1234567890"
const (
defaultTestTargetId = "ttcp_1234567890"
defaultSecondaryTestTargetId = "ttcp_0987654321"
const rootKmsConfig = `
rootKmsConfig = `
kms "aead" {
purpose = "root"
aead_type = "aes-gcm"
key = "%s"
key_id = "global_root"
}`
)
type testServerCommandOpts struct {
// Whether or not to create the dev database
@@ -34,8 +37,8 @@ type testServerCommandOpts struct {
// Use the well-known dev mode auth method id (1234567890)
UseDevAuthMethod bool
// Use the well-known dev mode target method id (1234567890)
UseDevTarget bool
// Use the well-known dev mode target method ids (1234567890 and 0987654321)
UseDevTargets bool
// Whether or not to enable metric collection. If enable metrics will use
// prometheus' default registerer.
@@ -71,8 +74,9 @@ func testServerCommand(t *testing.T, opts testServerCommandOpts) *Command {
cmd.Server.DevPassword = controller.DefaultTestPassword
}
if opts.UseDevTarget {
if opts.UseDevTargets {
cmd.Server.DevTargetId = defaultTestTargetId
cmd.Server.DevSecondaryTargetId = defaultSecondaryTestTargetId
}
err = cmd.CreateDevDatabase(cmd.Context, base.WithDatabaseTemplate("boundary_template"), base.WithSkipOidcAuthMethodCreation())
@@ -78,9 +78,10 @@ func TestServer_ShutdownWorker(t *testing.T) {
tcl := targets.NewClient(client)
tgtL, err := tcl.List(ctx, scope.Global.String(), targets.WithRecursive(true))
require.NoError(err)
require.Len(tgtL.Items, 1)
require.Len(tgtL.Items, 2)
tgt := tgtL.Items[0]
require.NotNil(tgt)
require.NotNil(tgtL.GetItems()[1])
// Create test server, update default port on target
ts := helper.NewTestTcpServer(t)