Skip to content

Commit 30f40f1

Browse files
committed
fix: accept legacy redirect HTTP environment variables
1 parent ba955f4 commit 30f40f1

File tree

4 files changed

+102
-7
lines changed

4 files changed

+102
-7
lines changed

cli/clibase/cmd.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,15 @@ func (inv *Invocation) SignalNotifyContext(parent context.Context, signals ...os
226226
return inv.signalNotifyContext(parent, signals...)
227227
}
228228

229+
func (inv *Invocation) WithTestParsedFlags(
230+
_ testing.TB, // ensure we only call this from tests
231+
parsedFlags *pflag.FlagSet,
232+
) *Invocation {
233+
return inv.with(func(i *Invocation) {
234+
i.parsedFlags = parsedFlags
235+
})
236+
}
237+
229238
func (inv *Invocation) Context() context.Context {
230239
if inv.ctx == nil {
231240
return context.Background()

cli/server.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2319,12 +2319,7 @@ func ConfigureHTTPServers(logger slog.Logger, inv *clibase.Invocation, cfg *code
23192319
return nil, xerrors.New("tls address must be set if tls is enabled")
23202320
}
23212321

2322-
// DEPRECATED: This redirect used to default to true.
2323-
// It made more sense to have the redirect be opt-in.
2324-
if inv.Environ.Get("CODER_TLS_REDIRECT_HTTP") == "true" || inv.ParsedFlags().Changed("tls-redirect-http-to-https") {
2325-
logger.Warn(ctx, "--tls-redirect-http-to-https is deprecated, please use --redirect-to-access-url instead")
2326-
cfg.RedirectToAccessURL = cfg.TLS.RedirectHTTP
2327-
}
2322+
redirectHTTPToHTTPSDeprecation(ctx, logger, inv, cfg)
23282323

23292324
tlsConfig, err := configureServerTLS(
23302325
ctx,
@@ -2374,6 +2369,24 @@ func ConfigureHTTPServers(logger slog.Logger, inv *clibase.Invocation, cfg *code
23742369
return httpServers, nil
23752370
}
23762371

2372+
// redirectHTTPToHTTPSDeprecation handles deprecation of the --tls-redirect-http-to-https flag and
2373+
// "related" environment variables.
2374+
//
2375+
// --tls-redirect-http-to-https used to default to true.
2376+
// It made more sense to have the redirect be opt-in.
2377+
//
2378+
// Also, for a while we have been accepting the environment variable (but not the
2379+
// corresponding flag!) "CODER_TLS_REDIRECT_HTTP", and it appeared in a configuration
2380+
// example, so we keep accepting it to not break backward compat.
2381+
func redirectHTTPToHTTPSDeprecation(ctx context.Context, logger slog.Logger, inv *clibase.Invocation, cfg *codersdk.DeploymentValues) {
2382+
if inv.Environ.Get("CODER_TLS_REDIRECT_HTTP") == "true" ||
2383+
inv.Environ.Get("CODER_TLS_REDIRECT_HTTP_TO_HTTPS") == "true" ||
2384+
inv.ParsedFlags().Changed("tls-redirect-http-to-https") {
2385+
logger.Warn(ctx, "--tls-redirect-http-to-https is deprecated, please use --redirect-to-access-url instead")
2386+
cfg.RedirectToAccessURL = cfg.TLS.RedirectHTTP
2387+
}
2388+
}
2389+
23772390
// ReadExternalAuthProvidersFromEnv is provided for compatibility purposes with
23782391
// the viper CLI.
23792392
func ReadExternalAuthProvidersFromEnv(environ []string) ([]codersdk.ExternalAuthConfig, error) {

cli/ssh_internal_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@ import (
44
"net/url"
55
"testing"
66

7+
"github.com/spf13/pflag"
78
"github.com/stretchr/testify/assert"
89
"github.com/stretchr/testify/require"
910

11+
"cdr.dev/slog/sloggers/slogtest"
12+
13+
"github.com/coder/coder/v2/cli/clibase"
1014
"github.com/coder/coder/v2/codersdk"
15+
"github.com/coder/coder/v2/testutil"
1116
)
1217

1318
const (
@@ -56,3 +61,71 @@ func TestBuildWorkspaceLink(t *testing.T) {
5661

5762
assert.Equal(t, workspaceLink.String(), fakeServerURL+"/@"+fakeOwnerName+"/"+fakeWorkspaceName)
5863
}
64+
65+
func TestRedirectHTTPToHTTPSDeprecation(t *testing.T) {
66+
t.Parallel()
67+
68+
testcases := []struct {
69+
name string
70+
environ clibase.Environ
71+
flags []string
72+
expected bool
73+
}{
74+
{
75+
name: "AllUnset",
76+
environ: clibase.Environ{},
77+
flags: []string{},
78+
expected: false,
79+
},
80+
{
81+
name: "CODER_TLS_REDIRECT_HTTP=true",
82+
environ: clibase.Environ{{Name: "CODER_TLS_REDIRECT_HTTP", Value: "true"}},
83+
flags: []string{},
84+
expected: true,
85+
},
86+
{
87+
name: "CODER_TLS_REDIRECT_HTTP_TO_HTTPS=true",
88+
environ: clibase.Environ{{Name: "CODER_TLS_REDIRECT_HTTP_TO_HTTPS", Value: "true"}},
89+
flags: []string{},
90+
expected: true,
91+
},
92+
{
93+
name: "CODER_TLS_REDIRECT_HTTP=false",
94+
environ: clibase.Environ{{Name: "CODER_TLS_REDIRECT_HTTP", Value: "false"}},
95+
flags: []string{},
96+
expected: false,
97+
},
98+
{
99+
name: "CODER_TLS_REDIRECT_HTTP_TO_HTTPS=false",
100+
environ: clibase.Environ{{Name: "CODER_TLS_REDIRECT_HTTP_TO_HTTPS", Value: "false"}},
101+
flags: []string{},
102+
expected: false,
103+
},
104+
{
105+
name: "--tls-redirect-http-to-https",
106+
environ: clibase.Environ{},
107+
flags: []string{"--tls-redirect-http-to-https"},
108+
expected: true,
109+
},
110+
}
111+
112+
for _, tc := range testcases {
113+
tc := tc
114+
t.Run(tc.name, func(t *testing.T) {
115+
t.Parallel()
116+
ctx := testutil.Context(t, testutil.WaitShort)
117+
logger := slogtest.Make(t, nil)
118+
flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
119+
_ = flags.Bool("tls-redirect-http-to-https", true, "")
120+
err := flags.Parse(tc.flags)
121+
require.NoError(t, err)
122+
inv := (&clibase.Invocation{Environ: tc.environ}).WithTestParsedFlags(t, flags)
123+
cfg := &codersdk.DeploymentValues{}
124+
opts := cfg.Options()
125+
err = opts.SetDefaults()
126+
require.NoError(t, err)
127+
redirectHTTPToHTTPSDeprecation(ctx, logger, inv, cfg)
128+
require.Equal(t, tc.expected, cfg.RedirectToAccessURL.Value())
129+
})
130+
}
131+
}

docs/admin/configure.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export CODER_TLS_ENABLE=true
2929
export CODER_TLS_ADDRESS=0.0.0.0:443
3030

3131
## Redirect from HTTP to HTTPS
32-
export CODER_TLS_REDIRECT_HTTP=true
32+
export CODER_REDIRECT_TO_ACCESS_URL=true
3333

3434
# Start the Coder server
3535
coder server

0 commit comments

Comments
 (0)