diff --git a/src/middlewared/middlewared/plugins/crypto_/cert_info.py b/src/middlewared/middlewared/plugins/crypto_/cert_info.py index 64ca0aecaa6cb..a357145f2e8da 100644 --- a/src/middlewared/middlewared/plugins/crypto_/cert_info.py +++ b/src/middlewared/middlewared/plugins/crypto_/cert_info.py @@ -1,5 +1,6 @@ from middlewared.schema import accepts, Dict, Ref, returns, Str from middlewared.service import private, Service +from middlewared.utils.country_codes import get_country_codes from .utils import EC_CURVES, EKU_OIDS @@ -18,11 +19,9 @@ async def get_domain_names(self, cert_id): @accepts() @returns(Ref('country_choices')) - async def country_choices(self): - """ - Returns country choices for creating a certificate/csr. - """ - return await self.middleware.call('system.general.country_choices') + def country_choices(self): + """Returns country choices for creating a certificate/csr.""" + return get_country_codes() @accepts() @returns(Dict('acme_server_choices', additional_attrs=True)) diff --git a/src/middlewared/middlewared/plugins/system_general/country.py b/src/middlewared/middlewared/plugins/system_general/country.py index 6a6deb9ee9105..645adaea5f79d 100644 --- a/src/middlewared/middlewared/plugins/system_general/country.py +++ b/src/middlewared/middlewared/plugins/system_general/country.py @@ -1,58 +1,17 @@ -import csv - from middlewared.schema import accepts, Dict, returns -from middlewared.service import private, Service - +from middlewared.service import Service +from middlewared.utils.country_codes import get_country_codes class SystemGeneralService(Service): - COUNTRY_CHOICES = None - class Config: namespace = 'system.general' cli_namespace = 'system.general' @accepts() @returns(Dict('country_choices', additional_attrs=True, register=True)) - async def country_choices(self): - """ - Returns country choices. - """ - if not self.COUNTRY_CHOICES: - self.COUNTRY_CHOICES = await self.middleware.call('system.general.get_country_choices') - - return self.COUNTRY_CHOICES - - @private - def get_country_choices(self): - def _get_index(country_columns, column): - index = -1 - i = 0 - for c in country_columns: - if c.lower() == column.lower(): - index = i - break - - i += 1 - return index - - country_file = '/etc/iso_3166_2_countries.csv' - cni, two_li = None, None - country_choices = {} - with open(country_file, 'r', encoding='utf-8') as csvfile: - reader = csv.reader(csvfile) - - for index, row in enumerate(reader): - if index != 0: - if row[cni] and row[two_li]: - if row[two_li] in country_choices: - # If two countries in the iso file have the same key, we concatenate their names - country_choices[row[two_li]] += f' + {row[cni]}' - else: - country_choices[row[two_li]] = row[cni] - else: - # ONLY CNI AND TWO_LI ARE BEING CONSIDERED FROM THE CSV - cni = _get_index(row, 'Common Name') - two_li = _get_index(row, 'ISO 3166-1 2 Letter Code') - - return country_choices + def country_choices(self): + """Return a dictionary whose keys represent the + ISO 3166-1 alpha 2 country code and values represent + the English short name (used in ISO 3166/MA)""" + return get_country_codes() diff --git a/src/middlewared/middlewared/utils/country_codes.py b/src/middlewared/middlewared/utils/country_codes.py new file mode 100644 index 0000000000000..98c28a68a80ce --- /dev/null +++ b/src/middlewared/middlewared/utils/country_codes.py @@ -0,0 +1,13 @@ +from functools import cache +from json import load + +__all__ = ("get_country_codes",) + + +@cache +def get_country_codes() -> dict[str, str]: + """Return the ISO 3166-1 alpha 2 code as the key and the + English short name (used in ISO 3166/MA) of the country + as the value (i.e {"US": "United States of America", ...})""" + with open("/usr/share/iso-codes/json/iso_3166-1.json") as f: + return {i["alpha_2"]: i["name"] for i in load(f)["3166-1"]}