示例#1
0
 def test_jss_with_args(self):
     try:
         prefs = FoundationPlist.readPlist(os.path.expanduser(PREF_PATH))
     except NameError:
         # Plist files are probably not binary on non-OS X
         # machines, so this should be safe.
         try:
             prefs = plistlib.readPlist(os.path.expanduser(PREF_PATH))
         except ExpatError:
             subprocess.call(['plutil', '-convert', 'xml1', PREF_PATH])
             prefs = plistlib.readPlist(os.path.expanduser(PREF_PATH))
     authUser = prefs["jss_user"]
     authPass = prefs["jss_pass"]
     repoUrl = prefs["jss_url"]
     j = JSS(url=repoUrl, user=authUser, password=authPass)
     assert_is_instance(j, JSS)
示例#2
0
 def test_jss_with_args(self):
     try:
         prefs = FoundationPlist.readPlist(os.path.expanduser(PREF_PATH))
     except NameError:
         # Plist files are probably not binary on non-OS X
         # machines, so this should be safe.
         try:
             prefs = plistlib.readPlist(os.path.expanduser(PREF_PATH))
         except ExpatError:
             subprocess.call(['plutil', '-convert', 'xml1',
                                 PREF_PATH])
             prefs = plistlib.readPlist(os.path.expanduser(PREF_PATH))
     authUser = prefs["jss_user"]
     authPass = prefs["jss_pass"]
     repoUrl = prefs["jss_url"]
     j = JSS(url=repoUrl, user=authUser, password=authPass)
     assert_is_instance(j, JSS)
示例#3
0
 def test_jssprefs(self):
     jp = JSSPrefs(PREF_PATH)
     try:
         prefs = FoundationPlist.readPlist(os.path.expanduser(PREF_PATH))
     except NameError:
         # Plist files are probably not binary on non-OS X
         # machines, so this should be safe.
         try:
             prefs = plistlib.readPlist(os.path.expanduser(PREF_PATH))
         except ExpatError:
             subprocess.call(['plutil', '-convert', 'xml1', PREF_PATH])
             prefs = plistlib.readPlist(os.path.expanduser(PREF_PATH))
     #result = subprocess.check_output(['defaults', 'read', 'com.github.sheagcraig.python-jss', 'jss_user'])
     assert_in(jp.user, prefs["jss_user"])
     #result = subprocess.check_output(['defaults', 'read', 'com.github.sheagcraig.python-jss', 'jss_pass'])
     assert_in(jp.password, prefs["jss_pass"])
     #result = subprocess.check_output(['defaults', 'read', 'com.github.sheagcraig.python-jss', 'jss_url'])
     assert_in(jp.url, prefs["jss_url"])
示例#4
0
 def test_jssprefs(self):
     jp = JSSPrefs(PREF_PATH)
     try:
         prefs = FoundationPlist.readPlist(os.path.expanduser(PREF_PATH))
     except NameError:
         # Plist files are probably not binary on non-OS X
         # machines, so this should be safe.
         try:
             prefs = plistlib.readPlist(os.path.expanduser(PREF_PATH))
         except ExpatError:
             subprocess.call(['plutil', '-convert', 'xml1',
                                 PREF_PATH])
             prefs = plistlib.readPlist(os.path.expanduser(PREF_PATH))
     #result = subprocess.check_output(['defaults', 'read', 'com.github.sheagcraig.python-jss', 'jss_user'])
     assert_in(jp.user, prefs["jss_user"])
     #result = subprocess.check_output(['defaults', 'read', 'com.github.sheagcraig.python-jss', 'jss_pass'])
     assert_in(jp.password, prefs["jss_pass"] )
     #result = subprocess.check_output(['defaults', 'read', 'com.github.sheagcraig.python-jss', 'jss_url'])
     assert_in(jp.url, prefs["jss_url"])
示例#5
0
    def parse_plist(self, preferences_file):
        """Try to reset preferences from preference_file."""
        preferences_file = os.path.expanduser(preferences_file)

        # Try to open using FoundationPlist. If it's not available,
        # fall back to plistlib and hope it's not binary encoded.
        try:
            prefs = FoundationPlist.readPlist(preferences_file)
        except NameError:
            try:
                prefs = plistlib.readPlist(preferences_file)
            except ExpatError:
                # If we're on OSX, try to convert using another
                # tool.
                if is_osx():
                    subprocess.call(
                        ["plutil", "-convert", "xml1", preferences_file])
                    prefs = plistlib.readPlist(preferences_file)

        self.preferences_file = preferences_file

        self.user = prefs.get("jss_user")
        self.url = prefs.get("jss_url")

        plain_password = prefs.get("jss_pass")

        # Previous versions might have left a plaintext password in
        # a preferences file. Offer to move it to the keychain and
        # bail if the user refuses: this is, after all, the 'K'JSSPrefs
        # class.
        if self.url and self.user and plain_password:
            answer = None
            question = (
                "Warning: we found a plaintext password in the "
                "prefs file, and you didn't specify '--no-keychain'.\n"
                "git2jss can remove the plaintext password "
                "from the file and move it to the keychain for you. \n"
                "This is almost certainly a good idea unless you really "
                "know what you are doing, and just forgot the --no-keychain "
                "flag.\n")
            print(question)

            while answer not in ['y', 'n']:
                answer = input(
                    'Do you want to move the password out of the plist file? (y|n)'
                )

            if answer == 'y':
                store_creds_in_keychain(self.url, self.user, plain_password)
                prefs.pop("jss_pass")
                self.write_plist_from_dict(prefs)
                print("Password moved into keychain")

            else:
                print(("OK, on your own head be it.\n"
                       "You can use the --no-keychain flag to continue with "
                       "the plaintext password."))
                raise JSSError("Plaintext password without --no-keychain")

        # This will throw an exception if the password is missing
        self.password = get_creds_from_keychain(self.url, self.user)

        if not all([self.user, self.password, self.url]):
            raise JSSPrefsMissingKeyError(
                "Some preferences are missing. Please "
                "delete %s and try again." % self.preferences_file)

        # Optional file repository array. Defaults to empty list.
        self.repos = []
        for repo in prefs.get("repos", []):
            self.repos.append(dict(repo))

        self.verify = prefs.get("verify", True)
        self.suppress_warnings = prefs.get("suppress_warnings", True)