Skip to content
This repository was archived by the owner on Nov 1, 2023. It is now read-only.

Commit 313047c

Browse files
committed
Make the log sas url last as long as the job duration (#2116)
1 parent 358c57a commit 313047c

File tree

2 files changed

+17
-18
lines changed

2 files changed

+17
-18
lines changed

src/api-service/__app__/download/__init__.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
# Copyright (c) Microsoft Corporation.
44
# Licensed under the MIT License.
55

6+
from datetime import timedelta
7+
68
import azure.functions as func
79
from onefuzztypes.enums import ErrorCode
810
from onefuzztypes.models import Error, FileEntry
@@ -40,8 +42,7 @@ def get(req: func.HttpRequest) -> func.HttpResponse:
4042
request.filename,
4143
StorageType.corpus,
4244
read=True,
43-
days=0,
44-
minutes=5,
45+
duration=timedelta(minutes=5),
4546
)
4647
)
4748

src/api-service/__app__/onefuzzlib/azure/containers.py

+14-16
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
get_storage_account_name_key_by_name,
3232
)
3333

34+
CONTAINER_SAS_DEFAULT_DURATION = datetime.timedelta(days=30)
35+
3436

3537
def get_url(account_name: str) -> str:
3638
return f"https://{account_name}.blob.core.windows.net/"
@@ -109,12 +111,15 @@ def get_container_metadata(
109111
return cast(Dict[str, str], result)
110112

111113

112-
def add_container_sas_url(container_url: str) -> str:
114+
def add_container_sas_url(
115+
container_url: str, duration: datetime.timedelta = CONTAINER_SAS_DEFAULT_DURATION
116+
) -> str:
113117
parsed = urllib.parse.urlparse(container_url)
114118
query = urllib.parse.parse_qs(parsed.query)
115119
if "sig" in query:
116120
return container_url
117121
else:
122+
start, expiry = sas_time_window(duration)
118123
account_name = parsed.netloc.split(".")[0]
119124
account_key = get_storage_account_name_key_by_name(account_name)
120125
sas_token = generate_container_sas(
@@ -124,7 +129,8 @@ def add_container_sas_url(container_url: str) -> str:
124129
permission=ContainerSasPermissions(
125130
read=True, write=True, delete=True, list=True
126131
),
127-
expiry=datetime.datetime.utcnow() + datetime.timedelta(hours=1),
132+
expiry=expiry,
133+
start=start,
128134
)
129135
return f"{container_url}?{sas_token}"
130136

@@ -189,7 +195,7 @@ def delete_container(container: Container, storage_type: StorageType) -> bool:
189195

190196

191197
def sas_time_window(
192-
*, days: int, hours: int, minutes: int
198+
duration: datetime.timedelta,
193199
) -> Tuple[datetime.datetime, datetime.datetime]:
194200
# SAS URLs are valid 6 hours earlier, primarily to work around dev
195201
# workstations having out-of-sync time. Additionally, SAS URLs are stopped
@@ -201,11 +207,7 @@ def sas_time_window(
201207

202208
now = datetime.datetime.utcnow()
203209
start = now - SAS_START_TIME_DELTA
204-
expiry = (
205-
now
206-
+ datetime.timedelta(days=days, hours=hours, minutes=minutes)
207-
+ SAS_END_TIME_DELTA
208-
)
210+
expiry = now + duration + SAS_END_TIME_DELTA
209211
return (start, expiry)
210212

211213

@@ -218,15 +220,13 @@ def get_container_sas_url_service(
218220
list_: bool = False,
219221
delete_previous_version: bool = False,
220222
tag: bool = False,
221-
days: int = 30,
222-
hours: int = 0,
223-
minutes: int = 0,
223+
duration: datetime.timedelta = CONTAINER_SAS_DEFAULT_DURATION,
224224
) -> str:
225225
account_name = client.account_name
226226
container_name = client.container_name
227227
account_key = get_storage_account_name_key_by_name(account_name)
228228

229-
start, expiry = sas_time_window(days=days, hours=hours, minutes=minutes)
229+
start, expiry = sas_time_window(duration)
230230

231231
sas = generate_container_sas(
232232
account_name,
@@ -295,16 +295,14 @@ def get_file_sas_url(
295295
delete: bool = False,
296296
delete_previous_version: bool = False,
297297
tag: bool = False,
298-
days: int = 30,
299-
hours: int = 0,
300-
minutes: int = 0,
298+
duration: datetime.timedelta = CONTAINER_SAS_DEFAULT_DURATION,
301299
) -> str:
302300
client = find_container(container, storage_type)
303301
if not client:
304302
raise Exception("unable to find container: %s - %s" % (container, storage_type))
305303

306304
account_key = get_storage_account_name_key_by_name(client.account_name)
307-
start, expiry = sas_time_window(days=days, hours=hours, minutes=minutes)
305+
start, expiry = sas_time_window(duration)
308306

309307
permission = BlobSasPermissions(
310308
read=read,

0 commit comments

Comments
 (0)