From 4d36b6afb14ed88bb88959a9682801cdfb8f9d61 Mon Sep 17 00:00:00 2001 From: Michael Kuron Date: Fri, 13 Nov 2015 14:54:54 +0100 Subject: [PATCH 1/6] Set User-Agent header in HTTP requests Previously Munki only set a User-Agent header on requests made from the appleupdates.py code. Everything else (catalog, manifest, package downloading) reported something like `"Python/2.7.5 CFNetwork/673.6 Darwin/13.4.0 (x86_64) (VMware7%2C1)"` to the server. --- code/client/munkilib/fetch.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/code/client/munkilib/fetch.py b/code/client/munkilib/fetch.py index e00245fe..95afef8b 100644 --- a/code/client/munkilib/fetch.py +++ b/code/client/munkilib/fetch.py @@ -93,8 +93,15 @@ def writeCachedChecksum(file_path, fhash=None): def header_dict_from_list(array): """Given a list of strings in http header format, return a dict. If array is None, return None""" + header_dict = {} + machine = munkicommon.getMachineFacts() + darwin_version = os.uname()[2] + header_dict["User-Agent"] = ("managedsoftwareupdate/%s Darwin/%s (%s) (%s)" + % (machine['munki_version'], darwin_version, + machine['arch'], machine['machine_model'])) + if array is None: - return array + return header_dict header_dict = {} for item in array: (key, sep, value) = item.partition(':') From d1f9475c54a1fa3c48f011ed388738294335ffd4 Mon Sep 17 00:00:00 2001 From: Michael Kuron Date: Fri, 13 Nov 2015 15:07:53 +0100 Subject: [PATCH 2/6] header_dict_from_list documentation --- code/client/munkilib/fetch.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/code/client/munkilib/fetch.py b/code/client/munkilib/fetch.py index 95afef8b..dea1a8a7 100644 --- a/code/client/munkilib/fetch.py +++ b/code/client/munkilib/fetch.py @@ -92,7 +92,8 @@ def writeCachedChecksum(file_path, fhash=None): def header_dict_from_list(array): """Given a list of strings in http header format, return a dict. - If array is None, return None""" + A User-Agent header is added if none is present in the list. + If array is None, returns a dict with only the User-Agent header.""" header_dict = {} machine = munkicommon.getMachineFacts() darwin_version = os.uname()[2] From 088302be1de3978bac5b69dfc7db466fb612172b Mon Sep 17 00:00:00 2001 From: Michael Kuron Date: Fri, 13 Nov 2015 15:35:29 +0100 Subject: [PATCH 3/6] User-Agent header if other headers are specified --- code/client/munkilib/fetch.py | 1 - 1 file changed, 1 deletion(-) diff --git a/code/client/munkilib/fetch.py b/code/client/munkilib/fetch.py index dea1a8a7..1a7a7b8e 100644 --- a/code/client/munkilib/fetch.py +++ b/code/client/munkilib/fetch.py @@ -103,7 +103,6 @@ def header_dict_from_list(array): if array is None: return header_dict - header_dict = {} for item in array: (key, sep, value) = item.partition(':') if sep and value: From 0bc12e57bf65ac60c74893889d54a5c2412e25c4 Mon Sep 17 00:00:00 2001 From: Michael Kuron Date: Wed, 18 Nov 2015 10:29:59 +0100 Subject: [PATCH 4/6] Python and CFNetwork version in User-Agent --- code/client/munkilib/fetch.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/code/client/munkilib/fetch.py b/code/client/munkilib/fetch.py index 1a7a7b8e..0657b3e6 100644 --- a/code/client/munkilib/fetch.py +++ b/code/client/munkilib/fetch.py @@ -25,6 +25,7 @@ Created by Greg Neagle on 2011-09-29. import calendar import errno import os +import sys import re import shutil import subprocess @@ -39,6 +40,7 @@ import munkicommon from gurl import Gurl from Foundation import NSHTTPURLResponse +import FoundationPlist # XATTR name storing the ETAG of the file when downloaded via http(s). @@ -97,8 +99,10 @@ def header_dict_from_list(array): header_dict = {} machine = munkicommon.getMachineFacts() darwin_version = os.uname()[2] - header_dict["User-Agent"] = ("managedsoftwareupdate/%s Darwin/%s (%s) (%s)" - % (machine['munki_version'], darwin_version, + python_version = "%d.%d.%d" % (sys.version_info[0],sys.version_info[1],sys.version_info[2]) + cfnetwork_version = FoundationPlist.readPlist("/System/Library/Frameworks/CFNetwork.framework/Resources/Info.plist")['CFBundleShortVersionString'] + header_dict["User-Agent"] = ("Python/%s CFNetwork/%s managedsoftwareupdate/%s Darwin/%s (%s) (%s)" + % (python_version, cfnetwork_version, machine['munki_version'], darwin_version, machine['arch'], machine['machine_model'])) if array is None: From 6f0a10f1da0999c132c6e82247e1c55c44b7fe6f Mon Sep 17 00:00:00 2001 From: Michael Kuron Date: Thu, 19 Nov 2015 09:48:52 +0100 Subject: [PATCH 5/6] Cache default user agent --- code/client/munkilib/fetch.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/code/client/munkilib/fetch.py b/code/client/munkilib/fetch.py index 0657b3e6..f04ea627 100644 --- a/code/client/munkilib/fetch.py +++ b/code/client/munkilib/fetch.py @@ -48,6 +48,15 @@ XATTR_ETAG = 'com.googlecode.munki.etag' # XATTR name storing the sha256 of the file after original download by munki. XATTR_SHA = 'com.googlecode.munki.sha256' +# default value for User-Agent header +machine = munkicommon.getMachineFacts() +darwin_version = os.uname()[2] +python_version = "%d.%d.%d" % sys.version_info[:3] +cfnetwork_version = FoundationPlist.readPlist("/System/Library/Frameworks/CFNetwork.framework/Resources/Info.plist")['CFBundleShortVersionString'] +DEFAULT_USER_AGENT = "Python/%s CFNetwork/%s managedsoftwareupdate/%s Darwin/%s (%s) (%s)" + % (python_version, cfnetwork_version, machine['munki_version'], + darwin_version, machine['arch'], machine['machine_model']) + class GurlError(Exception): pass @@ -97,13 +106,7 @@ def header_dict_from_list(array): A User-Agent header is added if none is present in the list. If array is None, returns a dict with only the User-Agent header.""" header_dict = {} - machine = munkicommon.getMachineFacts() - darwin_version = os.uname()[2] - python_version = "%d.%d.%d" % (sys.version_info[0],sys.version_info[1],sys.version_info[2]) - cfnetwork_version = FoundationPlist.readPlist("/System/Library/Frameworks/CFNetwork.framework/Resources/Info.plist")['CFBundleShortVersionString'] - header_dict["User-Agent"] = ("Python/%s CFNetwork/%s managedsoftwareupdate/%s Darwin/%s (%s) (%s)" - % (python_version, cfnetwork_version, machine['munki_version'], darwin_version, - machine['arch'], machine['machine_model'])) + header_dict["User-Agent"] = DEFAULT_USER_AGENT if array is None: return header_dict From 918ebc64e4db26fb200919f635f11f06e721f57e Mon Sep 17 00:00:00 2001 From: Michael Kuron Date: Thu, 19 Nov 2015 09:52:26 +0100 Subject: [PATCH 6/6] Fix wrong line break --- code/client/munkilib/fetch.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/code/client/munkilib/fetch.py b/code/client/munkilib/fetch.py index f04ea627..c3946e83 100644 --- a/code/client/munkilib/fetch.py +++ b/code/client/munkilib/fetch.py @@ -53,9 +53,9 @@ machine = munkicommon.getMachineFacts() darwin_version = os.uname()[2] python_version = "%d.%d.%d" % sys.version_info[:3] cfnetwork_version = FoundationPlist.readPlist("/System/Library/Frameworks/CFNetwork.framework/Resources/Info.plist")['CFBundleShortVersionString'] -DEFAULT_USER_AGENT = "Python/%s CFNetwork/%s managedsoftwareupdate/%s Darwin/%s (%s) (%s)" - % (python_version, cfnetwork_version, machine['munki_version'], - darwin_version, machine['arch'], machine['machine_model']) +DEFAULT_USER_AGENT = "Python/%s CFNetwork/%s managedsoftwareupdate/%s Darwin/%s (%s) (%s)" % ( + python_version, cfnetwork_version, machine['munki_version'], + darwin_version, machine['arch'], machine['machine_model']) class GurlError(Exception):