From 137239b5b74bc9c006a61e08d5d12b97d2dfa197 Mon Sep 17 00:00:00 2001 From: Kevin Burke Date: Thu, 1 Feb 2018 12:21:44 -0800 Subject: [PATCH] crypto/tls: handle errors in generate_cert.go I don't expect these to hit often, but we should still alert users if we fail to write the correct data to the file, or fail to close it. Change-Id: I33774e94108f7f18ed655ade8cca229b1993d4d2 Reviewed-on: https://go-review.googlesource.com/91456 Reviewed-by: Brad Fitzpatrick --- generate_cert.go | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/generate_cert.go b/generate_cert.go index 8ee2b59..8d012be 100644 --- a/generate_cert.go +++ b/generate_cert.go @@ -146,16 +146,24 @@ func main() { if err != nil { log.Fatalf("failed to open cert.pem for writing: %s", err) } - pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes}) - certOut.Close() - log.Print("written cert.pem\n") + if err := pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes}); err != nil { + log.Fatalf("failed to write data to cert.pem: %s", err) + } + if err := certOut.Close(); err != nil { + log.Fatalf("error closing cert.pem: %s", err) + } + log.Print("wrote cert.pem\n") keyOut, err := os.OpenFile("key.pem", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) if err != nil { log.Print("failed to open key.pem for writing:", err) return } - pem.Encode(keyOut, pemBlockForKey(priv)) - keyOut.Close() - log.Print("written key.pem\n") + if err := pem.Encode(keyOut, pemBlockForKey(priv)); err != nil { + log.Fatalf("failed to write data to key.pem: %s", err) + } + if err := keyOut.Close(); err != nil { + log.Fatalf("error closing key.pem: %s", err) + } + log.Print("wrote key.pem\n") }