|
14 | 14 | # See the License for the specific language governing permissions and |
15 | 15 | # limitations under the License. |
16 | 16 | """Renku service utility functions.""" |
| 17 | +import os |
| 18 | +import urllib |
17 | 19 | from time import sleep |
18 | 20 | from typing import Any, Dict, Optional, overload |
19 | 21 |
|
20 | 22 | import requests |
21 | | -import urllib |
22 | 23 | from jwt import PyJWKClient |
23 | 24 |
|
| 25 | +from renku.core.util.requests import get |
24 | 26 | from renku.ui.service.config import CACHE_PROJECTS_PATH, CACHE_UPLOADS_PATH, OIDC_URL |
25 | 27 | from renku.ui.service.errors import ProgramInternalError |
26 | 28 | from renku.ui.service.logger import service_log |
27 | | -from renku.core.util.requests import get |
28 | 29 |
|
29 | 30 |
|
30 | 31 | def make_project_path(user, project): |
@@ -101,28 +102,36 @@ def oidc_discovery() -> Dict[str, Any]: |
101 | 102 | retries = 0 |
102 | 103 | max_retries = 30 |
103 | 104 | sleep_seconds = 2 |
| 105 | + renku_domain = os.environ.get("RENKU_DOMAIN") |
| 106 | + if not renku_domain: |
| 107 | + raise ProgramInternalError( |
| 108 | + error_message="Cannot perform OIDC discovery without the renku domain expected " |
| 109 | + "to be found in the RENKU_DOMAIN environment variable." |
| 110 | + ) |
| 111 | + full_oidc_url = f"http://{renku_domain}{OIDC_URL}" |
104 | 112 | while True: |
105 | 113 | retries += 1 |
106 | 114 | try: |
107 | | - res: requests.Response = get(OIDC_URL) |
| 115 | + res: requests.Response = get(full_oidc_url) |
108 | 116 | except (requests.exceptions.HTTPError, urllib.error.HTTPError) as e: |
109 | 117 | if not retries < max_retries: |
110 | 118 | service_log.error("Failed to get OIDC discovery data after all retries - the server cannot start.") |
111 | 119 | raise e |
112 | 120 | service_log.info( |
113 | | - f"Failed to get OIDC discovery data from {OIDC_URL}, sleeping for {sleep_seconds} seconds and retrying" |
| 121 | + f"Failed to get OIDC discovery data from {full_oidc_url}, " |
| 122 | + f"sleeping for {sleep_seconds} seconds and retrying" |
114 | 123 | ) |
115 | 124 | sleep(sleep_seconds) |
116 | 125 | else: |
117 | | - service_log.info(f"Successfully fetched OIDC discovery data from {OIDC_URL}") |
| 126 | + service_log.info(f"Successfully fetched OIDC discovery data from {full_oidc_url}") |
118 | 127 | return res.json() |
119 | 128 |
|
120 | 129 |
|
121 | 130 | def jwk_client() -> PyJWKClient: |
122 | | - """Return a JWK client for Keycloak that can be used to provide JWT keys for JWT signature validation""" |
| 131 | + """Return a JWK client for Keycloak that can be used to provide JWT keys for JWT signature validation.""" |
123 | 132 | oidc_data = oidc_discovery() |
124 | 133 | jwks_uri = oidc_data.get("jwks_uri") |
125 | 134 | if not jwks_uri: |
126 | | - raise ProgramInternalError(error_message="Could not find JWK URI in the OIDC discovery data") |
| 135 | + raise ProgramInternalError(error_message="Could not find jwks_uri in the OIDC discovery data") |
127 | 136 | jwk = PyJWKClient(jwks_uri) |
128 | 137 | return jwk |
0 commit comments