Skip to content

Commit

Permalink
Fail gracefully on missing certificate (#527)
Browse files Browse the repository at this point in the history
  • Loading branch information
ShashikanthRaoT authored and GitHub Enterprise committed Oct 6, 2023
1 parent 71b2516 commit ff8a0bc
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 4 deletions.
12 changes: 9 additions & 3 deletions internal/tls/tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,12 @@ func processKeys(tlsStore *TLSStore, keystoreDir string, keyDir string) (string,
return "", err
}

// Return an error if corresponding public certificate was not found. Both private key and
// it's corresponding public certificate are required.
if publicCertificate == nil {
return "", fmt.Errorf("Failed to find public certificate in directory %s", keyDir)
}

// Validate certificates for duplicate Subject DNs
if len(caCertificate) > 0 {
errCertValid := validateCertificates(publicCertificate, caCertificate)
Expand Down Expand Up @@ -411,7 +417,7 @@ func processCertificates(keyDir string, keySetName, keyPrefix string, keys []os.
// Add to known certificates for the CMS Keystore
err = addToKnownCertificates(block, cmsKeystore, false)
if err != nil {
return nil, nil, fmt.Errorf("Failed to add to know certificates for CMS Keystore")
return nil, nil, fmt.Errorf("Failed to add to known certificates for CMS Keystore")
}

} else if strings.HasSuffix(key.Name(), ".crt") {
Expand All @@ -431,14 +437,14 @@ func processCertificates(keyDir string, keySetName, keyPrefix string, keys []os.
// Add to known certificates for the CMS Keystore
err = addToKnownCertificates(block, cmsKeystore, false)
if err != nil {
return nil, nil, fmt.Errorf("Failed to add to know certificates for CMS Keystore")
return nil, nil, fmt.Errorf("Failed to add to known certificates for CMS Keystore")
}

if p12Truststore.Keystore != nil {
// Add to known certificates for the PKCS#12 Truststore
err = addToKnownCertificates(block, p12Truststore, true)
if err != nil {
return nil, nil, fmt.Errorf("Failed to add to know certificates for PKCS#12 Truststore")
return nil, nil, fmt.Errorf("Failed to add to known certificates for PKCS#12 Truststore")
}
}

Expand Down
46 changes: 45 additions & 1 deletion test/container/docker_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1428,7 +1428,7 @@ func TestLoggingConsoleSource(t *testing.T) {
if errJson != nil {
t.Errorf("%v", errJson)
}

//Check for web server logs existence in console logs since its visibility is default along with qmgr logs
jsonLogs, errJson = waitForMessageInLog(t, cli, id, "CWWKF0011I")
if errJson != nil {
Expand Down Expand Up @@ -2026,3 +2026,47 @@ func TestRORFSVerifySymLinks(t *testing.T) {
// Stop the container cleanly
stopContainer(t, cli, ID)
}

// Quick test to check expected error message is displayed on the
// console if only key file is provided and no certificate.
func TestMissingCertError(t *testing.T) {
t.Parallel()

cli := ce.NewContainerClient()

containerConfig := ce.ContainerConfig{
Env: []string{
"LICENSE=accept",
"MQ_QMGR_NAME=QM1",
},
Image: imageName(),
}
hostConfig := ce.ContainerHostConfig{
Binds: []string{
coverageBind(t),
tlsDirDN(t, false, "../tlsnocert") + ":/etc/mqm/pki/keys/QM1",
},
}

networkingConfig := ce.ContainerNetworkSettings{}
ctrID, err := cli.ContainerCreate(&containerConfig, &hostConfig, &networkingConfig, t.Name())
if err != nil {
t.Fatal(err)
}
defer cleanContainer(t, cli, ctrID)
startContainer(t, cli, ctrID)

rc := waitForContainer(t, cli, ctrID, 30*time.Second)
// Expect return code 1 if container failed to create.
if rc == 1 {
// Get container logs and search for specific message.
logs := inspectLogs(t, cli, ctrID)
expectedMessage := "Failed to find public certificate in directory"
if !strings.Contains(logs, expectedMessage) {
t.Errorf("Expected to find '%s' but was not found", expectedMessage)
}
} else {
// Some other error occurred
t.Errorf("Some other error occurred %v", rc)
}
}
28 changes: 28 additions & 0 deletions test/tlsnocert/server.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCy5g19zgf+A5cy
cys/xHXOalXY3SDf7vKQKUkD3r09JgLEitbsNjDm3jqOAgwgZmm2Hrrxs2eLd0on
o3bByvqgGDBVudZcU27pb+ZFD70wTmNiQhDZFQxY6nCvEvSLSP1M05pwaJb2ZlCs
0Th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