strongswan / strongTNC

BYOD TNC Database Management Tool
GNU Affero General Public License v3.0
21 stars 10 forks source link

tests/create_test_db.py fails with DoesNotExist: Device matching query does not exist. #17

Closed adelton closed 6 years ago

adelton commented 6 years ago

The https://github.com/strongswan/strongTNC#testing section suggests that

Setup a database with test data:

$ ./manage.py shell
>>> execfile('tests/create_test_db.py')

However, running that fails with

[...]
Added Ubuntu_12.04-i686-strongswan-ikev1-4.5.2-1.2
Added Ubuntu_12.04-i686-strongswan-ikev2-4.5.2-1.2
Added Ubuntu_12.04-i686-strongswan-starter-4.5.2-1.2
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "tests/create_test_db.py", line 42, in <module>
    nexus_prime = Device.objects.get(pk=2)
  File "/strongTNC-master/VIRTUAL/lib/python2.7/site-packages/django/db/models/manager.py", line 127, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/strongTNC-master/VIRTUAL/lib/python2.7/site-packages/django/db/models/query.py", line 334, in get
    self.model._meta.object_name
DoesNotExist: Device matching query does not exist.

The code in tests/create_test_db.py does

nexus_prime = Device.objects.get(pk=2)
lenovo_twist = Device.objects.get(pk=1)

but there does not seem to be any code which would create those device records, or the product records that devices need.

How is that part of tests supposed to work?

tobiasbrunner commented 6 years ago

That example data (not really test data, although the imported SWID tags are used in some tests) is not really needed anyway. Basically there was a database in the repo, already filled with some data, but that was removed with e89dbc1b43. So I guess since then the create_test_db.py script doesn't work anymore.

Not even filling the database with the data from the strongSwan repository (see https://github.com/strongswan/strongswan/blob/master/src/libimcv/imv/data.sql) would help as it does not create any devices, which the script tries to retrieve to associate sessions with SWID tags. So the script is basically useless and I've removed it with the associated commit.

adelton commented 6 years ago

That's unfortunate, I've found the script very useful, at least it populated the SWID Tag entries.

What is the recommended way of populating the database with some content? Wouldn't it make sense to prepare some bogus device / session entries so that people could see the application filled with data without setting strongSwan?

tobiasbrunner commented 6 years ago

You can use ./manage.py importswid <file> to import SWID tags from a file. Use swidGenerator to collect SWID tags on hosts if you don't want to use TNC to do that.

dbrgn commented 6 years ago

Maybe testdata could also be provided via Django fixtures: https://docs.djangoproject.com/en/2.0/howto/initial-data/

adelton commented 6 years ago

You can use ./manage.py importswid to import SWID tags from a file.

I've tried ./tests/test_tags/strongswan.full.swidtag but it seems to fail with

Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/strongTNC/VIRTUAL/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute_from_command_line
    utility.execute()
  File "/strongTNC/VIRTUAL/lib/python2.7/site-packages/django/core/management/__init__.py", line 346, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/strongTNC/VIRTUAL/lib/python2.7/site-packages/django/core/management/base.py", line 394, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/strongTNC/VIRTUAL/lib/python2.7/site-packages/django/core/management/base.py", line 445, in execute
    output = self.handle(*args, **options)
  File "/strongTNC/apps/swid/management/commands/importswid.py", line 58, in handle
    tag, replaced = utils.process_swid_tag(tag_xml, allow_tag_update=True)
  File "/strongTNC/VIRTUAL/lib/python2.7/site-packages/django/utils/decorators.py", line 145, in inner
    return func(*args, **kwargs)
  File "/strongTNC/apps/swid/utils.py", line 187, in process_swid_tag
    tag, files, entities = etree.fromstring(tag_xml.encode('utf8'), parser)
  File "src/lxml/lxml.etree.pyx", line 3213, in lxml.etree.fromstring (src/lxml/lxml.etree.c:77737)
  File "src/lxml/parser.pxi", line 1830, in lxml.etree._parseMemoryDocument (src/lxml/lxml.etree.c:116674)
  File "src/lxml/parser.pxi", line 1718, in lxml.etree._parseDoc (src/lxml/lxml.etree.c:115324)
  File "src/lxml/parser.pxi", line 1090, in lxml.etree._BaseParser._parseDoc (src/lxml/lxml.etree.c:109723)
  File "src/lxml/parsertarget.pxi", line 171, in lxml.etree._TargetParserContext._handleParseResultDoc (src/lxml/lxml.etree.c:129077)
  File "src/lxml/parsertarget.pxi", line 161, in lxml.etree._TargetParserContext._handleParseResultDoc (src/lxml/lxml.etree.c:128863)
  File "src/lxml/parser.pxi", line 624, in lxml.etree._raiseParseError (src/lxml/lxml.etree.c:104147)
lxml.etree.XMLSyntaxError: Start tag expected, '<' not found, line 1, column 39

It looks like the importswid does not like the XML declaration, and when I remove it, I also have to remove all formatting from the file -- basically importswid only seems to support single-line XML documents without XML declaration. Is that expected?

tobiasbrunner commented 6 years ago

Is that expected?

Yes. I think it's just meant to work with the output of swidGenerator (which puts each tag on one line, unless --pretty is used). The XML declaration shouldn't matter though (if it's on the same line).

strongX509 commented 6 years ago

Hi Jan,

yes this is the expected behaviour. Multiple SWID tags separated by a newline can be imported by the "importswid" command, therefore the tags themselves cannot contain any line separators.

Alternatively you can upload multiple SWID tags optionally formated in multi-line or pretty format using the "../api/swid/add-tags" REST-ful command as in

https://tnc.strongswan.org/api/swid/add-tags/

with either "application/x-www-form-urlencoded" encoding

data='tag-xml-1'&data='tag-xml-2'&data='tag-xml-3'

or you can use "application/json" encoding:

{"data": ["tag-xml-1", "tag-xml-2", "tag-xml-3"]}

Hope this helps.

Best regards

Andreas

On 31.05.2018 09:36, Jan Pazdziora wrote:

You can use ./manage.py importswid to import SWID tags from a file.

It looks like the |importswid| does not like the XML declaration, and when I remove it, I also have to remove all formatting from the file -- basically |importswid| only seems to support single-line XML documents without XML declaration. Is that expected?

====================================================================== Andreas Steffen andreas.steffen@strongswan.org strongSwan - the Open Source VPN Solution! www.strongswan.org Institute for Networked Solutions University of Applied Sciences Rapperswil CH-8640 Rapperswil (Switzerland) ===========================================================[INS-HSR]==