4
4
Configuration for jamfutil
5
5
"""
6
6
7
- __author__ = ' Sam Forester'
8
-
9
- __copyright__ = ' Copyright (c) 2020 University of Utah, Marriott Library'
10
- __license__ = ' MIT'
7
+ __author__ = " Sam Forester"
8
+
9
+ __copyright__ = " Copyright (c) 2020 University of Utah, Marriott Library"
10
+ __license__ = " MIT"
11
11
__version__ = "1.2.4"
12
12
13
13
import getpass
14
14
import logging
15
15
import plistlib
16
16
import keyring
17
17
from os import path , remove
18
- LINUX_PREFS_TILDA = '~/.edu.utah.mlib.jamfutil.plist'
19
- MACOS_PREFS_TILDA = '~/Library/Preferences/edu.utah.mlib.jamfutil.plist'
20
- AUTOPKG_PREFS_TILDA = '~/Library/Preferences/com.github.autopkg.plist'
21
- JAMF_PREFS = '/Library/Preferences/com.jamfsoftware.jamf.plist'
18
+
19
+ LINUX_PREFS_TILDA = "~/.edu.utah.mlib.jamfutil.plist"
20
+ MACOS_PREFS_TILDA = "~/Library/Preferences/edu.utah.mlib.jamfutil.plist"
21
+ AUTOPKG_PREFS_TILDA = "~/Library/Preferences/com.github.autopkg.plist"
22
+ JAMF_PREFS = "/Library/Preferences/com.jamfsoftware.jamf.plist"
22
23
logging .getLogger (__name__ ).addHandler (logging .NullHandler ())
23
24
24
25
25
26
class Config :
26
- def __init__ (self ,
27
- config_path = None ,
28
- hostname = None ,
29
- username = None ,
30
- password = None ,
31
- prompt = False ,
32
- explain = False ):
27
+ def __init__ (
28
+ self ,
29
+ config_path = None ,
30
+ hostname = None ,
31
+ username = None ,
32
+ password = None ,
33
+ prompt = False ,
34
+ explain = False ,
35
+ ):
33
36
self .log = logging .getLogger (f"{ __name__ } .{ self .__class__ .__name__ } " )
34
37
self .prompt = prompt
35
38
self .hostname = hostname
@@ -42,39 +45,39 @@ def __init__(self,
42
45
autopkg_prefs = path .expanduser (AUTOPKG_PREFS_TILDA )
43
46
if path .exists (macos_prefs ):
44
47
if explain :
45
- print ("Using macos: " + macos_prefs )
48
+ print ("Using macos: " + macos_prefs )
46
49
config_path = macos_prefs
47
50
elif path .exists (linux_prefs ):
48
51
if explain :
49
- print ("Using linux: " + linux_prefs )
52
+ print ("Using linux: " + linux_prefs )
50
53
config_path = linux_prefs
51
54
elif path .exists (autopkg_prefs ):
52
55
if explain :
53
- print ("Using autopkg: " + autopkg_prefs )
56
+ print ("Using autopkg: " + autopkg_prefs )
54
57
config_path = autopkg_prefs
55
58
elif path .exists (JAMF_PREFS ):
56
59
if explain :
57
- print ("Using jamf: " + JAMF_PREFS )
60
+ print ("Using jamf: " + JAMF_PREFS )
58
61
config_path = JAMF_PREFS
59
62
else :
60
63
if explain :
61
- print ("Using " + macos_prefs + " but it doesn't exist yet." )
64
+ print ("Using " + macos_prefs + " but it doesn't exist yet." )
62
65
config_path = macos_prefs
63
66
elif explain :
64
- print ("Using " + config_path + " because you said so." )
67
+ print ("Using " + config_path + " because you said so." )
65
68
66
- if config_path [0 ] == '~' :
69
+ if config_path [0 ] == "~" :
67
70
config_path = path .expanduser (config_path )
68
71
if explain :
69
- print ("Expanding the path. Using " + config_path )
72
+ print ("Expanding the path. Using " + config_path )
70
73
71
74
if not self .hostname and not self .username and not self .password :
72
75
if path .exists (config_path ):
73
- fptr = open (config_path , 'rb' )
76
+ fptr = open (config_path , "rb" )
74
77
prefs = plistlib .load (fptr )
75
78
fptr .close ()
76
- if ' JSSHostname' in prefs :
77
- if ' Credentials' in prefs :
79
+ if " JSSHostname" in prefs :
80
+ if " Credentials" in prefs :
78
81
cmessage = f"""
79
82
ATTENTION
80
83
To improve security with storing credentials used with the jctl tool, we have
@@ -87,15 +90,16 @@ def __init__(self,
87
90
the "./jamf/setconfig.py" script.
88
91
"""
89
92
raise Exception (cmessage )
90
- self .hostname = prefs ['JSSHostname' ]
91
- self .username = prefs ['Username' ]
92
- self .password = keyring .get_password (self .hostname ,
93
- self .username )
94
- elif 'JSS_URL' in prefs :
93
+ self .hostname = prefs ["JSSHostname" ]
94
+ self .username = prefs ["Username" ]
95
+ self .password = keyring .get_password (
96
+ self .hostname , self .username
97
+ )
98
+ elif "JSS_URL" in prefs :
95
99
self .hostname = prefs ["JSS_URL" ]
96
100
self .username = prefs ["API_USERNAME" ]
97
101
self .password = prefs ["API_PASSWORD" ]
98
- elif ' jss_url' in prefs :
102
+ elif " jss_url" in prefs :
99
103
self .hostname = prefs ["jss_url" ]
100
104
# No auth in that file
101
105
else :
@@ -112,16 +116,13 @@ def __init__(self,
112
116
if not self .password :
113
117
self .password = getpass .getpass ()
114
118
elif not self .hostname and not self .username and not self .password :
115
- raise Exception (' No jamf config file could be found and prompt is off.' )
119
+ raise Exception (" No jamf config file could be found and prompt is off." )
116
120
117
121
def save (self , config_path = None ):
118
122
keyring .set_password (self .hostname , self .username , self .password )
119
- data = {
120
- 'JSSHostname' : self .hostname ,
121
- 'Username' : self .username
122
- }
123
+ data = {"JSSHostname" : self .hostname , "Username" : self .username }
123
124
self .log .info (f"saving: { config_path } " )
124
- fptr = open (config_path , 'wb' )
125
+ fptr = open (config_path , "wb" )
125
126
plistlib .dump (data , fptr )
126
127
fptr .close ()
127
128
@@ -131,4 +132,4 @@ def reset(self, path):
131
132
132
133
133
134
def prompt_hostname ():
134
- return input (' Hostname (don\ ' t forget https:// and :8443): ' )
135
+ return input (" Hostname (don't forget https:// and :8443): " )
0 commit comments