Skip to content

Commit ea7d948

Browse files
committed
feat(backend): enable SSL by default for backend create
Make --use-ssl default to true when creating backends to provide better security defaults. Add a --no-use-ssl flag. Update documentation and tests.
1 parent 2ca8021 commit ea7d948

3 files changed

Lines changed: 73 additions & 2 deletions

File tree

pkg/app/metadata.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,18 @@
159159
"examples": [
160160
{
161161
"cmd": "fastly backend create --name example --address example.com --version active --autoclone",
162-
"description": "Create a backend with a hostname assigned to the `--address` flag. The `--override-host`, `--ssl-cert-hostname` and `--ssl-sni-hostname` will default to the same hostname assigned to `--address`.",
162+
"description": "Create a backend with a hostname assigned to the `--address` flag. The `--override-host`, `--ssl-cert-hostname` and `--ssl-sni-hostname` will default to the same hostname assigned to `--address`. SSL is enabled by default with port 443.",
163163
"title": "Create a backend on the currently active service version"
164+
},
165+
{
166+
"cmd": "fastly backend create --name my_backend --address http-me.fastly.dev --version latest --autoclone",
167+
"description": "Create a backend with SSL enabled by default. The backend will use port 443 unless explicitly specified with `--port`.",
168+
"title": "Create a backend with default SSL settings"
169+
},
170+
{
171+
"cmd": "fastly backend create --name my_backend --address http-me.fastly.dev --version latest --autoclone --no-use-ssl --port 80",
172+
"description": "Create a backend without SSL using the `--no-use-ssl` flag. When SSL is disabled, you should explicitly specify the port.",
173+
"title": "Create a backend without SSL"
164174
}
165175
],
166176
"apis": [

pkg/commands/backend/backend_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,32 @@ func TestBackendCreate(t *testing.T) {
205205
},
206206
WantError: "'prefer-ipv6' flag must be one of the following [true, false]",
207207
},
208+
// The following test verifies that --use-ssl defaults to true and port defaults to 443
209+
{
210+
Args: "--service-id 123 --version 3 --address http-me.fastly.dev --name my_backend",
211+
API: mock.API{
212+
ListVersionsFn: testutil.ListVersions,
213+
CreateBackendFn: createBackendWithSSL(true, 443),
214+
},
215+
WantOutput: "Created backend my_backend (service 123 version 3)",
216+
},
217+
// The following test verifies that --no-use-ssl disables SSL
218+
{
219+
Args: "--service-id 123 --version 3 --address http-me.fastly.dev --name my_backend --no-use-ssl --port 80",
220+
API: mock.API{
221+
ListVersionsFn: testutil.ListVersions,
222+
CreateBackendFn: createBackendWithSSL(false, 80),
223+
},
224+
WantOutput: "Created backend my_backend (service 123 version 3)",
225+
},
226+
// The following test verifies that both --use-ssl and --no-use-ssl results in an error
227+
{
228+
Args: "--service-id 123 --version 3 --address http-me.fastly.dev --name my_backend --use-ssl --no-use-ssl",
229+
API: mock.API{
230+
ListVersionsFn: testutil.ListVersions,
231+
},
232+
WantError: "cannot specify both --use-ssl and --no-use-ssl",
233+
},
208234
}
209235
testutil.RunCLIScenarios(t, []string{root.CommandName, "create"}, scenarios)
210236
}
@@ -454,6 +480,18 @@ func createBackendWithPort(wantPort int) func(_ context.Context, _ *fastly.Creat
454480
}
455481
}
456482

483+
func createBackendWithSSL(wantSSL bool, wantPort int) func(_ context.Context, _ *fastly.CreateBackendInput) (*fastly.Backend, error) {
484+
return func(ctx context.Context, i *fastly.CreateBackendInput) (*fastly.Backend, error) {
485+
useSSL := i.UseSSL != nil && bool(*i.UseSSL)
486+
port := i.Port != nil && *i.Port == wantPort
487+
488+
if useSSL == wantSSL && port {
489+
return createBackendOK(ctx, i)
490+
}
491+
return createBackendError(ctx, i)
492+
}
493+
}
494+
457495
func listBackendsOK(_ context.Context, i *fastly.ListBackendsInput) ([]*fastly.Backend, error) {
458496
return []*fastly.Backend{
459497
{

pkg/commands/backend/create.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ type CreateCommand struct {
5656
tcpKaTime argparser.OptionalInt
5757
httpKaTime argparser.OptionalInt
5858
useSSL argparser.OptionalBool
59+
noUseSSL argparser.OptionalBool
5960
weight argparser.OptionalInt
6061
}
6162

@@ -123,7 +124,8 @@ func NewCreateCommand(parent argparser.Registerer, g *global.Data) *CreateComman
123124
c.CmdClause.Flag("tcp-ka-probes", "Configure how many unacknowledged TCP keepalive probes to send before considering the connection dead.").Action(c.tcpKaProbes.Set).IntVar(&c.tcpKaProbes.Value)
124125
c.CmdClause.Flag("tcp-ka-time", "Configure how long to wait after the last sent data before sending TCP keepalive probes.").Action(c.tcpKaTime.Set).IntVar(&c.tcpKaTime.Value)
125126
c.CmdClause.Flag("http-ka-time", "Configure how long to keep idle HTTP keepalive connections in the connection pool.").Action(c.httpKaTime.Set).IntVar(&c.httpKaTime.Value)
126-
c.CmdClause.Flag("use-ssl", "Whether or not to use SSL to reach the backend").Action(c.useSSL.Set).BoolVar(&c.useSSL.Value)
127+
c.CmdClause.Flag("use-ssl", "Whether or not to use SSL to reach the backend (default: true)").Action(c.useSSL.Set).BoolVar(&c.useSSL.Value)
128+
c.CmdClause.Flag("no-use-ssl", "Disable SSL for the backend (overrides default)").Action(c.noUseSSL.Set).BoolVar(&c.noUseSSL.Value)
127129
c.CmdClause.Flag("weight", "Weight used to load balance this backend against others").Action(c.weight.Set).IntVar(&c.weight.Value)
128130

129131
return &c
@@ -268,15 +270,36 @@ func (c *CreateCommand) Exec(_ io.Reader, out io.Writer) error {
268270
if c.weight.WasSet {
269271
input.Weight = &c.weight.Value
270272
}
273+
// Set UseSSL: handle --use-ssl, --no-use-ssl, and default behavior
274+
if c.noUseSSL.WasSet && c.useSSL.WasSet {
275+
err := errors.New("cannot specify both --use-ssl and --no-use-ssl")
276+
c.Globals.ErrLog.Add(err)
277+
return err
278+
}
279+
if c.noUseSSL.WasSet {
280+
input.UseSSL = fastly.ToPointer(fastly.Compatibool(false))
281+
} else if c.useSSL.WasSet {
282+
input.UseSSL = fastly.ToPointer(fastly.Compatibool(c.useSSL.Value))
283+
} else {
284+
// Default to true
285+
input.UseSSL = fastly.ToPointer(fastly.Compatibool(true))
286+
}
271287

272288
switch {
273289
case c.port.WasSet:
274290
input.Port = &c.port.Value
291+
case c.noUseSSL.WasSet:
292+
// If --no-use-ssl is set, don't set a default port
293+
// User should specify --port explicitly
275294
case c.useSSL.WasSet && c.useSSL.Value:
295+
// If use-ssl is explicitly set to true and no port is specified, use 443
276296
if c.Globals.Flags.Verbose {
277297
text.Warning(out, "Use-ssl was set but no port was specified, using default port 443\n\n")
278298
}
279299
input.Port = fastly.ToPointer(443)
300+
case !c.useSSL.WasSet && !c.noUseSSL.WasSet:
301+
// If neither flag is set (defaults to SSL true), use 443
302+
input.Port = fastly.ToPointer(443)
280303
}
281304

282305
if input.Address != nil && !c.overrideHost.WasSet && !c.sslCertHostname.WasSet && !c.sslSNIHostname.WasSet {

0 commit comments

Comments
 (0)