Skip to content

Commit

Permalink
fix: make API urls from GitGuardian work with GGShield
Browse files Browse the repository at this point in the history
  • Loading branch information
Séverine Bonnechère committed Jan 8, 2025
1 parent dcd87b4 commit df36e15
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
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.
15 changes: 14 additions & 1 deletion ggshield/core/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
from ggshield.core.config.auth_config import AuthConfig
from ggshield.core.config.user_config import UserConfig
from ggshield.core.config.utils import remove_url_trailing_slash
from ggshield.core.constants import DEFAULT_HMSL_URL, DEFAULT_INSTANCE_URL
from ggshield.core.constants import (
DEFAULT_HMSL_URL,
DEFAULT_INSTANCE_URL,
ON_PREMISE_API_URL_PATH_PREFIX,
)
from ggshield.core.url_utils import (
api_to_dashboard_url,
clean_url,
Expand Down Expand Up @@ -86,13 +90,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 (
"api" in self._cmdline_instance_name
or ON_PREMISE_API_URL_PATH_PREFIX in self._cmdline_instance_name
):
return (
api_to_dashboard_url(self._cmdline_instance_name, warn=True),
ConfigSource.CMD_OPTION,
)
return self._cmdline_instance_name, ConfigSource.CMD_OPTION

try:
Expand Down
49 changes: 49 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,36 @@ 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.eu1.gitguardian.com/v1",
"https://dashboard.eu1.gitguardian.com",
),
],
)
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 +996,22 @@ 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",
],
)
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()

0 comments on commit df36e15

Please sign in to comment.