Skip to content

Commit b6212ca

Browse files
torsmcopybara-github
authored andcommitted
Add a DeleteFile operation to the Fleetspeak FileStore and admin service, allowing for the removal of files previously stored via StoreFile.
The changes include: - Implemented `DeleteFile` in all datastore implementations. - Implemented the `DeleteFile` RPC - Added a `deletefile` command to the Fleetspeak admin CLI. This provides feature parity with the existing `StoreFile` operation, enabling better management of files stored within Fleetspeak. PiperOrigin-RevId: 792168672
1 parent ebfa04c commit b6212ca

10 files changed

Lines changed: 403 additions & 231 deletions

File tree

fleetspeak/src/server/admin/admin.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,16 @@ func (s adminServer) StoreFile(ctx context.Context, req *spb.StoreFileRequest) (
296296
return &fspb.EmptyMessage{}, nil
297297
}
298298

299+
func (s adminServer) DeleteFile(ctx context.Context, req *spb.DeleteFileRequest) (*fspb.EmptyMessage, error) {
300+
if req.ServiceName == "" || req.FileName == "" {
301+
return nil, errors.New("file must have service_name and file_name")
302+
}
303+
if err := s.store.DeleteFile(ctx, req.ServiceName, req.FileName); err != nil {
304+
return nil, err
305+
}
306+
return &fspb.EmptyMessage{}, nil
307+
}
308+
299309
func (s adminServer) KeepAlive(ctx context.Context, _ *fspb.EmptyMessage) (*fspb.EmptyMessage, error) {
300310
return &fspb.EmptyMessage{}, nil
301311
}

fleetspeak/src/server/db/store.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,10 @@ type FileStore interface {
373373
// StoreFile stores data into the Filestore, organized by service and name.
374374
StoreFile(ctx context.Context, service, name string, data io.Reader) error
375375

376+
// DeleteFile deletes a file from the filestore. It is not an error if the
377+
// file does not exist.
378+
DeleteFile(ctx context.Context, service, name string) error
379+
376380
// StatFile returns the modification time of a file previously stored by
377381
// StoreFile. Returns ErrNotFound if not found.
378382
StatFile(ctx context.Context, servce, name string) (time.Time, error)

fleetspeak/src/server/dbtesting/filestore_suite.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,20 @@ func FileStoreTest(t *testing.T, fs db.Store) {
5353
if _, _, err := fs.ReadFile(ctx, "testService", "missingFile"); err == nil || !fs.IsNotFound(err) {
5454
t.Errorf("Wrong error for ReadFile(testService, missingFile), want IsNotFound(err)=true, got %v", err)
5555
}
56+
57+
if err := fs.DeleteFile(ctx, "testService", "testFile"); err != nil {
58+
t.Errorf("Error from DeleteFile(testService, testFile): %v", err)
59+
}
60+
if _, err := fs.StatFile(ctx, "testService", "testFile"); err == nil || !fs.IsNotFound(err) {
61+
t.Errorf("Wrong error for StatFile(testService, testFile) after delete, want IsNotFound(err)=true, got %v", err)
62+
}
63+
// Deleting a non-existent file should not error.
64+
if err := fs.DeleteFile(ctx, "testService", "testFile"); err != nil {
65+
t.Errorf("Error from DeleteFile(testService, testFile) when file not present: %v", err)
66+
}
67+
if err := fs.DeleteFile(ctx, "testService", "missingFile"); err != nil {
68+
t.Errorf("Error from DeleteFile(testService, missingFile): %v", err)
69+
}
5670
}
5771

5872
func fileStoreTestSuite(t *testing.T, env DbTestEnv) {

fleetspeak/src/server/mysql/filestore.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ func (d *Datastore) StoreFile(ctx context.Context, service, name string, data io
3636
})
3737
}
3838

39+
func (d *Datastore) DeleteFile(ctx context.Context, service, name string) error {
40+
return d.runInTx(ctx, false, func(tx *sql.Tx) error {
41+
_, err := tx.ExecContext(ctx, "DELETE FROM files WHERE service = ? AND name = ?", service, name)
42+
return err
43+
})
44+
}
45+
3946
func (d *Datastore) StatFile(ctx context.Context, service, name string) (time.Time, error) {
4047
var ts int64
4148

fleetspeak/src/server/proto/fleetspeak_server/admin.pb.go

Lines changed: 298 additions & 231 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

fleetspeak/src/server/proto/fleetspeak_server/admin.proto

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ message StoreFileRequest {
9292
bytes data = 3;
9393
}
9494

95+
message DeleteFileRequest {
96+
string service_name = 1;
97+
string file_name = 2;
98+
}
99+
95100
message ListClientContactsRequest {
96101
bytes client_id = 1;
97102
}
@@ -179,6 +184,9 @@ service Admin {
179184
// StoreFile inserts a file into the Fleetspeak system.
180185
rpc StoreFile(StoreFileRequest) returns (fleetspeak.EmptyMessage) {}
181186

187+
// DeleteFile deletes a file from the Fleetspeak system.
188+
rpc DeleteFile(DeleteFileRequest) returns (fleetspeak.EmptyMessage) {}
189+
182190
// KeepAlive does as little as possible.
183191
rpc KeepAlive(fleetspeak.EmptyMessage) returns (fleetspeak.EmptyMessage) {}
184192

fleetspeak/src/server/proto/fleetspeak_server/admin_grpc.pb.go

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

fleetspeak/src/server/spanner/filestore.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ func (d *Datastore) tryStoreFile(txn *spanner.ReadWriteTransaction, service, nam
4545
return err
4646
}
4747

48+
// DeleteFile implements db.FileStore.
49+
func (d *Datastore) DeleteFile(ctx context.Context, service, name string) error {
50+
_, err := d.dbClient.ReadWriteTransaction(ctx, func(ctx context.Context, txn *spanner.ReadWriteTransaction) error {
51+
m := spanner.Delete(d.files, spanner.Key{service, name})
52+
return txn.BufferWrite([]*spanner.Mutation{m})
53+
})
54+
return err
55+
}
56+
4857
// StatFile implements db.FileStore.
4958
func (d *Datastore) StatFile(ctx context.Context, service, name string) (time.Time, error) {
5059
txn := d.dbClient.Single()

fleetspeak/src/server/sqlite/filestore.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ func (d *Datastore) StoreFile(ctx context.Context, service, name string, data io
3838
})
3939
}
4040

41+
func (d *Datastore) DeleteFile(ctx context.Context, service, name string) error {
42+
d.l.Lock()
43+
defer d.l.Unlock()
44+
return d.runInTx(func(tx *sql.Tx) error {
45+
_, err := tx.ExecContext(ctx, "DELETE FROM files WHERE service = ? AND name = ?", service, name)
46+
return err
47+
})
48+
}
49+
4150
func (d *Datastore) StatFile(ctx context.Context, service, name string) (time.Time, error) {
4251
d.l.Lock()
4352
defer d.l.Unlock()

fleetspeak/src/server/stats.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,10 @@ func (d MonitoredDatastore) StoreFile(ctx context.Context, service, name string,
282282
return d.D.StoreFile(ctx, service, name, data)
283283
}
284284

285+
func (d MonitoredDatastore) DeleteFile(ctx context.Context, service, name string) error {
286+
return d.D.DeleteFile(ctx, service, name)
287+
}
288+
285289
func (d MonitoredDatastore) StatFile(ctx context.Context, service, name string) (time.Time, error) {
286290
return d.D.StatFile(ctx, service, name)
287291
}

0 commit comments

Comments
 (0)