@@ -302,10 +302,7 @@ func TestFetchTargetAliasProxyServerCertificate(t *testing.T) {
302
302
}
303
303
}
304
304
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 ) {
309
306
t .Parallel ()
310
307
ctx := context .Background ()
311
308
conn , _ := db .TestSetup (t , "postgres" )
@@ -315,50 +312,74 @@ func Test_LookupTargetForSessionAuthorization(t *testing.T) {
315
312
_ , proj := iam .TestScopes (t , iam .TestRepo (t , conn , wrapper ))
316
313
repo , err := target .NewRepository (context .Background (), rw , rw , testKms )
317
314
require .NoError (t , err )
315
+ databaseWrapper , err := testKms .GetWrapper (ctx , proj .PublicId , kms .KeyPurposeDatabase )
316
+ require .NoError (t , err )
318
317
319
318
tar := targettest .TestNewTestTarget (ctx , t , conn , proj .PublicId , "test-target" )
319
+ tar2 := targettest .TestNewTestTarget (ctx , t , conn , proj .PublicId , "test-target2" )
320
320
321
321
// Create an alias
322
322
aliasValue := "test-alias"
323
323
alias := talias .TestAlias (t , rw , aliasValue , talias .WithDestinationId (tar .GetPublicId ()))
324
+ require .NoError (t , err )
324
325
require .NotNil (t , alias )
325
326
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
+
326
339
tests := []struct {
327
340
name string
328
341
publicId string
329
342
opt []target.Option
330
- wantErr bool
343
+ wantCert bool
331
344
}{
332
345
{
333
- name : "success-get-target-no-cert " ,
346
+ name : "success-get-target-with-certificate " ,
334
347
publicId : tar .GetPublicId (),
335
- wantErr : false ,
348
+ wantCert : true ,
336
349
},
337
350
{
338
- name : "success-get-target-no-cert- with-alias" ,
351
+ name : "success-get-target-with-alias-certificate " ,
339
352
publicId : tar .GetPublicId (),
340
353
opt : []target.Option {
341
354
target .WithAlias (alias ),
342
355
},
343
- wantErr : false ,
356
+ wantCert : true ,
357
+ },
358
+ {
359
+ name : "success-get-target-no-cert" ,
360
+ publicId : tar2 .GetPublicId (),
361
+ wantCert : false ,
344
362
},
345
363
{
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 ,
349
370
},
350
371
}
351
372
for _ , tt := range tests {
352
373
t .Run (tt .name , func (t * testing.T ) {
353
374
assert , require := assert .New (t ), require .New (t )
354
375
got , err := repo .LookupTargetForSessionAuthorization (ctx , tt .publicId , proj .PublicId , tt .opt ... )
355
- if tt .wantErr {
356
- require .Error (err )
357
- return
358
- }
359
376
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
+ }
362
383
})
363
384
}
364
385
}
0 commit comments