Skip to content

Conversation

@alina-tuholukova-gg
Copy link
Contributor

Add new parameter source_uuid, when provided, the scan will also create incidents on GIM dashboard

Context

When calling ggshield secret scan with --source-uuid, the scan will also create incidents on GIM dashboard.

Validation

To validate change:

  • Create a custom source on GIM
  • Copy its uuid
  • Call ggshield secret scan with --source-uuid on some documents that have secrets
  • Check that the corresponding issues are created on GIM

PR check list

  • As much as possible, the changes include tests (unit and/or functional)
  • If the changes affect the end user (new feature, behavior change, bug fix) then the PR has a changelog entry (see doc/dev/getting-started.md). If the changes do not affect the end user, then the skip-changelog label has been added to the PR.

@alina-tuholukova-gg alina-tuholukova-gg requested a review from a team as a code owner June 25, 2025 14:36
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is just to check the pipeline with the new py-gitguardian

Comment on lines 73 to 81
if secret_config.source_uuid:
response = self.client.api_tokens()

if not isinstance(response, (Detail, APITokensResponse)):
raise UnexpectedError("Unexpected api_tokens response")
elif isinstance(response, Detail):
raise UnexpectedError(response.detail)
if TokenScope.SCAN_CREATE_INCIDENTS not in response.scopes:
raise MissingScopesError([TokenScope.SCAN_CREATE_INCIDENTS])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In certain situations, for example when scanning commit ranges, ggshield creates multiple SecretScanner instances. This is why we have the if check_api_key: at the start of the method: we only check the API key validity if it has not already be done by the caller.

I think these new checks should be done in check_client_api_key(). It does not have access to the config though, so you need to extend the function to accept a source_uuid optional argument.

Copy link
Collaborator

@agateau-gg agateau-gg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, looking at the code, I realize my suggestion was not very good. Plus there is already some code that calls client.api_tokens() in SecretScanner.__init__(), which is not ideal.

Here is a better (I think, but do tell me if you disagree!) suggestion:

  • Change check_client_api_key() to take a mandatory second argument that would be a set of the required scopes.

  • Change check_client_api_key() callers to pass this second argument:

    • oauth.py can pass an empty set (it only cares if the key is valid)
    • secret_scanner.py and repo.py can generate the required set doing something like this:
scopes = {TokenScope.SCAN}
if secret_config.with_incident_details:
    scopes.add(TokenScope.INCIDENTS_READ)
if secret_config.source_uuid:
    scopes.add(TokenScope.SCAN_CREATE_INCIDENTS)
check_client_api_key(client, scopes)

What do you think?

Comment on lines 3 to 4
- `ggshield secret scan` now provides an `--source-uuid` option. When this option is set, it will create the incidents on the GIM
dashboard on the correspoding source. Note, that the token should have the scope `scan:create-incidents`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we want to officially document the option for now, or do we prefer to mark it as beta or hidden?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer we officially document and mark it as beta

@alina-tuholukova-gg
Copy link
Contributor Author

Sorry, looking at the code, I realize my suggestion was not very good. Plus there is already some code that calls client.api_tokens() in SecretScanner.__init__(), which is not ideal.

Here is a better (I think, but do tell me if you disagree!) suggestion:

  • Change check_client_api_key() to take a mandatory second argument that would be a set of the required scopes.

  • Change check_client_api_key() callers to pass this second argument:

    • oauth.py can pass an empty set (it only cares if the key is valid)
    • secret_scanner.py and repo.py can generate the required set doing something like this:
scopes = {TokenScope.SCAN}
if secret_config.with_incident_details:
    scopes.add(TokenScope.INCIDENTS_READ)
if secret_config.source_uuid:
    scopes.add(TokenScope.SCAN_CREATE_INCIDENTS)
check_client_api_key(client, scopes)

What do you think?

I did the change, tell me if it's what you had in mind :)
When the MR is ok for you, I'll rebase

@alina-tuholukova-gg alina-tuholukova-gg force-pushed the alina/add-source-uuid-secret-scan branch from 51e8549 to 7fbcd33 Compare July 1, 2025 15:32
Copy link
Collaborator

@agateau-gg agateau-gg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks nicer now. Just one last change and this can go in.

### Changed

- `ggshield secret scan` now provides an `--source-uuid` option. When this option is set, it will create the incidents on the GIM
dashboard on the correspoding source. Note, that the token should have the scope `scan:create-incidents`.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: typos

Suggested change
dashboard on the correspoding source. Note, that the token should have the scope `scan:create-incidents`.
dashboard on the corresponding source. Note that the token should have the scope `scan:create-incidents`.

raise MissingScopesError(list(missing_scopes))


def get_required_token_scopes_from_config(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a good idea to factorize this code, but I would prefer if it were kept in the secret vertical. Maybe you can move this function in verticals/secret_scanner.py?

@alina-tuholukova-gg alina-tuholukova-gg force-pushed the alina/add-source-uuid-secret-scan branch from 7fbcd33 to eff58e2 Compare July 2, 2025 13:00
Copy link
Collaborator

@agateau-gg agateau-gg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks!

@alina-tuholukova-gg alina-tuholukova-gg force-pushed the alina/add-source-uuid-secret-scan branch from eff58e2 to 3692feb Compare July 3, 2025 15:21
@codecov
Copy link

codecov bot commented Jul 3, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.95%. Comparing base (917c935) to head (57d8f9e).
Report is 3 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1109      +/-   ##
==========================================
+ Coverage   91.90%   91.95%   +0.05%     
==========================================
  Files         144      144              
  Lines        6102     6130      +28     
==========================================
+ Hits         5608     5637      +29     
+ Misses        494      493       -1     
Flag Coverage Δ
unittests 91.95% <100.00%> (+0.05%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@alina-tuholukova-gg alina-tuholukova-gg force-pushed the alina/add-source-uuid-secret-scan branch 3 times, most recently from a77b54d to 0e57b41 Compare July 4, 2025 09:53
@alina-tuholukova-gg alina-tuholukova-gg force-pushed the alina/add-source-uuid-secret-scan branch from 0e57b41 to 57d8f9e Compare July 4, 2025 10:14
@alina-tuholukova-gg alina-tuholukova-gg merged commit 549b1b2 into main Jul 4, 2025
29 checks passed
@alina-tuholukova-gg alina-tuholukova-gg deleted the alina/add-source-uuid-secret-scan branch July 4, 2025 13:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants