Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add File Shred command #513

Merged
Merged
Show file tree
Hide file tree
Changes from 4 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
8 changes: 8 additions & 0 deletions cmd/sansshell-server/default-policy.rego
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,11 @@ allow {
allow {
input.method = "/Network.Network/TCPCheck"
}

allow {
input.type = "LocalFile.ShredRequest"
input.message.filename = "/tmp/hello.txt"
input.message.force = true
input.message.zero = true
input.message.remove = true
}
25 changes: 25 additions & 0 deletions services/localfile/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,28 @@ Examples:
# Set file mode of `/opt/hello` to 644
sanssh --target $TARGET file chmod --mode=644 /opt/hello
```

### sanssh file shred
Shred file using `/usr/bin/shred` (`/opt/homebrew/bin/shred` on Mac OS) utility.
sfc-gh-iyashchyshyn marked this conversation as resolved.
Show resolved Hide resolved
Notice: this function is reliant on the presence of `shred` utility on the target system.
Note that this utility is only effective on Hard Drives, it will not provide same level of security on Solid State Drives.


```bash
sanssh <sanssh-args> file shred [-z] [-f] [-u] [--remove] <file-path>
```
Where:
- `<sanssh-args>` common sanssh arguments
- `<file-path>` is the absolute path to the file on remote machine.
- `-z` - add a final zero-ing pass to shred
- `-f` - force permissions change on the target file if required
- `-u` - remove the file after shredding
- `--remove` - alias for `-u`

Examples:
```bash
# Perform basic shredding of the file
sanssh --targets $TARGET file shred /tmp/hello.txt
# Shred, add zero-ing pass and remove file after, with file permissions change if required
sanssh --targets $TARGET file shred -f -u -z /tmp/hello.txt
```
1 change: 1 addition & 0 deletions services/localfile/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func (*fileCmd) GetSubpackage(f *flag.FlagSet) *subcommands.Commander {
c.Register(&sumCmd{}, "")
c.Register(&tailCmd{}, "")
c.Register(&mkdirCmd{}, "")
c.Register(&shredCmd{}, "")
return c
}

Expand Down
81 changes: 81 additions & 0 deletions services/localfile/client/shred.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/* Copyright (c) 2019 Snowflake Inc. All rights reserved.

Licensed under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/

package client

import (
"context"
"flag"
"fmt"
pb "github.com/Snowflake-Labs/sansshell/services/localfile"
"github.com/Snowflake-Labs/sansshell/services/util"
"github.com/google/subcommands"
"os"
)

type shredCmd struct {
force bool
zero bool
remove bool
}

func (*shredCmd) Name() string { return "shred" }
func (*shredCmd) Synopsis() string { return "Shred a file" }
func (*shredCmd) Usage() string {
return `shred <path-to-file>`
}

func (p *shredCmd) SetFlags(f *flag.FlagSet) {
f.BoolVar(&p.force, "f", false, "force permissions change in case current permissions are insufficient")
f.BoolVar(&p.remove, "u", false, "remove file after shredding - same as --remove")
f.BoolVar(&p.zero, "z", false, "add a zeroing pass after shredding to mask the shredding")
f.BoolVar(&p.remove, "remove", false, "remove file after shredding - same as -u")
}

func (p *shredCmd) Execute(ctx context.Context, f *flag.FlagSet, args ...interface{}) subcommands.ExitStatus {
state := args[0].(*util.ExecuteState)
if f.NArg() != 1 {
fmt.Fprintln(os.Stderr, "please specify a filename to shred")
return subcommands.ExitUsageError
}

req := &pb.ShredRequest{
Filename: f.Arg(0),
Force: p.force,
Zero: p.zero,
Remove: p.remove,
}

client := pb.NewLocalFileClientProxy(state.Conn)
respChan, err := client.ShredOneMany(ctx, req)

if err != nil {
// Emit this to every error file as it's not specific to a given target.
for _, e := range state.Err {
fmt.Fprintf(e, "All targets - shred error: %v\n", err)
}
return subcommands.ExitFailure
}

retCode := subcommands.ExitSuccess
for r := range respChan {
if r.Error != nil {
fmt.Fprintf(state.Err[r.Index], "shred error: %v\n", r.Error)
retCode = subcommands.ExitFailure
}
}
return retCode
}
298 changes: 199 additions & 99 deletions services/localfile/localfile.pb.go

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions services/localfile/localfile.proto
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ service LocalFile {

// Set data value to a file of specified type by provided key
rpc DataSet(DataSetRequest) returns (google.protobuf.Empty) {}

// Perform Shred on a single file
rpc Shred(ShredRequest) returns (google.protobuf.Empty) {}
}

// ReadActionRequest indicates the type of read we're performing.
Expand Down Expand Up @@ -316,3 +319,15 @@ message DataSetRequest {
string value = 4;
DataSetValueType value_type = 5;
}

// ShredRequest is a request to perform Shred operation on a specific file
message ShredRequest {
// absolute path to the file to be shredded
string filename = 1;
sfc-gh-iyashchyshyn marked this conversation as resolved.
Show resolved Hide resolved
// force permissions change if necessary
bool force = 2;
// add final zero-ing pass to mask presence of shredding
bool zero = 3;
// remove file after shredding
bool remove = 4;
}
46 changes: 43 additions & 3 deletions services/localfile/localfile_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 68 additions & 0 deletions services/localfile/localfile_grpcproxy.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading