From 44feadb94545689d84be086bff52ca104c3ffc7f Mon Sep 17 00:00:00 2001 From: Derek Brown Date: Wed, 14 Jun 2017 21:09:25 -0700 Subject: [PATCH 1/5] [windows] add ability to specify proxy parameters on install command line --- utils/windows_configuration.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/windows_configuration.py b/utils/windows_configuration.py index ae933f1f1e..9e217f23ff 100644 --- a/utils/windows_configuration.py +++ b/utils/windows_configuration.py @@ -23,7 +23,7 @@ def get_registry_conf(agentConfig): try: with _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, WINDOWS_REG_PATH) as reg_key: - for attribute in ['api_key', 'tags', 'hostname']: + for attribute in ['api_key', 'tags', 'hostname', 'proxy_host', 'proxy_port', 'proxy_user', 'proxy_password']: option = _winreg.QueryValueEx(reg_key, attribute)[0] if option != '': registry_conf[attribute] = option From fcc5220aea4a2b593883250063fc09dd8a72dca7 Mon Sep 17 00:00:00 2001 From: Derek Brown Date: Wed, 14 Jun 2017 22:10:07 -0700 Subject: [PATCH 2/5] Delete config from the registry after it's been added to the config file --- utils/windows_configuration.py | 18 +++++++++++++++++- win32/service.py | 8 ++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/utils/windows_configuration.py b/utils/windows_configuration.py index 9e217f23ff..dca12afb3d 100644 --- a/utils/windows_configuration.py +++ b/utils/windows_configuration.py @@ -17,13 +17,28 @@ log = logging.getLogger(__name__) +config_attributes = ['api_key', 'tags', 'hostname', 'proxy_host', 'proxy_port', 'proxy_user', 'proxy_password'] + +def remove_registry_conf(): + try: + with _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, + WINDOWS_REG_PATH) as reg_key: + for attribute in config_attributes: + try: + _winreg.DeleteValue(reg_key, attribute) + except Exception: + # it's ok if it's not there. + pass + except Exception: + # also OK if the whole tree isn't there + pass def get_registry_conf(agentConfig): registry_conf = {} try: with _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, WINDOWS_REG_PATH) as reg_key: - for attribute in ['api_key', 'tags', 'hostname', 'proxy_host', 'proxy_port', 'proxy_user', 'proxy_password']: + for attribute in config_attributes: option = _winreg.QueryValueEx(reg_key, attribute)[0] if option != '': registry_conf[attribute] = option @@ -99,5 +114,6 @@ def update_conf_file(registry_conf, config_path): os.rename(temp_config_path, config_path) except OSError as e: log.exception('Unable to save new datadog.conf') + raise else: log.debug('Successfully saved the new datadog.conf') diff --git a/win32/service.py b/win32/service.py index 609af3bf7e..bba2791a14 100644 --- a/win32/service.py +++ b/win32/service.py @@ -30,7 +30,7 @@ from config import get_config, get_config_path, get_confd_path from jmxfetch import JMXFetch from utils.jmx import JMXFiles -from utils.windows_configuration import get_registry_conf, update_conf_file +from utils.windows_configuration import get_registry_conf, update_conf_file, remove_registry_conf log = logging.getLogger('service') @@ -134,7 +134,11 @@ def _update_config_file(self, config): config.update(registry_conf) if registry_conf: log.info('Updating conf file options: %s', registry_conf.keys()) - update_conf_file(registry_conf, get_config_path()) + try: + update_conf_file(registry_conf, get_config_path()) + remove_registry_conf() + except Exception: + log.warning('Failed to update config file; registry configuration persisted') def SvcStop(self): # Stop all services. From 6187caab5e3e25b4a2791b8677e1c08a11555ea4 Mon Sep 17 00:00:00 2001 From: Derek Brown Date: Wed, 14 Jun 2017 22:50:19 -0700 Subject: [PATCH 3/5] Add additional logging --- utils/windows_configuration.py | 2 ++ win32/service.py | 1 + 2 files changed, 3 insertions(+) diff --git a/utils/windows_configuration.py b/utils/windows_configuration.py index dca12afb3d..329fe90177 100644 --- a/utils/windows_configuration.py +++ b/utils/windows_configuration.py @@ -20,10 +20,12 @@ config_attributes = ['api_key', 'tags', 'hostname', 'proxy_host', 'proxy_port', 'proxy_user', 'proxy_password'] def remove_registry_conf(): + log.info("deleting registry values") try: with _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, WINDOWS_REG_PATH) as reg_key: for attribute in config_attributes: + log.info("deleting %s", attribute) try: _winreg.DeleteValue(reg_key, attribute) except Exception: diff --git a/win32/service.py b/win32/service.py index bba2791a14..6b18f0f6d3 100644 --- a/win32/service.py +++ b/win32/service.py @@ -136,6 +136,7 @@ def _update_config_file(self, config): log.info('Updating conf file options: %s', registry_conf.keys()) try: update_conf_file(registry_conf, get_config_path()) + log.info('update succeeded, deleting old values') remove_registry_conf() except Exception: log.warning('Failed to update config file; registry configuration persisted') From 4080b58e05057581f068569a910ba787c3476cfa Mon Sep 17 00:00:00 2001 From: Derek Brown Date: Wed, 14 Jun 2017 23:31:15 -0700 Subject: [PATCH 4/5] Add proper permissions --- utils/windows_configuration.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/utils/windows_configuration.py b/utils/windows_configuration.py index 329fe90177..8240de6480 100644 --- a/utils/windows_configuration.py +++ b/utils/windows_configuration.py @@ -20,12 +20,10 @@ config_attributes = ['api_key', 'tags', 'hostname', 'proxy_host', 'proxy_port', 'proxy_user', 'proxy_password'] def remove_registry_conf(): - log.info("deleting registry values") try: with _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, - WINDOWS_REG_PATH) as reg_key: + WINDOWS_REG_PATH, _winreg.KEY_WRITE) as reg_key: for attribute in config_attributes: - log.info("deleting %s", attribute) try: _winreg.DeleteValue(reg_key, attribute) except Exception: From 841bc8d6d803b8ef197c38ba7f5b025cc79cf9a2 Mon Sep 17 00:00:00 2001 From: Derek Brown Date: Thu, 15 Jun 2017 08:16:41 -0700 Subject: [PATCH 5/5] More logging --- utils/windows_configuration.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/utils/windows_configuration.py b/utils/windows_configuration.py index 8240de6480..943749af26 100644 --- a/utils/windows_configuration.py +++ b/utils/windows_configuration.py @@ -22,11 +22,12 @@ def remove_registry_conf(): try: with _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, - WINDOWS_REG_PATH, _winreg.KEY_WRITE) as reg_key: + WINDOWS_REG_PATH, 0, _winreg.KEY_WRITE) as reg_key: for attribute in config_attributes: try: _winreg.DeleteValue(reg_key, attribute) - except Exception: + except Exception as e: + log.error("Failed to delete value %s %s", attribute, str(e)) # it's ok if it's not there. pass except Exception: