Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make API urls from GitGuardian work with GGShield #1045

Merged
merged 2 commits into from
Jan 14, 2025
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Fixed

- `--instance` param now handles input https://api.eu1.gitguardian.com/v1 or https://api.gitguardian.com/v1.
10 changes: 10 additions & 0 deletions ggshield/core/config/config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import os
import re
from enum import Enum
from pathlib import Path
from typing import Any, Optional, Set, Tuple
Expand Down Expand Up @@ -86,13 +87,22 @@ def get_instance_name_and_source(self) -> Tuple[str, ConfigSource]:
The instance name (defaulting to URL) of the selected instance
priority order is:
- set from the command line (by setting cmdline_instance_name)
- in case the user set the api url instead of dashboard url, we replace it
- GITGUARDIAN_INSTANCE env var
- GITGUARDIAN_API_URL env var
- in local user config (in user_config.dashboard_url)
- in global user config (in user_config.dashboard_url)
- the default instance
"""
if self._cmdline_instance_name:
if re.match(
r"^https:\/\/api(\.[a-z0-9]+)?\.gitguardian\.com",
self._cmdline_instance_name,
) or re.match(r"/exposed/?$", self._cmdline_instance_name):
return (
api_to_dashboard_url(self._cmdline_instance_name),
ConfigSource.CMD_OPTION,
)
return self._cmdline_instance_name, ConfigSource.CMD_OPTION

try:
Expand Down
59 changes: 59 additions & 0 deletions tests/unit/cmd/auth/test_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,45 @@ def test_auth_login_token_default_instance(self, monkeypatch, cli_fs_runner):
assert "ggshield config set instance" not in result.output
self._request_mock.assert_all_requests_happened()

@pytest.mark.parametrize(
("cmd_line_instance", "expected_instance"),
[
("https://api.gitguardian.com/v1", "https://dashboard.gitguardian.com"),
("https://api.gitguardian.com", "https://dashboard.gitguardian.com"),
(
"https://api.eu1.gitguardian.com/v1",
"https://dashboard.eu1.gitguardian.com",
),
(
"https://gitguardian.mycorp.local/exposed/",
"https://gitguardian.mycorp.local",
),
(
"https://gitguardian.mycorp.local/exposed",
"https://gitguardian.mycorp.local",
),
],
)
def test_api_instance_url(
self, cmd_line_instance, expected_instance, cli_fs_runner
):
"""
GIVEN a valid API token and an instance URL matching GitGuardian API urls
WHEN running the login command
THEN it succeeds
"""
token = "mysupertoken"
cmd = ["auth", "login", "--method=token", f"--instance={cmd_line_instance}"]
self._request_mock.add_GET(TOKEN_ENDPOINT, VALID_TOKEN_RESPONSE)
result = cli_fs_runner.invoke(cli, cmd, color=False, input=token + "\n")
config = Config()
config_instance_urls = [
instance_config.url for instance_config in config.auth_config.instances
]
assert_invoke_ok(result)
assert expected_instance in config_instance_urls
self._request_mock.assert_all_requests_happened()

@pytest.mark.parametrize(
("instance", "suggests"),
(
Expand Down Expand Up @@ -966,3 +1005,23 @@ def test_invalid_instance_url(self, instance_url, cli_fs_runner, monkeypatch):
exit_code, output = self.run_cmd(cli_fs_runner)
assert exit_code == ExitCode.USAGE_ERROR, output
self._webbrowser_open_mock.assert_not_called()

@pytest.mark.parametrize(
"instance_url",
[
"https://api.gitguardian.com/v1",
"https://api.eu1.gitguardian.com/v1",
"https://gitguardian.mycorp.local/exposed/",
],
)
def test_api_instance_url(self, instance_url, cli_fs_runner, monkeypatch):
"""
GIVEN an instance URL matching GitGuardian API urls
WHEN running the login command
THEN it succeeds
"""
monkeypatch.setenv("GITGUARDIAN_INSTANCE", instance_url)
self.prepare_mocks(monkeypatch)
exit_code, output = self.run_cmd(cli_fs_runner)
assert exit_code == ExitCode.SUCCESS, output
self._webbrowser_open_mock.assert_called()