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

Commit 169cef7

Browse files
authored
Remove Additional config params - require on each request (#3000)
* Only Overrite Config Cache * Lint * Fixing isort. * Removing expiry. * Removing import. * Removing config params. * Remove bad import. * Adjusting to type changes. * Remove whitespace. * Formatting. * Formatting. * null check. * Formatting.
1 parent 77c4293 commit 169cef7

File tree

3 files changed

+31
-47
lines changed

3 files changed

+31
-47
lines changed

src/cli/examples/azure-functions-example/info/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ def main(req: func.HttpRequest) -> func.HttpResponse:
1212
o = Onefuzz()
1313
o.config(
1414
endpoint=os.environ.get("ONEFUZZ_ENDPOINT"),
15-
override_authority=os.environ.get("ONEFUZZ_AUTHORITY"),
16-
client_id=os.environ.get("ONEFUZZ_CLIENT_ID"),
1715
)
1816
info = o.info.get()
1917
return func.HttpResponse(info.json())

src/cli/onefuzz/api.py

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,7 @@
4040

4141
UUID_EXPANSION = TypeVar("UUID_EXPANSION", UUID, str)
4242

43-
DEFAULT = BackendConfig(
44-
authority="",
45-
client_id="",
46-
tenant_domain="",
47-
)
43+
DEFAULT = BackendConfig(endpoint="")
4844

4945
# This was generated randomly and should be preserved moving forwards
5046
ONEFUZZ_GUID_NAMESPACE = uuid.UUID("27f25e3f-6544-4b69-b309-9b096c5a9cbc")
@@ -1310,7 +1306,7 @@ def get_config(self, pool_name: primitives.PoolName) -> models.AgentConfig:
13101306
raise Exception("Missing AgentConfig in response")
13111307

13121308
config = pool.config
1313-
if not pool.managed:
1309+
if not pool.managed and self.onefuzz._backend.config.authority:
13141310
config.client_credentials = models.ClientCredentials( # nosec
13151311
client_id=uuid.UUID(int=0),
13161312
client_secret="<client_secret>",
@@ -1894,19 +1890,14 @@ def login(self) -> str:
18941890
def config(
18951891
self,
18961892
endpoint: Optional[str] = None,
1897-
override_authority: Optional[str] = None,
1898-
client_id: Optional[str] = None,
1899-
override_tenant_domain: Optional[str] = None,
19001893
enable_feature: Optional[PreviewFeature] = None,
19011894
reset: Optional[bool] = None,
19021895
) -> BackendConfig:
19031896
"""Configure onefuzz CLI"""
19041897
self.logger.debug("set config")
19051898

19061899
if reset:
1907-
self._backend.config = BackendConfig(
1908-
authority="", client_id="", tenant_domain=""
1909-
)
1900+
self._backend.config = BackendConfig(endpoint="")
19101901

19111902
if endpoint is not None:
19121903
# The normal path for calling the API always uses the oauth2 workflow,
@@ -1922,17 +1913,12 @@ def config(
19221913
"Missing HTTP Authentication"
19231914
)
19241915
self._backend.config.endpoint = endpoint
1925-
if client_id is not None:
1926-
self._backend.config.client_id = client_id
1927-
if override_authority is not None:
1928-
self._backend.config.authority = override_authority
1916+
19291917
if enable_feature:
19301918
self._backend.enable_feature(enable_feature.name)
1931-
if override_tenant_domain is not None:
1932-
self._backend.config.tenant_domain = override_tenant_domain
1919+
19331920
self._backend.app = None
19341921
self._backend.save_config()
1935-
19361922
data = self._backend.config.copy(deep=True)
19371923

19381924
if not data.endpoint:

src/cli/onefuzz/backend.py

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import tempfile
1313
import time
1414
from dataclasses import asdict, is_dataclass
15-
from datetime import datetime, timedelta
1615
from enum import Enum
1716
from typing import (
1817
Any,
@@ -33,7 +32,7 @@
3332
import requests
3433
from azure.storage.blob import ContainerClient
3534
from onefuzztypes import responses
36-
from pydantic import BaseModel, Field
35+
from pydantic import BaseModel
3736
from requests import Response
3837
from tenacity import RetryCallState, retry
3938
from tenacity.retry import retry_if_exception_type
@@ -93,20 +92,26 @@ def check_application_error(response: requests.Response) -> None:
9392

9493

9594
class BackendConfig(BaseModel):
96-
authority: str
97-
client_id: str
98-
endpoint: Optional[str]
99-
features: Set[str] = Field(default_factory=set)
100-
tenant_domain: str
101-
expires_on: datetime = datetime.utcnow() + timedelta(hours=24)
95+
authority: Optional[str]
96+
client_id: Optional[str]
97+
endpoint: str
98+
features: Optional[Set[str]]
99+
tenant_domain: Optional[str]
102100

103101
def get_multi_tenant_domain(self) -> Optional[str]:
104-
if "https://login.microsoftonline.com/common" in self.authority:
102+
if (
103+
self.authority
104+
and "https://login.microsoftonline.com/common" in self.authority
105+
):
105106
return self.tenant_domain
106107
else:
107108
return None
108109

109110

111+
class CacheConfig(BaseModel):
112+
endpoint: Optional[str]
113+
114+
110115
class Backend:
111116
def __init__(
112117
self,
@@ -129,10 +134,14 @@ def __init__(
129134
atexit.register(self.save_cache)
130135

131136
def enable_feature(self, name: str) -> None:
137+
if not self.config.features:
138+
self.config.features = Set[str]()
132139
self.config.features.add(name)
133140

134141
def is_feature_enabled(self, name: str) -> bool:
135-
return name in self.config.features
142+
if self.config.features:
143+
return name in self.config.features
144+
return False
136145

137146
def load_config(self) -> None:
138147
if os.path.exists(self.config_path):
@@ -143,7 +152,8 @@ def load_config(self) -> None:
143152
def save_config(self) -> None:
144153
os.makedirs(os.path.dirname(self.config_path), exist_ok=True)
145154
with open(self.config_path, "w") as handle:
146-
handle.write(self.config.json(indent=4, exclude_none=True))
155+
endpoint_cache = {"endpoint": f"{self.config.endpoint}"}
156+
handle.write(json.dumps(endpoint_cache, indent=4, sort_keys=True))
147157

148158
def init_cache(self) -> None:
149159
# Ensure the token_path directory exists
@@ -331,15 +341,13 @@ def config_params(
331341
endpoint_params = responses.Config.parse_obj(response.json())
332342

333343
# Will override values in storage w/ provided values for SP use
334-
if self.config.client_id == "":
344+
if not self.config.client_id:
335345
self.config.client_id = endpoint_params.client_id
336-
if self.config.authority == "":
346+
if not self.config.authority:
337347
self.config.authority = endpoint_params.authority
338-
if self.config.tenant_domain == "":
348+
if not self.config.tenant_domain:
339349
self.config.tenant_domain = endpoint_params.tenant_domain
340350

341-
self.save_config()
342-
343351
def request(
344352
self,
345353
method: str,
@@ -353,17 +361,9 @@ def request(
353361
if not endpoint:
354362
raise Exception("endpoint not configured")
355363

356-
# If file expires, remove and force user to reset
357-
if datetime.utcnow() > self.config.expires_on:
358-
os.remove(self.config_path)
359-
self.config = BackendConfig(
360-
endpoint=endpoint, authority="", client_id="", tenant_domain=""
361-
)
362-
363364
url = endpoint + "/api/" + path
364-
365-
if self.config.client_id == "" or (
366-
self.config.authority == "" and self.config.tenant_domain == ""
365+
if not self.config.client_id or (
366+
not self.config.authority and not self.config.tenant_domain
367367
):
368368
self.config_params()
369369
headers = self.headers()

0 commit comments

Comments
 (0)