MCPcopy Index your code
hub / github.com/coder/coder / ensureTLSConfig

Method ensureTLSConfig

cli/root.go:626–670  ·  view source on GitHub ↗

ensureTLSConfig loads the TLS configuration from files if specified. The resulting config is used for both API requests and DERP connections. If tlsConfig is already set programmatically, file-based configuration is skipped.

()

Source from the content-addressed store, hash-verified

624// The resulting config is used for both API requests and DERP connections.
625// If tlsConfig is already set programmatically, file-based configuration is skipped.
626func (r *RootCmd) ensureTLSConfig() error {
627 // Already loaded or programmatically set - skip file loading
628 if r.tlsConfig != nil {
629 return nil
630 }
631
632 // No TLS config needed
633 if r.tlsCAFile == "" && r.tlsClientCertFile == "" && r.tlsClientKeyFile == "" {
634 return nil
635 }
636
637 // Validate that cert and key are specified together
638 if (r.tlsClientCertFile == "") != (r.tlsClientKeyFile == "") {
639 return xerrors.Errorf("--%s and --%s must be specified together", varClientTLSCertFile, varClientTLSKeyFile)
640 }
641
642 tlsConfig := &tls.Config{
643 MinVersion: tls.VersionTLS12,
644 }
645
646 // Load CA certificate if specified
647 if r.tlsCAFile != "" {
648 caData, err := os.ReadFile(r.tlsCAFile)
649 if err != nil {
650 return xerrors.Errorf("read TLS CA file %q: %w", r.tlsCAFile, err)
651 }
652 caPool := x509.NewCertPool()
653 if !caPool.AppendCertsFromPEM(caData) {
654 return xerrors.Errorf("failed to parse CA certificate in %q", r.tlsCAFile)
655 }
656 tlsConfig.RootCAs = caPool
657 }
658
659 // Load client certificate if specified
660 if r.tlsClientCertFile != "" && r.tlsClientKeyFile != "" {
661 cert, err := tls.LoadX509KeyPair(r.tlsClientCertFile, r.tlsClientKeyFile)
662 if err != nil {
663 return xerrors.Errorf("load TLS client certificate: %w", err)
664 }
665 tlsConfig.Certificates = []tls.Certificate{cert}
666 }
667
668 r.tlsConfig = tlsConfig
669 return nil
670}
671
672// InitClient creates and configures a new client with authentication, telemetry,
673// and version checks.

Callers 4

Test_ensureTLSConfigFunction · 0.95
InitClientMethod · 0.95
TryInitClientMethod · 0.95

Calls 2

ReadFileMethod · 0.65
ErrorfMethod · 0.45

Tested by 1

Test_ensureTLSConfigFunction · 0.76