Skip to content

Commit 5fa4f57

Browse files
committed
Add support for base URL in config.plist
1 parent b312810 commit 5fa4f57

3 files changed

Lines changed: 50 additions & 19 deletions

File tree

CHANGELOG.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## [1.3.0] - 2023-07-26
4+
5+
### Added
6+
7+
- Support for file-based base url configuration
8+
39
## [1.2.4] - 2020-12-07
410

511
### Fixed
@@ -10,7 +16,7 @@
1016

1117
### Removed
1218

13-
- Notice during plugin execution that the MUNKI_REPO setting is ignored.
19+
- Notice during plugin execution that the MUNKI_REPO setting is ignored.
1420

1521
## [1.2.2] - 2020-09-10
1622

@@ -55,7 +61,7 @@
5561

5662
## [1.1.0] - 2020-08-10
5763

58-
### Added
64+
### Added
5965

6066
- Better reporting when authentication and authorization errors occur
6167
- Ability to change repo base url

SimpleMDMRepo.py

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# encoding: utf-8
22

33
# SimpleMDMRepo.py
4-
# Version 1.2.4
4+
# Version 1.3.0
55

66
from __future__ import absolute_import, print_function
77

@@ -21,20 +21,37 @@
2121

2222
from munkilib.munkirepo import Repo, RepoError
2323
from munkilib.wrappers import readPlistFromString, PlistReadError
24-
24+
2525
DEFAULT_BASE_URL = 'https://a.simplemdm.com/munki/plugin'
2626
CONFIG_PATH = '/usr/local/simplemdm/munki-plugin/config.plist'
2727

2828
class SimpleMDMRepo(Repo):
2929

3030
def __init__(self, baseurl):
31-
self.base_url = DEFAULT_BASE_URL
32-
if 'SIMPLEMDM_BASE_URL' in os.environ:
33-
self.base_url = os.environ['SIMPLEMDM_BASE_URL']
31+
self.base_url = self._fetch_base_url()
3432

3533
self.getter = URLGetter()
3634
self.auth_header = self._fetch_auth_header()
37-
35+
36+
def _fetch_base_url(self):
37+
# fetch from environment argument
38+
39+
if 'SIMPLEMDM_BASE_URL' in os.environ:
40+
print('Using base URL provided by environment variable.')
41+
return os.environ['SIMPLEMDM_BASE_URL']
42+
43+
# fetch from config file
44+
45+
config = self._read_config_file()
46+
47+
if config:
48+
key = config.get('base_url', None)
49+
if key and len(key) > 0:
50+
print('Using base URL provided in config file.')
51+
return key
52+
53+
return DEFAULT_BASE_URL
54+
3855
def _fetch_api_key(self):
3956
# fetch from environment argument
4057

@@ -44,6 +61,19 @@ def _fetch_api_key(self):
4461

4562
# fetch from config file
4663

64+
config = self._read_config_file()
65+
if config:
66+
key = config.get('key', None)
67+
if key and len(key) > 0:
68+
print('Using API key provided in config file.')
69+
return key
70+
71+
# fetch interactively
72+
73+
print('Please provide a SimpleMDM API key')
74+
return getpass.getpass()
75+
76+
def _read_config_file(self):
4777
try:
4878
with open(CONFIG_PATH,'rb') as f:
4979
config_str = f.read()
@@ -58,15 +88,8 @@ def _fetch_api_key(self):
5888
except PlistReadError as e:
5989
print('WARNING: Could not parse config file: {error}'.format(error=e))
6090
else:
61-
key = config.get('key', None)
62-
if key and len(key) > 0:
63-
print('Using API key provided in key file.')
64-
return key
91+
return config
6592

66-
# fetch interactively
67-
68-
print('Please provide a SimpleMDM API key')
69-
return getpass.getpass()
7093

7194
def _fetch_auth_header(self):
7295
key = self._fetch_api_key()
@@ -100,7 +123,7 @@ def _curl(self, simplemdm_path_or_url, commands=None, form_data=None, headers=No
100123
curl_cmd.extend(['-F', '{key}={value}'.format(key=key,value=value)])
101124

102125
# commands
103-
126+
104127
curl_cmd.extend(commands)
105128
curl_cmd.append('-v')
106129

@@ -157,7 +180,7 @@ def put_from_local_file(self, resource_identifier, local_file_path):
157180
}
158181
resp = self._curl('pkgs/create_url', form_data=form_data)
159182
upload_url = resp.decode("UTF-8")
160-
183+
161184
# upload binary
162185

163186
headers = { 'Content-type': 'application/octet-stream' }
@@ -177,7 +200,7 @@ def put_from_local_file(self, resource_identifier, local_file_path):
177200
self._curl(resource_identifier, headers=headers, commands=commands)
178201

179202
def delete(self, resource_identifier):
180-
raise ProcessorError("This action is not supported by SimpleMDM")
203+
raise ProcessorError("This action is not supported by SimpleMDM")
181204

182205
def makecatalogs(self, options, output_fn=None):
183206
return []

config.plist.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@
44
<dict>
55
<key>key</key>
66
<string></string>
7+
<key>base_url</key>
8+
<string></string>
79
</dict>
810
</plist>

0 commit comments

Comments
 (0)