Skip to content

Commit 3d782f7

Browse files
author
Rachel Williams
committed
TUN-3048: Handle error when user tries to delete active tunnel
1 parent 058598e commit 3d782f7

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

cmd/cloudflared/tunnel/subcommands.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ var (
5555
Aliases: []string{credFileFlagAlias},
5656
Usage: "File path of tunnel credentials",
5757
}
58+
forceDeleteFlag = &cli.BoolFlag{
59+
Name: "force",
60+
Aliases: []string{"f"},
61+
Usage: "Allows you to delete a tunnel, even if it has active connections.",
62+
}
5863
)
5964

6065
const hideSubcommands = true
@@ -309,7 +314,7 @@ func buildDeleteCommand() *cli.Command {
309314
Usage: "Delete existing tunnel with given ID",
310315
ArgsUsage: "TUNNEL-ID",
311316
Hidden: hideSubcommands,
312-
Flags: []cli.Flag{credentialsFileFlag},
317+
Flags: []cli.Flag{credentialsFileFlag, forceDeleteFlag},
313318
}
314319
}
315320

@@ -330,6 +335,28 @@ func deleteTunnel(c *cli.Context) error {
330335
}
331336
client := newTunnelstoreClient(c, cert, logger)
332337

338+
forceFlagSet := c.Bool("force")
339+
340+
tunnel, err := client.GetTunnel(id)
341+
if err != nil {
342+
return errors.Wrapf(err, "Can't get tunnel information. Please check tunnel id: %s", id)
343+
}
344+
345+
// Check if tunnel DeletedAt field has already been set
346+
if !tunnel.DeletedAt.IsZero() {
347+
return errors.New("This tunnel has already been deleted.")
348+
}
349+
// Check if tunnel has existing connections and if force flag is set, cleanup connections
350+
if len(tunnel.Connections) > 0 {
351+
if !forceFlagSet {
352+
return errors.New("You can not delete this tunnel because it has active connections. To see connections run the 'list' command. If you believe the tunnel is not active, you can use a -f / --force flag with this command.")
353+
}
354+
355+
if err := client.CleanupConnections(id); err != nil {
356+
return errors.Wrapf(err, "Error cleaning up connections for tunnel %s", id)
357+
}
358+
}
359+
333360
if err := client.DeleteTunnel(id); err != nil {
334361
return errors.Wrapf(err, "Error deleting tunnel %s", id)
335362
}

0 commit comments

Comments
 (0)