-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsource.py
More file actions
286 lines (249 loc) · 9.65 KB
/
Copy pathsource.py
File metadata and controls
286 lines (249 loc) · 9.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import logging
from dataclasses import dataclass, field
from threading import Lock
from typing import Any, Union
import dlt
from dlt.common.configuration import configspec
from dlt.common.configuration.specs import CredentialsConfiguration
from dlt.sources.helpers import requests
from dlt.sources.helpers.rest_client.auth import AuthConfigBase
from dlt.sources.helpers.rest_client.auth import BearerTokenAuth
from dlt.sources.helpers.rest_client.client import RESTClient
from dlt.sources.helpers.rest_client.paginators import (
HeaderLinkPaginator,
)
from openhound_github.auth import (
GithubApp,
GitHubAppInstallationAuth,
GithubInstallation,
resolve_github_app_jwt_issuer,
)
from openhound_github.helpers import github_retry_policy
from openhound_github.main import app
from openhound_github.models.saml_helpers import (
DEFAULT_GITHUB_DEPLOYMENT_ID,
DEFAULT_GITHUB_WEB_ORIGIN,
github_deployment_context,
)
from .resources.enterprise import enterprise_resources
from .resources.organization import organization_resources
logger = logging.getLogger(__name__)
@dataclass
class OrgContext:
client: RESTClient
org_name: str
enterprise_name: str | None = None
github_deployment_id: str = DEFAULT_GITHUB_DEPLOYMENT_ID
github_web_origin: str = DEFAULT_GITHUB_WEB_ORIGIN
@dataclass
class SourceContext:
organizations: list[OrgContext] | None = field(default_factory=list)
client: RESTClient | None = None
sso_client: RESTClient | None = None
enterprise_name: str | None = None
emit_legacy_scim_correlations: bool = False
github_deployment_id: str = DEFAULT_GITHUB_DEPLOYMENT_ID
github_web_origin: str = DEFAULT_GITHUB_WEB_ORIGIN
cache_lock: Lock = field(default_factory=Lock)
organizations_cache: dict[str, dict[str, Any]] = field(default_factory=dict)
app_cache: dict[str, dict[str, Any]] = field(default_factory=dict)
actions_permissions_cache: dict[str, dict[str, Any]] = field(default_factory=dict)
runner_permissions_cache: dict[str, dict[str, Any]] = field(default_factory=dict)
workflow_permissions_cache: dict[str, dict[str, Any]] = field(default_factory=dict)
@property
def org_names(self) -> list[str]:
return [org.org_name for org in self.organizations or []]
def _canonicalize_org_names(ctx: SourceContext) -> None:
for org in ctx.organizations or []:
configured_name = org.org_name
try:
org_data = org.client.get(f"/orgs/{configured_name}").json()
except Exception as exc:
logger.warning(
"Unable to resolve canonical GitHub login for organization '%s': %s",
configured_name,
exc,
)
continue
canonical_name = org_data.get("login")
if not isinstance(canonical_name, str) or not canonical_name:
logger.warning(
"GitHub organization response for '%s' did not include a canonical login",
configured_name,
)
continue
org.org_name = canonical_name
ctx.organizations_cache[canonical_name] = org_data
@configspec
class GithubCredentials(CredentialsConfiguration):
org_name: str | None = None
enterprise_name: str | None = None
def auth(self):
pass
@configspec
class GithubEnterpriseAppCredentials(CredentialsConfiguration):
client_id: str | None = None
app_id: str | None = None
key_path: str = None
enterprise_name: str = None
pat_token: str | None = None
api_uri: str = "https://api.github.com"
@property
def auth(self) -> str:
return "enterprise_app"
@configspec
class GithubOrgAppCredentials(CredentialsConfiguration):
client_id: str = None
install_id: str = None
key_path: str = None
org_name: str = None
api_uri: str = "https://api.github.com"
@property
def auth(self) -> str:
return "org_app"
@configspec
class GithubTokenCredentials(GithubCredentials):
token: str = None
@property
def auth(self) -> str:
return "token"
@property
def header(self) -> str:
return f"{self.token}"
@app.source(name="github", max_table_nesting=0)
def source(
credentials: Union[
GithubEnterpriseAppCredentials, GithubOrgAppCredentials, GithubTokenCredentials
] = dlt.secrets.value,
host: str = "https://api.github.com",
emit_legacy_scim_correlations: bool | None = dlt.config.value,
):
"""DLT source, defines GitHub collection resources and transformers.
Args:
credentials (Union[GithubEnterpriseAppCredentials, GithubOrgAppCredentials, GithubTokenCredentials]): The GitHub credentials.
host (str): The base GitHub API URL used for API calls.
"""
github_deployment_id, github_web_origin = github_deployment_context(host)
def client(auth: AuthConfigBase) -> RESTClient:
return RESTClient(
base_url=host,
headers={
"Accept": "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28",
},
auth=auth,
paginator=HeaderLinkPaginator(),
session=requests.Client(
status_codes=tuple(range(500, 600)),
retry_condition=github_retry_policy(auth),
).session,
)
def token_client(token: str) -> RESTClient:
return client(BearerTokenAuth(token=token))
if credentials.auth == "enterprise_app":
jwt_issuer = resolve_github_app_jwt_issuer(
client_id=credentials.client_id,
app_id=credentials.app_id,
)
ctx = SourceContext(
enterprise_name=credentials.enterprise_name,
emit_legacy_scim_correlations=bool(emit_legacy_scim_correlations),
github_deployment_id=github_deployment_id,
github_web_origin=github_web_origin,
)
if credentials.pat_token:
ctx.sso_client = token_client(credentials.pat_token)
github_app_session = GithubApp(
jwt_issuer=jwt_issuer,
private_key_path=credentials.key_path,
api_uri=host,
)
for installation in github_app_session.installations:
if installation.target_type == "Organization":
org_installation = GithubInstallation(
installation_id=installation.id,
jwt_issuer=jwt_issuer,
private_key_path=credentials.key_path,
api_uri=host,
)
ctx.organizations.append(
OrgContext(
org_name=installation.account.login,
client=client(
GitHubAppInstallationAuth(
installation=org_installation,
api_uri=host,
)
),
enterprise_name=credentials.enterprise_name,
github_deployment_id=github_deployment_id,
github_web_origin=github_web_origin,
)
)
if installation.target_type == "Enterprise":
es_installation = GithubInstallation(
installation_id=installation.id,
jwt_issuer=jwt_issuer,
private_key_path=credentials.key_path,
api_uri=host,
)
ctx.client = client(
GitHubAppInstallationAuth(
installation=es_installation,
api_uri=host,
)
)
return (*enterprise_resources(ctx), *organization_resources(ctx))
elif credentials.auth == "org_app":
ctx = SourceContext(
enterprise_name=None,
github_deployment_id=github_deployment_id,
github_web_origin=github_web_origin,
)
org_installation = GithubInstallation(
installation_id=credentials.install_id,
jwt_issuer=credentials.client_id,
private_key_path=credentials.key_path,
api_uri=host,
)
ctx.organizations.append(
OrgContext(
org_name=credentials.org_name,
client=client(
GitHubAppInstallationAuth(
installation=org_installation,
api_uri=host,
)
),
github_deployment_id=github_deployment_id,
github_web_origin=github_web_origin,
)
)
_canonicalize_org_names(ctx)
return organization_resources(ctx)
else:
if credentials.enterprise_name:
token_api_client = token_client(credentials.token)
ctx = SourceContext(
client=token_api_client,
sso_client=token_api_client,
enterprise_name=credentials.enterprise_name,
emit_legacy_scim_correlations=bool(emit_legacy_scim_correlations),
github_deployment_id=github_deployment_id,
github_web_origin=github_web_origin,
)
return enterprise_resources(ctx)
ctx = SourceContext(
github_deployment_id=github_deployment_id,
github_web_origin=github_web_origin,
)
ctx.organizations.append(
OrgContext(
org_name=credentials.org_name,
client=token_client(credentials.token),
github_deployment_id=github_deployment_id,
github_web_origin=github_web_origin,
)
)
_canonicalize_org_names(ctx)
return organization_resources(ctx)