Skip to content

Commit 3dd238a

Browse files
authored
fix(target): Skip fetching proxy server cert (#6055)
1 parent 5ebf11c commit 3dd238a

File tree

2 files changed

+28
-49
lines changed

2 files changed

+28
-49
lines changed

internal/target/repository.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ 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"
2728
)
2829

2930
// RepositoryFactory enables `target.Repository` object instantiation,
@@ -51,6 +52,12 @@ type Repository struct {
5152
permissions []perms.Permission
5253
}
5354

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+
5461
// NewRepository creates a new target Repository.
5562
// Supports the following options:
5663
// - WithLimit: sets a limit on the number of results returned by various repo operations.
@@ -140,16 +147,9 @@ func (r *Repository) LookupTargetForSessionAuthorization(ctx context.Context, pu
140147
address = targetAddress.GetAddress()
141148
}
142149

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-
}
150+
cert, err = getTargetProxyServerCertificateFn(ctx, r, target, databaseWrapper, opts)
151+
if err != nil && !errors.IsNotFoundError(err) {
152+
return errors.Wrap(ctx, err, op)
153153
}
154154
return nil
155155
},

internal/target/repository_proxy_server_certificate_test.go

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

305-
func Test_FetchCertsWithinLookupTargetForSessionAuthorization(t *testing.T) {
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) {
306309
t.Parallel()
307310
ctx := context.Background()
308311
conn, _ := db.TestSetup(t, "postgres")
@@ -312,74 +315,50 @@ func Test_FetchCertsWithinLookupTargetForSessionAuthorization(t *testing.T) {
312315
_, proj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
313316
repo, err := target.NewRepository(context.Background(), rw, rw, testKms)
314317
require.NoError(t, err)
315-
databaseWrapper, err := testKms.GetWrapper(ctx, proj.PublicId, kms.KeyPurposeDatabase)
316-
require.NoError(t, err)
317318

318319
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)
325324
require.NotNil(t, alias)
326325

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-
339326
tests := []struct {
340327
name string
341328
publicId string
342329
opt []target.Option
343-
wantCert bool
330+
wantErr bool
344331
}{
345332
{
346-
name: "success-get-target-with-certificate",
333+
name: "success-get-target-no-cert",
347334
publicId: tar.GetPublicId(),
348-
wantCert: true,
335+
wantErr: false,
349336
},
350337
{
351-
name: "success-get-target-with-alias-certificate",
338+
name: "success-get-target-no-cert-with-alias",
352339
publicId: tar.GetPublicId(),
353340
opt: []target.Option{
354341
target.WithAlias(alias),
355342
},
356-
wantCert: true,
357-
},
358-
{
359-
name: "success-get-target-no-cert",
360-
publicId: tar2.GetPublicId(),
361-
wantCert: false,
343+
wantErr: false,
362344
},
363345
{
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,
346+
name: "fail-missing-target-id",
347+
publicId: "",
348+
wantErr: true,
370349
},
371350
}
372351
for _, tt := range tests {
373352
t.Run(tt.name, func(t *testing.T) {
374353
assert, require := assert.New(t), require.New(t)
375354
got, err := repo.LookupTargetForSessionAuthorization(ctx, tt.publicId, proj.PublicId, tt.opt...)
376-
require.NoError(err)
377-
assert.NotNil(got)
378-
if tt.wantCert {
379-
assert.NotNil(got.GetProxyServerCertificate())
380-
} else {
381-
assert.Nil(got.GetProxyServerCertificate())
355+
if tt.wantErr {
356+
require.Error(err)
357+
return
382358
}
359+
require.NoError(err)
360+
require.NotNil(got)
361+
assert.Nil(got.GetProxyServerCertificate())
383362
})
384363
}
385364
}

0 commit comments

Comments
 (0)