Skip to content

Commit

Permalink
Honor http.sslVerify git config key for GitHub and GitLab API requests
Browse files Browse the repository at this point in the history
  • Loading branch information
PawelLipski committed Aug 8, 2024
1 parent 5614a6b commit fab9e18
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 11 deletions.
4 changes: 4 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release notes

## New in git-machete 3.27.0

- added: git config key `http.sslVerify` is honored when connecting to GitHub and GitLab APIs (suggested by @scamden)

## New in git-machete 3.26.4

- fixed: avoid detecting a cycle when there's a PR/MR from `main` or `master` in a fork to the original repo (reported by @Joibel)
Expand Down
4 changes: 2 additions & 2 deletions docs/man/git-machete.1
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ level margin: \\n[rst2man-indent\\n[rst2man-indent-level]]
.\" new: \\n[rst2man-indent\\n[rst2man-indent-level]]
.in \\n[rst2man-indent\\n[rst2man-indent-level]]u
..
.TH "GIT-MACHETE" "1" "Aug 07, 2024" "" "git-machete"
.TH "GIT-MACHETE" "1" "Aug 08, 2024" "" "git-machete"
.SH NAME
git-machete \- git-machete 3.26.4
git-machete \- git-machete 3.27.0
.sp
git machete is a robust tool that \fBsimplifies your git workflows\fP\&.
.sp
Expand Down
2 changes: 1 addition & 1 deletion git_machete/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '3.26.4'
__version__ = '3.27.0'
13 changes: 12 additions & 1 deletion git_machete/code_hosting.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import re
import ssl
from abc import ABCMeta, abstractmethod
from typing import Dict, List, NamedTuple, Optional

from git_machete.git_operations import LocalBranchShortName
from git_machete.git_operations import GitContext, LocalBranchShortName
from git_machete.utils import bold


Expand Down Expand Up @@ -125,8 +126,18 @@ def __init__(self, spec: CodeHostingSpec, domain: str, organization: str, reposi
self.domain: str = domain
self.organization: str = organization
self.repository: str = repository
self.ssl_context = self.__create_ssl_context()
self.__org_repo_and_git_url_by_repo_id: Dict[int, Optional[OrganizationAndRepositoryAndGitUrl]] = {}

@staticmethod
def __create_ssl_context() -> ssl.SSLContext:
ctx = ssl.create_default_context()
ssl_verify = GitContext().get_boolean_config_attr_or_none("http.sslVerify")
if not ssl_verify:
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
return ctx

@abstractmethod
def create_pull_request(self, head: str, head_org_repo: OrganizationAndRepository,
base: str, title: str, description: str, draft: bool) -> PullRequest:
Expand Down
2 changes: 1 addition & 1 deletion git_machete/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def __fire_github_api_request(self, method: str, path: str, request_body: Option
f'bearer token and request body {compact_dict(request_body) if request_body else "<none>"}')

try:
with urllib.request.urlopen(http_request) as response:
with urllib.request.urlopen(http_request, context=self.ssl_context) as response:
parsed_response_body: Any = json.loads(response.read().decode())
# https://docs.github.com/en/rest/guides/using-pagination-in-the-rest-api?apiVersion=2022-11-28#using-link-headers
link_header: str = response.info()["link"]
Expand Down
2 changes: 1 addition & 1 deletion git_machete/gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def __fire_gitlab_api_request(self, method: str, path: str, request_body: Option
f'bearer token and request body {compact_dict(request_body) if request_body else "<none>"}')

try:
with urllib.request.urlopen(http_request) as response:
with urllib.request.urlopen(http_request, context=self.ssl_context) as response:
parsed_response_body: Any = json.loads(response.read().decode())
# https://docs.gitlab.com/ee/api/rest/#pagination-link-header
link_header: str = response.info()["link"]
Expand Down
6 changes: 4 additions & 2 deletions tests/mockers_github.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import re
import ssl
from contextlib import AbstractContextManager, contextmanager
from http import HTTPStatus
from typing import Any, Callable, Dict, Iterator, List, Optional
Expand Down Expand Up @@ -79,9 +80,10 @@ def add_pull(self, pull: Dict[str, Any]) -> None:


# Not including [MockAPIResponse] type argument to maintain compatibility with Python <= 3.8
def mock_urlopen(github_api_state: MockGitHubAPIState) -> Callable[[Request], AbstractContextManager]: # type: ignore[type-arg]
def mock_urlopen(github_api_state: MockGitHubAPIState,
_context: Optional[ssl.SSLContext] = None) -> Callable[[Request], AbstractContextManager]: # type: ignore[type-arg]
@contextmanager
def inner(request: Request) -> Iterator[MockAPIResponse]:
def inner(request: Request, **_kwargs: Any) -> Iterator[MockAPIResponse]:
yield __mock_urlopen_impl(github_api_state, request)
return inner

Expand Down
6 changes: 4 additions & 2 deletions tests/mockers_gitlab.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import re
import ssl
import urllib
from contextlib import AbstractContextManager, contextmanager
from http import HTTPStatus
Expand Down Expand Up @@ -94,9 +95,10 @@ def add_mr(self, mr: Dict[str, Any]) -> None:


# Not including [MockGitLabAPIResponse] type argument to maintain compatibility with Python <= 3.8
def mock_urlopen(gitlab_api_state: MockGitLabAPIState) -> Callable[[Request], AbstractContextManager]: # type: ignore[type-arg]
def mock_urlopen(gitlab_api_state: MockGitLabAPIState,
_context: Optional[ssl.SSLContext] = None) -> Callable[[Request], AbstractContextManager]: # type: ignore[type-arg]
@contextmanager
def inner(request: Request) -> Iterator[MockAPIResponse]:
def inner(request: Request, **_kwargs: Any) -> Iterator[MockAPIResponse]:
yield __mock_urlopen_impl(gitlab_api_state, request)
return inner

Expand Down
1 change: 1 addition & 0 deletions tests/test_gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def test_gitlab_enterprise_domain_unauthorized_without_token(self, mocker: Mocke
self.patch_symbol(mocker, 'git_machete.code_hosting.OrganizationAndRepository.from_url', mock_from_url)
self.patch_symbol(mocker, 'urllib.request.urlopen', mock_urlopen(MockGitLabAPIState.with_mrs()))

self.repo_sandbox.set_git_config_key('http.sslVerify', 'false')
self.repo_sandbox.set_git_config_key('machete.gitlab.domain', '403.example.org')

expected_error_message = (
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ commands = isort --check-only .
description = "Run `vulture` static code analyzer to detect unused code"
deps =
-r{[requirements]dir}/vulture-check.txt
commands = vulture git_machete/ tests/
commands = vulture --ignore-names check_hostname,verify_mode git_machete/ tests/

[testenv:venv]
commands = {posargs}
Expand Down

0 comments on commit fab9e18

Please sign in to comment.