2022-05-09 12:45:10 -07:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-02-02 17:15:17 -08:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
|
|
2021-11-04 11:44:22 -04:00
|
|
|
package tcp
|
|
|
|
|
|
|
|
|
|
import (
|
2023-05-25 15:52:03 -07:00
|
|
|
"context"
|
2023-04-21 09:34:59 -04:00
|
|
|
"math"
|
|
|
|
|
|
2021-11-04 11:44:22 -04:00
|
|
|
"github.com/golang/protobuf/ptypes/wrappers"
|
2022-05-09 12:45:10 -07:00
|
|
|
"github.com/hashicorp/boundary/internal/daemon/controller/handlers"
|
|
|
|
|
"github.com/hashicorp/boundary/internal/daemon/controller/handlers/targets"
|
2023-05-25 15:52:03 -07:00
|
|
|
"github.com/hashicorp/boundary/internal/session"
|
2021-11-04 11:44:22 -04:00
|
|
|
"github.com/hashicorp/boundary/internal/target"
|
2022-12-06 12:33:28 -08:00
|
|
|
"github.com/hashicorp/boundary/internal/target/store"
|
2023-11-02 16:03:47 -04:00
|
|
|
"github.com/hashicorp/boundary/internal/target/tcp"
|
2022-12-06 12:33:28 -08:00
|
|
|
tcpStore "github.com/hashicorp/boundary/internal/target/tcp/store"
|
2021-11-04 11:44:22 -04:00
|
|
|
pb "github.com/hashicorp/boundary/sdk/pbs/controller/api/resources/targets"
|
|
|
|
|
)
|
|
|
|
|
|
2023-04-21 09:34:59 -04:00
|
|
|
const (
|
|
|
|
|
defaultPortField = "attributes.default_port"
|
|
|
|
|
defaultClientPortField = "attributes.default_client_port"
|
|
|
|
|
)
|
2022-06-13 08:26:24 -07:00
|
|
|
|
2021-11-04 11:44:22 -04:00
|
|
|
type attribute struct {
|
|
|
|
|
*pb.TcpTargetAttributes
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *attribute) Options() []target.Option {
|
|
|
|
|
var opts []target.Option
|
|
|
|
|
if a.GetDefaultPort().GetValue() != 0 {
|
|
|
|
|
opts = append(opts, target.WithDefaultPort(a.GetDefaultPort().GetValue()))
|
|
|
|
|
}
|
2023-04-21 06:50:53 -04:00
|
|
|
if a.GetDefaultClientPort().GetValue() != 0 {
|
|
|
|
|
opts = append(opts, target.WithDefaultClientPort(a.GetDefaultClientPort().GetValue()))
|
|
|
|
|
}
|
2021-11-04 11:44:22 -04:00
|
|
|
return opts
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *attribute) Vet() map[string]string {
|
|
|
|
|
badFields := map[string]string{}
|
2022-06-13 08:26:24 -07:00
|
|
|
if a.GetDefaultPort() == nil {
|
2023-04-21 09:34:59 -04:00
|
|
|
badFields[defaultPortField] = "This field is required."
|
|
|
|
|
} else {
|
|
|
|
|
if a.GetDefaultPort().GetValue() == 0 {
|
|
|
|
|
badFields[defaultPortField] = "This field cannot be set to zero."
|
|
|
|
|
}
|
|
|
|
|
if a.GetDefaultPort().GetValue() > math.MaxUint16 {
|
|
|
|
|
badFields[defaultPortField] = "Value is greater than maximum port number."
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if a.GetDefaultClientPort() != nil {
|
|
|
|
|
if a.GetDefaultClientPort().GetValue() == 0 {
|
|
|
|
|
badFields[defaultClientPortField] = "This field cannot be set to zero."
|
|
|
|
|
}
|
|
|
|
|
if a.GetDefaultClientPort().GetValue() > math.MaxUint16 {
|
|
|
|
|
badFields[defaultClientPortField] = "Value is greater than maximum port number."
|
|
|
|
|
}
|
2022-06-13 08:26:24 -07:00
|
|
|
}
|
|
|
|
|
return badFields
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *attribute) VetForUpdate(p []string) map[string]string {
|
|
|
|
|
badFields := map[string]string{}
|
2023-04-21 09:34:59 -04:00
|
|
|
if handlers.MaskContains(p, defaultPortField) {
|
|
|
|
|
if a.GetDefaultPort() == nil {
|
|
|
|
|
badFields[defaultPortField] = "This field is required."
|
|
|
|
|
} else {
|
|
|
|
|
if a.GetDefaultPort().GetValue() == 0 {
|
|
|
|
|
badFields[defaultPortField] = "This cannot be set to zero."
|
|
|
|
|
}
|
|
|
|
|
if a.GetDefaultPort().GetValue() > math.MaxUint16 {
|
|
|
|
|
badFields[defaultPortField] = "Value is greater than maximum port number."
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if handlers.MaskContains(p, defaultClientPortField) && a.GetDefaultClientPort() != nil {
|
|
|
|
|
if a.GetDefaultClientPort().GetValue() == 0 {
|
|
|
|
|
badFields[defaultClientPortField] = "This cannot be set to zero."
|
|
|
|
|
}
|
|
|
|
|
if a.GetDefaultClientPort().GetValue() > math.MaxUint16 {
|
|
|
|
|
badFields[defaultClientPortField] = "Value is greater than maximum port number."
|
|
|
|
|
}
|
2021-11-04 11:44:22 -04:00
|
|
|
}
|
|
|
|
|
return badFields
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-25 18:29:38 -05:00
|
|
|
func newAttribute(m any) targets.Attributes {
|
2021-11-04 11:44:22 -04:00
|
|
|
a := &attribute{
|
|
|
|
|
&pb.TcpTargetAttributes{},
|
|
|
|
|
}
|
2022-04-13 16:27:48 -04:00
|
|
|
if tcpAttr, ok := m.(*pb.Target_TcpTargetAttributes); ok {
|
|
|
|
|
a.TcpTargetAttributes = tcpAttr.TcpTargetAttributes
|
2021-11-04 11:44:22 -04:00
|
|
|
}
|
|
|
|
|
return a
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-13 16:27:48 -04:00
|
|
|
func setAttributes(t target.Target, out *pb.Target) error {
|
|
|
|
|
if t == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
attrs := &pb.Target_TcpTargetAttributes{
|
|
|
|
|
TcpTargetAttributes: &pb.TcpTargetAttributes{},
|
|
|
|
|
}
|
|
|
|
|
if t.GetDefaultPort() > 0 {
|
|
|
|
|
attrs.TcpTargetAttributes.DefaultPort = &wrappers.UInt32Value{Value: t.GetDefaultPort()}
|
|
|
|
|
}
|
2023-04-21 06:50:53 -04:00
|
|
|
if t.GetDefaultClientPort() > 0 {
|
|
|
|
|
attrs.TcpTargetAttributes.DefaultClientPort = &wrappers.UInt32Value{Value: t.GetDefaultClientPort()}
|
|
|
|
|
}
|
2022-04-13 16:27:48 -04:00
|
|
|
|
|
|
|
|
out.Attrs = attrs
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-25 15:52:03 -07:00
|
|
|
func noopSessionValidation(context.Context, *session.Session) error { return nil }
|
|
|
|
|
|
2021-11-04 11:44:22 -04:00
|
|
|
func init() {
|
|
|
|
|
var maskManager handlers.MaskManager
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
|
|
if maskManager, err = handlers.NewMaskManager(
|
2023-06-06 17:09:08 -04:00
|
|
|
context.Background(),
|
2022-12-06 12:33:28 -08:00
|
|
|
handlers.MaskDestination{&tcpStore.Target{}, &store.TargetAddress{}},
|
2021-11-04 11:44:22 -04:00
|
|
|
handlers.MaskSource{&pb.Target{}, &pb.TcpTargetAttributes{}},
|
|
|
|
|
); err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-02 16:03:47 -04:00
|
|
|
targets.Register(tcp.Subtype, maskManager, newAttribute, setAttributes, noopSessionValidation)
|
2021-11-04 11:44:22 -04:00
|
|
|
}
|