Repository: google/timesketch

Issue:
Commit Sketch object to DB before setting ACL (in tsctl AddSearchIndex)
https://github.com/google/timesketch/blob/8caad1a4d75909004db4fefd87a0e21fde6c007a/timesketch/tsctl.py#L181

A similar bug has been already documented:
https://github.com/google/timesketch/issues/886
And fixed:
https://github.com/google/timesketch/pull/897
Issue importing timeline via tsctl import
Hello, in attempting to import a timeline generated from psort.py via the timesketch command line tool `tsctl`, I'm running into a NoneType error as shown in the traceback below. I tried this with the .plaso file format as well. I confirmed that the file does have readable data in it. Note that I am using the docker version of timesketch.
```
tsctl import -f <timeline>.jsonl -u <username>
Traceback (most recent call last):
  File "/usr/lib/python3.6/encodings/utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
TypeError: a bytes-like object is required, not 'NoneType'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/bin/tsctl", line 11, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.6/dist-packages/timesketch/tsctl.py", line 474, in main
    shell_manager.run()
  File "/usr/local/lib/python3.6/dist-packages/flask_script/__init__.py", line 417, in run
    result = self.handle(argv[0], argv[1:])
  File "/usr/local/lib/python3.6/dist-packages/flask_script/__init__.py", line 386, in handle
    res = handle(*args, **config)
  File "/usr/local/lib/python3.6/dist-packages/flask_script/commands.py", line 216, in __call__
    return self.run(*args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/timesketch/tsctl.py", line 389, in run
    timeline_name = codecs.decode(timeline_name, 'utf-8')
TypeError: <timesketch.tsctl.ImportTimeline object at 0x7f2648558080>: decoding with 'utf-8' codec failed (TypeError: a bytes-like object is required, not 'NoneType')
```



Discussion:

I'll take a look

Resolving patch:
diff --git a/timesketch/tsctl.py b/timesketch/tsctl.py
--- a/timesketch/tsctl.py
+++ b/timesketch/tsctl.py
@@ -178,9 +178,9 @@ def run(self, name, index, username):
             sys.exit(1)
         searchindex = SearchIndex(
             name=name, description=name, user=user, index_name=index)
-        searchindex.grant_permission('read')
         db_session.add(searchindex)
         db_session.commit()
+        searchindex.grant_permission('read')
         sys.stdout.write('Search index {0:s} created\n'.format(name))
 
 
@@ -385,6 +385,9 @@ def run(self, file_path, sketch_id, username, timeline_name):
                 pass
 
         if not timeline_name:
+            if timeline_name is None:
+                timeline_name = '{0:s}_timeline'.format(filename)
+
             if not isinstance(timeline_name, six.text_type):
                 timeline_name = codecs.decode(timeline_name, 'utf-8')
 
@@ -441,7 +444,7 @@ def run(self, file_path, sketch_id, username, timeline_name):
 
         # Start Celery pipeline for indexing and analysis.
         # Import here to avoid circular imports.
-        from timesketch.lib import tasks
+        from timesketch.lib import tasks  # pylint: disable=import-outside-toplevel
         pipeline = tasks.build_index_pipeline(
             file_path, timeline_name, index_name, extension, sketch.id)
         pipeline.apply_async(task_id=index_name)


Test patch: