# -*-mode: doctest; tab-width: 0; py-indent-offset: 4; coding: utf-8-unix -*-

=== Keyctl Binary ===

/bin/keyctl

    >>> import os,sys
    >>> os.system('/bin/keyctl --version') #doctest: +ELLIPSIS
    0
    >>> os.system('file /bin/keyctl') #doctest: +ELLIPSIS
    0
    >>> from os import system as S

===   Add a key to a keyring ===

keyctl add <type> <desc> <data> <keyring>
keyctl padd <type> <desc> <keyring>

This command creates a key of the specified type and  description;  in‐
stantiates  it  with  the  given  data and attaches it to the specified
keyring. It then prints the new key's ID on stdout:

    >>> S('keyctl add user mykey stuff @u')
    0

26

===    Create a keyring ===

keyctl newring <name> <keyring>

This command creates a new keyring of the specified name  and  attaches
it  to the specified keyring. The ID of the new keyring will be printed
to stdout if successful.

    >>> S('keyctl newring gridfire @us > /tmp/newring.id')
    0
    >>> sys.stderr.write(open('/tmp/newring.id', 'r').read() +'\n')
    1...


== /var/local/src/python-keyring-keyutils ==

    >>> gridfire_service = 'gridfire service'
    >>> from keyring.core import load_keyring
    >>> keyring = load_keyring('keyring_keyutils.backend.KeyutilsKeyringBackend')
    >>> keyring.set_password(gridfire_service, 'username foo', 'password bar')
    >>> keyring.get_password(gridfire_service, 'username foo')
    'password bar'
    >>> keyring.set_password(gridfire_service, 'foobar', 'secret')

    >>> import keyutils # different beast
    >>> iSess = keyutils.join_session_keyring()

    >>> os.system("keyctl setperm $(keyctl search  @s user 'gridfire service:foobar') 0x003f0000")
    512

This shoul have been zero but maybe the ssushell

    >>> description = gridfire_service+':'+ 'foobar'
    >>> i = keyutils.search(iSess, description.encode(), destination=0, keyType=b"user")
    >>> i is not None and type(i) == int and i > 0
    False

This should have been the id
    >>> os.system("keyctl search  @s user 'gridfire service:foobar'")

    >>> dir(keyutils)
    [...'absolute_import', 'add_key', 'clear', 'describe_key', 'join_session_keyring', 'link', 'read_key', 'request_key', 'revoke', 'search', 'session_to_parent', 'set_perm', 'set_timeout', 'unlink', 'update_key']
    >>> keyutils.set_perm
    <function...

    >>> keyring.delete_password(gridfire_service, 'username foo')
    >>> os.system(' keyctl show @s')
    0

marcus@linux:~/python-keyring-keyutils> keyctl show @s
Keyring
  46297307 --alswrv   1000   100  keyring: _ses
 990208180 --alswrv   1000 65534   \_ keyring: _uid.1000
 750093164 --alswrv   1000   100   \_ keyring: python-keyring-keyutils
  22790318 --alswrv   1000   100       \_ user: some service:foobar

keyctl show @s

marcus@linux:~/python-keyring-keyutils> keyctl print 22790318
secret

    >>> os.system('''/var/local/bin/python3.bash -c "from keyring.core import load_keyring; keyring = load_keyring('keyring_keyutils.backend.KeyutilsKeyringBackend'); print(keyring.get_password('gridfire service', 'foobar'))" > /tmp/passwd.tmp''')
    0

The file /tmp/passwd.tmp shoudl have the asswd secret

    >>> sys.stderr.write(open('/tmp/passwd.tmp', 'r').read() +'\n')
    8
