77from pathlib import Path
88import shutil
99import subprocess
10+ from typing import Optional
1011
1112from scfw .constants import DD_SERVICE , DD_SOURCE
1213
@@ -22,9 +23,9 @@ def configure_agent_logging(port: str):
2223
2324 Raises:
2425 ValueError: An invalid port number was provided.
25- RuntimeError: An error occurred while querying the Agent's status .
26+ RuntimeError: Failed to determine Datadog Agent configuration directory .
2627 """
27- if not (0 < int (port ) < 2 ** 16 ):
28+ if not (0 < int (port ) < 65536 ):
2829 raise ValueError ("Invalid port number provided for Datadog Agent logging" )
2930
3031 config_file = (
@@ -36,6 +37,9 @@ def configure_agent_logging(port: str):
3637 )
3738
3839 scfw_config_dir = _dd_agent_scfw_config_dir ()
40+ if not scfw_config_dir :
41+ raise RuntimeError ("Failed to determine Datadog Agent configuration directory" )
42+
3943 scfw_config_file = scfw_config_dir / "conf.yaml"
4044
4145 if not scfw_config_dir .is_dir ():
@@ -49,63 +53,57 @@ def configure_agent_logging(port: str):
4953def remove_agent_logging ():
5054 """
5155 Remove Datadog Agent configuration for Supply-Chain Firewall, if it exists.
52-
53- Raises:
54- RuntimeError: An error occurred while attempting to remove the configuration directory.
5556 """
56- try :
57- scfw_config_dir = _dd_agent_scfw_config_dir ()
58- except FileNotFoundError :
59- _log .info ("Datadog Agent binary is not available; no configuration to remove" )
60- return
61-
62- if not scfw_config_dir .is_dir ():
57+ scfw_config_dir = _dd_agent_scfw_config_dir ()
58+ if not (scfw_config_dir and scfw_config_dir .is_dir ()):
6359 _log .info ("No Datadog Agent configuration directory to remove" )
6460 return
6561
6662 try :
6763 shutil .rmtree (scfw_config_dir )
68- _log .info (f"Deleted directory { scfw_config_dir } with Datadog Agent configuration" )
69- except Exception :
70- raise RuntimeError (
71- f"Failed to delete directory { scfw_config_dir } with Datadog Agent configuration for Supply-Chain Firewall "
64+ _log .info (f"Removed directory { scfw_config_dir } with Datadog Agent configuration" )
65+ except Exception as e :
66+ _log . warning (
67+ f"Failed to remove Datadog Agent configuration directory { scfw_config_dir } : { e } "
7268 )
7369
7470
75- def _dd_agent_scfw_config_dir () -> Path :
71+ def _dd_agent_scfw_config_dir () -> Optional [ Path ] :
7672 """
77- Get the filesystem path to the firewall 's configuration directory for
78- Datadog Agent log forwarding.
73+ Return the filesystem path to Supply-Chain Firewall 's configuration directory
74+ for Datadog Agent log forwarding.
7975
8076 Returns:
81- A `Path` indicating the absolute filesystem path to this directory.
77+ A `Path` containing the local filesystem path to Supply-Chain Firewall's
78+ configuration directory for the Datadog Agent or `None` if the Agent binary
79+ is inaccessible or the Agent's global configuration directory (always the
80+ returned directory's parent) does not exist.
81+
82+ The returned path is what Supply-Chain Firewall's configuration directory
83+ would be if it existed, but this function does not check that this directory
84+ actually exists. It is the caller's responsibility to do so.
8285
8386 Raises:
84- RuntimeError:
85- * Unable to query Datadog Agent status to read the location of its global
86- configuration directory
87- * Datadog Agent global configuration directory is not set or does not exist
88- ValueError: Failed to parse Datadog Agent status JSON report.
87+ RuntimeError: Failed to query the Datadog Agent's status.
8988 """
89+ agent_path = shutil .which ("datadog-agent" )
90+ if not agent_path :
91+ _log .info ("No Datadog Agent binary is accessible in the current environment" )
92+ return None
93+
94+ agent_config_dir = None
9095 try :
9196 agent_status = subprocess .run (
92- ["datadog-agent" , "status" , "--json" ], check = True , text = True , capture_output = True
97+ [agent_path , "status" , "--json" ], check = True , text = True , capture_output = True
9398 )
94- config_confd_path = json .loads (agent_status .stdout ).get ("config" , {}).get ("confd_path" )
95- agent_config_dir = Path (config_confd_path ) if config_confd_path else None
99+ if ( config_confd_path : = json .loads (agent_status .stdout ).get ("config" , {}).get ("confd_path" )):
100+ agent_config_dir = Path (config_confd_path ). absolute ()
96101
97- except subprocess .CalledProcessError :
98- raise RuntimeError (
99- "Unable to query Datadog Agent status: please ensure the Agent is running. "
100- "Linux users may need sudo to run this command."
101- )
102-
103- except json .JSONDecodeError :
104- raise ValueError ("Failed to parse Datadog Agent status report as JSON" )
102+ except Exception as e :
103+ raise RuntimeError (f"Failed to query Datadog Agent status: { e } " )
105104
106105 if not (agent_config_dir and agent_config_dir .is_dir ()):
107- raise RuntimeError (
108- "Datadog Agent global configuration directory is not set or does not exist"
109- )
106+ _log .info ("No Datadog Agent global configuration directory found" )
107+ return None
110108
111109 return agent_config_dir / "scfw.d"
0 commit comments