@@ -56,10 +56,8 @@ def get_expose_api_map():
5656 return expose_api
5757
5858
59- def get_jwt_keys ():
60- gitlab_server_url = f'{ GITLAB_SERVER } /oauth/discovery/keys'
61-
62- request_jwk = requests .get (gitlab_server_url )
59+ def get_jwt_keys (jwt_server_url ):
60+ request_jwk = requests .get (jwt_server_url )
6361 request_jwk .raise_for_status ()
6462
6563 jwk = request_jwk .json ()
@@ -93,7 +91,7 @@ def request_github_sync(image):
9391 """
9492 request = requests .post (
9593 url = (
96- f'https://api.github.com/repos/{ GITHUB_REPO } /actions/workflows/{ GITHUB_WORKFLOW } /dispatches'
94+ f'https://api.github.com/repos/{ GITHUB_REPO } /actions/workflows/{ GITHUB_WORKFLOW } /dispatches' # noqa
9795 ),
9896 data = json .dumps ({
9997 'ref' : 'action-testing' ,
@@ -135,9 +133,18 @@ def check_authorization(
135133
136134expose_api = get_expose_api_map ()
137135
138- jwks_keys = None
136+ gitlab_jwks_keys = None
139137if expose_api ['gitlab' ]:
140- jwks_keys = get_jwt_keys ()
138+ gitlab_jwks_keys = get_jwt_keys (
139+ f'{ GITLAB_SERVER } /oauth/discovery/keys' ,
140+ )
141+
142+
143+ github_jwks_keys = None
144+ if expose_api ['github' ]:
145+ github_jwks_keys = get_jwt_keys (
146+ 'https://token.actions.githubusercontent.com/.well-known/jwks' ,
147+ )
141148
142149
143150@app .get ('/' )
@@ -147,7 +154,7 @@ def root():
147154
148155if expose_api ['gitlab' ]:
149156 @app .post ('/api/gitlab/sync/jwt' )
150- def sync_jwt (
157+ def gitlab_sync_jwt (
151158 authorization : Annotated [str | None , HTTPBearer ()] = None ,
152159 image : str | None = None ,
153160 ):
@@ -156,7 +163,7 @@ def sync_jwt(
156163 try :
157164 claims = jwt .decode (
158165 authorization ,
159- jwks_keys ,
166+ gitlab_jwks_keys ,
160167 )
161168 except DecodeError :
162169 raise HTTPException (
@@ -177,9 +184,41 @@ def sync_jwt(
177184 request_gitlab_sync (image )
178185
179186
187+ if expose_api ['github' ]:
188+ @app .post ('/api/github/sync/jwt' )
189+ def github_sync_jwt (
190+ authorization : Annotated [str | None , HTTPBearer ()] = None ,
191+ image : str | None = None ,
192+ ):
193+
194+ check_authorization (authorization )
195+ try :
196+ claims = jwt .decode (
197+ authorization ,
198+ github_jwks_keys ,
199+ )
200+ except DecodeError :
201+ raise HTTPException (
202+ status_code = 403 ,
203+ detail = 'Invalid token: DecodeError' ,
204+ )
205+ except BadSignatureError :
206+ raise HTTPException (
207+ status_code = 403 ,
208+ detail = 'Invalid token: BadSignatureError' ,
209+ )
210+ if claims ['iss' ] != 'https://github.com' :
211+ raise HTTPException (
212+ status_code = 403 ,
213+ detail = f"Invalid issuer { claims ['iss' ]} " ,
214+ )
215+
216+ request_gitlab_sync (image )
217+
218+
180219if expose_api ['secret' ] and expose_api ['gitlab' ]:
181220 @app .post ('/api/gitlab/sync/secret' )
182- def sync_secret (
221+ def gitlab_sync_secret (
183222 authorization : Annotated [str | None , HTTPBearer ()] = None ,
184223 image : str | None = None ,
185224 ):
@@ -197,7 +236,7 @@ def sync_secret(
197236
198237if expose_api ['secret' ] and expose_api ['github' ]:
199238 @app .post ('/api/github/sync/secret' )
200- def sync_secret (
239+ def github_sync_secret (
201240 authorization : Annotated [str | None , HTTPBearer ()] = None ,
202241 image : str | None = None ,
203242 ):
0 commit comments