Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions internal/cmd/database/ip_restriction.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,14 @@ func IPRestrictionCmd(ch *cmdutil.Helper) *cobra.Command {
Short: "Manage Postgres IP restrictions",
Long: `Manage IP restrictions for a PostgreSQL database.

IP restrictions limit which IPv4 addresses or ranges can connect. Rules can
optionally be scoped to a specific Postgres schema and/or role. This command
is only available for PostgreSQL databases.`,
IP restrictions are an allowlist of IPv4 CIDR ranges. With no rules, all
connections are allowed. When one or more rules exist, each connection must
match at least one rule (client IP in a CIDR range, role empty or matching
the login role, and database empty or matching the connection database).

Rules apply to all branches of the database, including branches created later,
and cannot be scoped to a single branch. This command is only available for
PostgreSQL databases.`,
}

cmd.AddCommand(IPRestrictionListCmd(ch))
Expand All @@ -37,7 +42,7 @@ is only available for PostgreSQL databases.`,
// IPRestrictionEntry is the human/JSON/CSV view of a Postgres IP restriction entry.
type IPRestrictionEntry struct {
ID string `header:"id" json:"id"`
Schema string `header:"schema" json:"schema"`
Schema string `header:"database" json:"schema"`
Role string `header:"role" json:"role"`
CIDRs string `header:"cidrs" json:"cidrs"`
Description string `header:"description" json:"description"`
Expand Down
17 changes: 12 additions & 5 deletions internal/cmd/database/ip_restriction_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,18 @@ func IPRestrictionCreateCmd(ch *cmdutil.Helper) *cobra.Command {
cmd := &cobra.Command{
Use: "create <database>",
Short: "Create an IP restriction entry",
Long: `Create an IP restriction entry for a PostgreSQL database.
Long: `Create an IP restriction allowlist rule for a PostgreSQL database.

At least one IPv4 CIDR is required (e.g. 192.168.1.0/24 or 203.0.113.10/32).
Omit --schema / --role (or leave them empty) to apply the restriction to all
schemas and roles.`,
Rules are an allowlist and apply to all branches of the database (including
branches created later); they cannot be scoped to a single branch. Once any
rule exists, every connection must match at least one rule.

Scoping a rule with --role allows that role from the given CIDRs and does not
allow other roles to connect unless another rule matches them. --schema matches
the PostgreSQL logical database name in the connection startup packet (not a
schema within a database). Omit --schema or --role to allow all databases or
roles.`,
Args: cmdutil.RequiredArgs("database"),
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return cmdutil.DatabaseCompletionFunc(ch, cmd, args, toComplete)
Expand Down Expand Up @@ -72,8 +79,8 @@ schemas and roles.`,
}

cmd.Flags().StringSliceVar(&flags.cidrs, "cidrs", nil, "IPv4 CIDR ranges to allow (required, repeatable or comma-separated)")
cmd.Flags().StringVar(&flags.schema, "schema", "", "Postgres schema to restrict (omit for all schemas)")
cmd.Flags().StringVar(&flags.role, "role", "", "Postgres role to restrict (omit for all roles)")
cmd.Flags().StringVar(&flags.schema, "schema", "", "PostgreSQL logical database name to allow (matches the connection database, not a schema; omit for all databases)")
cmd.Flags().StringVar(&flags.role, "role", "", "PostgreSQL role to allow (omit for all roles)")
cmd.Flags().StringVar(&flags.description, "description", "", "Optional description for the IP restriction entry")

cmd.MarkFlagRequired("cidrs") // nolint:errcheck
Expand Down
10 changes: 7 additions & 3 deletions internal/cmd/database/ip_restriction_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ func IPRestrictionDeleteCmd(ch *cmdutil.Helper) *cobra.Command {
var force bool

cmd := &cobra.Command{
Use: "delete <database> <entry-id>",
Short: "Delete an IP restriction entry",
Long: "Delete an IP restriction entry from a PostgreSQL database. Deleting an entry removes the restriction from all database branches.",
Use: "delete <database> <entry-id>",
Short: "Delete an IP restriction entry",
Long: `Delete an IP restriction allowlist rule from a PostgreSQL database.

Rules apply to all branches of the database. Deleting a rule removes the access
it granted; deleting the last remaining rule allows connections from all IPs on
all branches.`,
Args: cmdutil.RequiredArgs("database", "entry-id"),
Aliases: []string{"rm"},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
Expand Down
6 changes: 5 additions & 1 deletion internal/cmd/database/ip_restriction_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ func IPRestrictionListCmd(ch *cmdutil.Helper) *cobra.Command {
cmd := &cobra.Command{
Use: "list <database>",
Short: "List IP restriction entries for a Postgres database",
Args: cmdutil.RequiredArgs("database"),
Long: `List IP restriction allowlist rules for a PostgreSQL database.

Rules apply to all branches of the database. When no rules exist, all
connections are allowed.`,
Args: cmdutil.RequiredArgs("database"),
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return cmdutil.DatabaseCompletionFunc(ch, cmd, args, toComplete)
},
Expand Down
13 changes: 8 additions & 5 deletions internal/cmd/database/ip_restriction_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ func IPRestrictionUpdateCmd(ch *cmdutil.Helper) *cobra.Command {
cmd := &cobra.Command{
Use: "update <database> <entry-id>",
Short: "Update an IP restriction entry",
Long: `Update an IP restriction entry for a PostgreSQL database.
Long: `Update an IP restriction allowlist rule for a PostgreSQL database.

Only provided flags are changed. Pass an empty --description to clear it.
Pass an empty --schema or --role to allow access for all schemas or roles.`,
Only provided flags are changed. Rules are an allowlist and apply to all
branches of the database (including branches created later). --schema matches
the PostgreSQL logical database name in the connection startup packet (not a
schema within a database). Pass an empty --schema or --role to match all
databases or roles. Pass an empty --description to clear it.`,
Args: cmdutil.RequiredArgs("database", "entry-id"),
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return cmdutil.DatabaseCompletionFunc(ch, cmd, args, toComplete)
Expand Down Expand Up @@ -90,8 +93,8 @@ Pass an empty --schema or --role to allow access for all schemas or roles.`,
}

cmd.Flags().StringSliceVar(&flags.cidrs, "cidrs", nil, "IPv4 CIDR ranges to allow (replaces the existing list)")
cmd.Flags().StringVar(&flags.schema, "schema", "", "Postgres schema to restrict (empty for all schemas)")
cmd.Flags().StringVar(&flags.role, "role", "", "Postgres role to restrict (empty for all roles)")
cmd.Flags().StringVar(&flags.schema, "schema", "", "PostgreSQL logical database name to allow (matches the connection database, not a schema; empty for all databases)")
cmd.Flags().StringVar(&flags.role, "role", "", "PostgreSQL role to allow (empty for all roles)")
cmd.Flags().StringVar(&flags.description, "description", "", "Description for the IP restriction entry (empty to clear)")

return cmd
Expand Down