1
0
mirror of https://gitlab.com/fdroid/fdroidserver.git synced 2024-10-02 09:10:11 +02:00

Merge branch 'smartcardoptions-parsing' into 'master'

Fix parsing of smartcardoptions config

See merge request fdroid/fdroidserver!1106
This commit is contained in:
Hans-Christoph Steiner 2022-05-26 15:06:01 +00:00
commit bc81237d0c
2 changed files with 96 additions and 1 deletions

View File

@ -385,7 +385,8 @@ def read_config(opts=None):
# smartcardoptions must be a list since its command line args for Popen
smartcardoptions = config.get('smartcardoptions')
if isinstance(smartcardoptions, str):
config['smartcardoptions'] = re.sub(r'\s+', r' ', config['smartcardoptions']).split(' ')
options = re.sub(r'\s+', r' ', config['smartcardoptions']).split(' ')
config['smartcardoptions'] = [i.strip() for i in options if i]
elif not smartcardoptions and 'keystore' in config and config['keystore'] == 'NONE':
# keystore='NONE' means use smartcard, these are required defaults
config['smartcardoptions'] = ['-storetype', 'PKCS11', '-providerName',

View File

@ -54,6 +54,8 @@ class CommonTest(unittest.TestCase):
os.makedirs(self.tmpdir)
os.chdir(self.basedir)
fdroidserver.common.config = None
fdroidserver.common.options = mock.Mock()
fdroidserver.common.options.verbose = False
self.path = os.environ['PATH']
self.android_home = os.environ.get('ANDROID_HOME')
@ -2438,6 +2440,98 @@ class CommonTest(unittest.TestCase):
self.assertTrue(os.path.exists(f), f + ' was created')
self.assertFalse(is_repo_file(f), f + ' not repo file')
def test_get_smartcardoptions_list(self):
os.chdir(self.tmpdir)
with open('config.yml', 'w') as fp:
fp.write(
textwrap.dedent(
"""
smartcardoptions:
- -storetype
- PKCS11
- -providerName
- SunPKCS11-OpenSC
- -providerClass
- sun.security.pkcs11.SunPKCS11
- -providerArg
- opensc-fdroid.cfg
"""
)
)
config = fdroidserver.common.read_config()
fdroidserver.common.config = config
self.assertTrue(isinstance(config['smartcardoptions'], list))
self.assertEqual(
[
'-storetype',
'PKCS11',
'-providerName',
'SunPKCS11-OpenSC',
'-providerClass',
'sun.security.pkcs11.SunPKCS11',
'-providerArg',
'opensc-fdroid.cfg',
],
config['smartcardoptions'],
)
def test_get_smartcardoptions_spaces(self):
os.chdir(self.tmpdir)
with open('config.yml', 'w') as fp:
fp.write(
textwrap.dedent(
"""smartcardoptions: |
-storetype PKCS11
-providerClass sun.security.pkcs11.SunPKCS11
-providerArg /etc/pkcs11_java.cfg
"""
)
)
config = fdroidserver.common.read_config()
fdroidserver.common.config = config
self.assertTrue(isinstance(config['smartcardoptions'], list))
self.assertEqual(
[
'-storetype',
'PKCS11',
'-providerClass',
'sun.security.pkcs11.SunPKCS11',
'-providerArg',
'/etc/pkcs11_java.cfg',
],
config['smartcardoptions'],
)
def test_get_smartcardoptions_config_py(self):
os.chdir(self.tmpdir)
with open('config.py', 'w') as fp:
fp.write(
textwrap.dedent(
"""
smartcardoptions = '''
\t-storetype\tPKCS11
\t-providerClass\tsun.security.pkcs11.SunPKCS11
\t-providerArg\t/etc/pkcs11_java.cfg
'''
"""
)
)
config = fdroidserver.common.read_config()
fdroidserver.common.config = config
self.assertEqual(
[
'-storetype',
'PKCS11',
'-providerClass',
'sun.security.pkcs11.SunPKCS11',
'-providerArg',
'/etc/pkcs11_java.cfg',
],
config['smartcardoptions'],
)
if __name__ == "__main__":
os.chdir(os.path.dirname(__file__))