Skip to content

Commit 1a6a0b9

Browse files
authored
Revert "fix(target): Skip fetching proxy server cert (#6055)" (#6066)
This reverts commit 3dd238a.
1 parent 8444611 commit 1a6a0b9

File tree

2 files changed

+49
-28
lines changed

2 files changed

+49
-28
lines changed

internal/target/repository.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"github.com/hashicorp/boundary/internal/types/scope"
2525
"github.com/hashicorp/boundary/internal/util"
2626
"github.com/hashicorp/go-dbw"
27-
wrapping "github.com/hashicorp/go-kms-wrapping/v2"
2827
)
2928

3029
// RepositoryFactory enables `target.Repository` object instantiation,
@@ -52,12 +51,6 @@ type Repository struct {
5251
permissions []perms.Permission
5352
}
5453

55-
// getTargetProxyServerCertificateFn can be overridden for testing or extension purposes.
56-
// By default, it returns nil, nil because TCP targets do not currently use a proxy server certificate.
57-
var getTargetProxyServerCertificateFn = func(ctx context.Context, r *Repository, target targetView, databaseWrapper wrapping.Wrapper, opts options) (*ServerCertificate, error) {
58-
return nil, nil
59-
}
60-
6154
// NewRepository creates a new target Repository.
6255
// Supports the following options:
6356
// - WithLimit: sets a limit on the number of results returned by various repo operations.
@@ -147,9 +140,16 @@ func (r *Repository) LookupTargetForSessionAuthorization(ctx context.Context, pu
147140
address = targetAddress.GetAddress()
148141
}
149142

150-
cert, err = getTargetProxyServerCertificateFn(ctx, r, target, databaseWrapper, opts)
151-
if err != nil && !errors.IsNotFoundError(err) {
152-
return errors.Wrap(ctx, err, op)
143+
if opts.WithAlias != nil {
144+
cert, err = fetchTargetAliasProxyServerCertificate(ctx, read, w, target.PublicId, target.ProjectId, opts.WithAlias, databaseWrapper, target.GetSessionMaxSeconds())
145+
if err != nil && !errors.IsNotFoundError(err) {
146+
return errors.Wrap(ctx, err, op)
147+
}
148+
} else {
149+
cert, err = fetchTargetProxyServerCertificate(ctx, read, w, target.PublicId, target.ProjectId, databaseWrapper, target.GetSessionMaxSeconds())
150+
if err != nil && !errors.IsNotFoundError(err) {
151+
return errors.Wrap(ctx, err, op)
152+
}
153153
}
154154
return nil
155155
},

internal/target/repository_proxy_server_certificate_test.go

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -302,10 +302,7 @@ func TestFetchTargetAliasProxyServerCertificate(t *testing.T) {
302302
}
303303
}
304304

305-
// Test_LookupTargetForSessionAuthorization tests looking up a target for session both with and without an alias.
306-
// The target used in this test does not have a proxy server certificate because TCP targets do not currently support proxy server certificates.
307-
// Fetching the proxy server certificate is tested in other enterprise tests.
308-
func Test_LookupTargetForSessionAuthorization(t *testing.T) {
305+
func Test_FetchCertsWithinLookupTargetForSessionAuthorization(t *testing.T) {
309306
t.Parallel()
310307
ctx := context.Background()
311308
conn, _ := db.TestSetup(t, "postgres")
@@ -315,50 +312,74 @@ func Test_LookupTargetForSessionAuthorization(t *testing.T) {
315312
_, proj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
316313
repo, err := target.NewRepository(context.Background(), rw, rw, testKms)
317314
require.NoError(t, err)
315+
databaseWrapper, err := testKms.GetWrapper(ctx, proj.PublicId, kms.KeyPurposeDatabase)
316+
require.NoError(t, err)
318317

319318
tar := targettest.TestNewTestTarget(ctx, t, conn, proj.PublicId, "test-target")
319+
tar2 := targettest.TestNewTestTarget(ctx, t, conn, proj.PublicId, "test-target2")
320320

321321
// Create an alias
322322
aliasValue := "test-alias"
323323
alias := talias.TestAlias(t, rw, aliasValue, talias.WithDestinationId(tar.GetPublicId()))
324+
require.NoError(t, err)
324325
require.NotNil(t, alias)
325326

327+
// Create our default localhost target cert
328+
cer, err := target.NewTargetProxyCertificate(ctx, target.WithTargetId(tar.GetPublicId()))
329+
require.NoError(t, err)
330+
require.NotNil(t, cer)
331+
id, err := db.NewPublicId(ctx, globals.ProxyServerCertificatePrefix)
332+
require.NoError(t, err)
333+
cer.PublicId = id
334+
err = cer.Encrypt(ctx, databaseWrapper)
335+
require.NoError(t, err)
336+
err = rw.Create(ctx, cer)
337+
require.NoError(t, err)
338+
326339
tests := []struct {
327340
name string
328341
publicId string
329342
opt []target.Option
330-
wantErr bool
343+
wantCert bool
331344
}{
332345
{
333-
name: "success-get-target-no-cert",
346+
name: "success-get-target-with-certificate",
334347
publicId: tar.GetPublicId(),
335-
wantErr: false,
348+
wantCert: true,
336349
},
337350
{
338-
name: "success-get-target-no-cert-with-alias",
351+
name: "success-get-target-with-alias-certificate",
339352
publicId: tar.GetPublicId(),
340353
opt: []target.Option{
341354
target.WithAlias(alias),
342355
},
343-
wantErr: false,
356+
wantCert: true,
357+
},
358+
{
359+
name: "success-get-target-no-cert",
360+
publicId: tar2.GetPublicId(),
361+
wantCert: false,
344362
},
345363
{
346-
name: "fail-missing-target-id",
347-
publicId: "",
348-
wantErr: true,
364+
name: "success-get-target-no-cert-with-alias",
365+
publicId: tar2.GetPublicId(),
366+
opt: []target.Option{
367+
target.WithAlias(alias),
368+
},
369+
wantCert: false,
349370
},
350371
}
351372
for _, tt := range tests {
352373
t.Run(tt.name, func(t *testing.T) {
353374
assert, require := assert.New(t), require.New(t)
354375
got, err := repo.LookupTargetForSessionAuthorization(ctx, tt.publicId, proj.PublicId, tt.opt...)
355-
if tt.wantErr {
356-
require.Error(err)
357-
return
358-
}
359376
require.NoError(err)
360-
require.NotNil(got)
361-
assert.Nil(got.GetProxyServerCertificate())
377+
assert.NotNil(got)
378+
if tt.wantCert {
379+
assert.NotNil(got.GetProxyServerCertificate())
380+
} else {
381+
assert.Nil(got.GetProxyServerCertificate())
382+
}
362383
})
363384
}
364385
}

0 commit comments

Comments
 (0)