Skip to content

Commit e4d4274

Browse files
committed
ci: add more tests for PrintData to address certificate fetch errors from a TLS endpoint
1 parent 294ce37 commit e4d4274

2 files changed

Lines changed: 135 additions & 46 deletions

File tree

cmd/certinfo.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,6 @@ Examples:
7171
return
7272
}
7373

74-
certinfoCfg.SetTLSInsecure(tlsInsecure).SetTLSServerName(tlsServerName)
75-
7674
if err = certinfoCfg.SetCaPoolFromFile(caBundleValue, fileReader); err != nil {
7775
fmt.Printf("Error importing CA Certificate bundle from file: %s", err)
7876
}
@@ -81,6 +79,11 @@ Examples:
8179
fmt.Printf("Error importing Certificate bundle from file: %s", err)
8280
}
8381

82+
certinfoCfg.SetTLSInsecure(tlsInsecure).SetTLSServerName(tlsServerName)
83+
84+
// SetTLSEndpoint may need the SNI/ServerName and insecure options to be set
85+
// before being able to ask details about the certificate we want to a
86+
// webserver using self-signed and valid certificates
8487
if err = certinfoCfg.SetTLSEndpoint(tlsEndpoint); err != nil {
8588
fmt.Printf("Error setting TLS endpoint: %s", err)
8689
}

internal/certinfo/certinfo_handlers_test.go

Lines changed: 130 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -267,14 +267,18 @@ func TestCertinfo_CertsToTables(t *testing.T) {
267267
}
268268

269269
func TestCertinfo_PrintData(t *testing.T) {
270-
noErrorsTests := []struct {
271-
desc string
272-
keyFile string
273-
certFile string
274-
caCertFile string
275-
keyCertMatch bool
276-
tlsEndpoint string
277-
srvCfg demoHTTPServerConfig
270+
tests := []struct {
271+
desc string
272+
keyFile string
273+
certFile string
274+
caCertFile string
275+
keyCertMatch bool
276+
tlsEndpoint string
277+
tlsInsecure bool
278+
tlsServerName string
279+
srvCfg demoHTTPServerConfig
280+
expectCertsFetchErr bool
281+
expectCertsFetcMsg string
278282
}{
279283
{
280284
desc: "local CA cert and key",
@@ -290,21 +294,82 @@ func TestCertinfo_PrintData(t *testing.T) {
290294
keyCertMatch: true,
291295
},
292296
{
293-
desc: "local key and remote TLS Endpoint",
294-
keyFile: RSASampleCertKeyFile,
295-
caCertFile: RSACaCertFile,
296-
keyCertMatch: true,
297-
tlsEndpoint: "localhost:46401",
297+
desc: "local key and remote TLS Endpoint, certs validated",
298+
keyFile: RSASampleCertKeyFile,
299+
caCertFile: RSACaCertFile,
300+
keyCertMatch: true,
301+
tlsEndpoint: "localhost:46401",
302+
tlsServerName: "example.com",
298303
srvCfg: demoHTTPServerConfig{
299304
serverAddr: "localhost:46401",
300305
serverName: "example.com",
301306
serverCertFile: RSASampleCertFile,
302307
serverKeyFile: RSASampleCertKeyFile,
303308
},
304309
},
310+
{
311+
desc: "local key and remote TLS Endpoint, certs NOT validated",
312+
keyFile: RSASampleCertKeyFile,
313+
caCertFile: emptyString,
314+
tlsEndpoint: "localhost:46402",
315+
tlsServerName: "example.com",
316+
srvCfg: demoHTTPServerConfig{
317+
serverAddr: "localhost:46402",
318+
serverName: "example.com",
319+
serverCertFile: RSASampleCertFile,
320+
serverKeyFile: RSASampleCertKeyFile,
321+
},
322+
expectCertsFetchErr: true,
323+
expectCertsFetcMsg: "unable to get endpoint certificates: TLS handshake failed: tls: failed to verify certificate: x509: certificate signed by unknown authority",
324+
},
325+
326+
{
327+
desc: "local key and remote TLS Endpoint, TLS Insecure",
328+
keyFile: RSASampleCertKeyFile,
329+
caCertFile: emptyString,
330+
keyCertMatch: true,
331+
tlsEndpoint: "localhost:46403",
332+
tlsInsecure: true,
333+
tlsServerName: "example.com",
334+
srvCfg: demoHTTPServerConfig{
335+
serverAddr: "localhost:46403",
336+
serverName: "example.com",
337+
serverCertFile: RSASampleCertFile,
338+
serverKeyFile: RSASampleCertKeyFile,
339+
},
340+
},
341+
{
342+
desc: "local key and remote TLS Endpoint, missing TLS ServerName",
343+
keyFile: RSASampleCertKeyFile,
344+
caCertFile: RSACaCertFile,
345+
tlsEndpoint: "localhost:46404",
346+
tlsServerName: emptyString,
347+
srvCfg: demoHTTPServerConfig{
348+
serverAddr: "localhost:46404",
349+
serverName: "example.com",
350+
serverCertFile: RSASampleCertFile,
351+
serverKeyFile: RSASampleCertKeyFile,
352+
},
353+
expectCertsFetchErr: true,
354+
expectCertsFetcMsg: "unable to get endpoint certificates: TLS handshake failed: tls: failed to verify certificate: x509: certificate is valid for example.com, example.net, example.de, not localhost",
355+
},
356+
{
357+
desc: "local key and remote TLS Endpoint, no key match",
358+
keyFile: ED25519SamplePlaintextPrivateKey,
359+
caCertFile: RSACaCertFile,
360+
keyCertMatch: false,
361+
tlsEndpoint: "localhost:46405",
362+
tlsServerName: "example.com",
363+
srvCfg: demoHTTPServerConfig{
364+
serverAddr: "localhost:46405",
365+
serverName: "example.com",
366+
serverCertFile: RSASampleCertFile,
367+
serverKeyFile: RSASampleCertKeyFile,
368+
},
369+
},
305370
}
306371

307-
for _, tc := range noErrorsTests {
372+
for _, tc := range tests {
308373
tt := tc
309374
t.Run("No errors test - "+tt.desc, func(t *testing.T) {
310375
t.Parallel()
@@ -324,32 +389,32 @@ func TestCertinfo_PrintData(t *testing.T) {
324389

325390
defer ts.Close()
326391

327-
cc.SetTLSServerName(tt.srvCfg.serverName)
328-
cc.SetTLSEndpoint(tt.tlsEndpoint)
392+
cc.SetTLSServerName(tt.tlsServerName)
393+
cc.SetTLSInsecure(tt.tlsInsecure)
394+
395+
// in most of these test cases SetTLSEndpoint depends
396+
// on SetTLSServerName and/or SetTLSInsecure to be set
397+
// before being able to fetch certificates from the TLS
398+
// endpoint.
399+
// The dependency is addressed with the order of method calls
400+
// in cmd/certinfo.go.
401+
// In this test, we call SetTLSServerName and SetTLSInsecure before
402+
// SetTLSEndpoint to be sure the dependency is being addressed the
403+
// same way.
404+
err = cc.SetTLSEndpoint(tt.tlsEndpoint)
405+
if !tt.expectCertsFetchErr {
406+
require.NoError(t, err, "SetTLSEndpoint require NoError")
407+
}
408+
409+
if tt.expectCertsFetchErr {
410+
require.EqualError(t, err, tt.expectCertsFetcMsg)
411+
}
329412
}
330413

331414
errPrint := cc.PrintData(&buffer)
332415
require.NoError(t, errPrint)
333416

334417
got := buffer.String()
335-
for _, want := range []string{
336-
"Certinfo",
337-
"Certificate",
338-
"Subject",
339-
"Issuer",
340-
"NotBefore",
341-
"NotAfter",
342-
"Expiration",
343-
"IsCA",
344-
"AuthorityKeyId",
345-
"SubjectKeyId",
346-
"PublicKeyAlgorithm",
347-
"SignatureAlgorithm",
348-
"SerialNumber",
349-
"Fingerprint SHA-256",
350-
} {
351-
require.Contains(t, got, want)
352-
}
353418

354419
if tt.keyFile != emptyString {
355420
require.Contains(t, got, "PrivateKey file: "+tt.keyFile)
@@ -363,16 +428,37 @@ func TestCertinfo_PrintData(t *testing.T) {
363428
require.Contains(t, got, "CA Certificates file: "+tt.caCertFile)
364429
}
365430

366-
if tt.keyFile != emptyString && tt.keyCertMatch {
367-
require.Contains(t, got, "PrivateKey match: true")
368-
} else {
369-
require.Contains(t, got, "PrivateKey match: false")
370-
}
371-
372-
if tt.tlsEndpoint != emptyString {
373-
require.Contains(t, got, "TLSEndpoint Certificates")
374-
require.Contains(t, got, "Endpoint: "+tt.tlsEndpoint)
375-
require.Contains(t, got, "ServerName: "+tt.srvCfg.serverName)
431+
if !tt.expectCertsFetchErr {
432+
for _, want := range []string{
433+
"Certinfo",
434+
"Certificate",
435+
"Subject",
436+
"Issuer",
437+
"NotBefore",
438+
"NotAfter",
439+
"Expiration",
440+
"IsCA",
441+
"AuthorityKeyId",
442+
"SubjectKeyId",
443+
"PublicKeyAlgorithm",
444+
"SignatureAlgorithm",
445+
"SerialNumber",
446+
"Fingerprint SHA-256",
447+
} {
448+
require.Contains(t, got, want)
449+
}
450+
451+
if tt.keyFile != emptyString && tt.keyCertMatch {
452+
require.Contains(t, got, "PrivateKey match: true")
453+
} else {
454+
require.Contains(t, got, "PrivateKey match: false")
455+
}
456+
457+
if tt.tlsEndpoint != emptyString {
458+
require.Contains(t, got, "TLSEndpoint Certificates")
459+
require.Contains(t, got, "Endpoint: "+tt.tlsEndpoint)
460+
require.Contains(t, got, "ServerName: "+tt.tlsServerName)
461+
}
376462
}
377463
})
378464
}

0 commit comments

Comments
 (0)