Skip to content

Commit

Permalink
chore: stricter regex on api url. Handle on-prem
Browse files Browse the repository at this point in the history
  • Loading branch information
Séverine Bonnechère committed Jan 13, 2025
1 parent 6c6e01b commit ce964f3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
9 changes: 5 additions & 4 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 @@ -94,10 +95,10 @@ def get_instance_name_and_source(self) -> Tuple[str, ConfigSource]:
- the default instance
"""
if self._cmdline_instance_name:
if (
"api" in self._cmdline_instance_name
and "gitguardian" in 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,
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/cmd/auth/test_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,19 @@ def test_auth_login_token_default_instance(self, monkeypatch, cli_fs_runner):
("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(
Expand Down Expand Up @@ -1002,6 +1011,7 @@ def test_invalid_instance_url(self, instance_url, cli_fs_runner, monkeypatch):
[
"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):
Expand Down

0 comments on commit ce964f3

Please sign in to comment.