Skip to content

Commit 66d5d74

Browse files
authored
Sync libs (#884)
1 parent d165940 commit 66d5d74

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

lib/charms/postgresql_k8s/v0/postgresql.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
# Increment this PATCH version before using `charmcraft publish-lib` or reset
3737
# to 0 if you are raising the major API version
38-
LIBPATCH = 53
38+
LIBPATCH = 52
3939

4040
# Groups to distinguish HBA access
4141
ACCESS_GROUP_IDENTITY = "identity_access"
@@ -700,15 +700,22 @@ def list_valid_privileges_and_roles(self) -> Tuple[Set[str], Set[str]]:
700700
"superuser",
701701
}, {role[0] for role in cursor.fetchall() if role[0]}
702702

703-
def set_up_database(self) -> None:
703+
def set_up_database(self, temp_location: Optional[str] = None) -> None:
704704
"""Set up postgres database with the right permissions."""
705705
connection = None
706+
cursor = None
706707
try:
707-
with self._connect_to_database() as connection, connection.cursor() as cursor:
708-
cursor.execute("SELECT TRUE FROM pg_roles WHERE rolname='admin';")
709-
if cursor.fetchone() is not None:
710-
return
708+
connection = self._connect_to_database()
709+
cursor = connection.cursor()
711710

711+
if temp_location is not None:
712+
cursor.execute("SELECT TRUE FROM pg_tablespace WHERE spcname='temp';")
713+
if cursor.fetchone() is None:
714+
cursor.execute(f"CREATE TABLESPACE temp LOCATION '{temp_location}';")
715+
cursor.execute("GRANT CREATE ON TABLESPACE temp TO public;")
716+
717+
cursor.execute("SELECT TRUE FROM pg_roles WHERE rolname='admin';")
718+
if cursor.fetchone() is None:
712719
# Allow access to the postgres database only to the system users.
713720
cursor.execute("REVOKE ALL PRIVILEGES ON DATABASE postgres FROM PUBLIC;")
714721
cursor.execute("REVOKE CREATE ON SCHEMA public FROM PUBLIC;")
@@ -727,6 +734,8 @@ def set_up_database(self) -> None:
727734
logger.error(f"Failed to set up databases: {e}")
728735
raise PostgreSQLDatabasesSetupError() from e
729736
finally:
737+
if cursor is not None:
738+
cursor.close()
730739
if connection is not None:
731740
connection.close()
732741

lib/charms/postgresql_k8s/v0/postgresql_tls.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import logging
2424
import re
2525
import socket
26-
from typing import List, Optional
26+
from typing import Iterator, List, Optional
2727

2828
from charms.certificate_transfer_interface.v0.certificate_transfer import (
2929
CertificateAvailableEvent as CertificateAddedEvent,
@@ -55,7 +55,7 @@
5555

5656
# Increment this PATCH version before using `charmcraft publish-lib` or reset
5757
# to 0 if you are raising the major API version.
58-
LIBPATCH = 14
58+
LIBPATCH = 15
5959

6060
logger = logging.getLogger(__name__)
6161
SCOPE = "unit"
@@ -269,6 +269,17 @@ def is_ip_address(address: str) -> bool:
269269
"sans_dns": sans_dns,
270270
}
271271

272+
def get_ca_secret_names(self) -> Iterator[str]:
273+
"""Get a secret-name for each relation fulfilling the CA transfer interface.
274+
275+
Returns:
276+
Secret name for a CA transfer fulfilled interface.
277+
"""
278+
relations = self.charm.model.relations.get(TLS_TRANSFER_RELATION, [])
279+
280+
for relation in relations:
281+
yield f"ca-{relation.app.name}"
282+
272283
def get_tls_files(self) -> (Optional[str], Optional[str], Optional[str]):
273284
"""Prepare TLS files in special PostgreSQL way.
274285

0 commit comments

Comments
 (0)