Skip to content

Commit

Permalink
Enable getting Stomp authentication credentials from the environment
Browse files Browse the repository at this point in the history
  • Loading branch information
DiamondJoseph committed Feb 21, 2024
1 parent 598d789 commit d58dc34
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/blueapi/config.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import os
from enum import Enum
from pathlib import Path
from typing import Any, Dict, Generic, Literal, Mapping, Optional, Type, TypeVar, Union

import yaml
from pydantic import BaseModel, Field, ValidationError, parse_obj_as
from pydantic import BaseModel, Field, ValidationError, parse_obj_as, validator

from blueapi.utils import BlueapiBaseModel, InvalidConfigError

Expand All @@ -23,12 +24,21 @@ class Source(BaseModel):

class BasicAuthentication(BaseModel):
"""
Log in details for when a server uses authentication
Log in details for when a server uses authentication.
If username or passcode match exactly the regex ^\\${(.*)}$
they attempt to replace with an environment variable of the same.
i.e. ${foo} or ${FOO} are replaced with the value of FOO
"""

username: str = "guest"
passcode: str = "guest"

@validator("username", "passcode")
def get_from_env(cls, v: str):
if v.startswith("${") and v.endswith("}"):
return os.environ[v.removeprefix("${").removesuffix("}").upper()]
return v

Check warning on line 40 in src/blueapi/config.py

View check run for this annotation

Codecov / codecov/patch

src/blueapi/config.py#L38-L40

Added lines #L38 - L40 were not covered by tests


class StompConfig(BaseModel):
"""
Expand Down

0 comments on commit d58dc34

Please sign in to comment.