changed file properties table so "ON DELETE CASCADE" works
This commit is contained in:
		
							parent
							
								
									0eaf4b1fb1
								
							
						
					
					
						commit
						d1423e4af9
					
				
					 6 changed files with 38 additions and 16 deletions
				
			
		| 
						 | 
					@ -22,15 +22,29 @@ func GetFileByID(fileID string) (sqlc.File, error) {
 | 
				
			||||||
	return file, nil
 | 
						return file, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func DeleteFileByID(fileID string) error {
 | 
				
			||||||
 | 
						var pgUUID pgtype.UUID
 | 
				
			||||||
 | 
						err := pgUUID.Scan(fileID)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							slog.Error("Unable to convert string to UUID", "file-uuid", fileID, "error", err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						query := sqlc.New(pool)
 | 
				
			||||||
 | 
						err = query.DeleteFile(context.Background(), pgUUID)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							slog.Error("Unable to delete file", "file-uuid", fileID, "error", err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return err
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func InsertFileProperties(properties sqlc.InsertFilePropertiesParams) error {
 | 
					func InsertFileProperties(properties sqlc.InsertFilePropertiesParams) error {
 | 
				
			||||||
	query := sqlc.New(pool)
 | 
						query := sqlc.New(pool)
 | 
				
			||||||
	slog.Debug("InsertFileProperties", "file-uuid", properties.ID.String(), "file-sha256",
 | 
						slog.Debug("InsertFileProperties", "file-uuid", properties.FileID.String(), "file-sha256",
 | 
				
			||||||
		hex.EncodeToString(properties.Sha256), "file-md5", hex.EncodeToString(properties.Md5),
 | 
							hex.EncodeToString(properties.Sha256), "file-md5", hex.EncodeToString(properties.Md5),
 | 
				
			||||||
		"file-mime", properties.LibmagicMime.String, "file-extension", properties.LibmagicExtension.String,
 | 
							"file-mime", properties.LibmagicMime.String, "file-extension", properties.LibmagicExtension.String,
 | 
				
			||||||
		"file-apple", properties.LibmagicApple.String)
 | 
							"file-apple", properties.LibmagicApple.String)
 | 
				
			||||||
	err := query.InsertFileProperties(context.Background(), properties)
 | 
						err := query.InsertFileProperties(context.Background(), properties)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		slog.Error("Unable to add file properties", "file-uuid", properties.ID.String(), "error", err)
 | 
							slog.Error("Unable to add file properties", "file-uuid", properties.FileID.String(), "error", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return err
 | 
						return err
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,11 +1,12 @@
 | 
				
			||||||
-- name: InsertFileProperties :exec
 | 
					-- name: InsertFileProperties :exec
 | 
				
			||||||
INSERT INTO file_properties (
 | 
					INSERT INTO file_properties (
 | 
				
			||||||
  id, sha256, md5, libmagic_mime, libmagic_extension, libmagic_apple
 | 
					  file_id, sha256, md5, libmagic_mime, libmagic_extension, libmagic_apple
 | 
				
			||||||
) VALUES ($1, $2, $3, $4, $5, $6);
 | 
					) VALUES ($1, $2, $3, $4, $5, $6)
 | 
				
			||||||
 | 
					;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
-- name: GetFileProperties :one
 | 
					-- name: GetFileProperties :one
 | 
				
			||||||
SELECT * FROM file_properties
 | 
					SELECT * FROM file_properties
 | 
				
			||||||
WHERE id = $1;
 | 
					WHERE file_id = $1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
-- name: InsertFileDIEC :exec
 | 
					-- name: InsertFileDIEC :exec
 | 
				
			||||||
INSERT INTO diec (
 | 
					INSERT INTO diec (
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -69,7 +69,8 @@ CREATE TABLE IF NOT EXISTS msoffice (
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
CREATE TABLE IF NOT EXISTS file_properties (
 | 
					CREATE TABLE IF NOT EXISTS file_properties (
 | 
				
			||||||
  id UUID PRIMARY KEY,
 | 
					  id BIGSERIAL PRIMARY KEY,
 | 
				
			||||||
 | 
					  file_id UUID REFERENCES files (id) ON DELETE CASCADE,
 | 
				
			||||||
  sha256 BYTEA,
 | 
					  sha256 BYTEA,
 | 
				
			||||||
  md5 BYTEA,
 | 
					  md5 BYTEA,
 | 
				
			||||||
  libmagic_mime TEXT,
 | 
					  libmagic_mime TEXT,
 | 
				
			||||||
| 
						 | 
					@ -91,5 +92,6 @@ CREATE INDEX idx_msoffice_oleid_file_id ON msoffice_oleid (file_id);
 | 
				
			||||||
CREATE INDEX idx_msoffice_olevba_file_id ON msoffice_olevba (file_id);
 | 
					CREATE INDEX idx_msoffice_olevba_file_id ON msoffice_olevba (file_id);
 | 
				
			||||||
CREATE INDEX idx_msoffice_mraptor_file_id ON msoffice_mraptor (file_id);
 | 
					CREATE INDEX idx_msoffice_mraptor_file_id ON msoffice_mraptor (file_id);
 | 
				
			||||||
CREATE INDEX idx_msoffice_results_file_id ON msoffice (file_id);
 | 
					CREATE INDEX idx_msoffice_results_file_id ON msoffice (file_id);
 | 
				
			||||||
CREATE INDEX idx_file_properties_id ON file_properties (id);
 | 
					CREATE INDEX idx_file_properties_file_id ON file_properties (file_id);
 | 
				
			||||||
CREATE INDEX idx_file_id ON files (id);
 | 
					CREATE INDEX idx_file_id ON files (id);
 | 
				
			||||||
 | 
					CREATE INDEX idx_yara_results_file_id ON yara_results (file_id);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -28,7 +28,10 @@ func BasicProcessing(job sqlc.ProcessingJob) error {
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	fileProperties := sqlc.InsertFilePropertiesParams{}
 | 
						fileProperties := sqlc.InsertFilePropertiesParams{}
 | 
				
			||||||
	fileProperties.ID = job.FileID
 | 
						fileProperties.LibmagicMime.Valid = true
 | 
				
			||||||
 | 
						fileProperties.LibmagicApple.Valid = true
 | 
				
			||||||
 | 
						fileProperties.LibmagicExtension.Valid = true
 | 
				
			||||||
 | 
						fileProperties.FileID = job.FileID
 | 
				
			||||||
	fileProperties.Md5 = md5sum[:]
 | 
						fileProperties.Md5 = md5sum[:]
 | 
				
			||||||
	fileProperties.Sha256 = sha256sum[:]
 | 
						fileProperties.Sha256 = sha256sum[:]
 | 
				
			||||||
	fileProperties.LibmagicMime.String = fileCmdResult.MimeType
 | 
						fileProperties.LibmagicMime.String = fileCmdResult.MimeType
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -26,7 +26,8 @@ type File struct {
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type FileProperty struct {
 | 
					type FileProperty struct {
 | 
				
			||||||
	ID                pgtype.UUID
 | 
						ID                int64
 | 
				
			||||||
 | 
						FileID            pgtype.UUID
 | 
				
			||||||
	Sha256            []byte
 | 
						Sha256            []byte
 | 
				
			||||||
	Md5               []byte
 | 
						Md5               []byte
 | 
				
			||||||
	LibmagicMime      pgtype.Text
 | 
						LibmagicMime      pgtype.Text
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -12,15 +12,16 @@ import (
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const getFileProperties = `-- name: GetFileProperties :one
 | 
					const getFileProperties = `-- name: GetFileProperties :one
 | 
				
			||||||
SELECT id, sha256, md5, libmagic_mime, libmagic_extension, libmagic_apple FROM file_properties
 | 
					SELECT id, file_id, sha256, md5, libmagic_mime, libmagic_extension, libmagic_apple FROM file_properties
 | 
				
			||||||
WHERE id = $1
 | 
					WHERE file_id = $1
 | 
				
			||||||
`
 | 
					`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (q *Queries) GetFileProperties(ctx context.Context, id pgtype.UUID) (FileProperty, error) {
 | 
					func (q *Queries) GetFileProperties(ctx context.Context, fileID pgtype.UUID) (FileProperty, error) {
 | 
				
			||||||
	row := q.db.QueryRow(ctx, getFileProperties, id)
 | 
						row := q.db.QueryRow(ctx, getFileProperties, fileID)
 | 
				
			||||||
	var i FileProperty
 | 
						var i FileProperty
 | 
				
			||||||
	err := row.Scan(
 | 
						err := row.Scan(
 | 
				
			||||||
		&i.ID,
 | 
							&i.ID,
 | 
				
			||||||
 | 
							&i.FileID,
 | 
				
			||||||
		&i.Sha256,
 | 
							&i.Sha256,
 | 
				
			||||||
		&i.Md5,
 | 
							&i.Md5,
 | 
				
			||||||
		&i.LibmagicMime,
 | 
							&i.LibmagicMime,
 | 
				
			||||||
| 
						 | 
					@ -48,12 +49,12 @@ func (q *Queries) InsertFileDIEC(ctx context.Context, arg InsertFileDIECParams)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const insertFileProperties = `-- name: InsertFileProperties :exec
 | 
					const insertFileProperties = `-- name: InsertFileProperties :exec
 | 
				
			||||||
INSERT INTO file_properties (
 | 
					INSERT INTO file_properties (
 | 
				
			||||||
  id, sha256, md5, libmagic_mime, libmagic_extension, libmagic_apple
 | 
					  file_id, sha256, md5, libmagic_mime, libmagic_extension, libmagic_apple
 | 
				
			||||||
) VALUES ($1, $2, $3, $4, $5, $6)
 | 
					) VALUES ($1, $2, $3, $4, $5, $6)
 | 
				
			||||||
`
 | 
					`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type InsertFilePropertiesParams struct {
 | 
					type InsertFilePropertiesParams struct {
 | 
				
			||||||
	ID                pgtype.UUID
 | 
						FileID            pgtype.UUID
 | 
				
			||||||
	Sha256            []byte
 | 
						Sha256            []byte
 | 
				
			||||||
	Md5               []byte
 | 
						Md5               []byte
 | 
				
			||||||
	LibmagicMime      pgtype.Text
 | 
						LibmagicMime      pgtype.Text
 | 
				
			||||||
| 
						 | 
					@ -63,7 +64,7 @@ type InsertFilePropertiesParams struct {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (q *Queries) InsertFileProperties(ctx context.Context, arg InsertFilePropertiesParams) error {
 | 
					func (q *Queries) InsertFileProperties(ctx context.Context, arg InsertFilePropertiesParams) error {
 | 
				
			||||||
	_, err := q.db.Exec(ctx, insertFileProperties,
 | 
						_, err := q.db.Exec(ctx, insertFileProperties,
 | 
				
			||||||
		arg.ID,
 | 
							arg.FileID,
 | 
				
			||||||
		arg.Sha256,
 | 
							arg.Sha256,
 | 
				
			||||||
		arg.Md5,
 | 
							arg.Md5,
 | 
				
			||||||
		arg.LibmagicMime,
 | 
							arg.LibmagicMime,
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue