Skip to content

Commit 4814afd

Browse files
committed
feat(metadata): add remediation messages
1 parent a99ced7 commit 4814afd

File tree

4 files changed

+76
-1
lines changed

4 files changed

+76
-1
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
### Added
2+
3+
- GGclient now contain remediation messages obtained from the api metadata endpoint.

pygitguardian/client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
JWTService,
3636
MultiScanResult,
3737
QuotaResponse,
38+
RemediationMessages,
3839
ScanResult,
3940
SecretScanPreferences,
4041
ServerMetadata,
@@ -151,6 +152,7 @@ class GGClient:
151152
user_agent: str
152153
extra_headers: Dict
153154
secret_scan_preferences: SecretScanPreferences
155+
remediation_messages: RemediationMessages
154156
callbacks: Optional[GGClientCallbacks]
155157

156158
def __init__(
@@ -214,6 +216,7 @@ def __init__(
214216
)
215217
self.maximum_payload_size = MAXIMUM_PAYLOAD_SIZE
216218
self.secret_scan_preferences = SecretScanPreferences()
219+
self.remediation_messages = RemediationMessages()
217220

218221
def request(
219222
self,
@@ -676,6 +679,7 @@ def read_metadata(self) -> Optional[Detail]:
676679
"general__maximum_payload_size", MAXIMUM_PAYLOAD_SIZE
677680
)
678681
self.secret_scan_preferences = metadata.secret_scan_preferences
682+
self.remediation_messages = metadata.remediation_messages
679683
return None
680684

681685
def create_jwt(

pygitguardian/config.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,55 @@
55
MULTI_DOCUMENT_LIMIT = 20
66
DOCUMENT_SIZE_THRESHOLD_BYTES = 1048576 # 1MB
77
MAXIMUM_PAYLOAD_SIZE = 2621440 # 25MB
8+
9+
10+
DEFAULT_REWRITE_GIT_HISTORY_MESSAGE = """ To prevent having to rewrite git history in the future, setup ggshield as a pre-commit hook:
11+
https://docs.gitguardian.com/ggshield-docs/integrations/git-hooks/pre-commit\n"""
12+
13+
DEFAULT_PRE_COMMIT_MESSAGE = """> How to remediate
14+
15+
Since the secret was detected before the commit was made:
16+
1. replace the secret with its reference (e.g. environment variable).
17+
2. commit again.
18+
19+
> [To apply with caution] If you want to bypass ggshield (false positive or other reason), run:
20+
- if you use the pre-commit framework:
21+
22+
SKIP=ggshield git commit -m "<your message>"
23+
24+
- otherwise (warning: the following command bypasses all pre-commit hooks):
25+
26+
git commit -m "<your message>" --no-verify"""
27+
28+
DEFAULT_PRE_PUSH_MESSAGE = (
29+
"""> How to remediate
30+
31+
Since the secret was detected before the push BUT after the commit, you need to:
32+
1. rewrite the git history making sure to replace the secret with its reference (e.g. environment variable).
33+
2. push again.
34+
35+
"""
36+
+ DEFAULT_REWRITE_GIT_HISTORY_MESSAGE
37+
+ """\n> [To apply with caution] If you want to bypass ggshield (false positive or other reason), run:
38+
- if you use the pre-commit framework:
39+
40+
SKIP=ggshield-push git push
41+
42+
- otherwise (warning: the following command bypasses all pre-push hooks):
43+
44+
git push --no-verify"""
45+
)
46+
47+
DEFAULT_PRE_RECEIVE_MESSAGE = (
48+
"""> How to remediate
49+
50+
A pre-receive hook set server side prevented you from pushing secrets.
51+
Since the secret was detected during the push BUT after the commit, you need to:
52+
1. rewrite the git history making sure to replace the secret with its reference (e.g. environment variable).
53+
2. push again.
54+
55+
"""
56+
+ DEFAULT_REWRITE_GIT_HISTORY_MESSAGE
57+
+ """\n> [To apply with caution] If you want to bypass ggshield (false positive or other reason), run:
58+
\n git push -o breakglass"""
59+
)

pygitguardian/models.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@
1919
)
2020
from typing_extensions import Self
2121

22-
from .config import DOCUMENT_SIZE_THRESHOLD_BYTES, MULTI_DOCUMENT_LIMIT
22+
from .config import (
23+
DEFAULT_PRE_COMMIT_MESSAGE,
24+
DEFAULT_PRE_PUSH_MESSAGE,
25+
DEFAULT_PRE_RECEIVE_MESSAGE,
26+
DOCUMENT_SIZE_THRESHOLD_BYTES,
27+
MULTI_DOCUMENT_LIMIT,
28+
)
2329

2430

2531
class ToDictMixin:
@@ -734,13 +740,23 @@ class SecretScanPreferences:
734740
maximum_documents_per_scan: int = MULTI_DOCUMENT_LIMIT
735741

736742

743+
@dataclass
744+
class RemediationMessages:
745+
pre_commit: str = DEFAULT_PRE_COMMIT_MESSAGE
746+
pre_push: str = DEFAULT_PRE_PUSH_MESSAGE
747+
pre_receive: str = DEFAULT_PRE_RECEIVE_MESSAGE
748+
749+
737750
@dataclass
738751
class ServerMetadata(Base, FromDictMixin):
739752
version: str
740753
preferences: Dict[str, Any]
741754
secret_scan_preferences: SecretScanPreferences = field(
742755
default_factory=SecretScanPreferences
743756
)
757+
remediation_messages: RemediationMessages = field(
758+
default_factory=RemediationMessages
759+
)
744760

745761

746762
ServerMetadata.SCHEMA = cast(

0 commit comments

Comments
 (0)