pyceo/ceod/model/KerberosService.py

56 lines
1.5 KiB
Python
Raw Normal View History

2021-07-19 01:47:39 -04:00
import os
import subprocess
2021-08-22 00:36:19 -04:00
from typing import List
2021-07-19 01:47:39 -04:00
2021-08-17 21:59:24 -04:00
from zope import component
2021-07-19 01:47:39 -04:00
from zope.interface import implementer
2021-08-17 21:59:24 -04:00
from ceo_common.interfaces import IKerberosService, IConfig
2021-07-19 01:47:39 -04:00
@implementer(IKerberosService)
class KerberosService:
2021-08-03 23:30:19 -04:00
def __init__(
self,
admin_principal: str,
):
2021-08-17 21:59:24 -04:00
cfg = component.getUtility(IConfig)
2021-07-24 17:09:10 -04:00
self.admin_principal = admin_principal
2021-08-17 21:59:24 -04:00
self.realm = cfg.get('ldap_sasl_realm')
2021-08-25 22:19:18 -04:00
# We don't need a credentials cache because the client forwards
# their credentials to us
os.environ['KRB5CCNAME'] = 'FILE:/dev/null'
2021-07-19 01:47:39 -04:00
2021-08-22 00:36:19 -04:00
def _run(self, args: List[str]):
subprocess.run(args, check=True)
2021-07-19 01:47:39 -04:00
def addprinc(self, principal: str, password: str):
2021-08-22 00:36:19 -04:00
self._run([
2021-07-19 01:47:39 -04:00
'kadmin', '-k', '-p', self.admin_principal, 'addprinc',
'-pw', password,
'-policy', 'default',
'+needchange',
2021-08-13 20:11:56 -04:00
'+requires_preauth',
2021-07-19 01:47:39 -04:00
principal
2021-08-22 00:36:19 -04:00
])
2021-07-19 01:47:39 -04:00
2021-07-24 17:09:10 -04:00
def delprinc(self, principal: str):
2021-08-22 00:36:19 -04:00
self._run([
2021-07-24 17:09:10 -04:00
'kadmin', '-k', '-p', self.admin_principal, 'delprinc',
'-force',
principal
2021-08-22 00:36:19 -04:00
])
2021-07-24 17:09:10 -04:00
2021-07-19 01:47:39 -04:00
def change_password(self, principal: str, password: str):
2021-08-22 00:36:19 -04:00
self._run([
2021-07-19 01:47:39 -04:00
'kadmin', '-k', '-p', self.admin_principal, 'cpw',
'-pw', password,
principal
2021-08-22 00:36:19 -04:00
])
self._run([
2021-07-19 01:47:39 -04:00
'kadmin', '-k', '-p', self.admin_principal, 'modprinc',
'+needchange',
principal
2021-08-22 00:36:19 -04:00
])