Skip to content

Allow restricting snapshot management commands to acting on preconfigured paths and backends #14562

Description

@brandond

Is your feature request related to a problem? Please describe.
Currently, cluster administrators can use the k3s etcd-snapshot CLI (or undocumented snapshot HTTP API) to create and delete snapshots against any local filesystem path or S3-compatible backend. The paths and/or backends do not necessarily have to be the same as those configured for scheduled snapshots, which allows administrators a high level of flexibility.

Describe the solution you'd like
Some administrators have asked for the ability to restrict on-demand snapshots and snapshot management commands (delete) to using the same snapshot directory and/or etcd backend configuration as scheduled snapshots. This offers administrators a sense of security in the form of knowing that etcd snapshots can only be created or deleted against predefined paths and backends, at the cost of reduced flexibility.

Describe alternatives you've considered
Not have this feature, and continue to allow administrators to create/delete snapshots against any path or storage backend.

Additional context

Activity

  1. added theissue type on Aug 31, 2026
  2. added
    kind/enhancement
    on Aug 31, 2026
  3. moved this from To Triage to Accepted in K3s Developmenton Aug 31, 2026
  4. changed the title [-]Allow restricting on-demand etcd snapshots to preconfigured paths and backends[/-] [+]Allow restricting snapshot management commands to acting on preconfigured paths and backends[/+] on Aug 31, 2026
  5. sRiSh-JaNa-tech commented on Sep 16, 2026

    @sRiSh-JaNa-tech
    Contributor

    Hi team,

    My suggestion for this would be to add an opt-in server setting such as --etcd-snapshot-restrict-destination. When enabled, the server's existing snapshot configuration would become the source of truth for all snapshot management operations (save, delete, prune, and list).

    When the option is enabled, the /db/snapshot handler could validate the destination from the incoming SnapshotRequest before performing the requested operation.

    For local snapshots, the requested directory should resolve to the same directory configured by EtcdSnapshotDir, including the default snapshot directory when no explicit directory is configured.

    For S3 snapshots, the request should only be allowed when S3 is configured on the server, and the requested S3 destination should match the server's configured target, such as the endpoint, bucket, folder, region, and other fields that define where the snapshots are stored. For EtcdS3.ConfigSecret, the validation should use the server's active/resolved S3 configuration rather than relying on client-supplied credentials or endpoint settings.

    I think this validation is better placed in the server handler rather than only in the CLI, since /db/snapshot can also be called directly without going through the CLI. It would also be useful to preserve EtcdSnapshotDir in withRequest() when SnapshotRequest.Dir is not provided, so requests without an explicit override naturally continue using the server's configured snapshot directory.

    For delete, snapshot names should also be treated strictly as filenames rather than paths. This would prevent path traversal outside the validated snapshot directory, especially since the current implementation builds the deletion path using filepath.Join(snapshotDir, s).

    The new restriction could default to false so existing behavior and scripts remain unchanged. When enabled, any explicit destination that differs from the server's configured destination would be rejected before the operation runs, while requests without overrides would continue using the existing server configuration.

    Overall, this would give administrators an opt-in way to ensure that scheduled snapshots and on-demand snapshot management always operate against the same configured local directory and/or S3 backend, while keeping the current behavior unchanged for users who do not enable the restriction.

  6. brandond commented on Sep 16, 2026

    @brandond
    MemberAuthor

    When the option is enabled, the /db/snapshot handler could validate the destination from the incoming SnapshotRequest before performing the requested operation.

    For local snapshots, the requested directory should resolve to the same directory configured by EtcdSnapshotDir, including the default snapshot directory when no explicit directory is configured.

    For S3 snapshots, the request should only be allowed when S3 is configured on the server, and the requested S3 destination should match the server's configured target, such as the endpoint, bucket, folder, region, and other fields that define where the snapshots are stored. For EtcdS3.ConfigSecret, the validation should use the server's active/resolved S3 configuration rather than relying on client-supplied credentials or endpoint settings.

    I don't want to bike shed too much about the flag name at this point but... I don't know if we need separate flags for restricting the local path and the s3 config, or a single flag for both, or what.

    An easier way to handle this would just be to validate that the data-dir and s3 config in the incoming request are empty/nil, and reject the request if they are not. Since the user-provided values are used to override the defaults, this would ensure that they are not modified, without having to do a bunch of extra work.

    I could see wanting to allow users to manage snapshots UNDER the configured paths (any relative path or prefix that does not traverse outside the parent directory) but that would be more work than just requiring the users to not set the directory or endpoint. From a UX perspective, the CLI could avoid setting the affected values from config.yaml, if it sees the restriction flags present.

    Alternately, the backend could just ignore the restricted fields, and add a warning header in the response (that is printed by the client) indicating that the snapshot is being saved with the defaults. There are warning header helpers in the Kubernetes codebase for this.

  7. moved this from Accepted to Done Issue in K3s Developmenton Sep 16, 2026
  8. added this to the Backlog milestone on Sep 16, 2026
  9. sRiSh-JaNa-tech commented on Sep 17, 2026

    @sRiSh-JaNa-tech
    Contributor

    When the option is enabled, the /db/snapshot handler could validate the destination from the incoming SnapshotRequest before performing the requested operation.
    For local snapshots, the requested directory should resolve to the same directory configured by EtcdSnapshotDir, including the default snapshot directory when no explicit directory is configured.
    For S3 snapshots, the request should only be allowed when S3 is configured on the server, and the requested S3 destination should match the server's configured target, such as the endpoint, bucket, folder, region, and other fields that define where the snapshots are stored. For EtcdS3.ConfigSecret, the validation should use the server's active/resolved S3 configuration rather than relying on client-supplied credentials or endpoint settings.

    I don't want to bike shed too much about the flag name at this point but... I don't know if we need separate flags for restricting the local path and the s3 config, or a single flag for both, or what.

    An easier way to handle this would just be to validate that the data-dir and s3 config in the incoming request are empty/nil, and reject the request if they are not. Since the user-provided values are used to override the defaults, this would ensure that they are not modified, without having to do a bunch of extra work.

    I could see wanting to allow users to manage snapshots UNDER the configured paths (any relative path or prefix that does not traverse outside the parent directory) but that would be more work than just requiring the users to not set the directory or endpoint. From a UX perspective, the CLI could avoid setting the affected values from config.yaml, if it sees the restriction flags present.

    Alternately, the backend could just ignore the restricted fields, and add a warning header in the response (that is printed by the client) indicating that the snapshot is being saved with the defaults. There are warning header helpers in the Kubernetes codebase for this.

    I went through the current snapshot request flow and your suggestions again, and I think I thought about a simpler implementation direction now.

    My understanding is that we don't really need to compare the requested path/S3 config against the server config. Since SnapshotRequest.Dir and SnapshotRequest.S3 are used specifically as overrides, we can treat the presence of those fields as the thing we want to restrict.

    So my current plan is:

    • Add an opt-in server setting for the restriction. I’m using --etcd-snapshot-restrict-destination as the working name for now, but I’m not attached to the name.
    • Store that setting in the server Control config.
    • In pkg/etcd/snapshot_handler.go, check the incoming SnapshotRequest before dispatching the operation. When the restriction is enabled, a non-empty Dir or non-nil S3 override would be rejected.
    • Keep withRequest() using the server's existing EtcdSnapshotDir and EtcdS3 when those fields are not present in the request, so the server configuration remains the source of truth.
    • In pkg/cli/etcdsnapshot/etcd_snapshot.go, handle the config-file case so that the CLI doesn't turn values from config.yaml into explicit overrides when the restriction is enabled. This seems important because commandSetup() currently populates SnapshotRequest.Dir and SnapshotRequest.S3 from the CLI/config.
    • Add tests around the handler for restricted/unrestricted requests, and integration coverage for the snapshot commands.

    For delete, I also noticed the snapshot name is eventually passed through filepath.Join(snapshotDir, s), so I think it makes sense to make sure the supplied snapshot names are treated as filenames and can't escape the configured directory.

    That would give us a pretty small enforcement point:

    CLI / HTTP API
          |
          v
     /db/snapshot
          |
          v
    check restriction
      |            |
    override?     no override
      |            |
    reject       use server config
                   |
                   v
              save/list/prune/delete
    

    This also keeps the restriction on the server side, so calling /db/snapshot directly can't bypass it.

    Does this sound like the direction you had in mind? If so, I’ll trace the exact config/CLI plumbing and start working from there.

  10. brandond commented on Sep 17, 2026

    @brandond
    MemberAuthor

    I would suggest maybe something like:

      &cli.StringSliceFlag{
        Name:        "etcd-snapshot-restrictions",
        Usage:       "(db) Enforce restrictions on snapshot configuration; when set the selected defaults cannot be overridden via 'etcd-snapshot' options (valid values: zero or more of 'snapshot-dir', 's3-endpoint', 's3-bucket', 's3-folder', 's3-proxy', 'all')",
        Destination: &ServerConfig.EtcdSnapshotRestrictions,
      },

    For deletions, the command has not historically required that the user pass a full path, just the snapshot name. This MUST continue to work properly when deleting snapshots from S3. If S3 is enabled, the expectation is that the snapshot is deleted both from local disk, and S3.

  11. self-assigned this
    on Oct 7, 2026
  12. mdrahman-suse commented on Oct 13, 2026

    @mdrahman-suse

    QA Validation Report: etcd Snapshot Restrictions

    PR: [k3s-io/k3s#14678](#14678)

    Issue: [#14562](https://www.google.com/search?q=https://github.com/k3s-io/k3s/issues/14562)

    Tested Commit: 82a6ddcd820bdfdd787455fae69e79afb5ef3102 on main branch

    Test Date: October 12, 2026

    Result: ✅ PASSED (All 12 Tests Passed)


    Environment Details

    Component Value
    Operating System Linux (Ubuntu / RHEL) x86_64
    K3s Binary / Installation INSTALL_K3S_COMMIT=82a6ddcd820bdfdd787455fae69e79afb5ef3102
    Cluster Architecture Embedded etcd (--cluster-init)
    Storage Backend Local disk + S3-compatible backend

    Configurations Evaluated

    • Config 1 (Single Local Restriction): etcd-snapshot-restrictions: "snapshot-dir"
    • Config 2 (Selective S3 Restrictions): etcd-snapshot-restrictions: "s3-bucket,s3-folder"
    • Config 3 (Global Enforcement): etcd-snapshot-restrictions: "all"
    • Config 4 (Native YAML List Syntax): etcd-snapshot-restrictions: ["snapshot-dir", "s3-bucket", "s3-proxy"]
    • Config 5 (Compact CSV Syntax): etcd-snapshot-restrictions: "snapshot-dir,s3-bucket,s3-proxy"
    • Config 6 (Unrestricted / Baseline): No restriction flag set (Legacy compatibility)

    Test Results

    Test ID Category Description Configuration Expected Log / Behavior Status
    TC-1.1a Functional Attempt override --etcd-snapshot-dir via CLI Config 1 Warning: snapshot-dir override ignored by server-side snapshot restrictions... ✅ PASS
    TC-1.1b Functional Run standard k3s etcd-snapshot save Config 1 Clean snapshot saved to server-configured directory ✅ PASS
    TC-1.2a Functional Single override --etcd-s3-bucket on multi-flag config Config 2 Warning: s3-bucket override ignored by server-side snapshot restrictions... ✅ PASS
    TC-1.2b Functional Single override --etcd-s3-folder on multi-flag config Config 2 Warning: s3-folder override ignored by server-side snapshot restrictions... ✅ PASS
    TC-1.2c Functional Multiple simultaneous overrides (s3-bucket & s3-folder) Config 2 Warning: restricted snapshot options were ignored: s3-bucket, s3-folder... ✅ PASS
    TC-1.3a Functional Global override attempt for --etcd-snapshot-dir Config 3 Warning: snapshot-dir override ignored... ✅ PASS
    TC-1.3b Functional Global override attempt for --etcd-s3-endpoint Config 3 Warning: s3-endpoint override ignored... ✅ PASS
    TC-1.3c Functional Global override attempt for --etcd-s3-proxy Config 3 Warning: s3-proxy override ignored... ✅ PASS
    TC-2.1a Edge Case YAML array parsing for snapshot-dir Config 4 Warning: snapshot-dir override ignored... ✅ PASS
    TC-2.1b Edge Case YAML array parsing for s3-proxy Config 4 Warning: s3-proxy override ignored... ✅ PASS
    TC-2.2 Edge Case Compact CSV parsing without spaces Config 5 Space-free string parsed cleanly; override ignored ✅ PASS
    TC-3.1 Regression Manual override with no restriction flag set Config 6 Allowed; overrides function as in previous releases ✅ PASS

    Key QA Findings & Notes

    1. Non-Blocking Fallback Behavior:
      When a restricted flag is supplied via CLI, K3s does not abort execution with a non-zero exit code. Instead, it logs a descriptive warning, ignores the unauthorized parameters, and falls back to the server-configured destination.
    2. Template Output Differences:
    • Single flag override:
      msg="<flag> override ignored by server-side snapshot restrictions. Using the server-configured destination."
    • Multiple flag overrides:
      msg="restricted snapshot options were ignored: <flag1>, <flag2>. Using the server-configured snapshot destination."
    1. Configuration Formatting Note:
      Whitespace padding inside comma-separated strings (e.g., "flag1, flag2") is not automatically trimmed by the config parser and will cause validation errors during service initialization. Multi-flag restrictions must be defined as either standard YAML lists or unpadded CSV strings ("flag1,flag2").

    2. Helper command:

    $ k3s etcd-snapshot --help
    NAME:
       k3s etcd-snapshot - Manage etcd snapshots
    
    USAGE:
       k3s etcd-snapshot [command options]
    
    COMMANDS:
       save         Trigger an immediate etcd snapshot
       delete       Delete given snapshot(s)
       ls, list, l  List snapshots
       prune        Remove snapshots that match the name prefix that exceed the configured retention count
       help, h      Shows a list of commands or help for one command
    
    OPTIONS:
       --etcd-snapshot-restrictions value [ --etcd-snapshot-restrictions value ]  (db) Enforce restrictions on snapshot configuration; when set the selected defaults cannot be overridden via 'etcd-snapshot' options (valid values: zero or more of 'snapshot-dir', 's3-endpoint', 's3-bucket', 's3-folder', 's3-proxy', 'all')
  13. mdrahman-suse commented on Oct 21, 2026

    @mdrahman-suse

    Closing as validate on the release branch commits

  14. moved this from To Test to Done Issue in K3s Developmenton Oct 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area/etcdkind/enhancement

Projects

Relationships

None yet

Development

No branches or pull requests

Issue actions