Skip to content

Instantly share code, notes, and snippets.

@SegFaultAX
Created February 19, 2013 08:34
Show Gist options
  • Save SegFaultAX/4984063 to your computer and use it in GitHub Desktop.
Save SegFaultAX/4984063 to your computer and use it in GitHub Desktop.
Dump gnome2 keyring to CSV
#!/usr/bin/env python
# The MIT License (MIT) Copyright (c) 2013 Michael-Keith Bernard
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import sys
import csv
from StringIO import StringIO
from getpass import getpass
from contextlib import contextmanager
import gnomekeyring
@contextmanager
def keyring(name, password):
try:
gnomekeyring.unlock_sync(name, password)
yield
except gnomekeyring.IOError:
sys.exit("Unable to access gnome2 keyring!")
finally:
gnomekeyring.lock_sync(name)
def dump_keyring(name):
ids = gnomekeyring.list_item_ids_sync(name)
for id in ids:
item = gnomekeyring.item_get_info_sync(name, id)
yield (id, item.get_display_name(), item.get_secret())
def main():
name = sys.argv[1]
password = getpass("Password: ")
buff = StringIO()
writer = csv.writer(buff)
writer.writerow(["key_id", "key_name", "key_secret"])
with keyring(name, password):
for key_info in dump_keyring(name):
writer.writerow(key_info)
print buff.getvalue()
def usage():
sys.exit("usage: %s <keyring name>" % sys.argv[0])
if __name__ == "__main__":
if len(sys.argv) != 2:
usage()
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment