Remove announced deprecated code allowing ports in target addrs (#4265)

We made it an error in the API to do this a while back; this cleans up
the logic to disallow it for existing targets too.
This commit is contained in:
Jeff Mitchell authored and GitHub committed 2024-01-22 13:49:17 -05:00
1 parent 30066c06f5
commit 3414e09cbb
4 files changed
+28 -21

No files matched your search

@@ -935,22 +935,16 @@ func (s Service) AuthorizeSession(ctx context.Context, req *pbs.AuthorizeSession
}
// Ensure we don't have a port from the address, which would be unexpected
// FIXME: We've decided to hold off on making this an error until 0.14. In
// the meantime, ignore any port coming from the host address.
hostWithoutPort, _, err := net.SplitHostPort(h)
_, _, err = net.SplitHostPort(h)
switch {
case err != nil && strings.Contains(err.Error(), globals.MissingPortErrStr):
// This is what we expect
case err != nil:
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("error when parsing the chosen endpoint host address"))
case err == nil:
h = hostWithoutPort
// Use below logic for 0.14:
/*
return nil, handlers.ApiErrorWithCodeAndMessage(
codes.FailedPrecondition,
"Address specified for use unexpectedly contains a port.")
*/
return nil, handlers.ApiErrorWithCodeAndMessage(
codes.FailedPrecondition,
"Address specified for use unexpectedly contains a port.")
}
// Generate the endpoint URL
@@ -2916,7 +2916,6 @@ func TestAuthorizeSession(t *testing.T) {
shsWithPort := static.TestSets(t, conn, hcWithPort.GetPublicId(), 1)[0]
_ = static.TestSetMembers(t, conn, shsWithPort.GetPublicId(), []*static.Host{hWithPort})
hWithPortBareAddress := hWithPort.GetAddress()
hWithPort.Address = fmt.Sprintf("%s:54321", hWithPort.GetAddress())
hWithPort, _, err = staticRepo.UpdateHost(ctx, hcWithPort.GetProjectId(), hWithPort, hWithPort.GetVersion(), []string{"address"})
require.NoError(t, err)
@@ -3822,7 +3821,6 @@ func TestAuthorizeSession_Errors(t *testing.T) {
HostSourceIds: []string{hs.GetPublicId()},
})
require.NoError(t, err)
h.Address = fmt.Sprintf("%s:54321", h.GetAddress())
repo, err := staticHostRepoFn()
require.NoError(t, err)
_, _, err = repo.UpdateHost(ctx, hc.GetProjectId(), h, h.GetVersion(), []string{"address"})
@@ -3931,7 +3929,6 @@ func TestAuthorizeSession_Errors(t *testing.T) {
name: "no worker",
setup: []func(tcpTarget target.Target) uint32{hostExists, libraryExists},
useTargetId: true,
wantErr: true,
wantErrContains: "No workers are available to handle this session",
},
{
@@ -3943,7 +3940,6 @@ func TestAuthorizeSession_Errors(t *testing.T) {
name: "no target",
setup: []func(tcpTarget target.Target) uint32{workerExists, hostExists, libraryExists},
useTargetId: false,
wantErr: true,
wantErrContains: "Resource not found",
},
{
@@ -3951,25 +3947,37 @@ func TestAuthorizeSession_Errors(t *testing.T) {
setup: []func(tcpTarget target.Target) uint32{workerExists, hostWithoutPort, libraryExists},
useTargetId: true,
},
{
name: "host port",
setup: []func(tcpTarget target.Target) uint32{
workerExists, func(tcpTarget target.Target) uint32 {
tcpTarget.SetAddress("127.0.0.1:22")
repo, err := repoFn()
require.NoError(t, err)
tcpTarget, _, err = repo.UpdateTarget(ctx, tcpTarget, tcpTarget.GetVersion(), []string{"address"})
require.NoError(t, err)
return tcpTarget.GetVersion()
},
},
wantErrContains: "Address specified for use unexpectedly contains a port",
useTargetId: true,
},
{
name: "no hosts",
setup: []func(tcpTarget target.Target) uint32{workerExists, hostSetNoHostExists, libraryExists},
useTargetId: true,
wantErr: true,
wantErrContains: "No host sources or address found for given target",
},
{
name: "bad library configuration",
setup: []func(tcpTarget target.Target) uint32{workerExists, hostExists, misConfiguredlibraryExists},
useTargetId: true,
wantErr: true,
wantErrContains: "external system issue: error #3014: Error making API request",
},
{
name: "expired token library",
setup: []func(tcpTarget target.Target) uint32{workerExists, hostExists, expiredTokenLibrary},
useTargetId: true,
wantErr: true,
wantErrContains: "vault.newClient: invalid configuration",
},
}
@@ -3990,7 +3998,7 @@ func TestAuthorizeSession_Errors(t *testing.T) {
res, err := s.AuthorizeSession(ctx, &pbs.AuthorizeSessionRequest{
Id: id,
})
if tc.wantErr {
if tc.wantErrContains != "" {
require.Error(t, err)
require.Nil(t, res)
require.ErrorContains(t, err, tc.wantErrContains)