-
Notifications
You must be signed in to change notification settings - Fork 24
ASI-07: Add mTLS and HMAC request signing for agent↔gateway communication #4679
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e77703c
Initial plan
Copilot c19af07
feat(ASI-07): add mTLS and HMAC request signing for agent↔gateway com…
Copilot e37e740
refactor: simplify test cert helpers using writePEMFile/os.WriteFile
Copilot 8f56e9f
fix: address review feedback on mTLS/HMAC implementation
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package cmd | ||
|
|
||
| // TLS and HMAC security flags (ASI-07: Secure Agent↔Gateway Communication) | ||
|
|
||
| import ( | ||
| "github.com/github/gh-aw-mcpg/internal/envutil" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| // TLS/HMAC flag variables | ||
| var ( | ||
| tlsCertPath string | ||
| tlsKeyPath string | ||
| tlsCAPath string | ||
| hmacSecret string | ||
| ) | ||
|
|
||
| func init() { | ||
| RegisterFlag(func(cmd *cobra.Command) { | ||
| cmd.Flags().StringVar(&tlsCertPath, "tls-cert", envutil.GetEnvString("MCP_GATEWAY_TLS_CERT", ""), "Path to TLS server certificate PEM file (enables HTTPS)") | ||
| cmd.Flags().StringVar(&tlsKeyPath, "tls-key", envutil.GetEnvString("MCP_GATEWAY_TLS_KEY", ""), "Path to TLS server private key PEM file (enables HTTPS)") | ||
| cmd.Flags().StringVar(&tlsCAPath, "tls-ca", envutil.GetEnvString("MCP_GATEWAY_CA_CERT", ""), "Path to CA certificate PEM file for client certificate verification (enables mTLS)") | ||
| cmd.Flags().StringVar(&hmacSecret, "hmac-secret", envutil.GetEnvString("MCP_GATEWAY_HMAC_SECRET", ""), "Shared HMAC-SHA256 secret for request signing and replay protection") | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| package server | ||
|
|
||
| import ( | ||
| "crypto/tls" | ||
| "crypto/x509" | ||
| "fmt" | ||
| "os" | ||
|
|
||
| "github.com/github/gh-aw-mcpg/internal/logger" | ||
| ) | ||
|
|
||
| var logGatewayTLS = logger.New("server:tls") | ||
|
|
||
| // LoadGatewayTLS loads a TLS configuration for the gateway HTTP server from PEM | ||
| // certificate and key files. When caPath is non-empty the returned config | ||
| // requires client certificates signed by that CA (mutual TLS / mTLS). | ||
| // | ||
| // Pass an empty caPath to use one-way TLS (server-only authentication). | ||
| // | ||
| // Example — one-way TLS (server cert only): | ||
| // | ||
| // tlsCfg, err := LoadGatewayTLS("/path/server.crt", "/path/server.key", "") | ||
| // | ||
| // Example — mutual TLS (client certs required): | ||
| // | ||
| // tlsCfg, err := LoadGatewayTLS("/path/server.crt", "/path/server.key", "/path/ca.crt") | ||
| func LoadGatewayTLS(certPath, keyPath, caPath string) (*tls.Config, error) { | ||
| logGatewayTLS.Printf("loading gateway TLS: cert=%s, key=%s, ca=%s", certPath, keyPath, caPath) | ||
|
|
||
| serverCert, err := tls.LoadX509KeyPair(certPath, keyPath) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to load server TLS certificate/key: %w", err) | ||
| } | ||
| logGatewayTLS.Print("server TLS key pair loaded") | ||
|
|
||
| cfg := &tls.Config{ | ||
| Certificates: []tls.Certificate{serverCert}, | ||
| MinVersion: tls.VersionTLS12, | ||
| } | ||
|
|
||
| if caPath != "" { | ||
| caPEM, err := os.ReadFile(caPath) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to read CA certificate: %w", err) | ||
| } | ||
|
|
||
| caPool := x509.NewCertPool() | ||
| if !caPool.AppendCertsFromPEM(caPEM) { | ||
| return nil, fmt.Errorf("failed to parse CA certificate from %s", caPath) | ||
| } | ||
|
|
||
| // Require and verify client certificates signed by the provided CA. | ||
| cfg.ClientCAs = caPool | ||
| cfg.ClientAuth = tls.RequireAndVerifyClientCert | ||
| logGatewayTLS.Printf("mTLS enabled: client certificates required, CA=%s", caPath) | ||
| } | ||
|
|
||
| return cfg, nil | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When TLS is enabled via
--tls-cert/--tls-key, the server will speak HTTPS on the listener, butwriteGatewayConfigToStdoutstill emitshttp://...URLs. This will cause clients that consume the emitted config to attempt plaintext connections and fail. Consider threading the effective scheme (http/https) into the config writer (or deriving it from the TLS flags) so the emitted URLs match the actual listener.