Search code examples
gocaddy

Caddy Server Certmagic - invalid memory address


I'm trying to setup a server using certmagic but every time I run the code I just get the below stack message, everywhere i have looked online says you just need to do certmagic.HTTPS([]string{"my-domain.com"}, router) but there must be something missing here?

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x6dd407]

goroutine 1 [running]:
go.uber.org/zap.(*Logger).check(0x0, 0xff, 0xc000026858, 0x11, 0xc000064f40)
    /.../go/pkg/mod/go.uber.org/[email protected]/logger.go:265 +0x987
go.uber.org/zap.(*Logger).Debug(0x0, 0xc000026858, 0x11, 0xc000064f40, 0x1, 0x1)
    /.../go/pkg/mod/go.uber.org/[email protected]/logger.go:180 +0x45
github.com/caddyserver/certmagic.(*Config).obtainCert.func2(0x8f2e70, 0xc0000240e0, 0x8f5370, 0xb14d30)
    /.../go/pkg/mod/github.com/caddyserver/[email protected]/config.go:511 +0x5c5
github.com/caddyserver/certmagic.(*Config).obtainCert(0xc0000952c0, 0x8f2e70, 0xc0000240e0, 0x873baf, 0x16, 0x1, 0x0, 0x0)
    /.../go/pkg/mod/github.com/caddyserver/[email protected]/config.go:566 +0x3a8
github.com/caddyserver/certmagic.(*Config).ObtainCertSync(...)
    /.../go/pkg/mod/github.com/caddyserver/[email protected]/config.go:421
github.com/caddyserver/certmagic.(*Config).manageOne.func1(0x842da0, 0xc000064400)
    /.../go/pkg/mod/github.com/caddyserver/[email protected]/config.go:334 +0x412
github.com/caddyserver/certmagic.(*Config).manageOne(0xc0000952c0, 0x8f2e70, 0xc0000240e0, 0x873baf, 0x16, 0x0, 0x0, 0x8f5370)
    /.../go/pkg/mod/github.com/caddyserver/[email protected]/config.go:361 +0x27b
github.com/caddyserver/certmagic.(*Config).manageAll(0xc0000952c0, 0x8f2e70, 0xc0000240e0, 0xc000020f60, 0x1, 0x1, 0xb45100, 0x0, 0x0)
    /.../go/pkg/mod/github.com/caddyserver/[email protected]/config.go:312 +0x1f1
github.com/caddyserver/certmagic.(*Config).ManageSync(...)
    /.../go/pkg/mod/github.com/caddyserver/[email protected]/config.go:251
github.com/caddyserver/certmagic.HTTPS(0xc000020f60, 0x1, 0x1, 0x8ec800, 0xc0000da0c0, 0x0, 0x0)
    /.../go/pkg/mod/github.com/caddyserver/[email protected]/certmagic.go:76 +0xdb
main.main()
    /var/www/test/main.go:17 +0xe7

The code is as bare minimum as it can be at the moment as just starting this from scratch to understand how it works before looking at expanding it to other projects

package main

import (
    "github.com/caddyserver/certmagic"
    "github.com/gorilla/mux"
    "log"
    "net/http"
)

func main() {
    router := mux.NewRouter()
    router.HandleFunc("/", func (w http.ResponseWriter, r *http.Request) {
        _, _ = w.Write([]byte("Hello world"))
    })
    log.Fatal(certmagic.HTTPS([]string{"my-domain.com"}, router))
}

and even simpler (and an example provided on certmagic) gets the same error stack

func main() {
    handler := http.HandlerFunc(func (w http.ResponseWriter, r *http.Request) {
        _, _ = w.Write([]byte("Hello world"))
    })
    certmagic.HTTPS([]string{"my-domain.com"}, handler)
}

Solution

  • This is a known bug in certmagic which has already been fixed and merged.

    You can see the details here: https://github.com/caddyserver/certmagic/pull/135

    They seem to release pretty often, so can either wait a few days for the new release, or use the main branch instead of v0.14.0, meanwhile.