datalake-scripts 2.7.2__py3-none-any.whl → 2.7.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -18,7 +18,13 @@ class TokenManager:
18
18
  """
19
19
 
20
20
  def __init__(
21
- self, endpoint_config: dict, *, environment: str, username=None, password=None
21
+ self,
22
+ endpoint_config: dict,
23
+ *,
24
+ environment: str,
25
+ username=None,
26
+ password=None,
27
+ longterm_token=None,
22
28
  ):
23
29
  """environment can be either prod or preprod"""
24
30
  base_url = urljoin(
@@ -33,32 +39,59 @@ class TokenManager:
33
39
 
34
40
  self.username = username
35
41
  self.password = password
42
+ self.longterm_token = longterm_token
36
43
  self.access_token = None
37
44
  self.refresh_token = None
38
45
  self.get_token()
39
46
 
40
47
  def get_token(self):
41
48
  """
42
- Generate token from user input, with email and password
49
+ Generate token from user input
50
+ by default, we use the values given as args,
51
+ then in second choice, we try the environment variables,
52
+ and finally we ask for user input
53
+ Priority is given to the long term token if it is provided at the same place (args, environment variable or prompt) than the username/password )
43
54
  """
44
- self.username = (
45
- self.username or os.getenv("OCD_DTL_USERNAME") or input("Email: ")
46
- )
47
- self.password = self.password or os.getenv("OCD_DTL_PASSWORD") or getpass()
48
- print()
49
- data = {"email": self.username, "password": self.password}
55
+ if self.username is None and self.password is None:
56
+ """We do not ignore the values username and password in favor of long term token if the two credentials are given explicitly when instancing Datalake"""
57
+ self.longterm_token = self.longterm_token or os.getenv(
58
+ "OCD_DTL_LONGTERM_TOKEN"
59
+ )
50
60
 
51
- response = requests.post(url=self.url_token, json=data)
52
- json_response = json.loads(response.text)
53
- try:
54
- self.access_token = f'Token {json_response["access_token"]}'
55
- self.refresh_token = f'Token {json_response["refresh_token"]}'
56
- except KeyError:
57
- logger.error(
58
- f"An error occurred while retrieving an access token, for URL: {self.url_token}\n"
59
- f"response of the API: {response.text}"
61
+ self.username = self.username or os.getenv("OCD_DTL_USERNAME")
62
+ self.password = self.password or os.getenv("OCD_DTL_PASSWORD")
63
+
64
+ if self.longterm_token is None and (
65
+ self.username is None or self.password is None
66
+ ):
67
+ """I only ask in prompt if no other choice"""
68
+ self.username = self.username or input("Email: ")
69
+ self.password = self.password or getpass()
70
+ self.longterm_token = self.longterm_token or input(
71
+ "Long term token (Default : None): "
60
72
  )
61
- raise ValueError(f"Could not login: {response.text}")
73
+ print()
74
+
75
+ if self.longterm_token:
76
+ self.longterm_token = f"Token {self.longterm_token}"
77
+ if self.username or self.password:
78
+ logger.warning(
79
+ f"Using provided Long Term Token for Authentication to the Datalake API. Ignoring username and/or password."
80
+ )
81
+ else:
82
+ data = {"email": self.username, "password": self.password}
83
+
84
+ response = requests.post(url=self.url_token, json=data)
85
+ json_response = json.loads(response.text)
86
+ try:
87
+ self.access_token = f'Token {json_response["access_token"]}'
88
+ self.refresh_token = f'Token {json_response["refresh_token"]}'
89
+ except KeyError:
90
+ logger.error(
91
+ f"An error occurred while retrieving an access token, for URL: {self.url_token}\n"
92
+ f"response of the API: {response.text}"
93
+ )
94
+ raise ValueError(f"Could not login: {response.text}")
62
95
 
63
96
  def fetch_new_token(self):
64
97
  logger.debug("Token will be refreshed")
@@ -94,6 +127,21 @@ class TokenManager:
94
127
  ):
95
128
  self.get_token()
96
129
  elif error_msg == "Token has expired":
97
- self.fetch_new_token()
130
+ if self.longterm_token:
131
+ raise ValueError(f"Long term token has expired")
132
+ else:
133
+ self.fetch_new_token()
134
+ elif error_msg == "Token has been revoked":
135
+ raise ValueError(f"Long term token has been revoked")
136
+ elif error_msg == "Fresh token required":
137
+ raise ValueError(
138
+ f"You cannot use Long term token with this endpoint, please use only the credentials username and password to init the Datalake instance for this request"
139
+ )
140
+ elif (
141
+ error_msg
142
+ in ("Invalid token", "Not enough segments", "Signature verification failed")
143
+ and self.longterm_token
144
+ ):
145
+ raise ValueError(f"Long term token is invalid")
98
146
  else:
99
147
  raise ValueError(f"Unexpected msg: {error_msg}")
datalake/datalake.py CHANGED
@@ -19,13 +19,18 @@ class Datalake:
19
19
 
20
20
  Usage:
21
21
  >>> dtl = Datalake(username='some username', password='some password')
22
+ or
23
+ >>> dtl = Datalake(longterm_token='some longterm token')
24
+ then
22
25
  >>> dtl.Threats.lookup(atom_value='mayoclinic.org', atom_type=AtomType.DOMAIN, hashkey_only=False)
26
+
23
27
  """
24
28
 
25
29
  def __init__(
26
30
  self,
27
31
  username: str = None,
28
32
  password: str = None,
33
+ longterm_token: str = None,
29
34
  env="prod",
30
35
  log_level=logging.WARNING,
31
36
  ):
@@ -33,7 +38,11 @@ class Datalake:
33
38
  endpoint_config = Config().load_config()
34
39
  try:
35
40
  token_manager = TokenManager(
36
- endpoint_config, environment=env, username=username, password=password
41
+ endpoint_config,
42
+ environment=env,
43
+ username=username,
44
+ password=password,
45
+ longterm_token=longterm_token,
37
46
  )
38
47
  except Exception as e:
39
48
  if "Failed to resolve" in str(e) or "Failed to establish" in str(e):
@@ -65,7 +65,9 @@ class Endpoint:
65
65
  tries_left = self.SET_MAX_RETRY
66
66
 
67
67
  while tries_left > 0:
68
- headers["Authorization"] = self.token_manager.access_token
68
+ headers["Authorization"] = (
69
+ self.token_manager.access_token or self.token_manager.longterm_token
70
+ )
69
71
  logger.debug(self._pretty_debug_request(url, method, post_body, headers))
70
72
 
71
73
  response = self._send_request(
@@ -77,7 +79,7 @@ class Endpoint:
77
79
  logger.debug("API response:\n%s", response.text)
78
80
  if response.status_code == 401:
79
81
  logger.warning(
80
- "Token expired or Missing authorization header. Updating token"
82
+ "Missing authorization header or Token Error. Updating token"
81
83
  )
82
84
  self.token_manager.process_auth_error(response.json())
83
85
  elif response.status_code == 422:
@@ -148,6 +150,7 @@ class Endpoint:
148
150
  + f" - data: \n{data}\n"
149
151
  + f" - token: \n{self.token_manager.access_token}\n"
150
152
  + f" - refresh_token: \n{self.token_manager.refresh_token}\n"
153
+ + f" - longterm_token: \n{self.token_manager.longterm_token}\n"
151
154
  + "-" * self.terminal_size
152
155
  )
153
156
  return debug
@@ -73,7 +73,18 @@ class FilteredTagSubcategory(Endpoint):
73
73
  )
74
74
  if limit_match:
75
75
  print("The 'limit' parameter must be >= 0 and <= 5000.")
76
- if not tag_category_match and not ordering_match and not limit_match:
76
+ token_match = re.search(
77
+ r"token",
78
+ error_message,
79
+ )
80
+ if token_match:
81
+ raise ve
82
+ if (
83
+ not tag_category_match
84
+ and not ordering_match
85
+ and not limit_match
86
+ and not token_match
87
+ ):
77
88
  print(
78
89
  "An error occurred, but no specific pattern was matched in the 422 HTTP error message."
79
90
  )
datalake_scripts/cli.py CHANGED
@@ -20,7 +20,7 @@ from datalake_scripts.scripts import (
20
20
 
21
21
  class Cli:
22
22
  CLI_NAME = "ocd-dtl"
23
- VERSION = "2.7.2"
23
+ VERSION = "2.7.3"
24
24
 
25
25
  def __init__(self):
26
26
  parser = argparse.ArgumentParser(
@@ -50,7 +50,8 @@ class BaseEngine:
50
50
  """
51
51
  try:
52
52
  response = self.endpoint.datalake_requests(url, method, headers, post_body)
53
- except ValueError:
53
+ except ValueError as ve:
54
+ logger.error(f"Error while attempting to request datalake : {ve}")
54
55
  logger.error("Request failed: Will return nothing for this request")
55
56
  return {}
56
57
  if (
@@ -47,7 +47,10 @@ class HandleBulkTaskMixin(BaseEngine):
47
47
 
48
48
  json_response = None
49
49
  while not json_response:
50
- headers = {"Authorization": self.token_manager.access_token}
50
+ headers = {
51
+ "Authorization": self.token_manager.access_token
52
+ or self.token_manager.longterm_token
53
+ }
51
54
  response = requests.get(
52
55
  url=retrieve_bulk_result_url,
53
56
  headers=headers,
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: datalake-scripts
3
- Version: 2.7.2
3
+ Version: 2.7.3
4
4
  Summary: A collection of scripts to easily use the API of OCD Datalake
5
5
  Home-page: https://github.com/cert-orangecyberdefense/datalake/
6
6
  Author: OCD
@@ -60,10 +60,20 @@ You can also use a script directly by using the following command: `<script_name
60
60
  ## Environment variables
61
61
 
62
62
  #### Authentication
63
- In case you don't want to enter credential for each commands and you are on a secured terminal, set those variables:
63
+
64
+ There are two methods of authentication:
65
+ - The first one is the use of the username and password. Every request to the API, will then use fresh tokens periodically created with these credentials.
66
+ - The second one is the use of a long term token. You can create long term token through the GUI, it can have more restricted permissions than your account. You can create several long term tokens for one account.
67
+
68
+ In case you don't want to enter credentials for each commands and you are on a secured terminal, set those variables:
69
+ * `OCD_DTL_LONGTERM_TOKEN` a long term token associated to your Datalake account.
70
+ Please note that if this variable is set, then the long term token will be used for every request to the Datalake API, even if you set the username and passsword environment variables below. This is important because some endpoints / requests do not accept long term tokens but need fresh tokens (ie a Datalake instance with username and password). Check for the need of fresh tokens in each endpoint description [here](https://datalake.cert.orangecyberdefense.com/api/v2/docs/)
71
+
72
+ or
73
+
64
74
  * `OCD_DTL_USERNAME` email address used to login on Datalake API/GUI.
65
75
  * `OCD_DTL_PASSWORD` password used to login on Datalake API/GUI.
66
- > They are independent and one can be used without the other if you wish.
76
+ > These last two are independent and one can be used without the other if you wish.
67
77
 
68
78
  #### Throttling
69
79
  For throttling the request, those two environment variable can be used:
@@ -1,5 +1,5 @@
1
1
  datalake/__init__.py,sha256=A2Cmrxun_uvpfjPC_9kwwAFKTpUI5uBcln2-ortksMY,699
2
- datalake/datalake.py,sha256=fQHPRkgw1qp6GzhpHUJqblps9jVoreKx0w7AcD3pn8g,3868
2
+ datalake/datalake.py,sha256=fxlti1_8otlvy4AG5BMd7E_9njGDyf5jbKf7wlKo5o0,4078
3
3
  datalake/api_objects/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
4
  datalake/api_objects/bulk_search_task.py,sha256=2H1EKFNR6ttf5yJWW0F8kpHgWjA-Pawpu2i2Z3xP-kg,4627
5
5
  datalake/common/__init__.py,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
@@ -9,26 +9,26 @@ datalake/common/config.py,sha256=b9PAfwL2qhJzGBZXaUTe1HtH_WZole1xnbawFq9LbyI,476
9
9
  datalake/common/logger.py,sha256=vC03L_VZ6kN5YHGCRJX2_iSNlTNqx010bFH90Cyxf9A,460
10
10
  datalake/common/output.py,sha256=sU6wZLznokKjMvtNNcGq5BRn9nDKtE_3N2RrFM8NDqg,1537
11
11
  datalake/common/throttler.py,sha256=O9h1DsGiloatsnOrwJdZIZEFCGL4pPqF6dafHmMFvYA,1882
12
- datalake/common/token_manager.py,sha256=dHlxyKBai2xKsB7Ii2JPKp7e_FIksgZaubO4seWC8o8,3656
12
+ datalake/common/token_manager.py,sha256=q-0qCNl5Bw0QIQdAJcm1ml0RWQlLwuc3xdKp16lijdY,5906
13
13
  datalake/common/utils.py,sha256=b2wUo6t9H-l9ctCpthKpS9CgOMvWiSJGOci3t6-M330,3504
14
14
  datalake/common/warn.py,sha256=8-YFJHlH3q91vp4TDyifmG05a5uvlVjHzcztbOiVP44,271
15
15
  datalake/config/endpoints.json,sha256=D2XUhnOEoZsIUWhlqOnMTFl9QJdpI0OnTksHTi27BzQ,1426
16
16
  datalake/endpoints/__init__.py,sha256=4GqELe9-njbc5BikldIX9RLDvVqQ96RqKRW9ykUsAL0,49
17
17
  datalake/endpoints/advanced_search.py,sha256=zS4iSOkUsZES59NvlrPsmmy9mF2w2pi8It1NBcWkH8g,2538
18
18
  datalake/endpoints/bulk_search.py,sha256=KGHd3zSM-A_2oXCm6HO81xJ4xVS9eZfCJNCrxD445ms,2570
19
- datalake/endpoints/endpoint.py,sha256=KKwUQ_vtly62YykOH5jLzFJhhqUf_0weIP5EGbz44Us,5850
20
- datalake/endpoints/filtered_tag_subcategory.py,sha256=9ZX5tiltt0ojX0djdvTojqR-PahJwyx3mO-HFJ2ajnU,2835
19
+ datalake/endpoints/endpoint.py,sha256=8UodFEdERlFS9ilBqENzw0foi2JVu4eH_B2wYfo3Fvk,5993
20
+ datalake/endpoints/filtered_tag_subcategory.py,sha256=8ERU3PtSrNHM5A72gXGe6S6iAdHWnCKDT2ByU4oAuuI,3096
21
21
  datalake/endpoints/sightings.py,sha256=bXl_hEm8hrvyCG8vg1944jCnCgOfl_lVe7vvaDe1Bw8,7801
22
22
  datalake/endpoints/tags.py,sha256=zYKBjaNTuVOwUmdzDC2Og0IAvZSUirXTMVlJCPNQMoY,1027
23
23
  datalake/endpoints/threats.py,sha256=ZfDJA7SX4pJglpK42oWJdRriRkzVYPoqvN8RRX4pG2g,20320
24
24
  datalake/miscellaneous/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
25
25
  datalake/miscellaneous/search_watch.py,sha256=078kLpkDPW1fx9u1ANKg_haFSvUxjsFz9T3P-_nqFtI,6897
26
26
  datalake_scripts/__init__.py,sha256=EKgNnE5TNzttTOC4vPKID6gWB4S2Zj-qpKxI8ZA8mCw,147
27
- datalake_scripts/cli.py,sha256=7FAa2u5br7jmDCBqK3GFylSvhKiDor82pgGOroM1ztQ,2702
27
+ datalake_scripts/cli.py,sha256=0w7aWknip2R6ZAiR9s60nwDOoY_zriQdZGazUsWtqyg,2702
28
28
  datalake_scripts/common/__init__.py,sha256=CeFL01aRaUnr4v36sE_CKoTQ9rDRAGYZBOMLsoKKsLc,803
29
- datalake_scripts/common/base_engine.py,sha256=3B7AxkIk0N5vTYkICznlCInMb6SVEXMpiFI3axfGya0,4236
29
+ datalake_scripts/common/base_engine.py,sha256=wUEKIr1zvLWWHrCc4UNtUO78OEaNq-NodzowdfyNa2o,4321
30
30
  datalake_scripts/common/base_script.py,sha256=M6yRiUWwkuwj6iS9Bcg16D--tGA_UgAQCwUp_yqkUAo,2520
31
- datalake_scripts/common/mixins.py,sha256=sUF6hXNTc9kynH35rntV0cGVJ1fva1M1Bf2_6SdWOM4,3018
31
+ datalake_scripts/common/mixins.py,sha256=3Srf2TOA2FXxziqWNs4fhlSbEjsiU7mwjGwhYcoy3P8,3101
32
32
  datalake_scripts/engines/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
33
33
  datalake_scripts/engines/get_engine.py,sha256=CAkU1phNuFUYKdNob_keI2Qjwh_-fgVFhoLaD2J6vkc,3816
34
34
  datalake_scripts/engines/post_engine.py,sha256=H9FkwbWcW98S0tBlQ9ddojurB5muPKM6oBA9s6DLq_c,6136
@@ -52,13 +52,13 @@ datalake_scripts/scripts/search_watch.py,sha256=8CM79GsL5TBXwPB2pb7QMw8HeoVdivUi
52
52
  tests/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
53
53
  tests/conftest.py,sha256=MqEvz9qs2NtMjKnBj6IUJjMx34pJwKbCgRtzaJIpyxw,240
54
54
  tests/common/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
55
- tests/common/fixture.py,sha256=1oOq71ZEZ7_JLu7NLDqPl2uhENuGtJVkTPg3QckWO_Y,1672
55
+ tests/common/fixture.py,sha256=BfmI6BtPP-q6ki3JAEVuXSalNG6JcBVRXm9SdmpwJ70,1798
56
56
  tests/common/test_output.py,sha256=9_Aud8dxy2iLEUAK8u7IsyBWMjnUXDYzy5AbRHtNT8Y,888
57
57
  tests/common/test_utils.py,sha256=Fyz2r5ufgu6FyH79qCTKHsLc4AjICGvRsXhm4n83YPE,1740
58
58
  tests/lib/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
59
59
  tests/lib/test_advanced_search.py,sha256=dPsPeKf2fEayBgeD07TWX-PpimURbCCd4gjrCQOsaQk,4493
60
60
  tests/lib/test_atom_type.py,sha256=4XgCJAJEDuzyzxYdzqO4vL00Fv26HH-0rc7ZIiVuZXk,4418
61
- tests/lib/test_auth_via_tokens.py,sha256=gQffONnmY0v3oh7jF7nA4boDemXvhcW4sMs_jFIxLj4,5937
61
+ tests/lib/test_auth_via_tokens.py,sha256=3moKCwKbMN3awjJSjO0PJCAukY3tYG54SCmuAcvfVGU,11545
62
62
  tests/lib/test_bulk_search.py,sha256=Rh1P4X9L4K6jL4o1HHB1cQycorsRzxEAFWyz8lLn7OY,14237
63
63
  tests/lib/test_filtered_tag_subcategory.py,sha256=7cry-lf2e6KdR_bY8bIyRG8Gyt_CpcecEjDDnXwo94k,7132
64
64
  tests/lib/test_search_watch.py,sha256=dPmW5Wmruw7NejZlKA-l8YVZYc211BGz_UnB3inyzM0,10402
@@ -71,8 +71,8 @@ tests/scripts/test_base_engine.py,sha256=vk-qqVHXwo9riQUQccoMhebw3GQSMD456rsB_FQ
71
71
  tests/scripts/test_cli.py,sha256=X0zS6z1begL4304IpQ0AK861FuY_RFM9B4RoUZkUGYI,1652
72
72
  tests/scripts/test_csv_builder.py,sha256=QEhbzH4w_epcFXYieZ2rTij7TGGMYRLTR-7LbiwSA60,7997
73
73
  tests/scripts/test_endpoint_config.py,sha256=gqE12mfuh_qGx6e-bTArcWxhhg4MUQEIYNZyrcy3m8g,987
74
- datalake_scripts-2.7.2.dist-info/METADATA,sha256=b2pMXUzcKDy6HOD7SCzLRvEj3O7YM8qV8HQSshRqrqs,3286
75
- datalake_scripts-2.7.2.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
76
- datalake_scripts-2.7.2.dist-info/entry_points.txt,sha256=DlfjQgUDIGnK_Ef_35515ACzOw1Vpdx9cI3sI0pMEL0,471
77
- datalake_scripts-2.7.2.dist-info/top_level.txt,sha256=uU48AOj64UXu2kR5yxRNQrTU133Il-sI6FrwEYBjxkk,32
78
- datalake_scripts-2.7.2.dist-info/RECORD,,
74
+ datalake_scripts-2.7.3.dist-info/METADATA,sha256=v9o7jv8xOHJK1Ei1ok4is9sqRe7vOu2NT8h41aaOeX4,4276
75
+ datalake_scripts-2.7.3.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
76
+ datalake_scripts-2.7.3.dist-info/entry_points.txt,sha256=DlfjQgUDIGnK_Ef_35515ACzOw1Vpdx9cI3sI0pMEL0,471
77
+ datalake_scripts-2.7.3.dist-info/top_level.txt,sha256=uU48AOj64UXu2kR5yxRNQrTU133Il-sI6FrwEYBjxkk,32
78
+ datalake_scripts-2.7.3.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: bdist_wheel (0.42.0)
2
+ Generator: bdist_wheel (0.43.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
tests/common/fixture.py CHANGED
@@ -49,3 +49,9 @@ def datalake():
49
49
 
50
50
  responses.add(responses.POST, url, json=auth_response, status=200)
51
51
  return Datalake(username="username", password="password")
52
+
53
+
54
+ @pytest.fixture
55
+ @responses.activate
56
+ def datalake_longterm_token():
57
+ return Datalake(longterm_token="longterm_token1234")
@@ -6,18 +6,23 @@ import pytest
6
6
  import responses
7
7
 
8
8
  from datalake import Datalake, AtomType
9
- from tests.common.fixture import datalake # noqa needed fixture import
9
+ from tests.common.fixture import (
10
+ datalake,
11
+ datalake_longterm_token,
12
+ ) # noqa needed fixture import
10
13
 
11
14
 
12
15
  @responses.activate
13
16
  def test_token_auth(datalake):
14
17
  expected_tokens = {
18
+ "longterm_token": None,
15
19
  "access_token": "12345",
16
20
  "refresh_token": "123456",
17
21
  }
18
22
 
19
23
  token_manager = datalake.Threats.token_manager
20
24
 
25
+ assert token_manager.longterm_token == None
21
26
  assert token_manager.access_token == f"Token {expected_tokens['access_token']}"
22
27
  assert token_manager.refresh_token == f"Token {expected_tokens['refresh_token']}"
23
28
 
@@ -93,7 +98,7 @@ def test_access_token_expired(datalake, caplog):
93
98
  == expected_json
94
99
  )
95
100
  assert caplog.messages == [
96
- "Token expired or Missing authorization header. Updating token"
101
+ "Missing authorization header or Token Error. Updating token"
97
102
  ]
98
103
 
99
104
 
@@ -138,7 +143,7 @@ def test_refresh_token_expired(datalake, caplog):
138
143
  == expected_json
139
144
  )
140
145
  assert caplog.messages == [
141
- "Token expired or Missing authorization header. Updating token",
146
+ "Missing authorization header or Token Error. Updating token",
142
147
  "Refreshing the refresh token",
143
148
  ]
144
149
 
@@ -177,5 +182,151 @@ def test_invalid_token(datalake, caplog):
177
182
  == expected_json
178
183
  )
179
184
  assert caplog.messages == [
180
- "Token expired or Missing authorization header. Updating token"
185
+ "Missing authorization header or Token Error. Updating token"
186
+ ]
187
+
188
+
189
+ def test_active_longterm_token(datalake_longterm_token):
190
+ token_manager = datalake_longterm_token.Threats.token_manager
191
+ expected_tokens = {
192
+ "longterm_token": "longterm_token1234",
193
+ "access_token": None,
194
+ "refresh_token": None,
195
+ }
196
+
197
+ assert token_manager.longterm_token == f"Token {expected_tokens['longterm_token']}"
198
+ assert token_manager.access_token == None
199
+ assert token_manager.refresh_token == None
200
+
201
+
202
+ def lookup_callback_longterm_token(
203
+ request,
204
+ expired_longterm_token,
205
+ disabled_longterm_token,
206
+ not_fresh_token,
207
+ response_on_expired_longterm_token={"msg": "Token has expired"},
208
+ response_on_disabled_longterm_token={"msg": "Token has been revoked"},
209
+ response_on_not_fresh_token={"msg": "Fresh token required"},
210
+ response_on_invalid_token={"msg": "Invalid token"},
211
+ ):
212
+ headers = {}
213
+ if request.headers["Authorization"] == f"Token {expired_longterm_token}":
214
+ return 401, headers, json.dumps(response_on_expired_longterm_token)
215
+ elif request.headers["Authorization"] == f"Token {disabled_longterm_token}":
216
+ return 401, headers, json.dumps(response_on_disabled_longterm_token)
217
+ elif request.headers["Authorization"] == f"Token {not_fresh_token}":
218
+ return 401, headers, json.dumps(response_on_not_fresh_token)
219
+ else:
220
+ return 401, headers, json.dumps(response_on_invalid_token)
221
+
222
+
223
+ @responses.activate
224
+ def test_disabled_longterm_token(datalake_longterm_token, caplog):
225
+ caplog.set_level(level=logging.INFO, logger="OCD_DTL")
226
+
227
+ responses.add_callback(
228
+ responses.GET,
229
+ "https://datalake.cert.orangecyberdefense.com/api/v2/mrti/threats/lookup/",
230
+ callback=partial(
231
+ lookup_callback_longterm_token,
232
+ expired_longterm_token="expired_longterm_token_1234",
233
+ disabled_longterm_token="longterm_token1234",
234
+ not_fresh_token="not_fresh_token_1234",
235
+ ),
236
+ content_type="application/json",
237
+ )
238
+
239
+ with caplog.at_level(logging.WARNING):
240
+ with pytest.raises(ValueError) as ve:
241
+ r = datalake_longterm_token.Threats.lookup(
242
+ atom_value="wow.com", atom_type=AtomType.DOMAIN
243
+ )
244
+ assert str(ve.value) == "Long term token has been revoked"
245
+ assert caplog.messages == [
246
+ "Missing authorization header or Token Error. Updating token"
247
+ ]
248
+
249
+
250
+ @responses.activate
251
+ def test_expired_longterm_token(datalake_longterm_token, caplog):
252
+ caplog.set_level(level=logging.INFO, logger="OCD_DTL")
253
+
254
+ responses.add_callback(
255
+ responses.GET,
256
+ "https://datalake.cert.orangecyberdefense.com/api/v2/mrti/threats/lookup/",
257
+ callback=partial(
258
+ lookup_callback_longterm_token,
259
+ expired_longterm_token="longterm_token1234",
260
+ disabled_longterm_token="disabled_token1234",
261
+ not_fresh_token="not_fresh_token_1234",
262
+ ),
263
+ content_type="application/json",
264
+ )
265
+
266
+ with caplog.at_level(logging.WARNING):
267
+ with pytest.raises(ValueError) as ve:
268
+ r = datalake_longterm_token.Threats.lookup(
269
+ atom_value="wow.com", atom_type=AtomType.DOMAIN
270
+ )
271
+ assert str(ve.value) == "Long term token has expired"
272
+ assert caplog.messages == [
273
+ "Missing authorization header or Token Error. Updating token"
274
+ ]
275
+
276
+
277
+ # test long test token on endpoint requiring fresh token
278
+ @responses.activate
279
+ def test_requires_fresh_token(datalake_longterm_token, caplog):
280
+ caplog.set_level(level=logging.INFO, logger="OCD_DTL")
281
+
282
+ responses.add_callback(
283
+ responses.GET,
284
+ "https://datalake.cert.orangecyberdefense.com/api/v2/mrti/threats/lookup/",
285
+ callback=partial(
286
+ lookup_callback_longterm_token,
287
+ expired_longterm_token="expired_longterm_token_1234",
288
+ disabled_longterm_token="disabled_token1234",
289
+ not_fresh_token="longterm_token1234",
290
+ ),
291
+ content_type="application/json",
292
+ )
293
+
294
+ with caplog.at_level(logging.WARNING):
295
+ with pytest.raises(ValueError) as ve:
296
+ r = datalake_longterm_token.Threats.lookup(
297
+ atom_value="wow.com", atom_type=AtomType.DOMAIN
298
+ )
299
+ assert (
300
+ str(ve.value)
301
+ == "You cannot use Long term token with this endpoint, please use only the credentials username and password to init the Datalake instance for this request"
302
+ )
303
+ assert caplog.messages == [
304
+ "Missing authorization header or Token Error. Updating token"
305
+ ]
306
+
307
+
308
+ @responses.activate
309
+ def test_invalid_longterm_token(datalake_longterm_token, caplog):
310
+ caplog.set_level(level=logging.INFO, logger="OCD_DTL")
311
+
312
+ responses.add_callback(
313
+ responses.GET,
314
+ "https://datalake.cert.orangecyberdefense.com/api/v2/mrti/threats/lookup/",
315
+ callback=partial(
316
+ lookup_callback_longterm_token,
317
+ expired_longterm_token="expired_longterm_token_1234",
318
+ disabled_longterm_token="disabled_token1234",
319
+ not_fresh_token="not_fresh_token_1234",
320
+ ),
321
+ content_type="application/json",
322
+ )
323
+
324
+ with caplog.at_level(logging.WARNING):
325
+ with pytest.raises(ValueError) as ve:
326
+ r = datalake_longterm_token.Threats.lookup(
327
+ atom_value="wow.com", atom_type=AtomType.DOMAIN
328
+ )
329
+ assert str(ve.value) == "Long term token is invalid"
330
+ assert caplog.messages == [
331
+ "Missing authorization header or Token Error. Updating token"
181
332
  ]