Go: x509 certificate signed by unknown authority
Go cannot build a chain to a trusted root: no CA bundle in the image, a private CA, or a missing intermediate. Install roots or set RootCAs.

Introduction & Quick Summary
Get "https://api.internal.example/v1/health": tls: failed to verify certificate:
x509: certificate signed by unknown authorityGo verified the server's certificate chain against its root pool and found no root that signs it. The connection is refused, not downgraded. Either the machine has no CA bundle (very common in containers), or the certificate really is signed by a private or self-signed authority that Go does not trust.
The cause is in one of three layers:
- The runtime has no trusted roots.
FROM scratchand some distroless images ship without/etc/ssl/certs/ca-certificates.crt. - The server uses a private CA. An internal service, a test environment, or a corporate TLS-inspecting proxy.
- The chain is incomplete or wrong. The server sends a leaf certificate without its intermediate, so no path to a root can be built.
Diagnose from outside Go first, because it distinguishes the layers in one command:
openssl s_client -connect api.internal.example:443 -servername api.internal.example \
-showcerts </dev/null 2>/dev/null | openssl x509 -noout -issuer -subject -dates| Observation | Meaning | Fix |
|---|---|---|
unable to get local issuer certificate | Missing intermediate | Fix the server chain |
| Chain shows a self-signed issuer | Private CA | Add its root to the pool |
| Same binary works on the host, fails in Docker | No CA bundle in image | Install or copy certificates |
Works with curl, fails in Go | Go's root pool differs | Check SSL_CERT_FILE, image contents |
| Fails only behind the office network | TLS inspection | Trust the proxy root |
Core Mechanics, Signatures & Reproducible Setup
crypto/tls calls crypto/x509's verifier with a VerifyOptions carrying Roots. When Roots is nil, Go loads the system pool once per process: /etc/ssl/certs/ca-certificates.crt, /etc/pki/tls/certs/ca-bundle.crt, /etc/ssl/cert.pem and the hashed directory /etc/ssl/certs on Unix-like systems; the OS store on Windows and macOS. A missing file is not an error at startup — the pool is simply empty, and every public HTTPS request fails with this message.
Reproduce it with a throwaway self-signed certificate:
openssl req -x509 -newkey rsa:2048 -nodes -days 1 \
-keyout key.pem -out cert.pem -subj "/CN=localhost"
openssl s_server -accept 8443 -cert cert.pem -key key.pem -www &resp, err := http.Get("https://localhost:8443")
// Get "https://localhost:8443": tls: failed to verify certificate:
// x509: certificate signed by unknown authorityThe correct fix for a known private CA:
pool, err := x509.SystemCertPool()
if err != nil {
pool = x509.NewCertPool()
}
pem, err := os.ReadFile("/etc/certs/internal-root.pem")
if err != nil {
return nil, fmt.Errorf("read CA: %w", err)
}
if !pool.AppendCertsFromPEM(pem) {
return nil, errors.New("no certificates found in CA file")
}
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{RootCAs: pool, MinVersion: tls.VersionTLS12},
},
Timeout: 10 * time.Second,
}AppendCertsFromPEM returning false is the quiet failure to watch for: the call succeeded, the pool is unchanged, and the error stays identical.
Key Comparison Table
| Fix | Scope | Correct for |
|---|---|---|
Install ca-certificates in image | Whole container | Missing bundle |
SSL_CERT_FILE=/path/ca.pem | Process and children | Injected CA without rebuilding |
SSL_CERT_DIR=/etc/ssl/certs | Process | Hashed directory layouts |
RootCAs on tls.Config | That client only | App-specific private CA |
x509.SetFallbackRoots | Whole Go program | Custom fallback pool |
InsecureSkipVerify: true | That client, no verification | Almost never |
| Stack layout | Diagnostic command | Expected result |
|---|---|---|
| Alpine | apk info ca-certificates | Package installed |
| Debian slim | ls -l /etc/ssl/certs/ca-certificates.crt | File present |
| Distroless / scratch | docker run --rm img ls /etc/ssl/certs | May be empty |
| Any image | openssl s_client -connect ... | Full chain with root |
Practical Edge Cases, Error Fixes & Deep Dive Code Examples
Case 1: Alpine and Debian images
FROM golang:1.25-alpine AS build
WORKDIR /src
COPY . .
RUN CGO_ENABLED=0 go build -o /out/api ./cmd/api
FROM alpine:3.21
RUN apk add --no-cache ca-certificates
COPY /out/api /usr/local/bin/api
ENTRYPOINT ["/usr/local/bin/api"]CGO_ENABLED=0 makes the binary use Go's own resolver and certificate loading, which reads the bundle installed above. Without the apk add line, the image has the binary and no roots.
Case 2: scratch and distroless
FROM gcr.io/distroless/static-debian12:nonroot # includes ca-certificates
COPY /out/api /api
ENTRYPOINT ["/api"]For a true scratch image, copy the bundle from the builder:
FROM scratch
COPY /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY /out/api /api
ENTRYPOINT ["/api"]Verify inside the running container rather than trusting the Dockerfile:
docker run --rm --entrypoint sh myimage -c 'ls -l /etc/ssl/certs/ca-certificates.crt'Case 3: A corporate proxy that re-signs traffic
The proxy returns certificates signed by its own root, which no public bundle contains. Export the root once and mount it, then point the process at it:
# kubernetes deployment
env:
- name: SSL_CERT_FILE
value: /etc/corp/ca-bundle.pem
volumeMounts:
- name: corp-ca
mountPath: /etc/corp
readOnly: true
volumes:
- name: corp-ca
configMap:
name: corp-caSSL_CERT_FILE replaces the system bundle path for Go's loader; if you need both public roots and the proxy root, concatenate them into the mounted file. Setting it to a file with only the proxy root breaks connections to public sites, which is a common self-inflicted second incident.
Case 4: A server that omits its intermediate
openssl s_client -connect api.example.com:443 -servername api.example.com \
-verify_return_error </dev/null 2>&1 | grep -E 'verify error|Verify return code'
# Verify return code: 21 (unable to verify the first certificate)The client cannot build a path to a trusted root even with a complete local bundle. Fix the server: send the leaf and intermediate in that order (fullchain.pem in nginx ssl_certificate), not just the leaf. Adding the intermediate to your Go client's pool also works and multiplies the problem across every client that talks to that host.
Case 5: Per-client pool without touching globals
func newClient(caPath string) (*http.Client, error) {
rootCAs, err := x509.SystemCertPool()
if err != nil {
rootCAs = x509.NewCertPool()
}
pem, err := os.ReadFile(caPath)
if err != nil {
return nil, err
}
if ok := rootCAs.AppendCertsFromPEM(pem); !ok {
return nil, fmt.Errorf("%s: no PEM certificates", caPath)
}
return &http.Client{
Transport: &http.Transport{TLSClientConfig: &tls.Config{RootCAs: rootCAs}},
Timeout: 15 * time.Second,
}, nil
}One client for the internal service, the default client for everything else. Building a single global pool for the whole program couples unrelated dependencies to one certificate file.
Case 6: Pinning a certificate instead of trusting a CA
When you control both ends and want to reject everything else:
tlsConfig := &tls.Config{
InsecureSkipVerify: true, // verification done manually below
VerifyPeerCertificate: func(rawCerts [][]byte, _ [][]*x509.Certificate) error {
want, _ := hex.DecodeString("9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08")
got := sha256.Sum256(rawCerts[0])
if !hmac.Equal(got[:], want) {
return fmt.Errorf("certificate fingerprint mismatch: %x", got)
}
return nil
},
}Pinning survives a compromised CA but breaks on every certificate rotation, so it belongs on machine-to-machine links with a controlled rotation process — not on third-party APIs.
Common Compilation/Runtime Mistakes & Version Gotchas
InsecureSkipVerify: trueas a fix. It disables hostname and chain verification, turning a TLS error into a silent man-in-the-middle vulnerability. If it appears in a code review, require theRootCAsalternative.- Trusting the leaf as a root.
RootCAsmust contain the issuing CA certificate, not the server certificate; adding the leaf producesx509: certificate signed by unknown authoritystill. AppendCertsFromPEMreturn ignored. A PEM with only a private key, or with Windows line endings and a stray header, appends nothing and returnsfalse.SSL_CERT_FILEset to a directory. Go expects a file; a directory path silently yields an empty pool.- Time skew. An expired or not-yet-valid certificate reports as
x509: certificate has expired or is not yet valid, but a badly skewed clock in a VM can look like an authority problem in aggregated logs. - Go 1.20+ macOS/Windows verifiers. On those platforms Go uses the system verifier, so a certificate trusted in Keychain works without a bundle; the same binary in Linux CI fails, which is why container tests matter.
MinVersionand legacy endpoints. Some internal services still offer TLS 1.0; raisingMinVersionproduces a handshake error, not a verification error, and is the correct trade.
Inspect a certificate bundle by decoding it with the base64 tool after stripping the PEM headers, and check what the endpoint actually presents with the curl builder using --insecure once to see the chain it advertises.
Primary references: crypto/x509 documents CertPool, SystemCertPool and the loading rules, and crypto/tls specifies RootCAs, VerifyPeerCertificate and MinVersion.
Official references
primary documentationThe Go documentation that defines the behaviour this guide explains.
Try matching browser tools
All toolsSmall utilities that fit Go work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Go guides
All Go guidesGo: fatal error: concurrent map writes in goroutines
The runtime catches two writes to one map and stops the process. Guard access with a mutex, use sync.Map for caches, or give one owner.
Go: import cycle not allowed
Go forbids cycles in the package import graph. Break them by extracting shared types, declaring interfaces where consumed, or inverting init registration.
Go: http: superfluous response.WriteHeader call
net/http logs this when a handler writes the status twice. It almost always marks a later status write that was silently ignored, or no return.
Go: declared and not used compile error
Go rejects unused local variables and imports on purpose. Delete dead declarations, handle err values and keep imports honest instead of blanking them.
More in Go
All Go guidesThe guides filed next to this one in Go, in library order.