Skip to content

Commit 9d3f560

Browse files
committed
allow key to be stored in a config file
1 parent 77d7fbb commit 9d3f560

3 files changed

Lines changed: 73 additions & 12 deletions

File tree

CHANGELOG.md

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

3+
## [1.2.0] - 2020-08-24
4+
5+
### Added
6+
7+
- Support for file-based API key storage
8+
39
## [1.1.3] - 2020-08-18
410

511
### Added

README.md

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22

33
## Requirements
44

5-
If you plan on using [AutoPkg](https://github.com/autopkg/autopkg), a release greater than 2.1 is required as earlier versions do not have Munki repo plugin support. Currently (8/10/20) the latest build does not include support, so the following files need to be pulled from the AutoPkg repo and replaced on your local installation:
6-
7-
* `autopkglib/MunkiImporter.py`
8-
* `autopkglib/munkirepolibs/AutoPkgLib.py`
9-
* `autopkglib/munkirepolibs/MunkiLibAdapter.py`
5+
If you plan on using [AutoPkg](https://github.com/autopkg/autopkg), release 2.2 or greater is required as earlier versions do not have Munki repo plugin support.
106

117
## Installation
128

@@ -22,25 +18,56 @@ API keys may be generated within the API section of the SimpleMDM administrator
2218

2319
#### Setting the Key
2420

25-
The plugin will usually prompt for an API key interactively. To avoid this, you may set the key once per terminal session like so:
21+
The plugin attempts to fetch the API key three ways, in order:
22+
23+
1. environment variable
24+
1. configuration file
25+
1. interactively
26+
27+
##### Environment Variable
28+
29+
You may set the key once per terminal session like so:
2630

2731
```
2832
export SIMPLEMDM_API_KEY="Whvop7kWXxsva326ABDF8VDCSGFyEkuEx2xGgj4jab8AE90cn70QdBTq0fplli0a"
2933
```
3034

35+
You can also set the key for a single command by prepending it like so:
36+
37+
```
38+
SIMPLEMDM_API_KEY="Whvop7kWXxsva326ABDF8VDCSGFyEkuEx2xGgj4jab8AE90cn70QdBTq0fplli0a" autopkg run ...
39+
```
40+
41+
##### Configuration File
42+
43+
You may store the key in a configuration file at `/usr/local/simplemdm/munki-plugin/config.plist`. Please scope the permissions on this file so that it is restricted, however still allowing utilities using the repo plugin to access it.
44+
45+
The file should be formatted as below. Be sure to provide your own API key:
46+
47+
```
48+
<?xml version="1.0" encoding="UTF-8"?>
49+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
50+
<plist version="1.0">
51+
<dict>
52+
<key>key</key>
53+
<string>Whvop7kWXxsva326ABDF8VDCSGFyEkuEx2xGgj4jab8AE90cn70QdBTq0fplli0a</string>
54+
</dict>
55+
</plist>
56+
```
57+
3158
### Using AutoPkg
3259

3360
Any `.munki` recipe is supported. In this case, we are importing `GoogleChrome.munki`. Be sure to include `extract_icon` if you'd like the icon uploaded to SimpleMDM, if available.
3461

35-
**Please Note:** Running MakeCatalogs.munki is not necessary. See "Do not use makecatalogs" below for more information.
62+
**Please Note:** Running MakeCatalogs.munki is not necessary. See "Using Makecatalogs" below for more information.
3663

3764
```
3865
autopkg run -v GoogleChrome.munki -k MUNKI_REPO_PLUGIN="SimpleMDMRepo" -k extract_icon=True
3966
```
4067

4168
### Using munkiimport and manifestutil
4269

43-
**Please Note:** Running makecatalogs is not necessary. See "Do not use makecatalogs" below for more information.
70+
**Please Note:** Running makecatalogs is not necessary. See "Using Makecatalogs" below for more information.
4471

4572
Before using either of these tools, they must be configured by running `munkiimport --configure` or `manifestutil --configure`. Keep all settings default, except:
4673
- Set repo url to `BLANK` or some other dummy value, as it is unused.

SimpleMDMRepo.py

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

33
# SimpleMDMRepo.py
4-
# Version 1.1.3
4+
# Version 1.2.0
55

66
from __future__ import absolute_import, print_function
77

@@ -20,8 +20,10 @@
2020
from urllib.parse import quote
2121

2222
from munkilib.munkirepo import Repo, RepoError
23+
from munkilib.wrappers import readPlistFromString, PlistReadError
2324

2425
DEFAULT_BASE_URL = 'https://a.simplemdm.com/munki/plugin'
26+
CONFIG_PATH = '/usr/local/simplemdm/munki-plugin/config.plist'
2527

2628
class SimpleMDMRepo(Repo):
2729

@@ -35,11 +37,37 @@ def __init__(self, baseurl):
3537
print('NOTICE: The SimpleMDMRepo plugin ignores the MUNKI_REPO value. "{base_url}" will be used instead.'.format(base_url=self.base_url))
3638

3739
def _fetch_api_key(self):
40+
# fetch from environment argument
41+
3842
if 'SIMPLEMDM_API_KEY' in os.environ:
43+
print('Usig API key provided by environment variable.')
3944
return os.environ['SIMPLEMDM_API_KEY']
40-
else:
41-
print('Please provide a SimpleMDM API key')
42-
return getpass.getpass()
45+
46+
# fetch from config file
47+
48+
try:
49+
with open(CONFIG_PATH,'rb') as f:
50+
config_str = f.read()
51+
except IOError as e:
52+
if e.errno != 2:
53+
print('WARNING: Could not read config file: {error}'.format(error=e))
54+
config_str = None
55+
56+
if config_str:
57+
try:
58+
config = readPlistFromString(config_str)
59+
except PlistReadError as e:
60+
print('WARNING: Could not parse config file: {error}'.format(error=e))
61+
else:
62+
key = config.get('key', None)
63+
if key and len(key) > 0:
64+
print('Using API key provided in key file.')
65+
return key
66+
67+
# fetch interactively
68+
69+
print('Please provide a SimpleMDM API key')
70+
return getpass.getpass()
4371

4472
def _fetch_auth_header(self):
4573
key = self._fetch_api_key()

0 commit comments

Comments
 (0)