mirror of
https://gitea.osmocom.org/sim-card/pysim.git
synced 2026-05-08 02:15:07 +03:00
card_key_provider: pass CardKeyFieldCryptor to constructor
We currently create the CardKeyFieldCryptor object inside the constructor of the concrete CardKeyProvider classes. There is currently no problem with that, but when we create the CardKeyFieldCryptor object first and then pass it as parameter to the constructor, we gain more flexibility in case we want to support other CardKeyFieldCryptor variants in the future. Related: SYS#6959
This commit is contained in:
@@ -173,18 +173,18 @@ class CardKeyProvider(abc.ABC):
|
|||||||
class CardKeyProviderCsv(CardKeyProvider):
|
class CardKeyProviderCsv(CardKeyProvider):
|
||||||
"""Card key provider implementation that allows to query against a specified CSV file."""
|
"""Card key provider implementation that allows to query against a specified CSV file."""
|
||||||
|
|
||||||
def __init__(self, csv_filename: str, transport_keys: dict):
|
def __init__(self, csv_filename: str, field_cryptor: CardKeyFieldCryptor):
|
||||||
"""
|
"""
|
||||||
Args:
|
Args:
|
||||||
csv_filename : file name (path) of CSV file containing card-individual key/data
|
csv_filename : file name (path) of CSV file containing card-individual key/data
|
||||||
transport_keys : (see class CardKeyFieldCryptor)
|
field_cryptor : (see class CardKeyFieldCryptor)
|
||||||
"""
|
"""
|
||||||
log.info("Using CSV file as card key data source: %s" % csv_filename)
|
log.info("Using CSV file as card key data source: %s" % csv_filename)
|
||||||
self.csv_file = open(csv_filename, 'r')
|
self.csv_file = open(csv_filename, 'r')
|
||||||
if not self.csv_file:
|
if not self.csv_file:
|
||||||
raise RuntimeError("Could not open CSV file '%s'" % csv_filename)
|
raise RuntimeError("Could not open CSV file '%s'" % csv_filename)
|
||||||
self.csv_filename = csv_filename
|
self.csv_filename = csv_filename
|
||||||
self.crypt = CardKeyFieldCryptor(transport_keys)
|
self.crypt = field_cryptor
|
||||||
|
|
||||||
def get(self, fields: List[str], key: str, value: str) -> Dict[str, str]:
|
def get(self, fields: List[str], key: str, value: str) -> Dict[str, str]:
|
||||||
self.csv_file.seek(0)
|
self.csv_file.seek(0)
|
||||||
@@ -216,11 +216,11 @@ class CardKeyProviderCsv(CardKeyProvider):
|
|||||||
class CardKeyProviderPgsql(CardKeyProvider):
|
class CardKeyProviderPgsql(CardKeyProvider):
|
||||||
"""Card key provider implementation that allows to query against a specified PostgreSQL database table."""
|
"""Card key provider implementation that allows to query against a specified PostgreSQL database table."""
|
||||||
|
|
||||||
def __init__(self, config_filename: str, transport_keys: dict):
|
def __init__(self, config_filename: str, field_cryptor: CardKeyFieldCryptor):
|
||||||
"""
|
"""
|
||||||
Args:
|
Args:
|
||||||
config_filename : file name (path) of CSV file containing card-individual key/data
|
config_filename : file name (path) of CSV file containing card-individual key/data
|
||||||
transport_keys : (see class CardKeyFieldCryptor)
|
field_cryptor : (see class CardKeyFieldCryptor)
|
||||||
"""
|
"""
|
||||||
import psycopg2
|
import psycopg2
|
||||||
log.info("Using SQL database as card key data source: %s" % config_filename)
|
log.info("Using SQL database as card key data source: %s" % config_filename)
|
||||||
@@ -237,7 +237,7 @@ class CardKeyProviderPgsql(CardKeyProvider):
|
|||||||
host=config.get('host'))
|
host=config.get('host'))
|
||||||
self.tables = config.get('table_names')
|
self.tables = config.get('table_names')
|
||||||
log.info("Card key database tables: %s" % str(self.tables))
|
log.info("Card key database tables: %s" % str(self.tables))
|
||||||
self.crypt = CardKeyFieldCryptor(transport_keys)
|
self.crypt = field_cryptor
|
||||||
|
|
||||||
def get(self, fields: List[str], key: str, value: str) -> Dict[str, str]:
|
def get(self, fields: List[str], key: str, value: str) -> Dict[str, str]:
|
||||||
import psycopg2
|
import psycopg2
|
||||||
@@ -347,7 +347,8 @@ def init_card_key_provider(opts: argparse.Namespace):
|
|||||||
for par in opts.column_key:
|
for par in opts.column_key:
|
||||||
name, key = par.split(':')
|
name, key = par.split(':')
|
||||||
column_keys[name] = key
|
column_keys[name] = key
|
||||||
|
card_key_field_cryptor = CardKeyFieldCryptor(transport_keys)
|
||||||
if os.path.isfile(os.path.expanduser(opts.csv)):
|
if os.path.isfile(os.path.expanduser(opts.csv)):
|
||||||
card_key_provider_register(CardKeyProviderCsv(os.path.expanduser(opts.csv), column_keys))
|
card_key_provider_register(CardKeyProviderCsv(os.path.expanduser(opts.csv), card_key_field_cryptor))
|
||||||
if os.path.isfile(os.path.expanduser(opts.pgsql)):
|
if os.path.isfile(os.path.expanduser(opts.pgsql)):
|
||||||
card_key_provider_register(CardKeyProviderPgsql(os.path.expanduser(opts.pgsql), column_keys))
|
card_key_provider_register(CardKeyProviderPgsql(os.path.expanduser(opts.pgsql), card_key_field_cryptor))
|
||||||
|
|||||||
@@ -20,7 +20,8 @@ class TestCardKeyProviderCsv(unittest.TestCase):
|
|||||||
"KIK3" : "00010204040506070809488B0C0D0E0F"}
|
"KIK3" : "00010204040506070809488B0C0D0E0F"}
|
||||||
|
|
||||||
csv_file_path = os.path.dirname(os.path.abspath(__file__)) + "/test_card_key_provider.csv"
|
csv_file_path = os.path.dirname(os.path.abspath(__file__)) + "/test_card_key_provider.csv"
|
||||||
card_key_provider_register(CardKeyProviderCsv(csv_file_path, column_keys))
|
card_key_field_cryptor = CardKeyFieldCryptor(column_keys)
|
||||||
|
card_key_provider_register(CardKeyProviderCsv(csv_file_path, card_key_field_cryptor))
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
def test_card_key_provider_get(self):
|
def test_card_key_provider_get(self):
|
||||||
|
|||||||
Reference in New Issue
Block a user