From 99b69d3e560a0e98416df632a6738223cab8f637 Mon Sep 17 00:00:00 2001 From: Anshuman Goel Date: Thu, 29 Oct 2020 07:44:25 -0700 Subject: [PATCH] Adding cache for sas return (#224) --- src/cli/onefuzz/api.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/cli/onefuzz/api.py b/src/cli/onefuzz/api.py index b45d4e2edb..d236eebbd7 100644 --- a/src/cli/onefuzz/api.py +++ b/src/cli/onefuzz/api.py @@ -10,11 +10,12 @@ import subprocess # nosec import uuid from shutil import which -from typing import Callable, Dict, List, Optional, Tuple, Type, TypeVar +from typing import Callable, Dict, List, Optional, Tuple, Type, TypeVar, cast from uuid import UUID import pkg_resources import semver +from memoization import cached from onefuzztypes import enums, models, primitives, requests, responses from pydantic import BaseModel from six.moves import input # workaround for static analysis @@ -33,6 +34,8 @@ # This was generated randomly and should be preserved moving forwards ONEFUZZ_GUID_NAMESPACE = uuid.UUID("27f25e3f-6544-4b69-b309-9b096c5a9cbc") +ONE_HOUR_IN_SECONDS = 3600 + DEFAULT_LINUX_IMAGE = "Canonical:UbuntuServer:18.04-LTS:latest" DEFAULT_WINDOWS_IMAGE = "MicrosoftWindowsDesktop:Windows-10:rs5-pro:latest" @@ -146,6 +149,7 @@ class Files(Endpoint): endpoint = "files" + @cached(ttl=ONE_HOUR_IN_SECONDS) def _get_client(self, container: str) -> ContainerWrapper: sas = self.onefuzz.containers.get(container).sas_url return ContainerWrapper(sas) @@ -166,7 +170,8 @@ def get(self, container: str, filename: str) -> bytes: """ get a file from a container """ self.logger.debug("getting file from container: %s:%s", container, filename) client = self._get_client(container) - return client.download_blob(filename) + downloaded = cast(bytes, client.download_blob(filename)) + return downloaded def upload_file( self, container: str, file_path: str, blob_name: Optional[str] = None