Skip to content

Commit e4400f4

Browse files
authored
Adds a "private_key" alternative extra field to GitHub connection (#60640)
1 parent 47a4f1a commit e4400f4

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

providers/github/docs/connections/github.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ GitHub App authentication
5252
You can authenticate using a GitHub App installation by setting the extra field of your connection, instead of using a token.
5353

5454
- ``key_path``: Path to the private key file used for GitHub App authentication.
55+
- ``private_key``: The private key content used for GitHub App authentication. (alternative to ``key_path``)
5556
- ``app_id``: The application ID.
5657
- ``installation_id``: The ID of the app installation.
5758
- ``token_permissions``: A dictionary of permissions. - Properties of permissions - https://docs.github.com/en/rest/apps/apps?apiVersion=2022-11-28#create-an-installation-access-token-for-an-app
@@ -69,3 +70,17 @@ Example "extras" field:
6970
"contents":"read"
7071
}
7172
}
73+
74+
Example with embedded private key:
75+
76+
.. code-block:: json
77+
78+
{
79+
"private_key": "-----BEGIN+PRIVATE+KEY-----%0AM...%0A-----END+PRIVATE+KEY-----",
80+
"app_id": "123456s",
81+
"installation_id": 123456789,
82+
"token_permissions": {
83+
"issues":"write",
84+
"contents":"read"
85+
}
86+
}

providers/github/src/airflow/providers/github/hooks/github.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,13 @@ def get_conn(self) -> GithubClient:
6464
raise ValueError("Unrecognised key file: expected a .pem private key")
6565
with open(key_path) as key_file:
6666
private_key = key_file.read()
67+
elif key := extras.get("private_key"):
68+
private_key = key
69+
6770
else:
68-
raise ValueError("No key_path provided for GitHub App authentication.")
71+
raise ValueError(
72+
"Neither key_path nor private_key are provided for GitHub App authentication."
73+
)
6974

7075
app_id = extras.get("app_id")
7176
installation_id = extras.get("installation_id")

providers/github/tests/unit/github/hooks/test_github.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,19 @@ def setup_connections(self, create_connection_without_db):
5454
},
5555
)
5656
)
57+
create_connection_without_db(
58+
Connection(
59+
conn_id="github_app_embedded_key",
60+
conn_type="github",
61+
host="https://mygithub.com/api/v3",
62+
extra={
63+
"app_id": "123456",
64+
"installation_id": 654321,
65+
"private_key": "-----BEGIN+PRIVATE+KEY-----%0AM...%0A-----END+PRIVATE+KEY-----",
66+
"token_permissions": {"issues": "write", "pull_requests": "read"},
67+
},
68+
)
69+
)
5770

5871
@patch(
5972
"airflow.providers.github.hooks.github.GithubClient", autospec=True, return_value=github_client_mock
@@ -65,7 +78,7 @@ def test_github_client_connection(self, github_mock):
6578
assert isinstance(github_hook.client, Mock)
6679
assert github_hook.client.name == github_mock.return_value.name
6780

68-
@pytest.mark.parametrize("conn_id", ["github_default", "github_app_conn"])
81+
@pytest.mark.parametrize("conn_id", ["github_default", "github_app_conn", "github_app_embedded_key"])
6982
@patch(
7083
"airflow.providers.github.hooks.github.open",
7184
new_callable=mock_open,
@@ -81,7 +94,7 @@ def test_connection_success(self, mock_file, conn_id):
8194
assert status is True
8295
assert msg == "Successfully connected to GitHub."
8396

84-
@pytest.mark.parametrize("conn_id", ["github_default", "github_app_conn"])
97+
@pytest.mark.parametrize("conn_id", ["github_default", "github_app_conn", "github_app_embedded_key"])
8598
@patch(
8699
"airflow.providers.github.hooks.github.open",
87100
new_callable=mock_open,
@@ -118,7 +131,7 @@ def test_connection_failure(self, mock_file, conn_id):
118131
(
119132
"missing_key_path",
120133
{"app_id": "1", "installation_id": 1},
121-
"No key_path provided for GitHub App authentication.",
134+
"Neither key_path nor private_key are provided for GitHub App authentication.",
122135
),
123136
# installation_id is not integer
124137
(

0 commit comments

Comments
 (0)