Skip to content

Commit 4e3a227

Browse files
Use JWTs with sync-cli (#434)
* Use JWTs with sync-cli * Remove unused constant * Adding a warning message if both were provided * Updated message to explain what's actually going to happen
1 parent cf608d5 commit 4e3a227

File tree

4 files changed

+42
-8
lines changed

4 files changed

+42
-8
lines changed

datadog_sync/commands/shared/options.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ def handle_parse_result(self, ctx: Context, opts: Dict[Any, Any], args: List[Any
4242
help="Datadog source organization APP key.",
4343
cls=CustomOptionClass,
4444
),
45+
option(
46+
"--source-jwt",
47+
envvar=constants.DD_SOURCE_JWT,
48+
required=False,
49+
help="Datadog source organization JWT (takes precedence over API key).",
50+
cls=CustomOptionClass,
51+
),
4552
option(
4653
"--source-api-url",
4754
envvar=constants.DD_SOURCE_API_URL,
@@ -68,6 +75,13 @@ def handle_parse_result(self, ctx: Context, opts: Dict[Any, Any], args: List[Any
6875
help="Datadog destination organization APP key.",
6976
cls=CustomOptionClass,
7077
),
78+
option(
79+
"--destination-jwt",
80+
envvar=constants.DD_DESTINATION_JWT,
81+
required=False,
82+
help="Datadog destination organization JWT (takes precedence over API key).",
83+
cls=CustomOptionClass,
84+
),
7185
option(
7286
"--destination-api-url",
7387
envvar=constants.DD_DESTINATION_API_URL,

datadog_sync/constants.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
DD_SOURCE_API_URL = "DD_SOURCE_API_URL"
1010
DD_SOURCE_API_KEY = "DD_SOURCE_API_KEY"
1111
DD_SOURCE_APP_KEY = "DD_SOURCE_APP_KEY"
12+
DD_SOURCE_JWT = "DD_SOURCE_JWT"
1213
DD_DESTINATION_API_URL = "DD_DESTINATION_API_URL"
1314
DD_DESTINATION_API_KEY = "DD_DESTINATION_API_KEY"
1415
DD_DESTINATION_APP_KEY = "DD_DESTINATION_APP_KEY"
16+
DD_DESTINATION_JWT = "DD_DESTINATION_JWT"
1517
DD_HTTP_CLIENT_RETRY_TIMEOUT = "DD_HTTP_CLIENT_RETRY_TIMEOUT"
1618
DD_HTTP_CLIENT_TIMEOUT = "DD_HTTP_CLIENT_TIMEOUT"
1719
DD_RESOURCES = "DD_RESOURCES"

datadog_sync/utils/configuration.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,18 +144,30 @@ def build_config(cmd: Command, **kwargs: Optional[Any]) -> Configuration:
144144
send_metrics = kwargs.get("send_metrics")
145145
verify_ssl = kwargs.get("verify_ssl_certificates", True)
146146

147+
# JWT takes precedence over API keys, so warn if user provided both
148+
if (kwargs.get("source_jwt") and kwargs.get("source_api_key")) or (
149+
kwargs.get("destination_jwt") and kwargs.get("destination_api_key")
150+
):
151+
logger.warning("Both a JWT and an API key were found, the JWT will take precedence.")
152+
147153
source_auth = {}
148-
if k := kwargs.get("source_api_key"):
154+
# JWT takes precedence over API keys
155+
if jwt := kwargs.get("source_jwt"):
156+
source_auth["jwtAuth"] = jwt
157+
elif k := kwargs.get("source_api_key"):
149158
source_auth["apiKeyAuth"] = k
150-
if k := kwargs.get("source_app_key"):
151-
source_auth["appKeyAuth"] = k
159+
if k := kwargs.get("source_app_key"):
160+
source_auth["appKeyAuth"] = k
152161
source_client = CustomClient(source_api_url, source_auth, retry_timeout, timeout, send_metrics, verify_ssl)
153162

154163
destination_auth = {}
155-
if k := kwargs.get("destination_api_key"):
164+
# JWT takes precedence over API keys
165+
if jwt := kwargs.get("destination_jwt"):
166+
destination_auth["jwtAuth"] = jwt
167+
elif k := kwargs.get("destination_api_key"):
156168
destination_auth["apiKeyAuth"] = k
157-
if k := kwargs.get("destination_app_key"):
158-
destination_auth["appKeyAuth"] = k
169+
if k := kwargs.get("destination_app_key"):
170+
destination_auth["appKeyAuth"] = k
159171
destination_client = CustomClient(
160172
destination_api_url,
161173
destination_auth,

datadog_sync/utils/custom_client.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,11 +291,17 @@ async def get_ddr_status(self) -> Dict:
291291

292292
def build_default_headers(auth_obj: Dict[str, str]) -> Dict[str, str]:
293293
headers = {
294-
"DD-API-KEY": auth_obj.get("apiKeyAuth", ""),
295-
"DD-APPLICATION-KEY": auth_obj.get("appKeyAuth", ""),
296294
"Content-Type": "application/json",
297295
"User-Agent": _get_user_agent(),
298296
}
297+
298+
# JWT takes precedence over API keys
299+
if jwt := auth_obj.get("jwtAuth"):
300+
headers["dd-auth-jwt"] = jwt
301+
else:
302+
headers["DD-API-KEY"] = auth_obj.get("apiKeyAuth", "")
303+
headers["DD-APPLICATION-KEY"] = auth_obj.get("appKeyAuth", "")
304+
299305
return headers
300306

301307

0 commit comments

Comments
 (0)