From b9529d3570324d6cf64913822b4fd8344340d5d1 Mon Sep 17 00:00:00 2001 From: Jarek Potiuk Date: Tue, 26 Oct 2021 14:03:01 -0100 Subject: [PATCH] Fix hard-coded /tmp directory in CloudSQL Hook The /tmp directory is not "guaranteed" to be THE /tmp one. There are cases where TMPDIR or other env variables are set to override it (for example when user has no write access to /tmp dir) and we should respect that. The gettempdir() will produce the right temporary directory based on the env variables set in the system. --- airflow/providers/google/cloud/hooks/cloud_sql.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/airflow/providers/google/cloud/hooks/cloud_sql.py b/airflow/providers/google/cloud/hooks/cloud_sql.py index b157ab140948d..18ff970a33ddb 100644 --- a/airflow/providers/google/cloud/hooks/cloud_sql.py +++ b/airflow/providers/google/cloud/hooks/cloud_sql.py @@ -33,6 +33,7 @@ import uuid from pathlib import Path from subprocess import PIPE, Popen +from tempfile import gettempdir from typing import Any, Dict, List, Optional, Sequence, Union from urllib.parse import quote_plus @@ -838,12 +839,12 @@ def _generate_unique_path() -> str: can be close to 60 characters and there is a limitation in length of socket path to around 100 characters in total. We append project/location/instance to it later and postgres - appends its own prefix, so we chose a shorter "/tmp/[8 random characters]" + appends its own prefix, so we chose a shorter "${tempdir()}[8 random characters]" """ random.seed() while True: - candidate = "/tmp/" + ''.join( - random.choice(string.ascii_lowercase + string.digits) for _ in range(8) + candidate = os.path.join( + gettempdir(), ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(8)) ) if not os.path.exists(candidate): return candidate