Skip to content

Commit 977808a

Browse files
author
Timothy MacDonald
authored
feat: add support for user supplied access token (#174)
Signed-off-by: Timothy MacDonald <[email protected]>
1 parent 2acc932 commit 977808a

File tree

4 files changed

+20
-7
lines changed

4 files changed

+20
-7
lines changed

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,20 @@ To generate API credentials, you'll need to do the following in Lacework:
7878
## Environment Variables
7979

8080
If you wish to configure the LaceworkClient instance using environment variables, this module honors the same
81-
variables used by the Lacework CLI. The `account`, `subaccount`, `api_key`, `api_secret`, and `profile` parameters
81+
variables used by the Lacework CLI. The `account`, `subaccount`, `api_key`, `api_secret`, `api_token`, and `profile` parameters
8282
can all be configured as specified below.
8383

8484
| Environment Variable | Description | Required |
8585
| -------------------- | -------------------------------------------------------------------- | :------: |
8686
| `LW_PROFILE` | Lacework CLI profile to use (configured at ~/.lacework.toml) | N |
8787
| `LW_ACCOUNT` | Lacework account/organization domain (i.e. `<account>`.lacework.net) | Y |
8888
| `LW_SUBACCOUNT` | Lacework sub-account | N |
89-
| `LW_API_KEY` | Lacework API Access Key | Y |
90-
| `LW_API_SECRET` | Lacework API Access Secret | Y |
89+
| `LW_API_KEY` | Lacework API Access Key | N |
90+
| `LW_API_SECRET` | Lacework API Access Secret | N |
91+
| `LW_API_TOKEN` | Lacework API Token (alternative to key and secret) | N |
92+
93+
NOTE: To authenticate with the Lacework API you must specify either a key and secret OR a token. If you specify both the
94+
token will be used.
9195

9296
## Installation
9397

laceworksdk/api/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
LACEWORK_SUBACCOUNT_ENVIRONMENT_VARIABLE,
5050
LACEWORK_API_KEY_ENVIRONMENT_VARIABLE,
5151
LACEWORK_API_SECRET_ENVIRONMENT_VARIABLE,
52+
LACEWORK_API_TOKEN_ENVIRONMENT_VARIABLE,
5253
LACEWORK_API_BASE_DOMAIN_ENVIRONMENT_VARIABLE,
5354
LACEWORK_API_CONFIG_SECTION_ENVIRONMENT_VARIABLE,
5455
LACEWORK_CLI_CONFIG_RELATIVE_PATH,
@@ -66,6 +67,7 @@ def __init__(
6667
subaccount=None,
6768
api_key=None,
6869
api_secret=None,
70+
api_token=None,
6971
instance=None,
7072
base_domain=None,
7173
profile=None,
@@ -86,6 +88,7 @@ def __init__(
8688
self._subaccount = subaccount or os.getenv(
8789
LACEWORK_SUBACCOUNT_ENVIRONMENT_VARIABLE
8890
)
91+
self._api_token = api_token or os.getenv(LACEWORK_API_TOKEN_ENVIRONMENT_VARIABLE)
8992
self._api_key = api_key or os.getenv(LACEWORK_API_KEY_ENVIRONMENT_VARIABLE)
9093
self._api_secret = api_secret or os.getenv(
9194
LACEWORK_API_SECRET_ENVIRONMENT_VARIABLE
@@ -135,6 +138,7 @@ def __init__(
135138
self._api_key,
136139
self._api_secret,
137140
self._base_domain,
141+
api_token=self._api_token
138142
)
139143

140144
# API Wrappers

laceworksdk/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
LACEWORK_SUBACCOUNT_ENVIRONMENT_VARIABLE = "LW_SUBACCOUNT"
1515
LACEWORK_API_KEY_ENVIRONMENT_VARIABLE = "LW_API_KEY"
1616
LACEWORK_API_SECRET_ENVIRONMENT_VARIABLE = "LW_API_SECRET"
17+
LACEWORK_API_TOKEN_ENVIRONMENT_VARIABLE = "LW_API_TOKEN"
1718
LACEWORK_API_BASE_DOMAIN_ENVIRONMENT_VARIABLE = "LW_BASE_DOMAIN"
1819
LACEWORK_API_CONFIG_SECTION_ENVIRONMENT_VARIABLE = "LW_PROFILE"
1920

laceworksdk/http_session.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class HttpSession:
3030
_access_token = None
3131
_access_token_expiry = None
3232

33-
def __init__(self, account, subaccount, api_key, api_secret, base_domain):
33+
def __init__(self, account, subaccount, api_key, api_secret, base_domain, api_token=None):
3434
"""
3535
Initializes the HttpSession object.
3636
@@ -40,6 +40,7 @@ def __init__(self, account, subaccount, api_key, api_secret, base_domain):
4040
api_key (str): a Lacework API Key
4141
api_secret (str): a Lacework API Secret
4242
base_domain (str): a Lacework Domain (defaults to "lacework.net")
43+
api_token (str): a Lacework API token (instead of key and secret)
4344
4445
Returns:
4546
HttpSession: An instance of this class
@@ -59,7 +60,7 @@ def __init__(self, account, subaccount, api_key, api_secret, base_domain):
5960
self._account = account
6061
self._subaccount = subaccount
6162
self._org_level_access = False
62-
63+
self._access_token = api_token
6364
# Get an access token
6465
self._check_access_token()
6566

@@ -99,8 +100,11 @@ def _check_access_token(self):
99100
"""
100101
A method to check the validity of the access token.
101102
"""
102-
103-
if self._access_token is None or self._access_token_expiry < datetime.now(
103+
if self._access_token and self._access_token_expiry is None:
104+
# This catches the case that the user has provided an access token instead of
105+
# key and secret. We cannot know the expiry date so we simply return
106+
return
107+
elif self._access_token is None or self._access_token_expiry < datetime.now(
104108
timezone.utc
105109
):
106110
response = self._get_access_token()

0 commit comments

Comments
 (0)