-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
263 lines (215 loc) · 9.63 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
from __future__ import annotations
from importlib.metadata import version
from pathlib import Path
from typing import Optional
from environs import Env, EnvError
class Config:
"""Application configuration."""
def __init__(self) -> None:
"""
Create Config instance and load options from possible dotenv file.
To support application tests which may need to manipulate options, such as the application database, variables
loaded from a possible `.env` file, or that are set as environment variables directly, will be overriden by any
variables set in a possible `.test.env` file.
"""
self.env = Env()
self.env.read_env()
self.env.read_env(".test.env", override=True)
def validate(self) -> None:
"""
Validate required configuration options have valid values.
`AUTH_*` options are not validated as they are not required to use the core functionality of this application.
They will be validated at runtime if performing auth related functions.
"""
try:
self.env.str("APP_ODS_DB_DSN")
except EnvError as e:
msg = "Required config option `DB_DSN` not set."
raise RuntimeError(msg) from e
try:
self.env.list("APP_ODS_DATA_MANAGED_TABLE_NAMES")
except EnvError as e:
msg = "Required config option `DATA_MANAGED_TABLE_NAMES` not set."
raise RuntimeError(msg) from e
try:
outputs_path: Path = self.env.path("APP_ODS_DATA_AIRNET_OUTPUT_PATH")
if not outputs_path.is_dir():
msg = (
f"`DATA_AIRNET_OUTPUT_PATH` config value: '{outputs_path.resolve()}' not a directory "
f"or does not exist."
)
raise RuntimeError(msg)
except EnvError as e:
msg = "Required config option `DATA_AIRNET_OUTPUT_PATH` not set."
raise RuntimeError(msg) from e
try:
backups_count: int = self.env.int("APP_ODS_BACKUPS_COUNT")
if backups_count < 1:
msg = f"`BACKUPS_COUNT` config value: '{backups_count}' must be greater than 0."
raise RuntimeError(msg)
except EnvError as e:
msg = "Required config option `BACKUPS_COUNT` not set."
raise RuntimeError(msg) from e
try:
backups_path: Path = self.env.path("APP_ODS_BACKUPS_PATH")
if not backups_path.is_dir():
msg = f"`BACKUPS_PATH` config value: '{backups_path.resolve()}' not a directory or does not exist."
raise RuntimeError(msg)
except EnvError as e:
msg = "Required config option `BACKUPS_PATH` not set."
raise RuntimeError(msg) from e
def dump(self) -> dict:
"""Return application configuration as a dictionary."""
return {
"AUTH_AZURE_AUTHORITY": self.AUTH_AZURE_AUTHORITY,
"AUTH_AZURE_CLIENT_ID": self.AUTH_AZURE_CLIENT_ID,
"AUTH_AZURE_CLIENT_SECRET": self.AUTH_AZURE_CLIENT_SECRET,
"AUTH_AZURE_SCOPES": self.AUTH_AZURE_SCOPES,
"AUTH_LDAP_BASE_DN": self.AUTH_LDAP_BASE_DN,
"AUTH_LDAP_BIND_DN": self.AUTH_LDAP_BIND_DN,
"AUTH_LDAP_BIND_PASSWORD": self.AUTH_LDAP_BIND_PASSWORD,
"AUTH_LDAP_NAME_CONTEXT_GROUPS": self.AUTH_LDAP_NAME_CONTEXT_GROUPS,
"AUTH_LDAP_NAME_CONTEXT_USERS": self.AUTH_LDAP_NAME_CONTEXT_USERS,
"AUTH_LDAP_OU_GROUPS": self.AUTH_LDAP_OU_GROUPS,
"AUTH_LDAP_OU_USERS": self.AUTH_LDAP_OU_USERS,
"AUTH_LDAP_URL": self.AUTH_LDAP_URL,
"AUTH_MS_GRAPH_ENDPOINT": self.AUTH_MS_GRAPH_ENDPOINT,
"BACKUPS_COUNT": self.BACKUPS_COUNT,
"BACKUPS_PATH": self.BACKUPS_PATH,
"DATA_AIRNET_OUTPUT_PATH": self.DATA_AIRNET_OUTPUT_PATH,
"DATA_AIRNET_ROUTES_TABLE": self.DATA_AIRNET_ROUTES_TABLE,
"DATA_AIRNET_ROUTE_WAYPOINTS_TABLE": self.DATA_AIRNET_ROUTE_WAYPOINTS_TABLE,
"DATA_AIRNET_WAYPOINTS_TABLE": self.DATA_AIRNET_WAYPOINTS_TABLE,
"DATA_MANAGED_SCHEMA_NAME": self.DATA_MANAGED_SCHEMA_NAME,
"DATA_MANAGED_TABLE_NAMES": self.DATA_MANAGED_TABLE_NAMES,
"DATA_QGIS_TABLE_NAMES": self.DATA_QGIS_TABLE_NAMES,
"DB_DSN": self.DB_DSN,
"VERSION": self.VERSION,
}
@property
def AUTH_AZURE_AUTHORITY(self) -> Optional[str]:
"""
Azure Entra authority URL.
Typically defined as an Azure tenancy specific URL such in the form:
`https://login.microsoftonline.com/{TENANT_ID}`.
"""
return self.env.str("APP_ODS_AUTH_AZURE_AUTHORITY", default=None)
@property
def AUTH_AZURE_CLIENT_ID(self) -> Optional[str]:
"""
Azure Entra client ID.
As defined by the relevant Application Registration in the Azure Portal.
"""
return self.env.str("APP_ODS_AUTH_AZURE_CLIENT_ID", default=None)
@property
def AUTH_AZURE_CLIENT_SECRET(self) -> Optional[str]:
"""
Azure Entra client secret.
As defined by the relevant Application Registration in the Azure Portal.
"""
return self.env.str("APP_ODS_AUTH_AZURE_CLIENT_SECRET", default=None)
@property
def AUTH_AZURE_SCOPES(self) -> list[str]:
"""
Azure Entra scopes.
Scopes required to access relevant resources. The special `/.default` scope is used to access pre-configured
permissions within the MS Graph API.
Source: https://learn.microsoft.com/en-us/graph/auth-v2-service?tabs=http#4-request-an-access-token
"""
return ["https://graph.microsoft.com/.default"]
@property
def AUTH_LDAP_BASE_DN(self) -> Optional[str]:
"""Distinguished Name (DN) used as common base/root for all LDAP queries."""
return self.env.str("APP_ODS_AUTH_LDAP_BASE_DN", default=None)
@property
def AUTH_LDAP_BIND_DN(self) -> Optional[str]:
"""Distinguished Name (DN) used for LDAP client binding."""
return self.env.str("APP_ODS_AUTH_LDAP_BIND_DN", default=None)
@property
def AUTH_LDAP_BIND_PASSWORD(self) -> Optional[str]:
"""Password used for LDAP client binding."""
return self.env.str("APP_ODS_AUTH_LDAP_BIND_PASSWORD", default=None)
@property
def AUTH_LDAP_NAME_CONTEXT_GROUPS(self) -> str:
"""
LDAP naming context prefix used to identify groups.
Typically, `cn`.
"""
return self.env.str("APP_ODS_AUTH_LDAP_CXT_GROUPS", default=None)
@property
def AUTH_LDAP_NAME_CONTEXT_USERS(self) -> str:
"""
LDAP naming context prefix used to identify users (individuals).
Typically, either `cn` or `uid`.
"""
return self.env.str("APP_ODS_AUTH_LDAP_CXT_USERS", default=None)
@property
def AUTH_LDAP_OU_GROUPS(self) -> Optional[str]:
"""Organisational Unit (OU) containing groups."""
return self.env.str("APP_ODS_AUTH_LDAP_OU_GROUPS", default=None)
@property
def AUTH_LDAP_OU_USERS(self) -> Optional[str]:
"""Organisational Unit (OU) containing users (individuals)."""
return self.env.str("APP_ODS_AUTH_LDAP_OU_USERS", default=None)
@property
def AUTH_LDAP_URL(self) -> Optional[str]:
"""LDAP server URL."""
return self.env.str("APP_ODS_AUTH_LDAP_URL", default=None)
@property
def AUTH_MS_GRAPH_ENDPOINT(self) -> str:
"""
Base endpoint for the Microsoft Graph API.
Source: https://learn.microsoft.com/en-us/graph/api/overview?view=graph-rest-1.0#call-the-v10-endpoint
"""
return "https://graph.microsoft.com/v1.0"
@property
def BACKUPS_COUNT(self) -> int:
"""
Number of backup iterations to keep.
Applies to per backup series - i.e. if set to `3`, 3 DB and 3 controlled datasets backups will be kept.
"""
return self.env.int("APP_ODS_BACKUPS_COUNT")
@property
def BACKUPS_PATH(self) -> Path:
"""Where to store backups."""
return self.env.path("APP_ODS_BACKUPS_PATH")
@property
def DATA_AIRNET_OUTPUT_PATH(self) -> Path:
"""Where to store outputs from the Air Unit Network utility."""
return self.env.path("APP_ODS_DATA_AIRNET_OUTPUT_PATH")
@property
def DATA_AIRNET_ROUTES_TABLE(self) -> str:
"""Name of table used for Air Unit Network routes."""
return "route_container"
@property
def DATA_AIRNET_ROUTE_WAYPOINTS_TABLE(self) -> str:
"""Name of table used for Air Unit Network route waypoints."""
return "route_waypoint"
@property
def DATA_AIRNET_WAYPOINTS_TABLE(self) -> str:
"""Name of table used for Air Unit Network waypoints."""
return "waypoint"
@property
def DATA_MANAGED_SCHEMA_NAME(self) -> str:
"""Name of schema used for controlled datasets."""
return "controlled"
@property
def DATA_MANAGED_TABLE_NAMES(self) -> list[str]:
"""Names of tables used for controlled datasets."""
return self.env.list("APP_ODS_DATA_MANAGED_TABLE_NAMES")
@property
def DATA_QGIS_TABLE_NAMES(self) -> list[str]:
"""Names of tables used for optional QGIS features."""
return ["layer_styles"]
@property
def DB_DSN(self) -> str:
"""
DB connection string.
Must be defined as a Postgres connection URI.
Source: https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING-URIS
"""
return self.env.str("APP_ODS_DB_DSN")
@property
def VERSION(self) -> str:
"""Application version."""
return version("ops-data-store")