Skip to content
Snippets Groups Projects

Add extra validation in corpus model

Merged Aditya Tiwari requested to merge 345454-add-validation-for-corpus into master
All threads resolved!

What does this MR do and why?

Adds the following validations:

  1. Package with package file.
  2. Package file in supported zip format.
  3. Unique package for corpuses.

How to set up and validate locally

1. package_with_package_file


project = Project.last.id
package1 = Packages::Package.create!(project_id: project, name: 'package3', package_type: 'generic', version: '1')

AppSec::Fuzzing::Coverage::Corpus.create!(package: package1, project: package1.project)


pry(main)> AppSec::Fuzzing::Coverage::Corpus.create!(package: package1, project: package1.project)

  TRANSACTION (0.2ms)  BEGIN /*application:console,db_config_name:main,line:/ee/app/models/app_sec/fuzzing/coverage/corpus.rb:51:in `first_package_file'*/
  Packages::PackageFile Load (0.3ms)  SELECT "packages_package_files".* FROM "packages_package_files" WHERE "packages_package_files"."package_id" = 39 ORDER BY "packages_package_files"."id" ASC LIMIT 1 /*application:console,db_config_name:main,line:/ee/app/models/app_sec/fuzzing/coverage/corpus.rb:51:in `first_package_file'*/
  Packages::PackageFile Load (0.3ms)  SELECT "packages_package_files".* FROM "packages_package_files" WHERE "packages_package_files"."package_id" = 39 ORDER BY "packages_package_files"."id" ASC LIMIT 1 /*application:console,db_config_name:main,line:/ee/app/models/app_sec/fuzzing/coverage/corpus.rb:51:in `first_package_file'*/
  TRANSACTION (0.2ms)  ROLLBACK /*application:console,db_config_name:main,line:/lib/gitlab/database.rb:312:in `rollback'*/
ActiveRecord::RecordInvalid: Validation failed: Package should have an associated package file
from /Users/aditya-work/.asdf/installs/ruby/2.7.4/lib/ruby/gems/2.7.0/gems/activerecord-6.1.4.1/lib/active_record/validations.rb:80:in `raise_validation_error'
[28] pry(main)>

2. validate_file_format

file = Packages::PackageFile.last
file.update!(file_name: 'test')

package1.package_files << file

pry(main)> AppSec::Fuzzing::Coverage::Corpus.create!(package: package1, project: package1.project)
ActiveRecord::RecordInvalid: Validation failed: Package format is not supported



3. unique package_id
package1.package_files.reload
file = Packages::PackageFile.last
file.update!(file_name: 'test.zip')

package1.package_files << file

[73] pry(main)> AppSec::Fuzzing::Coverage::Corpus.create!(package: package1, project: package1.project)
  TRANSACTION (0.2ms)  BEGIN /*application:console,db_config_name:main,line:(pry):85:in `__pry__'*/
  AppSec::Fuzzing::Coverage::Corpus Create (0.5ms)  INSERT INTO "coverage_fuzzing_corpuses" ("project_id", "package_id", "created_at", "updated_at") VALUES (20, 39, '2021-11-15 10:34:00.598980', '2021-11-15 10:34:00.598980') RETURNING "id" /*application:console,db_config_name:main,line:(pry):85:in `__pry__'*/
  TRANSACTION (0.2ms)  ROLLBACK /*application:console,db_config_name:main,line:/lib/gitlab/database.rb:312:in `rollback'*/
ActiveRecord::RecordNotUnique: PG::UniqueViolation: ERROR:  duplicate key value violates unique constraint "index_coverage_fuzzing_corpuses_on_package_id"
DETAIL:  Key (package_id)=(39) already exists.

Numbered steps to set up and validate the change are strongly suggested.

Database


╰─>$ rails db:migrate:down VERSION=20211112073413
== 20211112073413 ChangePackageIndexOnCorpus: reverting =======================
-- transaction_open?()
   -> 0.0000s
-- index_exists?(:coverage_fuzzing_corpuses, :package_id, {:name=>"index_coverage_fuzzing_corpuses_on_package_id", :algorithm=>:concurrently})
   -> 0.0063s
-- execute("SET statement_timeout TO 0")
   -> 0.0005s
-- remove_index(:coverage_fuzzing_corpuses, {:name=>"index_coverage_fuzzing_corpuses_on_package_id", :algorithm=>:concurrently, :column=>:package_id})
   -> 0.0103s
-- execute("RESET statement_timeout")
   -> 0.0007s
-- transaction_open?()
   -> 0.0000s
-- index_exists?(:coverage_fuzzing_corpuses, :package_id, {:name=>"index_coverage_fuzzing_corpuses_on_package_id", :algorithm=>:concurrently})
   -> 0.0016s
-- add_index(:coverage_fuzzing_corpuses, :package_id, {:name=>"index_coverage_fuzzing_corpuses_on_package_id", :algorithm=>:concurrently})
   -> 0.0069s
== 20211112073413 ChangePackageIndexOnCorpus: reverted (0.0345s) ==============


╰─>$ rails db:migrate
== 20211112073413 ChangePackageIndexOnCorpus: migrating =======================
-- execute("DELETE FROM coverage_fuzzing_corpuses")
   -> 0.0045s
-- index_exists?(:coverage_fuzzing_corpuses, :package_id, {:name=>"index_coverage_fuzzing_corpuses_on_package_id"})
   -> 0.0041s
-- transaction_open?()
   -> 0.0000s
-- index_exists?(:coverage_fuzzing_corpuses, :package_id, {:name=>"index_coverage_fuzzing_corpuses_on_package_id", :algorithm=>:concurrently})
   -> 0.0019s
-- execute("SET statement_timeout TO 0")
   -> 0.0006s
-- remove_index(:coverage_fuzzing_corpuses, {:name=>"index_coverage_fuzzing_corpuses_on_package_id", :algorithm=>:concurrently, :column=>:package_id})
   -> 0.0045s
-- execute("RESET statement_timeout")
   -> 0.0005s
-- transaction_open?()
   -> 0.0000s
-- index_exists?(:coverage_fuzzing_corpuses, :package_id, {:unique=>true, :name=>"index_coverage_fuzzing_corpuses_on_package_id", :algorithm=>:concurrently})
   -> 0.0013s
-- add_index(:coverage_fuzzing_corpuses, :package_id, {:unique=>true, :name=>"index_coverage_fuzzing_corpuses_on_package_id", :algorithm=>:concurrently})
   -> 0.0026s
== 20211112073413 ChangePackageIndexOnCorpus: migrated (0.0230s) ==============

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Related to #345454 (closed)

Edited by Aditya Tiwari

Merge request reports

Loading
Loading

Activity

Filter activity
  • Approvals
  • Assignees & reviewers
  • Comments (from bots)
  • Comments (from users)
  • Commits & branches
  • Edits
  • Labels
  • Lock status
  • Mentions
  • Merge request status
  • Tracking
  • no impact to data warehouse

  • Philip Cunningham removed review request for @philipcunningham

    removed review request for @philipcunningham

  • Philip Cunningham approved this merge request

    approved this merge request

  • :wave: @philipcunningham, thanks for approving this merge request.

    This is the first time the merge request is approved. To ensure full test coverage, a new pipeline has been started.

    For more info, please refer to the following links:

  • requested review from @rspeicher

  • Robert Speicher
  • Robert Speicher resolved all threads

    resolved all threads

  • Robert Speicher approved this merge request

    approved this merge request

  • @atiwari71 Approving backend but it looks like this is still databasereview pending.

  • Aditya Tiwari changed the description

    changed the description

  • Aditya Tiwari added 3240 commits

    added 3240 commits

    Compare with previous version

  • Aditya Tiwari requested review from @stomlinson

    requested review from @stomlinson

  • Database migrations

    Migrations included in this change have been executed on gitlab.com data for testing purposes. For details, please see the migration testing pipeline (limited access).

    Migration Type Total runtime Result DB size change
    20211112073413 - ChangePackageIndexOnCorpus Regular 2.5 s :white_check_mark: +0.00 B
    Runtime Histogram for all migrations
    Query Runtime Count
    0 seconds - 0.01 seconds 0
    0.01 seconds - 0.1 seconds 4
    0.1 seconds - 1 second 1
    1 second - 5 minutes 0
    5 minutes + 0

    Migration: 20211112073413 - ChangePackageIndexOnCorpus

    • Type: Regular
    • Duration: 2.5 s
    • Database size change: +0.00 B
    Query Calls Total Time Max Time Mean Time Rows
    CREATE UNIQUE INDEX CONCURRENTLY "index_coverage_fuzzing_corpuses_on_package_id" ON "coverage_fuzzing_corpuses" ("package_id") /*application:test,db_config_name:main*/
    1 71.1 ms 71.1 ms 71.1 ms 0
    SELECT "feature_gates".*
    FROM "feature_gates"
    WHERE "feature_gates"."feature_key" = $1 /*application:test,db_config_name:main*/
    1 6.4 ms 6.4 ms 6.4 ms 1
    DROP INDEX CONCURRENTLY "index_coverage_fuzzing_corpuses_on_package_id" /*application:test,db_config_name:main*/
    1 5.6 ms 5.6 ms 5.6 ms 0
    SELECT "postgres_async_indexes".*
    FROM "postgres_async_indexes"
    WHERE "postgres_async_indexes"."name" = $1
    LIMIT $2 /*application:test,db_config_name:main*/
    2 0.1 ms 0.1 ms 0.0 ms 0
    Histogram for ChangePackageIndexOnCorpus
    Query Runtime Count
    0 seconds - 0.01 seconds 0
    0.01 seconds - 0.1 seconds 4
    0.1 seconds - 1 second 1
    1 second - 5 minutes 0
    5 minutes + 0

    Other migrations pending on GitLab.com
    Migration Type Total runtime Result DB size change

    Clone Details

    Clone ID Clone Created At Clone Data Timestamp Expected Removal Time
    database-testing-940932 2021-12-15 20:30:20 UTC 2021-12-15 20:00:52 UTC 2021-12-16 08:31:39 +0000

    Artifacts


    Brought to you by gitlab-org/database-team/gitlab-com-database-testing. Epic

  • Simon Tomlinson removed review request for @stomlinson

    removed review request for @stomlinson

  • Aditya Tiwari requested review from @stomlinson

    requested review from @stomlinson

  • Simon Tomlinson approved this merge request

    approved this merge request

  • Simon Tomlinson requested review from @mayra-cabrera and removed review request for @stomlinson

    requested review from @mayra-cabrera and removed review request for @stomlinson

  • Mayra Cabrera
  • Mayra Cabrera
  • Mayra Cabrera removed review request for @mayra-cabrera

    removed review request for @mayra-cabrera

  • Aditya Tiwari added 1 commit

    added 1 commit

    • 4dd191bf - Apply 1 suggestion(s) to 1 file(s)

    Compare with previous version

  • requested review from @mayra-cabrera

  • Mayra Cabrera removed review request for @mayra-cabrera

    removed review request for @mayra-cabrera

  • Aditya Tiwari added 1 commit

    added 1 commit

    Compare with previous version

  • Aditya Tiwari added 1075 commits

    added 1075 commits

    Compare with previous version

  • Aditya Tiwari mentioned in issue #348401

    mentioned in issue #348401

  • requested review from @mayra-cabrera

  • Mayra Cabrera resolved all threads

    resolved all threads

  • Mayra Cabrera approved this merge request

    approved this merge request

  • added databaseapproved label and removed databasereviewed label

  • Mayra Cabrera enabled an automatic merge when the pipeline for c4e18f0b succeeds

    enabled an automatic merge when the pipeline for c4e18f0b succeeds

  • Thanks @atiwari71! LGTM, MWPS set since have all the approvals :rocket:

  • merged

  • Mayra Cabrera mentioned in commit 1af74623

    mentioned in commit 1af74623

  • added workflowcanary label and removed workflowstaging label

  • added workflowproduction label and removed workflowcanary label

  • mentioned in issue #345454 (closed)

  • Please register or sign in to reply
    Loading