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 git auth prompting and allow config of '.git' auto suffix #5078

Merged
merged 1 commit into from
Nov 29, 2024
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
8 changes: 8 additions & 0 deletions backend/infrahub/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ def default_cors_allow_headers() -> list[str]:
return ["accept", "authorization", "content-type", "user-agent", "x-csrftoken", "x-requested-with"]


def default_append_git_suffix_domains() -> list[str]:
return ["github.com", "gitlab.com"]


class UserInfoMethod(str, Enum):
POST = "post"
GET = "get"
Expand Down Expand Up @@ -337,6 +341,10 @@ class GitSettings(BaseSettings):
description="Time (in seconds) between git repositories synchronizations",
deprecated="This setting is deprecated and not currently in use.",
)
append_git_suffix: list[str] = Field(
default_factory=default_append_git_suffix_domains,
description="Automatically append '.git' to HTTP URLs if for these domains.",
)


class HTTPSettings(BaseSettings):
Expand Down
6 changes: 5 additions & 1 deletion backend/infrahub/graphql/mutations/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import re
from typing import TYPE_CHECKING, Any, Optional, cast

import httpx
from graphene import Boolean, InputObjectType, Mutation, String

from infrahub import config
from infrahub.core.constants import InfrahubKind, RepositoryInternalStatus
from infrahub.core.manager import NodeManager
from infrahub.core.protocols import CoreGenericRepository, CoreReadOnlyRepository, CoreRepository
Expand Down Expand Up @@ -187,7 +189,9 @@ def cleanup_payload(data: InputObjectType | dict[str, Any]) -> None:
and data["location"].get("value")
and re.match(http_without_dotgit, data["location"]["value"])
):
data["location"]["value"] = f'{data["location"]["value"]}.git'
url = httpx.URL(data["location"]["value"])
if url.host in config.SETTINGS.git.append_git_suffix:
data["location"]["value"] = f'{data["location"]["value"]}.git'


class ProcessRepository(Mutation):
Expand Down
3 changes: 3 additions & 0 deletions backend/infrahub/workers/infrahub_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ async def setup(
logging.getLogger("aio_pika").setLevel(logging.ERROR)
logging.getLogger("aiormq").setLevel(logging.ERROR)
logging.getLogger("git").setLevel(logging.ERROR)
# Prevent git from interactively prompting the user for passwords if the credentials provided
# by the credential helper is failing.
os.environ["GIT_TERMINAL_PROMPT"] = "0"

if not config.SETTINGS.settings:
config_file = os.environ.get("INFRAHUB_CONFIG", "infrahub.toml")
Expand Down
8 changes: 8 additions & 0 deletions backend/tests/unit/graphql/mutations/test_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ async def test_repository_update(db: InfrahubDatabase, register_core_models_sche
{"name": "demo", "location": {"value": "http://github.com/opsmill/infrahub-demo-edge-develop"}},
{"name": "demo", "location": {"value": "http://github.com/opsmill/infrahub-demo-edge-develop.git"}},
),
(
{"name": "demo", "location": {"value": "http://gitlab.com/opsmill/infrahub-demo-edge-develop"}},
{"name": "demo", "location": {"value": "http://gitlab.com/opsmill/infrahub-demo-edge-develop.git"}},
),
(
{"name": "demo", "location": {"value": "http://example.com/opsmill/infrahub-demo-edge-develop"}},
{"name": "demo", "location": {"value": "http://example.com/opsmill/infrahub-demo-edge-develop"}},
),
(
{"name": "demo"},
{"name": "demo"},
Expand Down
1 change: 1 addition & 0 deletions changelog/+ddd89dc2.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Corrected configuration for prefect worker to never prompt for Git credentials on the console
1 change: 1 addition & 0 deletions changelog/5077.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added a 'append_git_suffix' configuration setting for Git repositories that allows you to define domains for auto appending '.git' to repositories defined with an HTTP URL
Loading