2020-08-26 13:58:46 -04:00
// Copyright (c) HashiCorp, Inc.
2022-12-06 12:28:55 -08:00
// SPDX-License-Identifier: BUSL-1.1
2023-02-02 17:15:17 -08:00
2021-09-29 13:13:49 -04:00
package tcp_test
2020-08-26 13:58:46 -04:00
import (
"context"
"testing"
"time"
2023-03-10 15:06:39 -05:00
"github.com/hashicorp/boundary/globals"
2024-03-12 13:04:20 -07:00
talias "github.com/hashicorp/boundary/internal/alias/target"
2021-10-26 09:57:22 -04:00
"github.com/hashicorp/boundary/internal/credential"
2021-06-22 22:26:26 -04:00
"github.com/hashicorp/boundary/internal/credential/vault"
2020-08-26 13:58:46 -04:00
"github.com/hashicorp/boundary/internal/db"
dbassert "github.com/hashicorp/boundary/internal/db/assert"
2020-11-05 16:54:03 -05:00
"github.com/hashicorp/boundary/internal/errors"
2020-08-26 13:58:46 -04:00
"github.com/hashicorp/boundary/internal/host/static"
"github.com/hashicorp/boundary/internal/iam"
"github.com/hashicorp/boundary/internal/kms"
"github.com/hashicorp/boundary/internal/oplog"
2021-09-29 13:13:49 -04:00
"github.com/hashicorp/boundary/internal/target"
2021-10-26 09:57:22 -04:00
"github.com/hashicorp/boundary/internal/target/store"
2021-09-29 13:13:49 -04:00
"github.com/hashicorp/boundary/internal/target/tcp"
2020-08-26 13:58:46 -04:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
)
2021-09-29 13:13:49 -04:00
func TestRepository_CreateTarget ( t * testing . T ) {
2020-08-26 13:58:46 -04:00
t . Parallel ()
conn , _ := db . TestSetup ( t , "postgres" )
rw := db . New ( conn )
wrapper := db . TestWrapper ( t )
testKms := kms . TestKms ( t , conn , wrapper )
2022-08-23 18:44:10 +01:00
ctx := context . Background ()
repo , err := target . NewRepository ( ctx , rw , rw , testKms )
2020-08-26 13:58:46 -04:00
require . NoError ( t , err )
_ , proj := iam . TestScopes ( t , iam . TestRepo ( t , conn , wrapper ))
cats := static . TestCatalogs ( t , conn , proj . PublicId , 1 )
2022-07-19 15:16:24 -07:00
static . TestSets ( t , conn , cats [ 0 ]. GetPublicId (), 2 )
2020-08-26 13:58:46 -04:00
2021-06-22 22:26:26 -04:00
cs := vault . TestCredentialStores ( t , conn , wrapper , proj . GetPublicId (), 1 )[ 0 ]
2022-07-19 15:16:24 -07:00
vault . TestCredentialLibraries ( t , conn , wrapper , cs . GetPublicId (), 2 )
2021-06-22 22:26:26 -04:00
2020-08-26 13:58:46 -04:00
type args struct {
2021-11-04 11:37:32 -04:00
target target . Target
2021-09-29 13:13:49 -04:00
opt [] target . Option
2020-08-26 13:58:46 -04:00
}
tests := [] struct {
2022-07-19 15:16:24 -07:00
name string
args args
wantErr bool
2023-02-02 18:51:39 +00:00
wantAddress string
2022-07-19 15:16:24 -07:00
wantIsError errors . Code
2020-08-26 13:58:46 -04:00
}{
{
name : "valid-org" ,
args : args {
2021-11-04 11:37:32 -04:00
target : func () target . Target {
2023-11-02 16:03:47 -04:00
target , err := target . New ( ctx , tcp . Subtype , proj . PublicId ,
2021-09-29 13:13:49 -04:00
target . WithName ( "valid-org" ),
target . WithDescription ( "valid-org" ),
target . WithDefaultPort ( uint32 ( 22 )))
2020-08-26 13:58:46 -04:00
require . NoError ( t , err )
return target
}(),
2021-06-22 22:26:26 -04:00
},
2022-07-19 15:16:24 -07:00
wantErr : false ,
2021-06-22 22:26:26 -04:00
},
2024-11-01 13:27:40 -04:00
{
name : "with-dns-name" ,
args : args {
target : func () * tcp . Target {
target , err := target . New ( ctx , tcp . Subtype , proj . PublicId ,
target . WithName ( "with-dns-name" ),
target . WithDescription ( "with-dns-name" ),
target . WithDefaultPort ( uint32 ( 22 )),
target . WithAddress ( "www.google.com" ),
)
require . NoError ( t , err )
return target .( * tcp . Target )
}(),
},
wantErr : false ,
wantAddress : "www.google.com" ,
},
2022-12-06 12:28:55 -08:00
{
2024-09-05 10:30:30 -07:00
name : "with-ipv4-address" ,
2022-12-06 12:28:55 -08:00
args : args {
target : func () target . Target {
2023-11-02 16:03:47 -04:00
target , err := target . New ( ctx , tcp . Subtype , proj . PublicId ,
2024-09-05 10:30:30 -07:00
target . WithName ( "with-ipv4-address" ),
target . WithDescription ( "with-ipv4-address" ),
2022-12-06 12:28:55 -08:00
target . WithDefaultPort ( 80 ),
target . WithAddress ( "8.8.8.8" ))
require . NoError ( t , err )
return target
}(),
},
2024-11-01 13:27:40 -04:00
wantErr : false ,
wantAddress : "8.8.8.8" ,
},
{
name : "with-invalid-ipv4-address-with-port" ,
args : args {
target : func () target . Target {
target , err := target . New ( ctx , tcp . Subtype , proj . PublicId ,
target . WithName ( "with-invalid-ipv4-address-with-port" ),
target . WithDescription ( "with-invalid-ipv4-address-with-port" ),
target . WithDefaultPort ( 80 ),
target . WithAddress ( "8.8.8.8:80" ))
require . NoError ( t , err )
return target
}(),
},
wantErr : true ,
wantIsError : errors . InvalidAddress ,
2022-12-06 12:28:55 -08:00
},
2024-09-05 10:30:30 -07:00
{
name : "with-abbreviated-ipv6-address" ,
args : args {
target : func () target . Target {
target , err := target . New ( ctx , tcp . Subtype , proj . PublicId ,
target . WithName ( "with-abbreviated-ipv6-address" ),
target . WithDescription ( "with-abbreviated-ipv6-address" ),
target . WithDefaultPort ( 80 ),
target . WithAddress ( "2001:4860:4860::8888" ))
require . NoError ( t , err )
return target
}(),
},
2024-11-01 13:27:40 -04:00
wantErr : false ,
wantAddress : "2001:4860:4860::8888" ,
2024-09-05 10:30:30 -07:00
},
{
name : "with-ipv6-address" ,
args : args {
target : func () target . Target {
target , err := target . New ( ctx , tcp . Subtype , proj . PublicId ,
target . WithName ( "with-ipv6-address" ),
target . WithDescription ( "with-ipv6-address" ),
target . WithDefaultPort ( 80 ),
target . WithAddress ( "2001:4860:4860:0:0:0:0:8888" ))
require . NoError ( t , err )
return target
}(),
},
2024-11-01 13:27:40 -04:00
wantErr : false ,
wantAddress : "2001:4860:4860::8888" ,
},
{
name : "with-abbreviated-[ipv6]-address" ,
args : args {
target : func () target . Target {
target , err := target . New ( ctx , tcp . Subtype , proj . PublicId ,
target . WithName ( "with-abbreviated-[ipv6]-address" ),
target . WithDescription ( "with-abbreviated-[ipv6]-address" ),
target . WithDefaultPort ( 80 ),
target . WithAddress ( "[2001:4860:4860::8888]" ))
require . NoError ( t , err )
return target
}(),
},
wantErr : false ,
wantAddress : "2001:4860:4860::8888" ,
},
{
name : "with-invalid-abbreviated-[ipv6]-address-with-port" ,
args : args {
target : func () target . Target {
target , err := target . New ( ctx , tcp . Subtype , proj . PublicId ,
target . WithName ( "with-invalid-abbreviated-[ipv6]-address-with-port" ),
target . WithDescription ( "with-invalid-abbreviated-[ipv6]-address-with-port" ),
target . WithDefaultPort ( 80 ),
target . WithAddress ( "[2001:4860:4860::8888]:80" ))
require . NoError ( t , err )
return target
}(),
},
wantErr : true ,
wantIsError : errors . InvalidAddress ,
},
{
name : "with-[ipv6]-address" ,
args : args {
target : func () target . Target {
target , err := target . New ( ctx , tcp . Subtype , proj . PublicId ,
target . WithName ( "with-[ipv6]-address" ),
target . WithDescription ( "with-[ipv6]-address" ),
target . WithDefaultPort ( 80 ),
target . WithAddress ( "[2001:4860:4860:0:0:0:0:8888]" ))
require . NoError ( t , err )
return target
}(),
},
wantErr : false ,
wantAddress : "2001:4860:4860:0:0:0:0:8888" ,
},
{
name : "with-invalid-[ipv6]-address-with-port" ,
args : args {
target : func () target . Target {
target , err := target . New ( ctx , tcp . Subtype , proj . PublicId ,
target . WithName ( "with-invalid-[ipv6]-address-with-port" ),
target . WithDescription ( "with-invalid-[ipv6]-address-with-port" ),
target . WithDefaultPort ( 80 ),
target . WithAddress ( "[2001:4860:4860:0:0:0:0:8888]:80" ))
require . NoError ( t , err )
return target
}(),
},
wantErr : true ,
wantIsError : errors . InvalidAddress ,
2024-09-05 10:30:30 -07:00
},
2023-02-02 18:51:39 +00:00
{
name : "with-address-whitespace" ,
args : args {
target : func () target . Target {
2023-11-02 16:03:47 -04:00
target , err := target . New ( ctx , tcp . Subtype , proj . PublicId ,
2023-02-02 18:51:39 +00:00
target . WithName ( "with-address-whitespace" ),
target . WithDescription ( "with-address-whitespace" ),
target . WithDefaultPort ( 80 ),
target . WithAddress ( " 8.8.8.8 " ))
require . NoError ( t , err )
return target
}(),
},
wantErr : false ,
wantAddress : "8.8.8.8" ,
},
2020-08-26 13:58:46 -04:00
{
name : "nil-target" ,
args : args {
target : nil ,
},
wantErr : true ,
2021-01-22 13:46:20 -08:00
wantIsError : errors . InvalidParameter ,
2020-08-26 13:58:46 -04:00
},
{
name : "nil-target-store" ,
args : args {
2021-09-29 13:13:49 -04:00
target : func () * tcp . Target {
target := & tcp . Target {}
2020-08-26 13:58:46 -04:00
return target
}(),
},
wantErr : true ,
2021-01-22 13:46:20 -08:00
wantIsError : errors . InvalidParameter ,
2020-08-26 13:58:46 -04:00
},
{
name : "public-id-not-empty" ,
args : args {
2021-11-04 11:37:32 -04:00
target : func () target . Target {
tar , err := target . New (
ctx ,
2023-11-02 16:03:47 -04:00
tcp . Subtype ,
2021-09-29 13:13:49 -04:00
proj . PublicId ,
target . WithName ( "valid-org" ),
target . WithDescription ( "valid-org" ),
target . WithDefaultPort ( uint32 ( 22 )),
)
2020-08-26 13:58:46 -04:00
require . NoError ( t , err )
2023-06-06 17:09:08 -04:00
id , err := db . NewPublicId ( ctx , globals . TcpTargetPrefix )
2020-08-26 13:58:46 -04:00
require . NoError ( t , err )
2021-11-04 11:37:32 -04:00
tar . SetPublicId ( ctx , id )
2021-09-29 13:13:49 -04:00
return tar
2020-08-26 13:58:46 -04:00
}(),
},
wantErr : true ,
2021-01-22 13:46:20 -08:00
wantIsError : errors . InvalidParameter ,
2020-08-26 13:58:46 -04:00
},
{
2022-07-20 16:24:46 -07:00
name : "empty-project-id" ,
2020-08-26 13:58:46 -04:00
args : args {
2021-11-04 11:37:32 -04:00
target : func () target . Target {
tar , err := target . New (
ctx ,
2023-11-02 16:03:47 -04:00
tcp . Subtype ,
2021-09-29 13:13:49 -04:00
proj . PublicId ,
2022-07-20 16:24:46 -07:00
target . WithName ( "empty-project-id" ),
2021-09-29 13:13:49 -04:00
)
2020-08-26 13:58:46 -04:00
require . NoError ( t , err )
2022-07-20 16:24:46 -07:00
tar . SetProjectId ( "" )
2021-09-29 13:13:49 -04:00
return tar
2020-08-26 13:58:46 -04:00
}(),
},
wantErr : true ,
2021-01-22 13:46:20 -08:00
wantIsError : errors . InvalidParameter ,
2020-08-26 13:58:46 -04:00
},
2022-11-30 15:23:37 -05:00
{
name : "valid-with-egress-filter" ,
args : args {
target : func () target . Target {
2023-11-02 16:03:47 -04:00
target , err := target . New ( ctx , tcp . Subtype , proj . PublicId ,
2022-11-30 15:23:37 -05:00
target . WithName ( "valid-egress-filter" ),
target . WithDescription ( "valid-org" ),
target . WithDefaultPort ( uint32 ( 22 )),
target . WithEgressWorkerFilter ( "test-filter" ))
require . NoError ( t , err )
return target
}(),
},
wantErr : false ,
},
{
name : "deprecated-worker-filter" ,
args : args {
target : func () target . Target {
2023-11-02 16:03:47 -04:00
target , err := target . New ( ctx , tcp . Subtype , proj . PublicId ,
2022-11-30 15:23:37 -05:00
target . WithName ( "bad-worker-filter" ),
target . WithDescription ( "valid-org" ),
target . WithDefaultPort ( uint32 ( 22 )),
target . WithWorkerFilter ( "test-filter" ))
require . NoError ( t , err )
return target
}(),
},
wantErr : true ,
wantIsError : errors . Exception ,
},
{
name : "invalid-setting-egress-and-worker-filter" ,
args : args {
target : func () target . Target {
2023-11-02 16:03:47 -04:00
target , err := target . New ( ctx , tcp . Subtype , proj . PublicId ,
2022-11-30 15:23:37 -05:00
target . WithName ( "bad-filters" ),
target . WithDescription ( "valid-org" ),
target . WithDefaultPort ( uint32 ( 22 )),
target . WithWorkerFilter ( "test-filter" ),
target . WithEgressWorkerFilter ( "test-filter" ))
require . NoError ( t , err )
return target
}(),
},
wantErr : true ,
wantIsError : errors . Exception ,
},
2020-08-26 13:58:46 -04:00
}
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
assert , require := assert . New ( t ), require . New ( t )
2023-03-30 15:19:30 -04:00
tar , err := repo . CreateTarget ( context . Background (), tt . args . target , tt . args . opt ... )
2020-08-26 13:58:46 -04:00
if tt . wantErr {
assert . Error ( err )
2021-09-29 13:13:49 -04:00
assert . Nil ( tar )
2021-01-22 13:46:20 -08:00
assert . True ( errors . Match ( errors . T ( tt . wantIsError ), err ))
2020-08-26 13:58:46 -04:00
return
}
require . NoError ( err )
2021-09-29 13:13:49 -04:00
assert . NotNil ( tar . GetPublicId ())
2021-06-22 22:26:26 -04:00
2023-03-30 15:19:30 -04:00
hostSources := tar . GetHostSources ()
credSources := tar . GetCredentialSources ()
foundTarget , err := repo . LookupTarget ( context . Background (), tar . GetPublicId ())
foundHostSources := foundTarget . GetHostSources ()
foundCredLibs := foundTarget . GetCredentialSources ()
2020-08-26 13:58:46 -04:00
assert . NoError ( err )
2023-02-02 18:51:39 +00:00
if len ( tt . wantAddress ) != 0 {
assert . Equal ( tt . wantAddress , tar . GetAddress ())
} else {
assert . Equal ( tt . args . target . GetAddress (), tar . GetAddress ())
}
2021-09-29 13:13:49 -04:00
assert . True ( proto . Equal ( tar .( * tcp . Target ), foundTarget .( * tcp . Target )))
2021-07-30 08:48:03 -04:00
assert . Equal ( hostSources , foundHostSources )
2021-07-27 12:48:36 -04:00
assert . Equal ( credSources , foundCredLibs )
2020-08-26 13:58:46 -04:00
2021-09-29 13:13:49 -04:00
err = db . TestVerifyOplog ( t , rw , tar . GetPublicId (), db . WithOperation ( oplog . OpType_OP_TYPE_CREATE ), db . WithCreateNotBefore ( 10 * time . Second ))
2020-08-26 13:58:46 -04:00
assert . NoError ( err )
// TODO (jimlambrt 9/2020) - unfortunately, we can currently
// test to make sure that the oplog entry for a target host sets
// create exist because the db.TestVerifyOplog doesn't really
// support that level of testing and the previous call to
2021-09-29 13:13:49 -04:00
// CreateTarget would create an oplog entry for the
2020-08-26 13:58:46 -04:00
// create on the target even if no host sets were added. Once
// TestVerifyOplog supports the appropriate granularity, we should
// add an appropriate assert.
})
}
2024-03-12 13:04:20 -07:00
t . Run ( "create with aliases" , func ( t * testing . T ) {
newAlias1 , err := talias . NewAlias ( context . Background (), "global" , "alias1" , talias . WithHostId ( "hst_1234567890" ))
require . NoError ( t , err )
newAlias2 , err := talias . NewAlias ( context . Background (), "global" , "alias2" , talias . WithHostId ( "hst_0987654321" ))
require . NoError ( t , err )
tar , err := target . New ( ctx , tcp . Subtype , proj . PublicId ,
target . WithName ( "create-with-alias" ),
target . WithDescription ( "create-with-alias" ),
target . WithDefaultPort ( uint32 ( 22 )))
require . NoError ( t , err )
tar , err = repo . CreateTarget ( context . Background (), tar , target . WithAliases ([] * talias . Alias { newAlias1 , newAlias2 }))
require . NoError ( t , err )
assert . NotNil ( t , tar )
assert . Len ( t , tar . GetAliases (), 2 )
assert . NotEmpty ( t , tar . GetAliases ()[ 0 ]. GetPublicId ())
assert . NotEmpty ( t , tar . GetAliases ()[ 1 ]. GetPublicId ())
assert . NotZero ( t , tar . GetAliases ()[ 0 ]. GetCreateTime ())
assert . NotZero ( t , tar . GetAliases ()[ 1 ]. GetCreateTime ())
assert . Contains ( t , [] string { newAlias1 . GetValue (), newAlias2 . GetValue ()}, tar . GetAliases ()[ 0 ]. GetValue ())
assert . Contains ( t , [] string { newAlias1 . GetValue (), newAlias2 . GetValue ()}, tar . GetAliases ()[ 1 ]. GetValue ())
assert . Contains ( t , [] string { newAlias1 . GetHostId (), newAlias2 . GetHostId ()}, tar . GetAliases ()[ 0 ]. GetHostId ())
assert . Contains ( t , [] string { newAlias1 . GetHostId (), newAlias2 . GetHostId ()}, tar . GetAliases ()[ 1 ]. GetHostId ())
})
t . Run ( "create an invalid aliases" , func ( t * testing . T ) {
invalidAlias , err := talias . NewAlias ( context . Background (), "global" , "invalid_alias" )
require . NoError ( t , err )
tar , err := target . New ( ctx , tcp . Subtype , proj . PublicId ,
target . WithName ( "create-with-invalid-alias" ),
target . WithDescription ( "create-with-invalid-alias" ),
target . WithDefaultPort ( uint32 ( 22 )))
require . NoError ( t , err )
tar , err = repo . CreateTarget ( context . Background (), tar , target . WithAliases ([] * talias . Alias { invalidAlias }))
require . Error ( t , err )
require . Nil ( t , tar )
})
t . Run ( "create with duplicate aliases" , func ( t * testing . T ) {
dupAlias , err := talias . NewAlias ( context . Background (), "global" , "dup-alias" )
require . NoError ( t , err )
tar , err := target . New ( ctx , tcp . Subtype , proj . PublicId ,
target . WithName ( "create-with-duplicate-alias" ),
target . WithDescription ( "create-with-duplicate-alias" ),
target . WithDefaultPort ( uint32 ( 22 )))
require . NoError ( t , err )
tar , err = repo . CreateTarget ( context . Background (), tar , target . WithAliases ([] * talias . Alias { dupAlias , dupAlias }))
require . Error ( t , err )
require . Nil ( t , tar )
})
2020-08-26 13:58:46 -04:00
}
func TestRepository_UpdateTcpTarget ( t * testing . T ) {
t . Parallel ()
conn , _ := db . TestSetup ( t , "postgres" )
rw := db . New ( conn )
wrapper := db . TestWrapper ( t )
testKms := kms . TestKms ( t , conn , wrapper )
2022-08-23 18:44:10 +01:00
repo , err := target . NewRepository ( context . Background (), rw , rw , testKms )
2020-08-26 13:58:46 -04:00
require . NoError ( t , err )
2021-09-29 13:13:49 -04:00
id := tcp . TestId ( t )
2020-08-26 13:58:46 -04:00
iamRepo := iam . TestRepo ( t , conn , wrapper )
_ , proj := iam . TestScopes ( t , iamRepo )
pubId := func ( s string ) * string { return & s }
type args struct {
name string
description string
2022-12-06 12:28:55 -08:00
address string
2020-08-26 13:58:46 -04:00
port uint32
fieldMaskPaths [] string
2021-09-29 13:13:49 -04:00
opt [] target . Option
2022-07-20 16:24:46 -07:00
ProjectId string
2020-08-26 13:58:46 -04:00
PublicId * string
}
tests := [] struct {
2022-12-06 12:28:55 -08:00
name string
newProjectId string
newName string
newTargetOpts [] target . Option
args args
wantRowsUpdate int
wantErr bool
wantErrMsg string
wantIsError errors . Code
wantDup bool
wantHostSources bool
2023-02-02 18:51:39 +00:00
wantAddress string
2020-08-26 13:58:46 -04:00
}{
{
name : "valid" ,
args : args {
name : "valid" + id ,
fieldMaskPaths : [] string { "Name" },
2022-07-20 16:24:46 -07:00
ProjectId : proj . PublicId ,
2020-08-26 13:58:46 -04:00
},
2022-12-06 12:28:55 -08:00
newProjectId : proj . PublicId ,
wantErr : false ,
wantRowsUpdate : 1 ,
wantHostSources : true ,
},
{
2024-09-05 10:30:30 -07:00
name : "valid-ipv4-address" ,
2022-12-06 12:28:55 -08:00
args : args {
2024-09-05 10:30:30 -07:00
name : "valid-ipv4-address" + id ,
2022-12-06 12:28:55 -08:00
fieldMaskPaths : [] string { "Name" , "Address" },
ProjectId : proj . PublicId ,
address : "8.8.8.8" ,
},
newProjectId : proj . PublicId ,
wantErr : false ,
wantRowsUpdate : 1 ,
wantHostSources : false ,
2024-11-01 13:27:40 -04:00
wantAddress : "8.8.8.8" ,
},
{
name : "invalid-ipv4-address-with-port" ,
args : args {
name : "invalid-ipv4-address-with-port" + id ,
fieldMaskPaths : [] string { "Name" , "Address" },
ProjectId : proj . PublicId ,
address : "8.8.8.8:80" ,
},
newProjectId : proj . PublicId ,
wantErr : true ,
wantIsError : errors . InvalidAddress ,
wantErrMsg : "invalid address" ,
2022-12-06 12:28:55 -08:00
},
2024-09-05 10:30:30 -07:00
{
name : "valid-abbreviated-ipv6-address" ,
args : args {
name : "valid-abbreviated-ipv6-address" + id ,
fieldMaskPaths : [] string { "Name" , "Address" },
ProjectId : proj . PublicId ,
address : "2001:4860:4860::8888" ,
},
newProjectId : proj . PublicId ,
wantErr : false ,
wantRowsUpdate : 1 ,
wantHostSources : false ,
2024-11-01 13:27:40 -04:00
wantAddress : "2001:4860:4860::8888" ,
2024-09-05 10:30:30 -07:00
},
{
name : "valid-ipv6-address" ,
args : args {
name : "valid-ipv6-address" + id ,
fieldMaskPaths : [] string { "Name" , "Address" },
ProjectId : proj . PublicId ,
address : "2001:4860:4860:0:0:0:0:8888" ,
},
newProjectId : proj . PublicId ,
wantErr : false ,
wantRowsUpdate : 1 ,
wantHostSources : false ,
2024-11-01 13:27:40 -04:00
wantAddress : "2001:4860:4860::8888" ,
},
{
name : "valid-abbreviated-[ipv6]-address" ,
args : args {
name : "valid-abbreviated-[ipv6]-address" + id ,
fieldMaskPaths : [] string { "Name" , "Address" },
ProjectId : proj . PublicId ,
address : "[2001:4860:4860::8888]" ,
},
newProjectId : proj . PublicId ,
wantErr : false ,
wantRowsUpdate : 1 ,
wantHostSources : false ,
wantAddress : "2001:4860:4860::8888" ,
},
{
name : "invalid-abbreviated-[ipv6]-address-with-port" ,
args : args {
name : "invalid-abbreviated-[ipv6]-address-with-port" + id ,
fieldMaskPaths : [] string { "Name" , "Address" },
ProjectId : proj . PublicId ,
address : "[2001:4860:4860::8888]:80" ,
},
newProjectId : proj . PublicId ,
wantErr : true ,
wantIsError : errors . InvalidAddress ,
wantErrMsg : "invalid address" ,
},
{
name : "valid-[ipv6]-address" ,
args : args {
name : "valid-[ipv6]-address" + id ,
fieldMaskPaths : [] string { "Name" , "Address" },
ProjectId : proj . PublicId ,
address : "[2001:4860:4860:0:0:0:0:8888]" ,
},
newProjectId : proj . PublicId ,
wantErr : false ,
wantRowsUpdate : 1 ,
wantHostSources : false ,
wantAddress : "2001:4860:4860:0:0:0:0:8888" ,
},
{
name : "invalid-[ipv6]-address-with-port" ,
args : args {
name : "invalid-[ipv6]-address-with-port" + id ,
fieldMaskPaths : [] string { "Name" , "Address" },
ProjectId : proj . PublicId ,
address : "[2001:4860:4860:0:0:0:0:8888]:80" ,
},
newProjectId : proj . PublicId ,
wantErr : true ,
wantIsError : errors . InvalidAddress ,
wantErrMsg : "invalid address" ,
2024-09-05 10:30:30 -07:00
},
2022-12-06 12:28:55 -08:00
{
name : "null-address" ,
args : args {
fieldMaskPaths : [] string { "Address" },
ProjectId : proj . PublicId ,
address : "null" ,
},
newProjectId : proj . PublicId ,
newTargetOpts : [] target . Option { target . WithAddress ( "8.8.8.8" )},
wantErr : false ,
wantRowsUpdate : 1 ,
wantHostSources : false ,
},
2023-02-02 18:51:39 +00:00
{
name : "address-with-whitespace" ,
args : args {
fieldMaskPaths : [] string { "Address" },
ProjectId : proj . PublicId ,
address : " 127.0.0.1 " ,
},
newProjectId : proj . PublicId ,
newTargetOpts : [] target . Option { target . WithAddress ( "10.0.0.1" )},
wantErr : false ,
wantRowsUpdate : 1 ,
wantHostSources : false ,
wantAddress : "127.0.0.1" ,
},
2022-12-06 12:28:55 -08:00
{
name : "delete-address" ,
args : args {
fieldMaskPaths : [] string { "Address" },
ProjectId : proj . PublicId ,
},
newProjectId : proj . PublicId ,
newTargetOpts : [] target . Option { target . WithAddress ( "8.8.8.8" )},
wantErr : false ,
wantRowsUpdate : 1 ,
wantHostSources : false ,
},
{
name : "host-source-mutually-exclusive-relationship" ,
args : args {
name : "invalid-address" + id ,
address : "8.8.8.8" ,
fieldMaskPaths : [] string { "Name" , "Address" },
ProjectId : proj . PublicId ,
},
newProjectId : proj . PublicId ,
wantErr : true ,
wantErrMsg : "unable to set address because one or more host sources is assigned to the given target" ,
wantRowsUpdate : 0 ,
wantHostSources : true ,
2020-08-26 13:58:46 -04:00
},
{
name : "valid-no-op" ,
args : args {
name : "valid-no-op" + id ,
fieldMaskPaths : [] string { "Name" },
2022-07-20 16:24:46 -07:00
ProjectId : proj . PublicId ,
2020-08-26 13:58:46 -04:00
},
2022-12-06 12:28:55 -08:00
newProjectId : proj . PublicId ,
newName : "valid-no-op" + id ,
wantErr : false ,
wantRowsUpdate : 1 ,
wantHostSources : true ,
2020-08-26 13:58:46 -04:00
},
{
name : "not-found" ,
args : args {
name : "not-found" + id ,
fieldMaskPaths : [] string { "Name" },
2022-07-20 16:24:46 -07:00
ProjectId : proj . PublicId ,
2020-08-26 13:58:46 -04:00
PublicId : func () * string { s := "1" ; return & s }(),
},
2022-12-06 12:28:55 -08:00
newProjectId : proj . PublicId ,
wantErr : true ,
wantRowsUpdate : 0 ,
wantErrMsg : "record not found, search issue: error #1100" ,
wantIsError : errors . RecordNotFound ,
wantHostSources : true ,
2020-08-26 13:58:46 -04:00
},
{
name : "null-name" ,
args : args {
name : "" ,
fieldMaskPaths : [] string { "Name" },
2022-07-20 16:24:46 -07:00
ProjectId : proj . PublicId ,
2020-08-26 13:58:46 -04:00
},
2022-12-06 12:28:55 -08:00
newProjectId : proj . PublicId ,
newName : "null-name" + id ,
wantErr : true ,
wantRowsUpdate : 0 ,
wantErrMsg : "db.DoTx: target.(Repository).UpdateTarget: db.Update: name must not be empty: not null constraint violated: integrity violation: error #1001" ,
wantHostSources : true ,
2020-08-26 13:58:46 -04:00
},
{
name : "null-description" ,
args : args {
name : "null-description" ,
fieldMaskPaths : [] string { "Description" },
2022-07-20 16:24:46 -07:00
ProjectId : proj . PublicId ,
2020-08-26 13:58:46 -04:00
},
2022-12-06 12:28:55 -08:00
newProjectId : proj . PublicId ,
newTargetOpts : [] target . Option { target . WithDescription ( "null-description" + id )},
wantErr : false ,
wantRowsUpdate : 1 ,
wantHostSources : true ,
2020-08-26 13:58:46 -04:00
},
{
name : "empty-field-mask" ,
args : args {
name : "valid" + id ,
fieldMaskPaths : [] string {},
2022-07-20 16:24:46 -07:00
ProjectId : proj . PublicId ,
2020-08-26 13:58:46 -04:00
},
2022-12-06 12:28:55 -08:00
newProjectId : proj . PublicId ,
wantErr : true ,
wantRowsUpdate : 0 ,
wantErrMsg : "target.(Repository).UpdateTarget: empty field mask: parameter violation: error #104" ,
wantIsError : errors . EmptyFieldMask ,
wantHostSources : true ,
2020-08-26 13:58:46 -04:00
},
{
name : "nil-fieldmask" ,
args : args {
name : "valid" + id ,
fieldMaskPaths : nil ,
2022-07-20 16:24:46 -07:00
ProjectId : proj . PublicId ,
2020-08-26 13:58:46 -04:00
},
2022-12-06 12:28:55 -08:00
newProjectId : proj . PublicId ,
wantErr : true ,
wantRowsUpdate : 0 ,
wantErrMsg : "target.(Repository).UpdateTarget: empty field mask: parameter violation: error #104" ,
wantIsError : errors . EmptyFieldMask ,
wantHostSources : true ,
2020-08-26 13:58:46 -04:00
},
{
name : "read-only-fields" ,
args : args {
name : "valid" + id ,
fieldMaskPaths : [] string { "CreateTime" },
2022-07-20 16:24:46 -07:00
ProjectId : proj . PublicId ,
2020-08-26 13:58:46 -04:00
},
2022-12-06 12:28:55 -08:00
newProjectId : proj . PublicId ,
wantErr : true ,
wantRowsUpdate : 0 ,
wantErrMsg : "target.(Repository).UpdateTarget: invalid field mask: CreateTime: parameter violation: error #103" ,
wantIsError : errors . InvalidFieldMask ,
wantHostSources : true ,
2020-08-26 13:58:46 -04:00
},
{
name : "unknown-fields" ,
args : args {
name : "valid" + id ,
fieldMaskPaths : [] string { "Alice" },
2022-07-20 16:24:46 -07:00
ProjectId : proj . PublicId ,
2020-08-26 13:58:46 -04:00
},
2022-12-06 12:28:55 -08:00
newProjectId : proj . PublicId ,
wantErr : true ,
wantRowsUpdate : 0 ,
wantErrMsg : "target.(Repository).UpdateTarget: invalid field mask: Alice: parameter violation: error #103" ,
wantIsError : errors . InvalidFieldMask ,
wantHostSources : true ,
2020-08-26 13:58:46 -04:00
},
{
name : "no-public-id" ,
args : args {
name : "valid" + id ,
fieldMaskPaths : [] string { "Name" },
2022-07-20 16:24:46 -07:00
ProjectId : proj . PublicId ,
2020-08-26 13:58:46 -04:00
PublicId : pubId ( "" ),
},
2022-12-06 12:28:55 -08:00
newProjectId : proj . PublicId ,
wantErr : true ,
wantErrMsg : "target.(Repository).UpdateTarget: missing target public id: parameter violation: error #100" ,
wantIsError : errors . InvalidParameter ,
wantRowsUpdate : 0 ,
wantHostSources : true ,
2020-08-26 13:58:46 -04:00
},
{
2022-07-20 16:24:46 -07:00
name : "project-id-no-mask" ,
2020-08-26 13:58:46 -04:00
args : args {
2022-07-20 16:24:46 -07:00
name : "project-id" + id ,
ProjectId : proj . PublicId ,
2020-08-26 13:58:46 -04:00
},
2022-12-06 12:28:55 -08:00
newProjectId : proj . PublicId ,
wantErr : true ,
wantErrMsg : "target.(Repository).UpdateTarget: empty field mask: parameter violation: error #104" ,
wantIsError : errors . EmptyFieldMask ,
wantHostSources : true ,
2020-08-26 13:58:46 -04:00
},
{
name : "dup-name" ,
args : args {
name : "dup-name" + id ,
fieldMaskPaths : [] string { "Name" },
2022-07-20 16:24:46 -07:00
ProjectId : proj . PublicId ,
2020-08-26 13:58:46 -04:00
},
2022-12-06 12:28:55 -08:00
newProjectId : proj . PublicId ,
wantErr : true ,
wantDup : true ,
wantErrMsg : " already exists in project " + proj . PublicId ,
wantIsError : errors . NotUnique ,
wantHostSources : true ,
2020-08-26 13:58:46 -04:00
},
}
2021-06-22 22:26:26 -04:00
css := vault . TestCredentialStores ( t , conn , wrapper , proj . GetPublicId (), len ( tests ))
for i , tt := range tests {
2020-08-26 13:58:46 -04:00
t . Run ( tt . name , func ( t * testing . T ) {
2021-06-22 22:26:26 -04:00
cs := css [ i ]
2020-08-26 13:58:46 -04:00
require , assert := require . New ( t ), assert . New ( t )
2021-11-04 11:37:32 -04:00
ctx := context . Background ()
2020-08-26 13:58:46 -04:00
if tt . wantDup {
2021-11-04 11:37:32 -04:00
_ = tcp . TestTarget ( ctx , t , conn , proj . PublicId , tt . args . name )
2020-08-26 13:58:46 -04:00
}
2022-12-06 12:28:55 -08:00
testHostSetIds := [] string {}
if tt . wantHostSources {
testCats := static . TestCatalogs ( t , conn , proj . PublicId , 1 )
hsets := static . TestSets ( t , conn , testCats [ 0 ]. GetPublicId (), 5 )
testHostSetIds = make ([] string , 0 , len ( hsets ))
for _ , hs := range hsets {
testHostSetIds = append ( testHostSetIds , hs . PublicId )
}
tt . newTargetOpts = append (
tt . newTargetOpts ,
target . WithHostSources ( testHostSetIds ),
)
2020-08-26 13:58:46 -04:00
}
2021-06-22 22:26:26 -04:00
cls := vault . TestCredentialLibraries ( t , conn , wrapper , cs . GetPublicId (), 5 )
var testClIds [] string
2021-10-26 09:57:22 -04:00
var testCredLibs [] * target . CredentialLibrary
2021-06-22 22:26:26 -04:00
for _ , cl := range cls {
2021-10-26 09:57:22 -04:00
testCredLibs = append ( testCredLibs , & target . CredentialLibrary {
CredentialLibrary : & store . CredentialLibrary {
CredentialLibraryId : cl . PublicId ,
2022-07-19 15:16:24 -07:00
CredentialPurpose : string ( credential . BrokeredPurpose ),
2021-10-26 09:57:22 -04:00
},
})
2021-06-22 22:26:26 -04:00
testClIds = append ( testClIds , cl . PublicId )
}
2021-10-26 09:57:22 -04:00
tt . newTargetOpts = append (
tt . newTargetOpts ,
target . WithCredentialLibraries ( testCredLibs ),
)
2020-08-26 13:58:46 -04:00
name := tt . newName
if name == "" {
2021-09-29 13:13:49 -04:00
name = tcp . TestId ( t )
2020-08-26 13:58:46 -04:00
}
2022-07-20 16:24:46 -07:00
tar := tcp . TestTarget ( ctx , t , conn , tt . newProjectId , name , tt . newTargetOpts ... )
2021-09-29 13:13:49 -04:00
updateTarget := tcp . NewTestTarget (
2023-06-06 17:09:08 -04:00
ctx ,
2022-07-20 16:24:46 -07:00
tt . args . ProjectId ,
2021-09-29 13:13:49 -04:00
target . WithName ( tt . args . name ),
target . WithDescription ( tt . args . description ),
target . WithDefaultPort ( tt . args . port ),
2022-12-06 12:28:55 -08:00
target . WithAddress ( tt . args . address ),
2021-09-29 13:13:49 -04:00
)
2021-11-04 11:37:32 -04:00
updateTarget . SetPublicId ( ctx , tar . GetPublicId ())
2020-08-26 13:58:46 -04:00
if tt . args . PublicId != nil {
2021-11-04 11:37:32 -04:00
ut := updateTarget .( * tcp . Target )
ut . PublicId = * tt . args . PublicId
2020-08-26 13:58:46 -04:00
}
2023-03-30 15:19:30 -04:00
targetAfterUpdate , updatedRows , err := repo . UpdateTarget ( ctx , updateTarget , tar . GetVersion (), tt . args . fieldMaskPaths , tt . args . opt ... )
2020-08-26 13:58:46 -04:00
if tt . wantErr {
assert . Error ( err )
2021-01-12 08:50:19 -08:00
assert . True ( errors . Match ( errors . T ( tt . wantIsError ), err ))
2020-08-26 13:58:46 -04:00
assert . Nil ( targetAfterUpdate )
assert . Equal ( 0 , updatedRows )
2022-12-06 12:28:55 -08:00
assert . NotEmpty ( err . Error ())
2020-08-26 13:58:46 -04:00
assert . Contains ( err . Error (), tt . wantErrMsg )
2021-11-04 11:37:32 -04:00
err = db . TestVerifyOplog ( t , rw , tar . GetPublicId (), db . WithOperation ( oplog . OpType_OP_TYPE_UPDATE ), db . WithCreateNotBefore ( 10 * time . Second ))
2020-08-26 13:58:46 -04:00
assert . Error ( err )
2020-12-21 10:07:34 -08:00
assert . True ( errors . IsNotFoundError ( err ))
2020-08-26 13:58:46 -04:00
return
}
require . NoError ( err )
require . NotNil ( targetAfterUpdate )
assert . Equal ( tt . wantRowsUpdate , updatedRows )
2023-03-30 15:19:30 -04:00
hostSources := targetAfterUpdate . GetHostSources ()
credSources := targetAfterUpdate . GetCredentialSources ()
2021-07-30 08:48:03 -04:00
afterUpdateIds := make ([] string , 0 , len ( hostSources ))
for _ , hs := range hostSources {
afterUpdateIds = append ( afterUpdateIds , hs . Id ())
2020-08-26 13:58:46 -04:00
}
assert . Equal ( testHostSetIds , afterUpdateIds )
2023-02-02 18:51:39 +00:00
if len ( tt . wantAddress ) != 0 {
assert . Equal ( tt . wantAddress , targetAfterUpdate . GetAddress ())
} else {
assert . Equal ( tt . args . address , targetAfterUpdate . GetAddress ())
}
2020-08-26 13:58:46 -04:00
2021-07-27 12:48:36 -04:00
afterUpdateIds = make ([] string , 0 , len ( credSources ))
for _ , cl := range credSources {
afterUpdateIds = append ( afterUpdateIds , cl . Id ())
2021-06-22 22:26:26 -04:00
}
2022-06-08 13:51:56 -07:00
assert . ElementsMatch ( testClIds , afterUpdateIds )
2021-06-22 22:26:26 -04:00
2020-08-26 13:58:46 -04:00
switch tt . name {
case "valid-no-op" :
2022-12-06 12:28:55 -08:00
assert . Equal ( tar . GetUpdateTime (). String (), targetAfterUpdate . GetUpdateTime (). String ())
2020-08-26 13:58:46 -04:00
default :
2021-11-04 11:37:32 -04:00
assert . NotEqual ( tar . GetUpdateTime (), targetAfterUpdate . GetUpdateTime ())
2020-08-26 13:58:46 -04:00
}
2023-03-30 15:19:30 -04:00
foundTarget , err := repo . LookupTarget ( context . Background (), tar . GetPublicId ())
2020-08-26 13:58:46 -04:00
assert . NoError ( err )
2021-09-29 13:13:49 -04:00
assert . True ( proto . Equal ( targetAfterUpdate .(( * tcp . Target )), foundTarget .(( * tcp . Target ))))
2022-12-06 12:28:55 -08:00
assert . Equal ( targetAfterUpdate . GetAddress (), foundTarget . GetAddress ())
2021-09-30 09:17:27 -04:00
underlyingDB , err := conn . SqlDB ( ctx )
2021-09-30 08:47:34 -04:00
require . NoError ( err )
dbassert := dbassert . New ( t , underlyingDB )
2020-08-26 13:58:46 -04:00
if tt . args . description == "" {
assert . Equal ( foundTarget . GetDescription (), "" )
dbassert . IsNull ( foundTarget , "description" )
}
2021-11-04 11:37:32 -04:00
err = db . TestVerifyOplog ( t , rw , tar . GetPublicId (), db . WithOperation ( oplog . OpType_OP_TYPE_UPDATE ), db . WithCreateNotBefore ( 10 * time . Second ))
2020-08-26 13:58:46 -04:00
assert . NoError ( err )
})
}
}