feat(cli): Implement address flag for boundary targets create/update

This commit adds CLI support for addresses to be attached directly to a
Target, for all current types of Target (ssh and tcp).

It also clarifies the relevant documentation regarding the autogen of
API option functionality.
This commit is contained in:
Hugo Vieira committed 2023-01-09 15:39:55 +00:00
1 parent 61c90c5623
commit c82d2efb14
12 files changed
+221 -125

No files matched your search

+3 -2
View File
@@ -37,8 +37,9 @@ The first step in the process is to add it to the API/SDK protobufs.
* Define a `custom_options.v1.mask_mapping` tag for the field
which maps the API `this` field to the storage `that` field
(yes, it's the opposite of how it's defined for a storage protobuf)
* Define a `custom_options.v1.generate_sdk_option` tag for the SDK
* Define a data classification/filter tag for the field via `@gotags`
* Define a `custom_options.v1.generate_sdk_option` tag to change the SDK and add
API options (DefaultX, WithX functions).
* Define a data classification/filter tag for the field via `@gotags`
which specifies how sensitive/secret/public data
will be handled for the API's audit events.
See the [classification rubric](./classification-rubric.md)
@@ -482,6 +482,11 @@ func (c *Command) printListTable(items []*targets.Target) string {
fmt.Sprintf(" Description: %s", item.Description),
)
}
if item.Address != "" {
output = append(output,
fmt.Sprintf(" Address: %s", item.Address),
)
}
if len(item.AuthorizedActions) > 0 {
output = append(output,
" Authorized Actions:",
@@ -516,6 +521,9 @@ func printItemTable(item *targets.Target, resp *api.Response) string {
if item.Description != "" {
nonAttributeMap["Description"] = item.Description
}
if item.Address != "" {
nonAttributeMap["Address"] = item.Address
}
if item.WorkerFilter != "" {
nonAttributeMap["Worker Filter"] = item.WorkerFilter
}
+17 -2
View File
@@ -19,8 +19,8 @@ func init() {
func extraSshActionsFlagsMapFuncImpl() map[string][]string {
return map[string][]string{
"create": {"default-port", "session-max-seconds", "session-connection-limit", "egress-worker-filter", "ingress-worker-filter"},
"update": {"default-port", "session-max-seconds", "session-connection-limit", "worker-filter", "egress-worker-filter", "ingress-worker-filter"},
"create": {"address", "default-port", "session-max-seconds", "session-connection-limit", "egress-worker-filter", "ingress-worker-filter"},
"update": {"address", "default-port", "session-max-seconds", "session-connection-limit", "worker-filter", "egress-worker-filter", "ingress-worker-filter"},
}
}
@@ -31,6 +31,7 @@ type extraSshCmdVars struct {
flagWorkerFilter string
flagEgressWorkerFilter string
flagIngressWorkerFilter string
flagAddress string
}
func (c *SshCommand) extraSshHelpFunc(helpMap map[string]func() string) string {
@@ -66,6 +67,12 @@ func extraSshFlagsFuncImpl(c *SshCommand, set *base.FlagSets, f *base.FlagSet) {
for _, name := range flagsSshMap[c.Func] {
switch name {
case "address":
fs.StringVar(&base.StringVar{
Name: "address",
Target: &c.flagAddress,
Usage: "Optionally, a valid network address to connect to for this target. Can not be used alongside host sources.",
})
case "default-port":
fs.StringVar(&base.StringVar{
Name: "default-port",
@@ -188,6 +195,14 @@ func extraSshFlagsHandlingFuncImpl(c *SshCommand, _ *base.FlagSets, opts *[]targ
*opts = append(*opts, targets.WithIngressWorkerFilter(c.flagIngressWorkerFilter))
}
switch c.flagAddress {
case "":
case "null":
*opts = append(*opts, targets.DefaultAddress())
default:
*opts = append(*opts, targets.WithAddress(c.flagAddress))
}
return true
}
+17 -2
View File
@@ -18,8 +18,8 @@ func init() {
func extraTcpActionsFlagsMapFuncImpl() map[string][]string {
return map[string][]string{
"create": {"default-port", "session-max-seconds", "session-connection-limit", "egress-worker-filter", "ingress-worker-filter"},
"update": {"default-port", "session-max-seconds", "session-connection-limit", "worker-filter", "egress-worker-filter", "ingress-worker-filter"},
"create": {"address", "default-port", "session-max-seconds", "session-connection-limit", "egress-worker-filter", "ingress-worker-filter"},
"update": {"address", "default-port", "session-max-seconds", "session-connection-limit", "worker-filter", "egress-worker-filter", "ingress-worker-filter"},
}
}
@@ -30,6 +30,7 @@ type extraTcpCmdVars struct {
flagWorkerFilter string
flagEgressWorkerFilter string
flagIngressWorkerFilter string
flagAddress string
}
func (c *TcpCommand) extraTcpHelpFunc(helpMap map[string]func() string) string {
@@ -65,6 +66,12 @@ func extraTcpFlagsFuncImpl(c *TcpCommand, set *base.FlagSets, f *base.FlagSet) {
for _, name := range flagsTcpMap[c.Func] {
switch name {
case "address":
fs.StringVar(&base.StringVar{
Name: "address",
Target: &c.flagAddress,
Usage: "Optionally, a valid network address to connect to for this target. Can not be used alongside host sources.",
})
case "default-port":
fs.StringVar(&base.StringVar{
Name: "default-port",
@@ -187,5 +194,13 @@ func extraTcpFlagsHandlingFuncImpl(c *TcpCommand, _ *base.FlagSets, opts *[]targ
*opts = append(*opts, targets.WithIngressWorkerFilter(c.flagIngressWorkerFilter))
}
switch c.flagAddress {
case "":
case "null":
*opts = append(*opts, targets.DefaultAddress())
default:
*opts = append(*opts, targets.WithAddress(c.flagAddress))
}
return true
}
+4
View File
@@ -5710,6 +5710,10 @@
},
"description": "Output only. The available actions on this resource for this user.",
"readOnly": true
},
"address": {
"type": "string",
"description": "Optionally, a valid network address to connect to for this target. Can not be used alongside host sources."
}
},
"title": "Target contains all fields related to a Target resource"
@@ -211,6 +211,9 @@ message Target {
// Output only. The available actions on this resource for this user.
repeated string authorized_actions = 300 [json_name = "authorized_actions"]; // @gotags: `class:"public"`
// Optionally, a valid network address to connect to for this target. Can not be used alongside host sources.
string address = 540 [(custom_options.v1.generate_sdk_option) = true]; // @gotags: `class:"public"`
// Deprecated fields
reserved "application_credential_library_ids", "application_credential_libraries";
reserved 150, 180;
@@ -14,7 +14,8 @@ extend google.protobuf.FieldOptions {
MaskMapping mask_mapping = 85464;
// generate_sdk_option is a directive used when generating the SDK to
// indicate that an option should be created for the field
// indicate that an option should be created for the field. This also controls
// API options (DefaultX, WithX functions).
bool generate_sdk_option = 85412;
// subtype is used for oneOf attribute subtypes, to identify that we only need to parse attributes
+24
View File
@@ -1,6 +1,7 @@
package targets_test
import (
"context"
"fmt"
"net/http"
"strings"
@@ -379,6 +380,29 @@ func TestList(t *testing.T) {
assert.Equal(filterItem.Id, ul.Items[0].Id)
}
func TestCreateTarget_DirectlyAttachedAddress(t *testing.T) {
tc := controller.NewTestController(t, nil)
t.Cleanup(tc.Shutdown)
client := tc.Client()
at := tc.Token()
client.SetToken(at.Token)
_, proj := iam.TestScopes(t, tc.IamRepo(), iam.WithUserId(at.UserId))
addr := "127.0.0.1"
rsp, err := targets.NewClient(client).Create(context.Background(), "tcp", proj.PublicId,
targets.WithName("test_target"),
targets.WithTcpTargetDefaultPort(22),
targets.WithAddress(addr),
)
require.NoError(t, err)
require.NotNil(t, rsp)
require.NotNil(t, rsp.Item)
// TODO: Assert that the address has been persisted, when the actual
// functionality is implemented - See below.
// require.Equal(t, addr, rsp.Item.Address)
}
func comparableSlice(in []*targets.Target) []targets.Target {
var filtered []targets.Target
for _, i := range in {