Skip to content

Commit 6bdad37

Browse files
committed
Fixes publishing HTTP and gRPC endpoints
Signed-off-by: Bartlomiej Gmerek <bartlomiej.gmerek@canonical.com>
1 parent 622ee52 commit 6bdad37

3 files changed

Lines changed: 75 additions & 7 deletions

File tree

backend/src/charm.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,7 @@ def _tls_config(self) -> Optional[TLSConfig]:
168168
@property
169169
def _http_api_endpoint(self):
170170
"""Internal (i.e. not ingressed) url."""
171-
# TODO: add support for HTTPS once https://github.com/canonical/litmus-operators/issues/23 is fixed
172-
return f"http://{get_app_hostname(self.app.name, self.model.name)}:{self.litmus_backend.http_port}"
171+
return f"{self._http_api_protocol}://{get_app_hostname(self.app.name, self.model.name)}:{self._http_api_port}"
173172

174173
@property
175174
def _certificate_request_attributes(self) -> CertificateRequestAttributes:
@@ -193,13 +192,32 @@ def _reconcile(self):
193192
self._auth.publish_endpoint(
194193
Endpoint(
195194
grpc_server_host=get_app_hostname(self.app.name, self.model.name),
196-
grpc_server_port=LitmusBackend.grpc_port,
197-
# TODO: check if TLS is enabled once https://github.com/canonical/litmus-operators/issues/23 is fixed
198-
insecure=True,
195+
grpc_server_port=self._grpc_port,
196+
insecure=False if self._tls_config else True,
199197
)
200198
)
201199
self._send_http_api.publish_endpoint(self._http_api_endpoint)
202200

201+
@property
202+
def _http_api_protocol(self):
203+
return "https" if self._tls_config else "http"
204+
205+
@property
206+
def _http_api_port(self):
207+
return (
208+
self.litmus_backend.https_port
209+
if self._tls_config
210+
else self.litmus_backend.http_port
211+
)
212+
213+
@property
214+
def _grpc_port(self):
215+
return (
216+
self.litmus_backend.grpc_tls_port
217+
if self._tls_config
218+
else self.litmus_backend.grpc_port
219+
)
220+
203221

204222
if __name__ == "__main__": # pragma: nocover
205223
from ops import main

backend/tests/unit/test_litmus_auth_integration.py

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ def test_get_auth_grpc_endpoint(
6666
),
6767
),
6868
)
69-
def test_publish_endpoint(ctx, auth_relation, backend_container, leader, expected):
69+
def test_publish_endpoint_without_tls(
70+
ctx, auth_relation, backend_container, leader, expected
71+
):
7072
# GIVEN an auth integration
7173
auth_relation = dataclasses.replace(auth_relation)
7274

@@ -84,3 +86,49 @@ def test_publish_endpoint(ctx, auth_relation, backend_container, leader, expecte
8486
# THEN the leader unit will publish it's grpc server endpoint
8587
relation_out = state_out.get_relation(auth_relation.id)
8688
assert relation_out.local_app_data == expected
89+
90+
91+
@pytest.mark.parametrize(
92+
"leader, expected",
93+
(
94+
(False, {}),
95+
(
96+
True,
97+
{
98+
"grpc_server_host": json.dumps(
99+
"litmus-backend-k8s.test.svc.cluster.local"
100+
),
101+
"grpc_server_port": json.dumps(8001),
102+
"insecure": json.dumps(False),
103+
"version": json.dumps(0),
104+
},
105+
),
106+
),
107+
)
108+
def test_publish_endpoint_with_tls(
109+
ctx,
110+
auth_relation,
111+
tls_certificates_relation,
112+
patch_cert_and_key,
113+
backend_container,
114+
leader,
115+
expected,
116+
):
117+
# GIVEN an auth integration
118+
auth_relation = dataclasses.replace(auth_relation)
119+
tls_certificates_relation = dataclasses.replace(tls_certificates_relation)
120+
121+
# WHEN a relation_changed event fires
122+
state_out = ctx.run(
123+
state=State(
124+
relations={auth_relation, tls_certificates_relation},
125+
containers={backend_container},
126+
leader=leader,
127+
model=Model(name="test"),
128+
),
129+
event=ctx.on.relation_changed(auth_relation),
130+
)
131+
132+
# THEN the leader unit will publish it's grpc server endpoint
133+
relation_out = state_out.get_relation(auth_relation.id)
134+
assert relation_out.local_app_data == expected

backend/tests/unit/test_tls_certificates_integration.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ def test_tls_certs_removed_from_disk_when_tls_certificates_relation_is_broken(
6262
f.write("CA certificate")
6363

6464
# WHEN a relation broken event is fired
65-
state_out = ctx.run(ctx.on.relation_broken(tls_certificates_relation), state=state)
65+
state_out = ctx.run(
66+
ctx.on.relation_broken(tls_certificates_relation), state=state
67+
)
6668

6769
# THEN TLS certs are removed from the workload container
6870
backend_container_out = state_out.get_container(backend_container.name)

0 commit comments

Comments
 (0)