-
Notifications
You must be signed in to change notification settings - Fork 57
implement the On-Demand TLS feature #63
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
Open
did
wants to merge
13
commits into
basecamp:main
Choose a base branch
from
did:on-demand-tls
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+388
−1
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
60f727b
implement the On-Demand TLS functionality
did d312f96
remove debugging statements and old code
did e8bdf8a
the on-demand URL must contain the http scheme
did f9e299c
Update README.md
did 760de1c
don't check for the len of hosts when evaluating the TLSOnDemandUrl
did 49a6a48
chore: lint the code
did e30ee9c
chore: remove a debug message
did 695c319
chore: better logging when contacting the on demand url failed + writ…
did 303332c
chore: remove a warning about a wrong type
did 3324058
feat: no need to pass the host config attribute if tls-on-demand-url …
did 526eed7
feat: allow path and external url for the TLSOnDemandURL option
did fe12051
chore: update the README.md based on the new path functionality
did 24e9c04
fix: don't pass a Nil body when contacting a local host
did 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
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,45 @@ | ||
package cmd | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestDeployCommand_preRun_TLSOnDemandUrl(t *testing.T) { | ||
t.Run("TLS enabled with TLS on-demand URL should set hosts to empty string", func(t *testing.T) { | ||
deployCmd := newDeployCommand() | ||
|
||
// Set flags for TLS with on-demand URL | ||
deployCmd.cmd.Flags().Set("target", "http://localhost:8080") | ||
deployCmd.cmd.Flags().Set("tls", "true") | ||
deployCmd.cmd.Flags().Set("tls-on-demand-url", "http://example.com/validate") | ||
deployCmd.cmd.Flags().Set("host", "example.com") | ||
deployCmd.cmd.Flags().Set("path-prefix", "/") | ||
|
||
// Call preRun | ||
err := deployCmd.preRun(deployCmd.cmd, []string{"test-service"}) | ||
require.NoError(t, err) | ||
|
||
// Verify that hosts is set to empty string | ||
assert.Equal(t, []string{""}, deployCmd.args.ServiceOptions.Hosts) | ||
}) | ||
|
||
t.Run("TLS enabled without TLS on-demand URL should not modify hosts", func(t *testing.T) { | ||
deployCmd := newDeployCommand() | ||
|
||
// Set flags for TLS without on-demand URL | ||
deployCmd.cmd.Flags().Set("target", "http://localhost:8080") | ||
deployCmd.cmd.Flags().Set("tls", "true") | ||
deployCmd.cmd.Flags().Set("host", "example.com") | ||
deployCmd.cmd.Flags().Set("path-prefix", "/") | ||
|
||
// Call preRun | ||
err := deployCmd.preRun(deployCmd.cmd, []string{"test-service"}) | ||
require.NoError(t, err) | ||
|
||
// Verify that hosts is not modified | ||
assert.Equal(t, []string{"example.com"}, deployCmd.args.ServiceOptions.Hosts) | ||
}) | ||
} |
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,105 @@ | ||
package server | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"time" | ||
|
||
"log/slog" | ||
|
||
"golang.org/x/crypto/acme/autocert" | ||
) | ||
|
||
type TLSOnDemandChecker struct { | ||
service *Service | ||
options ServiceOptions | ||
} | ||
|
||
func NewTLSOnDemandChecker(service *Service) *TLSOnDemandChecker { | ||
return &TLSOnDemandChecker{ | ||
service: service, | ||
options: service.options, | ||
} | ||
} | ||
|
||
func (c *TLSOnDemandChecker) HostPolicy() (autocert.HostPolicy, error) { | ||
if c.options.TLSOnDemandUrl == "" { | ||
return autocert.HostWhitelist(c.options.Hosts...), nil | ||
} | ||
|
||
// If the URL starts with '/', treat it as a local path | ||
if len(c.options.TLSOnDemandUrl) > 0 && c.options.TLSOnDemandUrl[0] == '/' { | ||
return c.LocalHostPolicy(), nil | ||
} | ||
|
||
// Otherwise, treat as external URL | ||
_, err := url.ParseRequestURI(c.options.TLSOnDemandUrl) | ||
|
||
if err != nil { | ||
slog.Error("Unable to parse the tls_on_demand_url URL") | ||
return nil, err | ||
} | ||
|
||
return c.ExternalHostPolicy(), nil | ||
} | ||
|
||
func (c *TLSOnDemandChecker) LocalHostPolicy() autocert.HostPolicy { | ||
return func(ctx context.Context, host string) error { | ||
path := c.buildURLOrPath(host) | ||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, path, http.NoBody) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// We use httptest.NewRecorder here to route the request through the service's | ||
// load balancer and handler, capturing the response in-memory without making | ||
// a real network request. This ensures the request is processed as if it were | ||
// an external client, but avoids network overhead and complexity. | ||
recorder := httptest.NewRecorder() | ||
c.service.ServeHTTP(recorder, req) | ||
|
||
if recorder.Code != http.StatusOK { | ||
body := recorder.Body.String() | ||
|
||
if len(body) > 256 { | ||
body = body[:256] | ||
} | ||
|
||
return c.handleError(host, recorder.Code, body) | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
func (c *TLSOnDemandChecker) ExternalHostPolicy() autocert.HostPolicy { | ||
return func(ctx context.Context, host string) error { | ||
client := &http.Client{Timeout: 2 * time.Second} | ||
url := c.buildURLOrPath(host) | ||
resp, err := client.Get(url) | ||
if err != nil { | ||
return err | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
body := make([]byte, 256) | ||
n, _ := resp.Body.Read(body) | ||
bodyStr := string(body[:n]) | ||
return c.handleError(host, resp.StatusCode, bodyStr) | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
func (c *TLSOnDemandChecker) buildURLOrPath(host string) string { | ||
return fmt.Sprintf("%s?host=%s", c.options.TLSOnDemandUrl, url.QueryEscape(host)) | ||
} | ||
|
||
func (c *TLSOnDemandChecker) handleError(host string, status int, body string) error { | ||
slog.Warn("TLS on demand denied host", "host", host, "status", status, "body", body) | ||
|
||
return fmt.Errorf("%s is not allowed to get a certificate (status: %d, body: \"%s\")", host, status, body) | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.