Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add config to overwrite grpc certificate #1870

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions config-example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ grpc_listen_addr: 127.0.0.1:50443
# are doing.
grpc_allow_insecure: false

# Use separate a certificate for gRPC, this overwrites
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Use separate a certificate for gRPC, this overwrites
# Use separate a x509 certificate for gRPC, this overwrites

# the global certificate.
grpc_tls_cert_path: ""
grpc_tls_key_path: ""
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this point, with the growing amount of options it should end up like:

Suggested change
grpc_tls_cert_path: ""
grpc_tls_key_path: ""
grpc:
allow_insecure: false
tls:
cert_path: ""
tls_key_path: ""

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed, though this would create breaking changes, which @ohdearaugustin asked to avoid

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be possible to map the allow insecure to the new value.


# The Noise section includes specific configuration for the
# TS2021 Noise protocol
noise:
Expand Down
24 changes: 21 additions & 3 deletions hscontrol/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -631,9 +631,27 @@ func (h *Headscale) Serve() error {
// https://github.com/soheilhy/cmux/issues/68
// https://github.com/soheilhy/cmux/issues/91

grpcTlsConfig := &tls.Config{
NextProtos: []string{"http/1.1"},
Certificates: make([]tls.Certificate, 1),
MinVersion: tls.VersionTLS12,
}

if h.cfg.TLS.GRPCCertPath == "" && h.cfg.TLS.GRPCKeyPath == "" {
grpcTlsConfig = tlsConfig
} else {
grpcTlsConfig.Certificates[0], err = tls.LoadX509KeyPair(h.cfg.TLS.GRPCCertPath, h.cfg.TLS.GRPCKeyPath)

if err != nil {
log.Error().Err(err).Msg("Failed to set up gRPC TLS configuration")

return err
}
}

var grpcServer *grpc.Server
var grpcListener net.Listener
if tlsConfig != nil || h.cfg.GRPCAllowInsecure {
if grpcTlsConfig != nil || h.cfg.GRPCAllowInsecure {
log.Info().Msgf("Enabling remote gRPC at %s", h.cfg.GRPCAddr)

grpcOptions := []grpc.ServerOption{
Expand All @@ -646,9 +664,9 @@ func (h *Headscale) Serve() error {
),
}

if tlsConfig != nil {
if grpcTlsConfig != nil {
grpcOptions = append(grpcOptions,
grpc.Creds(credentials.NewTLS(tlsConfig)),
grpc.Creds(credentials.NewTLS(grpcTlsConfig)),
)
} else {
log.Warn().Msg("gRPC is running without security")
Expand Down
8 changes: 8 additions & 0 deletions hscontrol/types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ type DatabaseConfig struct {
type TLSConfig struct {
CertPath string
KeyPath string
GRPCCertPath string
GRPCKeyPath string

LetsEncrypt LetsEncryptConfig
}
Expand Down Expand Up @@ -303,6 +305,12 @@ func GetTLSConfig() TLSConfig {
KeyPath: util.AbsolutePathFromConfigPath(
viper.GetString("tls_key_path"),
),
GRPCCertPath: util.AbsolutePathFromConfigPath(
viper.GetString("grpc_tls_cert_path"),
),
GRPCKeyPath: util.AbsolutePathFromConfigPath(
viper.GetString("grpc_tls_key_path"),
),
}
}

Expand Down