Skip to content

Commit

Permalink
Add proxy environment variables (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
amandahla authored Oct 19, 2023
1 parent 3916bf3 commit 8dd68d4
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 4 deletions.
37 changes: 33 additions & 4 deletions src-docs/charm_state.py.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Exception raised when a charm configuration is found to be invalid.

Attrs: msg (str): Explanation of the error.

<a href="../src/charm_state.py#L45"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../src/charm_state.py#L48"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

### <kbd>function</kbd> `__init__`

Expand Down Expand Up @@ -49,13 +49,25 @@ State of the Charm.
- <b>`synapse_config`</b>: synapse configuration.
- <b>`datasource`</b>: datasource information.
- <b>`saml_config`</b>: saml configuration.
- <b>`proxy`</b>: proxy information.


---

#### <kbd>property</kbd> proxy

Get charm proxy information from juju charm environment.



**Returns:**
charm proxy information in the form of ProxyConfig.



---

<a href="../src/charm_state.py#L139"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../src/charm_state.py#L173"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

### <kbd>classmethod</kbd> `from_charm`

Expand Down Expand Up @@ -86,6 +98,23 @@ Return: The CharmState instance created by the provided charm.
- <b>`CharmConfigInvalidError`</b>: if the charm configuration is invalid.


---

## <kbd>class</kbd> `ProxyConfig`
Configuration for accessing Synapse through proxy.



**Attributes:**

- <b>`http_proxy`</b>: The http proxy URL.
- <b>`https_proxy`</b>: The https proxy URL.
- <b>`no_proxy`</b>: Comma separated list of hostnames to bypass proxy.





---

## <kbd>class</kbd> `SynapseConfig`
Expand All @@ -98,7 +127,7 @@ Attrs: server_name: server_name config. report_stats: report_stats config. pu

---

<a href="../src/charm_state.py#L90"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../src/charm_state.py#L107"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

### <kbd>classmethod</kbd> `set_default_smtp_notif_from`

Expand All @@ -125,7 +154,7 @@ Set server_name as default value to smtp_notif_from.

---

<a href="../src/charm_state.py#L109"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../src/charm_state.py#L126"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

### <kbd>classmethod</kbd> `to_yes_or_no`

Expand Down
34 changes: 34 additions & 0 deletions src/charm_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@
"""State of the Charm."""
import dataclasses
import itertools
import os
import typing

import ops

# pydantic is causing this no-name-in-module problem
from pydantic import ( # pylint: disable=no-name-in-module,import-error
AnyHttpUrl,
BaseModel,
Extra,
Field,
ValidationError,
parse_obj_as,
validator,
)

Expand Down Expand Up @@ -51,6 +54,20 @@ def __init__(self, msg: str):
self.msg = msg


class ProxyConfig(BaseModel): # pylint: disable=too-few-public-methods
"""Configuration for accessing Synapse through proxy.
Attributes:
http_proxy: The http proxy URL.
https_proxy: The https proxy URL.
no_proxy: Comma separated list of hostnames to bypass proxy.
"""

http_proxy: typing.Optional[AnyHttpUrl]
https_proxy: typing.Optional[AnyHttpUrl]
no_proxy: typing.Optional[str]


class SynapseConfig(BaseModel): # pylint: disable=too-few-public-methods
"""Represent Synapse builtin configuration values.
Expand Down Expand Up @@ -130,12 +147,29 @@ class CharmState:
synapse_config: synapse configuration.
datasource: datasource information.
saml_config: saml configuration.
proxy: proxy information.
"""

synapse_config: SynapseConfig
datasource: typing.Optional[DatasourcePostgreSQL]
saml_config: typing.Optional[SAMLConfiguration]

@property
def proxy(self) -> "ProxyConfig":
"""Get charm proxy information from juju charm environment.
Returns:
charm proxy information in the form of ProxyConfig.
"""
http_proxy = os.environ.get("JUJU_CHARM_HTTP_PROXY")
https_proxy = os.environ.get("JUJU_CHARM_HTTPS_PROXY")
no_proxy = os.environ.get("JUJU_CHARM_NO_PROXY")
return ProxyConfig(
http_proxy=parse_obj_as(AnyHttpUrl, http_proxy) if http_proxy else None,
https_proxy=parse_obj_as(AnyHttpUrl, https_proxy) if https_proxy else None,
no_proxy=no_proxy,
)

@classmethod
def from_charm(
cls,
Expand Down
5 changes: 5 additions & 0 deletions src/synapse/workload.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,4 +517,9 @@ def get_environment(charm_state: CharmState) -> typing.Dict[str, str]:
environment["POSTGRES_PORT"] = datasource["port"]
environment["POSTGRES_USER"] = datasource["user"]
environment["POSTGRES_PASSWORD"] = datasource["password"]
for proxy_variable in ("http_proxy", "https_proxy", "no_proxy"):
proxy_value = getattr(charm_state.proxy, proxy_variable)
if proxy_value:
environment[proxy_variable] = str(proxy_value)
environment[proxy_variable.upper()] = str(proxy_value)
return environment

0 comments on commit 8dd68d4

Please sign in to comment.