Add multi connection parameters through targets and into session creation (#375)

* Update protos and gen

* Add new values to targets CLI command

* Lots more plumbing of new parameters through everything

* allow fields to be set to zero value.

* Disallow set but empty name/description strings in create/update verifiers

Co-authored-by: Jim Lambert <jlambert@hashicorp.com>
This commit is contained in:
Jeff MitchellandJim Lambert authored and GitHub committed 2020-09-19 10:31:49 -04:00
1 parent 07a7e9750a
commit 62baef1b7e
34 files changed
+725 -229

No files matched your search

@@ -11,6 +11,7 @@ import (
"github.com/golang/protobuf/ptypes/wrappers"
"github.com/hashicorp/boundary/internal/auth"
"github.com/hashicorp/boundary/internal/db"
"github.com/hashicorp/boundary/internal/db/timestamp"
pb "github.com/hashicorp/boundary/internal/gen/controller/api/resources/targets"
pbs "github.com/hashicorp/boundary/internal/gen/controller/api/services"
"github.com/hashicorp/boundary/internal/host"
@@ -27,6 +28,7 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/timestamppb"
"google.golang.org/protobuf/types/known/wrapperspb"
)
@@ -348,14 +350,19 @@ HOST_GATHERING_DONE:
endpointUrl.Host = endpointHost
}
expTime := timestamppb.Now()
expTime.Seconds += int64(t.GetSessionMaxDuration())
sessionComposition := session.ComposedOf{
UserId: authResults.UserId,
HostId: chosenId.hostId,
TargetId: t.GetPublicId(),
HostSetId: chosenId.hostSetId,
AuthTokenId: authResults.AuthTokenId,
ScopeId: authResults.Scope.Id,
Endpoint: endpointUrl.String(),
UserId: authResults.UserId,
HostId: chosenId.hostId,
TargetId: t.GetPublicId(),
HostSetId: chosenId.hostSetId,
AuthTokenId: authResults.AuthTokenId,
ScopeId: authResults.Scope.Id,
Endpoint: endpointUrl.String(),
ExpirationTime: &timestamp.Timestamp{Timestamp: expTime},
ConnectionLimit: t.GetSessionConnectionLimit(),
ConnectionIdleTimeoutSeconds: t.GetConnectionIdleTimeoutDuration(),
}
sess, err := session.New(sessionComposition)
@@ -428,6 +435,15 @@ func (s Service) createInRepo(ctx context.Context, item *pb.Target) (*pb.Target,
if item.GetDescription() != nil {
opts = append(opts, target.WithDescription(item.GetDescription().GetValue()))
}
if item.GetSessionMaxDuration() != nil {
opts = append(opts, target.WithSessionMaxDuration(item.GetSessionMaxDuration().GetValue()))
}
if item.GetSessionConnectionLimit() != nil {
opts = append(opts, target.WithSessionConnectionLimit(item.GetSessionConnectionLimit().GetValue()))
}
if item.GetConnectionIdleTimeoutDuration() != nil {
opts = append(opts, target.WithConnectionIdleTimeoutDuration(item.GetConnectionIdleTimeoutDuration().GetValue()))
}
tcpAttrs := &pb.TcpTargetAttributes{}
if err := handlers.StructToProto(item.GetAttributes(), tcpAttrs); err != nil {
return nil, status.Errorf(codes.InvalidArgument, "Provided attributes don't match expected format.")
@@ -461,7 +477,15 @@ func (s Service) updateInRepo(ctx context.Context, scopeId, id string, mask []st
if name := item.GetName(); name != nil {
opts = append(opts, target.WithName(name.GetValue()))
}
if item.GetSessionMaxDuration() != nil {
opts = append(opts, target.WithSessionMaxDuration(item.GetSessionMaxDuration().GetValue()))
}
if item.GetSessionConnectionLimit() != nil {
opts = append(opts, target.WithSessionConnectionLimit(item.GetSessionConnectionLimit().GetValue()))
}
if item.GetConnectionIdleTimeoutDuration() != nil {
opts = append(opts, target.WithConnectionIdleTimeoutDuration(item.GetConnectionIdleTimeoutDuration().GetValue()))
}
tcpAttrs := &pb.TcpTargetAttributes{}
if err := handlers.StructToProto(item.GetAttributes(), tcpAttrs); err != nil {
return nil, status.Errorf(codes.InvalidArgument, "Provided attributes don't match expected format.")
@@ -628,12 +652,15 @@ func (s Service) authResult(ctx context.Context, id string, a action.Type) auth.
func toProto(in target.Target, m []*target.TargetSet) (*pb.Target, error) {
out := pb.Target{
Id: in.GetPublicId(),
ScopeId: in.GetScopeId(),
CreatedTime: in.GetCreateTime().GetTimestamp(),
UpdatedTime: in.GetUpdateTime().GetTimestamp(),
Version: in.GetVersion(),
Type: target.TcpTargetType.String(),
Id: in.GetPublicId(),
ScopeId: in.GetScopeId(),
CreatedTime: in.GetCreateTime().GetTimestamp(),
UpdatedTime: in.GetUpdateTime().GetTimestamp(),
Version: in.GetVersion(),
Type: target.TcpTargetType.String(),
SessionMaxDuration: &wrapperspb.UInt32Value{Value: in.GetSessionMaxDuration()},
SessionConnectionLimit: &wrapperspb.UInt32Value{Value: in.GetSessionConnectionLimit()},
ConnectionIdleTimeoutDuration: &wrapperspb.UInt32Value{Value: in.GetConnectionIdleTimeoutDuration()},
}
if in.GetDescription() != "" {
out.Description = wrapperspb.String(in.GetDescription())
@@ -68,15 +68,18 @@ func TestGet(t *testing.T) {
tar := target.TestTcpTarget(t, conn, proj.GetPublicId(), "test", target.WithHostSets([]string{hs[0].GetPublicId(), hs[1].GetPublicId()}))
pTar := &pb.Target{
Id: tar.GetPublicId(),
ScopeId: proj.GetPublicId(),
Name: wrapperspb.String("test"),
CreatedTime: tar.CreateTime.GetTimestamp(),
UpdatedTime: tar.UpdateTime.GetTimestamp(),
Scope: &scopes.ScopeInfo{Id: proj.GetPublicId(), Type: scope.Project.String()},
Type: target.TcpTargetType.String(),
HostSetIds: []string{hs[0].GetPublicId(), hs[1].GetPublicId()},
Attributes: new(structpb.Struct),
Id: tar.GetPublicId(),
ScopeId: proj.GetPublicId(),
Name: wrapperspb.String("test"),
CreatedTime: tar.CreateTime.GetTimestamp(),
UpdatedTime: tar.UpdateTime.GetTimestamp(),
Scope: &scopes.ScopeInfo{Id: proj.GetPublicId(), Type: scope.Project.String()},
Type: target.TcpTargetType.String(),
HostSetIds: []string{hs[0].GetPublicId(), hs[1].GetPublicId()},
Attributes: new(structpb.Struct),
SessionMaxDuration: &wrapperspb.UInt32Value{Value: 28800},
ConnectionIdleTimeoutDuration: &wrapperspb.UInt32Value{Value: 3600},
SessionConnectionLimit: &wrapperspb.UInt32Value{Value: 1},
}
for _, ihs := range hs {
pTar.HostSets = append(pTar.HostSets, &pb.HostSet{Id: ihs.GetPublicId(), HostCatalogId: ihs.GetCatalogId()})
@@ -146,15 +149,18 @@ func TestList(t *testing.T) {
name := fmt.Sprintf("tar%d", i)
tar := target.TestTcpTarget(t, conn, proj.GetPublicId(), name, target.WithHostSets([]string{hss[0].GetPublicId(), hss[1].GetPublicId()}))
wantTars = append(wantTars, &pb.Target{
Id: tar.GetPublicId(),
ScopeId: proj.GetPublicId(),
Name: wrapperspb.String(name),
Scope: &scopes.ScopeInfo{Id: proj.GetPublicId(), Type: scope.Project.String()},
CreatedTime: tar.GetCreateTime().GetTimestamp(),
UpdatedTime: tar.GetUpdateTime().GetTimestamp(),
Version: tar.GetVersion(),
Type: target.TcpTargetType.String(),
Attributes: new(structpb.Struct),
Id: tar.GetPublicId(),
ScopeId: proj.GetPublicId(),
Name: wrapperspb.String(name),
Scope: &scopes.ScopeInfo{Id: proj.GetPublicId(), Type: scope.Project.String()},
CreatedTime: tar.GetCreateTime().GetTimestamp(),
UpdatedTime: tar.GetUpdateTime().GetTimestamp(),
Version: tar.GetVersion(),
Type: target.TcpTargetType.String(),
Attributes: new(structpb.Struct),
SessionMaxDuration: &wrapperspb.UInt32Value{Value: 28800},
ConnectionIdleTimeoutDuration: &wrapperspb.UInt32Value{Value: 3600},
SessionConnectionLimit: &wrapperspb.UInt32Value{Value: 1},
})
}
@@ -304,6 +310,9 @@ func TestCreate(t *testing.T) {
Attributes: &structpb.Struct{Fields: map[string]*structpb.Value{
"default_port": structpb.NewNumberValue(2),
}},
SessionMaxDuration: &wrapperspb.UInt32Value{Value: 28800},
ConnectionIdleTimeoutDuration: &wrapperspb.UInt32Value{Value: 3600},
SessionConnectionLimit: &wrapperspb.UInt32Value{Value: 1},
},
},
errCode: codes.OK,
@@ -446,11 +455,14 @@ func TestUpdate(t *testing.T) {
name: "Update an Existing Target",
req: &pbs.UpdateTargetRequest{
UpdateMask: &field_mask.FieldMask{
Paths: []string{"name", "description"},
Paths: []string{"name", "description", "session_max_duration", "connection_idle_timeout_duration", "session_connection_limit"},
},
Item: &pb.Target{
Name: wrapperspb.String("name"),
Description: wrapperspb.String("desc"),
Name: wrapperspb.String("name"),
Description: wrapperspb.String("desc"),
SessionMaxDuration: wrapperspb.UInt32(3600),
ConnectionIdleTimeoutDuration: wrapperspb.UInt32(180),
SessionConnectionLimit: wrapperspb.UInt32(5),
},
},
res: &pbs.UpdateTargetResponse{
@@ -464,9 +476,12 @@ func TestUpdate(t *testing.T) {
Attributes: &structpb.Struct{Fields: map[string]*structpb.Value{
"default_port": structpb.NewNumberValue(2),
}},
CreatedTime: tar.GetCreateTime().GetTimestamp(),
HostSetIds: hsIds,
HostSets: hostSets,
CreatedTime: tar.GetCreateTime().GetTimestamp(),
HostSetIds: hsIds,
HostSets: hostSets,
SessionMaxDuration: wrapperspb.UInt32(3600),
ConnectionIdleTimeoutDuration: wrapperspb.UInt32(180),
SessionConnectionLimit: wrapperspb.UInt32(5),
},
},
errCode: codes.OK,
@@ -494,8 +509,11 @@ func TestUpdate(t *testing.T) {
Attributes: &structpb.Struct{Fields: map[string]*structpb.Value{
"default_port": structpb.NewNumberValue(2),
}},
HostSetIds: hsIds,
HostSets: hostSets,
HostSetIds: hsIds,
HostSets: hostSets,
SessionMaxDuration: wrapperspb.UInt32(3600),
ConnectionIdleTimeoutDuration: wrapperspb.UInt32(180),
SessionConnectionLimit: wrapperspb.UInt32(5),
},
},
errCode: codes.OK,
@@ -577,8 +595,11 @@ func TestUpdate(t *testing.T) {
Attributes: &structpb.Struct{Fields: map[string]*structpb.Value{
"default_port": structpb.NewNumberValue(2),
}},
HostSetIds: hsIds,
HostSets: hostSets,
HostSetIds: hsIds,
HostSets: hostSets,
SessionMaxDuration: wrapperspb.UInt32(3600),
ConnectionIdleTimeoutDuration: wrapperspb.UInt32(180),
SessionConnectionLimit: wrapperspb.UInt32(5),
},
},
errCode: codes.OK,
@@ -606,8 +627,11 @@ func TestUpdate(t *testing.T) {
Attributes: &structpb.Struct{Fields: map[string]*structpb.Value{
"default_port": structpb.NewNumberValue(2),
}},
HostSetIds: hsIds,
HostSets: hostSets,
HostSetIds: hsIds,
HostSets: hostSets,
SessionMaxDuration: wrapperspb.UInt32(3600),
ConnectionIdleTimeoutDuration: wrapperspb.UInt32(180),
SessionConnectionLimit: wrapperspb.UInt32(5),
},
},
errCode: codes.OK,
@@ -634,9 +658,12 @@ func TestUpdate(t *testing.T) {
Attributes: &structpb.Struct{Fields: map[string]*structpb.Value{
"default_port": structpb.NewNumberValue(2),
}},
Type: target.TcpTargetType.String(),
HostSetIds: hsIds,
HostSets: hostSets,
Type: target.TcpTargetType.String(),
HostSetIds: hsIds,
HostSets: hostSets,
SessionMaxDuration: wrapperspb.UInt32(3600),
ConnectionIdleTimeoutDuration: wrapperspb.UInt32(180),
SessionConnectionLimit: wrapperspb.UInt32(5),
},
},
errCode: codes.OK,
@@ -29,6 +29,12 @@ func ValidateCreateRequest(i ApiResource, fn CustomValidatorFunc) error {
if i.GetId() != "" {
badFields["id"] = "This is a read only field."
}
if i.GetName() != nil {
i.GetName().Value = strings.TrimSpace(i.GetName().GetValue())
}
if i.GetDescription() != nil {
i.GetDescription().Value = strings.TrimSpace(i.GetDescription().GetValue())
}
if i.GetCreatedTime() != nil {
badFields["created_time"] = "This is a read only field."
}
@@ -66,6 +72,22 @@ func ValidateUpdateRequest(prefix string, r UpdateRequest, i ApiResource, fn Cus
// the mask will be marked as unset.
return nil
}
if i.GetName() != nil {
trimmed := strings.TrimSpace(i.GetName().GetValue())
if trimmed == "" {
badFields["name"] = "Cannot set empty string as name"
} else {
i.GetName().Value = trimmed
}
}
if i.GetDescription() != nil {
trimmed := strings.TrimSpace(i.GetDescription().GetValue())
if trimmed == "" {
badFields["name"] = "Cannot set empty string as description"
} else {
i.GetDescription().Value = trimmed
}
}
if i.GetVersion() == 0 {
badFields["version"] = "Existing resource version is required for an update."
}