Richard John Catalano
Richard John Catalano

Reputation: 449

Golang ListenAndServeTLS won't take my certificate

This has really stumped me...I'm just trying to setup a basic server using Cobra and the standard http package. I've followed the golang example here for how to create a certificate and key, but no matter what I throw at it, I'm not getting it to go through and keep getting this error:

Cannot serve on http port: crypto/tls: failed to find certificate PEM data in certificate input, but did find a private key; PEM inputs may have been switched

so I am left with the belief that it must be something wrong with my program that won't let me process certificates.

This is what I use to parse my flags to start the server:

var serverCmd = &cobra.Command{
    Use:   "server",
    Short: "start a compiler server",
    Run: func(cmd *cobra.Command, args []string) {
        addrUnsecure := ""
        addrSecure := ""

        addrUnsecure += ":" + strconv.FormatUint(serverPort, 10)
        addrSecure += ":" + strconv.FormatUint(securePort, 10)

        if noSSL {
            addrSecure = ""
        } else {
            if secureOnly {
                addrUnsecure = ""
            }
            if _, err := os.Stat(serverKey); os.IsNotExist(err) {
                log.Error("Can't find ssl key %s. Use --no-ssl flag to disable", serverKey)
                os.Exit(1)
            }
            if _, err := os.Stat(serverCert); os.IsNotExist(err) {
                log.Error("Can't find ssl cert %s. Use --no-ssl flag to disable", serverCert)
                os.Exit(1)
            }
        }

        server.StartServer(addrUnsecure, addrSecure, serverCert, serverKey)
    },
}

func addServerFlags() {
    serverCmd.Flags().Uint64VarP(&serverPort, "port", "p", setServerPort(), "set the listening port for http")
    serverCmd.Flags().Uint64VarP(&securePort, "secure-port", "s", setSecurePort(), "set the listening port for https")
    serverCmd.Flags().BoolVarP(&noSSL, "no-ssl", "n", setSSL(), "use only http")
    serverCmd.Flags().BoolVarP(&secureOnly, "secure-only", "o", setSecureOnly(), "use only https")
    serverCmd.Flags().StringVarP(&serverCert, "cert", "c", setDefaultServerCert(), "set the https certificate")
    serverCmd.Flags().StringVarP(&serverKey, "key", "k", setDefaultServerKey(), "set the key to interact with the https certificate")
}

func setServerPort() uint64 {
    return 9099
}

func setSecurePort() uint64 {
    return 9098
}

func setSSL() bool {
    return false
}

func setSecureOnly() bool {
    return false
}

func setDefaultServerCert() string {
    return ""
}

func setDefaultServerKey() string {
    return ""
}

and this is the function where the actual server is started:

func StartServer(addrUnsecure, addrSecure, key, cert string) {
    log.Warn("Hello I'm the marmots' compilers server")
    common.InitErisDir()
    // Routes

    http.HandleFunc("/", CompileHandler)
    // Use SSL ?
    log.Debug(cert)
    if addrSecure != "" {
        log.Debug("Using HTTPS")
        log.WithField("=>", addrSecure).Debug("Listening on...")
        if err := http.ListenAndServeTLS(addrUnsecure, cert, key, nil); err != nil {
            log.Error("Cannot serve on http port: ", err)
            os.Exit(1)
        }
    }
    if addrUnsecure != "" {
        log.Debug("Using HTTP")
        log.WithField("=>", addrUnsecure).Debug("Listening on...")
        if err := http.ListenAndServe(addrUnsecure, nil); err != nil {
            log.Error("Cannot serve on http port: ", err)
            os.Exit(1)
        }
    }
}

I've gotten this in both Docker running Ubuntu 14.04 and OS X locally on my machine. Thanks in advance to whoever can help me out. It's kind of embarassing because this seems like it should be a really simple thing to do and I'm willing to bet it's going to be something dumb that's got me here.

Upvotes: 0

Views: 1990

Answers (1)

Danilo
Danilo

Reputation: 3327

func StartServer(addrUnsecure, addrSecure, key, cert string) {

so key is at 3. place, cert at 4. place in the parameter list.

server.StartServer(addrUnsecure, addrSecure, serverCert, serverKey)

Now you are calling that func with cert being at the 3. place and key at the 4. place.

Maybe swapping them will help.

Upvotes: 3

Related Questions