Skip to content

Commit e1c2b6a

Browse files
feat: added safety firewall (#671)
Co-authored-by: jakub-safetycli <[email protected]>
1 parent 209e443 commit e1c2b6a

Some content is hidden

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

67 files changed

+4391
-725
lines changed

.devcontainer/devcontainer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313

1414
"mounts": [
1515
"source=/var/run/docker.sock,target=/var/run/docker.sock,type=bind",
16-
"source=${localEnv:HOME}${localEnv:USERPROFILE}/.ssh,target=/home/developer/.ssh,type=bind,consistency=cached"
16+
"source=${localEnv:HOME}${localEnv:USERPROFILE}/.ssh,target=/home/developer/.ssh,type=bind,consistency=cached",
17+
"source=${localEnv:HOME}/.safety,target=/home/developer/.safety,type=bind,consistency=cached"
1718
],
1819

1920
"remoteEnv": {

.github/workflows/pr.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,23 @@ jobs:
4343
#### Quick Test with Python Package
4444
\`\`\`bash
4545
# Download and run with uv
46-
gh run download ${context.runId} -n dist
46+
gh run download ${context.runId} -n dist -R pyupio/safety
4747
uv run --with safety-${version}-py3-none-any.whl safety --version
4848
\`\`\`
4949
5050
#### Binary Installation
5151
\`\`\`bash
5252
# Linux
53-
gh run download ${context.runId} -n safety-linux -D linux
53+
gh run download ${context.runId} -n safety-linux -D linux -R pyupio/safety
5454
cd linux && mv safety safety-pr && chmod +x safety-pr
5555
5656
# macOS
57-
gh run download ${context.runId} -n safety-macos -D macos
57+
gh run download ${context.runId} -n safety-macos -D macos -R pyupio/safety
5858
cd macos && mv safety safety-pr && chmod +x safety-pr
5959
60+
# Windows
61+
gh run download ${context.runId} -n safety-windows -D windows -R pyupio/safety
62+
cd windows && mv safety.exe safety-pr.exe
6063
6164
./safety-pr --version
6265
\`\`\`

.github/workflows/reusable-build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ jobs:
4949
exit 1
5050
fi
5151
BRANCH_NAME="${{ inputs.branch-name }}"
52-
SLUG=$(echo "$BRANCH_NAME" | iconv -t ascii//TRANSLIT | sed -r s/[^a-zA-Z0-9]+/-/g | sed -r s/^-+\|-+$//g | tr A-Z a-z)
52+
SLUG=$(echo "$BRANCH_NAME" | iconv -t ascii//TRANSLIT | sed -r 's/[^a-zA-Z0-9]+/./g' | sed -r 's/^.+\|.+$//g' | tr A-Z a-z)
5353
echo "SLUG=$SLUG" >> $GITHUB_OUTPUT
5454
5555
- name: Version bump (PR)

.vscode/launch.json

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
// This uses the default environment which is a virtual environment
1111
// created by Hatch
1212
"python": "${workspaceFolder}/.hatch/bin/python",
13-
"console": "integratedTerminal"
13+
"console": "integratedTerminal",
14+
"justMyCode": false,
1415
}
1516
],
1617
"inputs": [
@@ -64,23 +65,37 @@
6465
"auth login",
6566
"auth login --headless",
6667
"auth logout",
68+
"auth status",
6769

6870
// Scan commands
6971
"scan",
70-
"--key ADD-YOUR-API-KEY scan",
72+
"--key $SAFETY_API_KEY scan",
73+
"--stage cicd --key $SAFETY_API_KEY scan",
7174
"scan --use-server-matching",
7275
"scan --detailed-output",
7376
"--debug scan",
7477
"--disable-optional-telemetry scan",
7578
"scan --output json --output-file json",
79+
"scan --help",
80+
81+
// Firewall commands
82+
"init --help",
83+
"init local_prj", // Directory has to be created manually
84+
"init",
85+
"pip list",
86+
"pip install insecure-package",
87+
"pip install fastapi",
7688

7789
// Check commands
7890
"check",
7991
"--debug check",
8092

8193
// Other commands
8294
"license",
83-
"--help"
95+
"--help",
96+
"validate --help",
97+
"--key foo --help",
98+
"configure"
8499
],
85100
"default": "scan"
86101
}

docs/.ipynb_checkpoints/Safety-CLI-Quickstart-checkpoint.ipynb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
{
2-
"cells": [],
2+
"cells": [
3+
{
4+
"metadata": {},
5+
"cell_type": "raw",
6+
"source": "",
7+
"id": "e4a30302820cf149"
8+
}
9+
],
310
"metadata": {},
411
"nbformat": 4,
512
"nbformat_minor": 5

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ dependencies = [
4949
"setuptools>=65.5.1",
5050
"typer>=0.12.1",
5151
"typing-extensions>=4.7.1",
52+
"python-levenshtein>=0.25.1",
5253
]
5354
license = "MIT"
5455
license-files = ["LICENSES/*"]
@@ -255,6 +256,8 @@ reportMissingTypeStubs = false
255256
addopts = "--strict-markers"
256257
markers = [
257258
"basic: requires no extras",
259+
"windows_only: mark test to run only on Windows platforms",
260+
"unix_only: mark test to run only on Unix platforms"
258261
]
259262

260263
[tool.coverage.run]

safety/alerts/__init__.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
from dataclasses import dataclass
88

9+
from safety.constants import CONTEXT_COMMAND_TYPE
10+
911
from . import github
1012
from safety.util import SafetyPolicyFile
1113
from safety.scan.constants import CLI_ALERT_COMMAND_HELP
@@ -17,6 +19,10 @@ def get_safety_cli_legacy_group():
1719
from safety.cli_util import SafetyCLILegacyGroup
1820
return SafetyCLILegacyGroup
1921

22+
def get_context_settings():
23+
from safety.cli_util import CommandType
24+
return {CONTEXT_COMMAND_TYPE: CommandType.UTILITY}
25+
2026
@dataclass
2127
class Alert:
2228
"""
@@ -33,7 +39,8 @@ class Alert:
3339
policy: Any = None
3440
requirements_files: Any = None
3541

36-
@click.group(cls=get_safety_cli_legacy_group(), help=CLI_ALERT_COMMAND_HELP, deprecated=True, utility_command=True)
42+
@click.group(cls=get_safety_cli_legacy_group(), help=CLI_ALERT_COMMAND_HELP,
43+
deprecated=True, context_settings=get_context_settings())
3744
@click.option('--check-report', help='JSON output of Safety Check to work with.', type=click.File('r'), default=sys.stdin, required=True)
3845
@click.option("--key", envvar="SAFETY_API_KEY",
3946
help="API Key for safetycli.com's vulnerability database. Can be set as SAFETY_API_KEY "

safety/auth/cli.py

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
from datetime import datetime
21
import logging
32
import sys
4-
from safety.auth.models import Auth
3+
from datetime import datetime
54

6-
from safety.auth.utils import is_email_verified
5+
from safety.auth.models import Auth
6+
from safety.auth.utils import initialize, is_email_verified
77
from safety.console import main_console as console
88
from safety.constants import MSG_FINISH_REGISTRATION_TPL, MSG_VERIFICATION_HINT
9+
from safety.meta import get_version
910

1011
try:
1112
from typing import Annotated
@@ -15,22 +16,38 @@
1516
from typing import Optional
1617

1718
import click
18-
from typer import Typer
1919
import typer
20+
from rich.padding import Padding
21+
from typer import Typer
2022

21-
from safety.auth.main import get_auth_info, get_authorization_data, get_token, clean_session
23+
from safety.auth.main import (
24+
clean_session,
25+
get_auth_info,
26+
get_authorization_data,
27+
get_token,
28+
)
2229
from safety.auth.server import process_browser_callback
23-
from ..cli_util import get_command_for, pass_safety_cli_obj, SafetyCLISubGroup
24-
25-
from .constants import MSG_FAIL_LOGIN_AUTHED, MSG_FAIL_REGISTER_AUTHED, MSG_LOGOUT_DONE, MSG_LOGOUT_FAILED, MSG_NON_AUTHENTICATED
26-
from safety.scan.constants import CLI_AUTH_COMMAND_HELP, CLI_AUTH_HEADLESS_HELP, DEFAULT_EPILOG, CLI_AUTH_LOGIN_HELP, CLI_AUTH_LOGOUT_HELP, CLI_AUTH_STATUS_HELP
27-
28-
29-
from rich.padding import Padding
30+
from safety.scan.constants import (
31+
CLI_AUTH_COMMAND_HELP,
32+
CLI_AUTH_HEADLESS_HELP,
33+
CLI_AUTH_LOGIN_HELP,
34+
CLI_AUTH_LOGOUT_HELP,
35+
CLI_AUTH_STATUS_HELP,
36+
DEFAULT_EPILOG,
37+
)
38+
39+
from ..cli_util import SafetyCLISubGroup, get_command_for, pass_safety_cli_obj
40+
from .constants import (
41+
MSG_FAIL_LOGIN_AUTHED,
42+
MSG_FAIL_REGISTER_AUTHED,
43+
MSG_LOGOUT_DONE,
44+
MSG_LOGOUT_FAILED,
45+
MSG_NON_AUTHENTICATED,
46+
)
3047

3148
LOG = logging.getLogger(__name__)
3249

33-
auth_app = Typer(rich_markup_mode="rich")
50+
auth_app = Typer(rich_markup_mode="rich", name="auth")
3451

3552

3653

@@ -183,6 +200,8 @@ def login(
183200

184201
render_successful_login(ctx.obj.auth, organization=organization)
185202

203+
initialize(ctx, refresh=True)
204+
186205
console.print()
187206
if ctx.obj.auth.org or ctx.obj.auth.email_verified:
188207
console.print(
@@ -249,12 +268,13 @@ def status(ctx: typer.Context, ensure_auth: bool = False,
249268
"""
250269
LOG.info('status started')
251270
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
252-
from safety.util import get_safety_version
253-
safety_version = get_safety_version()
271+
safety_version = get_version()
254272
console.print(f"[{current_time}]: Safety {safety_version}")
255273

256274
info = get_auth_info(ctx)
257275

276+
initialize(ctx, refresh=True)
277+
258278
if ensure_auth:
259279
console.print("running: safety auth status --ensure-auth")
260280
console.print()

0 commit comments

Comments
 (0)