Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ The connectors supported by this script have some shared configuration in order
| default_branch_only | False | boolean | If enabled only use the latest report from each repo's default branch |
| from_time | False | int | Period in seconds to pull reports when not specifying a specific `report_id`. If not set defaults to 5 minutes |
| actions_override | False | list[str] | List of acceptable values to override the security policy configuration of issues to include. I.E. `error`, `warn`, `monitor`, and `ignore` |
| repos_filter | False | list[str] | List of repos to restrict results to if desired |


### Example
Expand All @@ -48,11 +49,16 @@ from_time = int((datetime.now(timezone.utc) - start_time).total_seconds())
if __name__ == '__main__':
socket_org = os.getenv("SOCKET_ORG") or exit(1)
api_key = os.getenv("SOCKET_API_KEY") or exit(1)
report_id = ""
repos = [
"example"
]
core = Core(
api_key=api_key,
from_time=from_time,
report_id=report_id,
request_timeout=300
request_timeout=300,
repos_filter=repos
)
issue_data = core.get_issues()
```
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ dynamic = ["version"]
requires-python = ">= 3.9"
dependencies = [
'requests',
"socket-sdk-python >= 1.0.14"
"socket-sdk-python >= 2.0.9"
]
readme = "README.md"
license = {file = "LICENSE"}
Expand Down
11 changes: 6 additions & 5 deletions socket-integration-example.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,25 @@
from socketsync.connectors.sentinel import Sentinel

from datetime import datetime, timezone
start_time = datetime.strptime("2024-09-10 10:00", "%Y-%m-%d %H:%M").replace(tzinfo=timezone.utc)
start_time = datetime.strptime("2025-02-20 10:00", "%Y-%m-%d %H:%M").replace(tzinfo=timezone.utc)
from_time = int((datetime.now(timezone.utc) - start_time).total_seconds())

if __name__ == '__main__':
api_key = os.getenv("SOCKET_API_KEY") or exit(1)
# from_time = os.getenv("FROM_TIME") or 300
default_branches = [
"master",
"main"
]
repos = [
'javascript-threats'
]
core = Core(
api_key=api_key,
from_time=from_time,
request_timeout=300,
report_id="a9b00b69-922a-4a4c-9022-dd994bfa4e80"
report_id=None,
repos_filter=repos
)
# logging.basicConfig(level=logging.DEBUG)
# core.set_log_level(logging.DEBUG)
issue_data = core.get_issues()

# CSV Example
Expand Down
2 changes: 1 addition & 1 deletion socketsync/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


__author__ = "socket.dev"
__version__ = "1.0.24"
__version__ = "1.0.25"
__all__ = ["log", "__version__", "columns", "default_headers"]

log = logging.getLogger("socketdev")
Expand Down
35 changes: 20 additions & 15 deletions socketsync/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
socket: socketdev
org_id: str
org_slug: str
filter_repos: list = []
report_from_time: int
actions: list[str]
timeout = 30
Expand Down Expand Up @@ -50,6 +51,7 @@ class Core:
actions_override: list[str]
enable_all_alerts: bool
properties: list
repos_filter: list

def __init__(
self,
Expand All @@ -64,6 +66,7 @@ def __init__(
from_time: int = 300,
actions_override: list = None,
properties: list = None,
repos_filter: list = None,
):
self.actions_override = actions_override
global actions
Expand All @@ -72,6 +75,9 @@ def __init__(
self.report_id = report_id
self.default_branches = default_branches
self.from_time = from_time
if repos_filter is not None and len(repos_filter) > 0:
global filter_repos
filter_repos = repos_filter
self.properties = properties
if self.default_branches is not None:
global default_branch_names
Expand Down Expand Up @@ -163,20 +169,10 @@ def get_security_policy() -> dict:
Get the Security policy and determine the effective Org security policy
:return:
"""
data = socket.settings.get(org_id)
defaults = data.get("defaults")
default_rules = defaults.get("issueRules")
entries = data.get("entries")
org_rules = {}
for org_set in entries:
settings = org_set.get("settings")
if settings is not None:
org_details = settings.get("organization")
org_rules = org_details.get("issueRules")
for default in default_rules:
if default not in org_rules:
action = default_rules[default]["action"]
org_rules[default] = {"action": action}
response = socket.settings.get(org_slug)
org_rules = response.get('securityPolicyRules')
if org_rules is None:
raise Exception("Unable to get security policy results")
return org_rules

@staticmethod
Expand Down Expand Up @@ -217,6 +213,8 @@ def get_repos() -> None:
next_page = None
for repo_data in all_repos:
repo = Repository(**repo_data)
if len(filter_repos) > 0 and repo.name not in filter_repos:
continue
repos_info[repo.id] = repo
global repos
repos = repos_info
Expand All @@ -228,6 +226,8 @@ def create_reports_list(raw_reports: dict, report_id: str = None) -> list:
commits = []
for raw_report in raw_reports:
report = Report(**raw_report)
if len(filter_repos) > 0 and report.repo not in filter_repos:
continue
if report_id is not None and report_id == report.id:
reports.append(report)
elif report_id is None:
Expand Down Expand Up @@ -258,10 +258,12 @@ def get_reports(self) -> list:
done = False
reports = []
next_page = None
print(report_from_time)
while not done:
results = socket.fullscans.get(org_slug, {"from": int(report_from_time), "page": next_page})
if next_page == 0:
done = True
params = {"per_page": 100, "from": int(report_from_time), "page": next_page}
results = socket.fullscans.get(org_slug, params)
next_page = results.get("nextPage")
if results.get("success") is False:
log.error(f"Unable to get full scans: {results.get('message')}")
Expand All @@ -279,6 +281,9 @@ def get_reports(self) -> list:
def handle_reports(reports: list, issues: list) -> list:
for report in reports:
# report: Report
if len(filter_repos) > 0 and report.repo not in filter_repos:
continue
log.debug(f"Getting results for report id {report.id}")
log.debug(f"Getting results for scan id {report.id}")
packages = socket.fullscans.stream(org_slug, report.id)

Expand Down
Loading