Skip to content

Commit 23dde01

Browse files
authored
Merge pull request #124 from abnamro/2375656-remove-branch
2375656 remove branch
2 parents 4d8517f + ded4b79 commit 23dde01

File tree

115 files changed

+1154
-4755
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

115 files changed

+1154
-4755
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ components of RESC.
9696

9797
### VCS Scanner Worker flow diagram
9898
The flow diagram below shows the different stages that a VCS Scanner Worker goes through and the choices it is confronted with to
99-
come to the desired result. It first picks up a branch from the queue where it is decided, in conjunction with user input, what type of scan to run.
100-
If it is a base scan, a full scan of all commits will be carried out to look for secrets. Possible findings are stored inside the database along with the last scanned commit hash of the branch. An incremental scan, where the branch was scanned earlier, only looks at the commits that were made after the last scanned commit hash. The process of finding secrets and storing them in the database is similar as previously described.
99+
come to the desired result. It first picks up a repository from the queue where it is decided, in conjunction with user input, what type of scan to run.
100+
If it is a base scan, a full scan of all commits will be carried out to look for secrets. Possible findings are stored inside the database along with the last scanned commit hash of the repository. An incremental scan, where the repository was scanned earlier, only looks at the commits that were made after the last scanned commit hash. The process of finding secrets and storing them in the database is similar as previously described.
101101

102102
![product-screenshot!](images/RESC_Scan_Flow_Diagram.png)
103103

components/resc-backend/.coveragerc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[report]
2-
fail_under=61
2+
fail_under=60
33
exclude_lines =
44
pragma: no cover
55
if __name__ == .__main__.:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
"""remove branch
2+
3+
Revision ID: 44ac9602612b
4+
Revises: 8dd0f349b5ad
5+
Create Date: 2023-06-27 10:03:22.197295
6+
7+
"""
8+
import logging
9+
import sys
10+
11+
from alembic import op
12+
import sqlalchemy as sa
13+
14+
from sqlalchemy.engine import Inspector
15+
16+
# revision identifiers, used by Alembic.
17+
revision = '44ac9602612b'
18+
down_revision = '8dd0f349b5ad'
19+
branch_labels = None
20+
depends_on = None
21+
22+
# Logger
23+
logger = logging.getLogger()
24+
25+
26+
def upgrade():
27+
inspector = Inspector.from_engine(op.get_bind())
28+
29+
# add column repository_id to scan and finding
30+
op.add_column('finding', sa.Column('repository_id', sa.Integer(), nullable=True))
31+
op.add_column('scan', sa.Column('repository_id', sa.Integer(), nullable=True))
32+
# Fill it with corresponding contents
33+
op.execute("update finding "
34+
"set finding.repository_id = branch.repository_id "
35+
"from branch "
36+
"where branch.id = finding.branch_id")
37+
op.execute("update scan "
38+
"set scan.repository_id = branch.repository_id "
39+
"from branch "
40+
"where branch.id = scan.branch_id")
41+
# make repository_id not nullable
42+
op.alter_column('finding', 'repository_id', existing_type=sa.Integer(), nullable=False)
43+
op.alter_column('scan', 'repository_id', existing_type=sa.Integer(), nullable=False)
44+
# Add foreign key constraint from scan and finding to repository
45+
op.create_foreign_key('fk_finding_repository_id', 'finding', 'repository', ['repository_id'], ['id'])
46+
op.create_foreign_key('fk_scan_repository_id', 'scan', 'repository', ['repository_id'], ['id'])
47+
# Update unique constraint in finding with repository_id instead of branch_id
48+
op.drop_constraint('uc_finding_per_branch', 'finding', type_='unique')
49+
op.create_unique_constraint('uc_finding_per_repository', 'finding',
50+
['commit_id', 'repository_id', 'rule_name', 'file_path', 'line_number',
51+
'column_start', 'column_end'])
52+
# Drop column branch_id from finding and scan
53+
op.drop_constraint(get_foreign_key_name(inspector, 'finding', 'branch'), 'finding', type_='foreignkey')
54+
op.drop_column('finding', 'branch_id')
55+
op.drop_constraint(get_foreign_key_name(inspector, 'scan', 'branch'), 'scan', type_='foreignkey')
56+
op.drop_column('scan', 'branch_id')
57+
# Drop table branch
58+
op.drop_table('branch')
59+
60+
61+
def downgrade():
62+
# Unable to make a reliable downgrade here as there would not be enough information in the database to restore the
63+
# branch table and re-link the finding and scan tables to it. Meaning that all findings would be invalidated
64+
pass
65+
66+
67+
def get_foreign_key_name(inspector: Inspector, table_name: str, reference_table: str):
68+
foreign_keys = inspector.get_foreign_keys(table_name=table_name)
69+
for foreign_key in foreign_keys:
70+
if foreign_key["referred_table"] == reference_table:
71+
return foreign_key["name"]
72+
logger.error(f"Unable to find foreign key name for {table_name} referencing {reference_table}")
73+
sys.exit(-1)

components/resc-backend/src/resc_backend/constants.py

-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
# RWS: RESC Web Service
99
RWS_VERSION_PREFIX = "/resc/v1"
1010
RWS_ROUTE_REPOSITORIES = "/repositories"
11-
RWS_ROUTE_BRANCHES = "/branches"
1211
RWS_ROUTE_SCANS = "/scans"
1312
RWS_ROUTE_LAST_SCAN = "/last-scan"
1413
RWS_ROUTE_FINDINGS = "/findings"
@@ -42,7 +41,6 @@
4241
RWS_ROUTE_HEALTH = "/health"
4342

4443
REPOSITORIES_TAG = "resc-repositories"
45-
BRANCHES_TAG = "resc-branches"
4644
SCANS_TAG = "resc-scans"
4745
FINDINGS_TAG = "resc-findings"
4846
RULES_TAG = "resc-rules"

components/resc-backend/src/resc_backend/db/model/__init__.py

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
# First Party
1515
from resc_backend.db.model.audit import DBaudit
16-
from resc_backend.db.model.branch import DBbranch
1716
from resc_backend.db.model.finding import DBfinding
1817
from resc_backend.db.model.repository import DBrepository
1918
from resc_backend.db.model.rule import DBrule

components/resc-backend/src/resc_backend/db/model/branch.py

-32
This file was deleted.

components/resc-backend/src/resc_backend/db/model/finding.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
class DBfinding(Base):
1313
__tablename__ = "finding"
1414
id_ = Column("id", Integer, primary_key=True)
15-
branch_id = Column(Integer, ForeignKey("branch.id"), nullable=False)
15+
repository_id = Column(Integer, ForeignKey("repository.id"), nullable=False)
1616
rule_name = Column(String(400), nullable=False)
1717
file_path = Column(String(500), nullable=False)
1818
line_number = Column(Integer, nullable=False)
@@ -25,11 +25,11 @@ class DBfinding(Base):
2525
email = Column(String(100))
2626
event_sent_on = Column(DateTime, nullable=True)
2727

28-
__table_args__ = (UniqueConstraint("commit_id", "branch_id", "rule_name", "file_path", "line_number",
29-
"column_start", "column_end", name="uc_finding_per_branch"),)
28+
__table_args__ = (UniqueConstraint("commit_id", "repository_id", "rule_name", "file_path", "line_number",
29+
"column_start", "column_end", name="uc_finding_per_repository"),)
3030

3131
def __init__(self, rule_name, file_path, line_number, commit_id, commit_message, commit_timestamp, author,
32-
email, event_sent_on, branch_id, column_start, column_end):
32+
email, event_sent_on, repository_id, column_start, column_end):
3333
self.email = email
3434
self.author = author
3535
self.commit_timestamp = commit_timestamp
@@ -39,7 +39,7 @@ def __init__(self, rule_name, file_path, line_number, commit_id, commit_message,
3939
self.file_path = file_path
4040
self.rule_name = rule_name
4141
self.event_sent_on = event_sent_on
42-
self.branch_id = branch_id
42+
self.repository_id = repository_id
4343
self.column_start = column_start
4444
self.column_end = column_end
4545

@@ -55,7 +55,7 @@ def create_from_finding(finding):
5555
commit_timestamp=finding.commit_timestamp,
5656
author=finding.author,
5757
event_sent_on=finding.event_sent_on,
58-
branch_id=finding.branch_id,
58+
repository_id=finding.repository_id,
5959
column_start=finding.column_start,
6060
column_end=finding.column_end
6161
)

components/resc-backend/src/resc_backend/db/model/scan.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,22 @@
1010
from resc_backend.db.model.rule_pack import DBrulePack
1111
from resc_backend.resc_web_service.schema.scan_type import ScanType
1212

13-
BRANCH_ID = "branch.id"
13+
REPOSITORY_ID = "repository.id"
1414

1515

1616
class DBscan(Base):
1717
__tablename__ = "scan"
1818
id_ = Column("id", Integer, primary_key=True)
19-
branch_id = Column(Integer, ForeignKey(BRANCH_ID))
19+
repository_id = Column(Integer, ForeignKey(REPOSITORY_ID))
2020
rule_pack = Column(String(100), ForeignKey(DBrulePack.version), nullable=False)
2121
scan_type = Column(Enum(ScanType), default=ScanType.BASE, server_default=BASE_SCAN, nullable=False)
2222
last_scanned_commit = Column(String(100), nullable=False)
2323
timestamp = Column(DateTime, nullable=False, default=datetime.utcnow)
2424
increment_number = Column(Integer, server_default=text("0"), default=0, nullable=False)
2525

26-
def __init__(self, branch_id: int, scan_type: ScanType, last_scanned_commit: str, timestamp: datetime,
26+
def __init__(self, repository_id: int, scan_type: ScanType, last_scanned_commit: str, timestamp: datetime,
2727
increment_number: int, rule_pack: str):
28-
self.branch_id = branch_id
28+
self.repository_id = repository_id
2929
self.scan_type = scan_type
3030
self.last_scanned_commit = last_scanned_commit
3131
self.timestamp = timestamp
@@ -34,13 +34,13 @@ def __init__(self, branch_id: int, scan_type: ScanType, last_scanned_commit: str
3434

3535
@staticmethod
3636
def create_from_metadata(timestamp: datetime, scan_type: ScanType, last_scanned_commit: str, increment_number: int,
37-
rule_pack: str, branch_id: int):
37+
rule_pack: str, repository_id: int):
3838
db_scan = DBscan(
3939
timestamp=timestamp,
4040
scan_type=scan_type,
4141
last_scanned_commit=last_scanned_commit,
4242
increment_number=increment_number,
4343
rule_pack=rule_pack,
44-
branch_id=branch_id
44+
repository_id=repository_id
4545
)
4646
return db_scan

components/resc-backend/src/resc_backend/helpers/git_operation.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,13 @@
1212

1313

1414
def clone_repository(repository_url: str,
15-
branch_name: str,
1615
repo_clone_path: str,
1716
username: str = "",
1817
personal_access_token: str = ""):
1918
"""
2019
Clones the given repository
2120
:param repository_url:
2221
Repository url to clone
23-
:param branch_name:
24-
Branch name of the repository url to clone
2522
:param repo_clone_path:
2623
Path where to clone the repository
2724
:param username:
@@ -36,5 +33,5 @@ def clone_repository(repository_url: str,
3633
repo_clone_url = f"https://{personal_access_token}@{url}"
3734
logger.debug(f"username is not specified, so cloning the repository with only personal access token: {url}")
3835

39-
Repo.clone_from(repo_clone_url, repo_clone_path, branch=branch_name)
40-
logger.debug(f"Repository {repository_url}:{branch_name} cloned successfully")
36+
Repo.clone_from(repo_clone_url, repo_clone_path)
37+
logger.debug(f"Repository {repository_url} cloned successfully")

components/resc-backend/src/resc_backend/resc_web_service/api.py

-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
requires_no_auth
2121
)
2222
from resc_backend.resc_web_service.endpoints import (
23-
branches,
2423
common,
2524
detailed_findings,
2625
findings,
@@ -92,7 +91,6 @@ def generate_logger_config(log_file_path, debug=True):
9291
{"name": "resc-rules", "description": "Manage rule information"},
9392
{"name": "resc-rule-packs", "description": "Manage rule pack information"},
9493
{"name": "resc-repositories", "description": "Manage repository information"},
95-
{"name": "resc-branches", "description": "Manage branch information"},
9694
{"name": "resc-scans", "description": "Manage scan information"},
9795
{"name": "resc-findings", "description": "Manage findings information"},
9896
{"name": "resc-vcs-instances", "description": "Manage vcs instance information"},
@@ -120,7 +118,6 @@ def generate_logger_config(log_file_path, debug=True):
120118

121119
app.include_router(health.router, prefix=RWS_VERSION_PREFIX)
122120
app.include_router(common.router, prefix=RWS_VERSION_PREFIX)
123-
app.include_router(branches.router, prefix=RWS_VERSION_PREFIX)
124121
app.include_router(rules.router, prefix=RWS_VERSION_PREFIX)
125122
app.include_router(rule_packs.router, prefix=RWS_VERSION_PREFIX)
126123
app.include_router(findings.router, prefix=RWS_VERSION_PREFIX)

0 commit comments

Comments
 (0)