Beispiel #1
0
    def test_add_task_header(self, *_):
        keys_auth_2 = KeysAuth(
            os.path.join(self.path, "2"),
            'priv_key',
            'password',
        )

        ts = self.ts

        task_header = get_example_task_header(keys_auth_2.public_key)

        with self.assertRaises(Exception) as raised:
            ts.add_task_header(task_header)
            self.assertEqual(raised.exception.message, "Invalid signature")
            self.assertEqual(len(ts.get_others_tasks_headers()), 0)

        task_header["signature"] = keys_auth_2.sign(
            TaskHeader.dict_to_binary(task_header))

        self.assertTrue(ts.add_task_header(task_header))
        self.assertEqual(len(ts.get_others_tasks_headers()), 1)

        task_header = get_example_task_header(keys_auth_2.public_key)
        task_id2 = task_header["fixed_header"]["task_id"]
        task_header["signature"] = keys_auth_2.sign(
            TaskHeader.dict_to_binary(task_header))

        self.assertTrue(ts.add_task_header(task_header))
        self.assertEqual(len(ts.get_others_tasks_headers()), 2)

        self.assertTrue(ts.add_task_header(task_header))
        self.assertEqual(len(ts.get_others_tasks_headers()), 2)

        new_header = dict(task_header)
        new_header["fixed_header"]["task_owner"]["pub_port"] = 9999
        new_header["signature"] = keys_auth_2.sign(
            TaskHeader.dict_to_binary(new_header))

        # An attempt to update fixed header should *not* succeed
        self.assertFalse(ts.add_task_header(new_header))
        self.assertEqual(len(ts.get_others_tasks_headers()), 2)
        saved_task = next(th for th in ts.get_others_tasks_headers()
                          if th["fixed_header"]["task_id"] == task_id2)
        self.assertEqual(saved_task["signature"], task_header["signature"])
Beispiel #2
0
    def test_add_task_header(self):
        config = self._get_config_desc()
        keys_auth = EllipticalKeysAuth(self.path)
        keys_auth_2 = EllipticalKeysAuth(os.path.join(self.path, "2"))

        self.ts = ts = TaskServer(Node(), config, keys_auth, self.client,
                                  use_docker_machine_manager=False)

        task_header = get_example_task_header()
        task_header["task_id"] = "xyz"

        with self.assertRaises(Exception) as raised:
            ts.add_task_header(task_header)
            self.assertEqual(raised.exception.message, "Invalid signature")
            self.assertEqual(len(ts.get_tasks_headers()), 0)

        task_header["task_owner_key_id"] = keys_auth_2.key_id
        task_header["signature"] = keys_auth_2.sign(TaskHeader.dict_to_binary(task_header))

        self.assertIsNotNone(ts.add_task_header(task_header))
        self.assertEqual(len(ts.get_tasks_headers()), 1)

        task_header = get_example_task_header()
        task_header["task_id"] = "xyz_2"
        task_header["task_owner_key_id"] = keys_auth_2.key_id
        task_header["signature"] = keys_auth_2.sign(TaskHeader.dict_to_binary(task_header))

        self.assertIsNotNone(ts.add_task_header(task_header))
        self.assertEqual(len(ts.get_tasks_headers()), 2)

        self.assertIsNotNone(ts.add_task_header(task_header))
        self.assertEqual(len(ts.get_tasks_headers()), 2)

        new_header = dict(task_header)
        new_header["task_owner"]["pub_port"] = 9999
        new_header["signature"] = keys_auth_2.sign(TaskHeader.dict_to_binary(new_header))

        self.assertIsNotNone(ts.add_task_header(new_header))
        self.assertEqual(len(ts.get_tasks_headers()), 2)
        saved_task = next(th for th in ts.get_tasks_headers() if th["task_id"] == "xyz_2")
        self.assertEqual(saved_task["signature"], new_header["signature"])
Beispiel #3
0
 def verify_header_sig(self, th_dict_repr):
     _bin = TaskHeader.dict_to_binary(th_dict_repr)
     _sig = th_dict_repr["signature"]
     _key = th_dict_repr["task_owner_key_id"]
     return self.verify_sig(_sig, _bin, _key)