Go TLS Configuration Generator (tls/tls.go)

due · core/tls/tls.go · go · MIT
security, database, infra, lang:go

Generates secure TLS and mTLS configurations for Redis and TCP clients using custom certificates. Reuse for securing backend microservices and databases in Go.

// Source: https://github.com/dobyte/due | License: MIT | Copyright 2022 dobyte
// Provided via justclaw.it.com; keep this notice with the code.
package tls

import (
	"crypto/tls"
	"crypto/x509"
	"os"

	"github.com/dobyte/due/v2/errors"
)

func MakeRedisTLSConfig(certFile, keyFile, caFile string) (*tls.Config, error) {
	// Load client cert
	cert, err := tls.LoadX509KeyPair(certFile, keyFile)
	if err != nil {
		return nil, err
	}

	// Load CA cert
	caCert, err := os.ReadFile(caFile)
	if err != nil {
		return nil, err
	}

	caCertPool := x509.NewCertPool()

	if !caCertPool.AppendCertsFromPEM(caCert) {
		return nil, errors.ErrInvalidCertFile
	}

	return &tls.Config{
		MinVersion:   tls.VersionTLS12,
		Certificates: []tls.Certificate{cert},
		RootCAs:      caCertPool,
	}, nil
}

func MakeTCPCl

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key