|
10 | 10 | # distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
11 | 11 | # ANY KIND, either express or implied. See the License for the specific |
12 | 12 | # language governing permissions and limitations under the License. |
13 | | -import hashlib |
14 | 13 | import io |
15 | 14 | import os |
16 | 15 | import sqlite3 |
|
22 | 21 | from functools import cached_property |
23 | 22 | from pathlib import Path |
24 | 23 |
|
| 24 | +from botocore.compat import get_md5 |
| 25 | +from botocore.exceptions import MD5UnavailableError |
25 | 26 | from botocore.useragent import UserAgentComponent |
26 | 27 |
|
27 | 28 | from awscli.compat import is_windows |
|
30 | 31 | _CACHE_DIR = Path.home() / '.aws' / 'cli' / 'cache' |
31 | 32 | _DATABASE_FILENAME = 'session.db' |
32 | 33 | _SESSION_LENGTH_SECONDS = 60 * 30 |
| 34 | +_SESSION_ID_LENGTH = 12 |
33 | 35 |
|
34 | 36 | _CACHE_DIR.mkdir(parents=True, exist_ok=True) |
35 | 37 |
|
36 | 38 |
|
| 39 | +def _get_checksum(): |
| 40 | + hashlib_params = {"usedforsecurity": False} |
| 41 | + try: |
| 42 | + checksum = get_md5(**hashlib_params) |
| 43 | + except MD5UnavailableError: |
| 44 | + import hashlib |
| 45 | + |
| 46 | + checksum = hashlib.sha256(**hashlib_params) |
| 47 | + return checksum |
| 48 | + |
| 49 | + |
37 | 50 | @dataclass |
38 | 51 | class CLISessionData: |
39 | 52 | key: str |
@@ -179,17 +192,19 @@ def sweep(self, timestamp): |
179 | 192 |
|
180 | 193 | class CLISessionGenerator: |
181 | 194 | def generate_session_id(self, host_id, tty, timestamp): |
182 | | - return self._generate_md5_hash(host_id, tty, timestamp) |
| 195 | + return self._generate_checksum(host_id, tty, timestamp) |
183 | 196 |
|
184 | 197 | def generate_cache_key(self, host_id, tty): |
185 | | - return self._generate_md5_hash(host_id, tty) |
| 198 | + return self._generate_checksum(host_id, tty) |
186 | 199 |
|
187 | | - def _generate_md5_hash(self, *args): |
| 200 | + def _generate_checksum(self, *args): |
| 201 | + checksum = _get_checksum() |
188 | 202 | str_to_hash = "" |
189 | 203 | for arg in args: |
190 | 204 | if arg is not None: |
191 | 205 | str_to_hash += str(arg) |
192 | | - return hashlib.md5(str_to_hash.encode('utf-8')).hexdigest() |
| 206 | + checksum.update(str_to_hash.encode('utf-8')) |
| 207 | + return checksum.hexdigest()[:_SESSION_ID_LENGTH] |
193 | 208 |
|
194 | 209 |
|
195 | 210 | class CLISessionOrchestrator: |
@@ -273,7 +288,16 @@ def _get_cli_session_orchestrator(): |
273 | 288 |
|
274 | 289 |
|
275 | 290 | def add_session_id_component_to_user_agent_extra(session, orchestrator=None): |
276 | | - cli_session_orchestrator = orchestrator or _get_cli_session_orchestrator() |
277 | | - add_component_to_user_agent_extra( |
278 | | - session, UserAgentComponent("sid", cli_session_orchestrator.session_id) |
279 | | - ) |
| 291 | + try: |
| 292 | + cli_session_orchestrator = ( |
| 293 | + orchestrator or _get_cli_session_orchestrator() |
| 294 | + ) |
| 295 | + add_component_to_user_agent_extra( |
| 296 | + session, |
| 297 | + UserAgentComponent("sid", cli_session_orchestrator.session_id), |
| 298 | + ) |
| 299 | + except Exception: |
| 300 | + # Ideally, the AWS CLI should never throw if the session id |
| 301 | + # can't be generated since it's not critical for users. Issues |
| 302 | + # with session dada should instead be caught server-side. |
| 303 | + pass |
0 commit comments