pyceo/ceod/model/KerberosService.py

48 lines
1.4 KiB
Python

import os
import subprocess
from zope.interface import implementer
from ceo_common.interfaces import IKerberosService
@implementer(IKerberosService)
class KerberosService:
def __init__(self, admin_principal: str):
self.admin_principal = admin_principal
cache_file = '/run/ceod/krb5_cache'
os.makedirs('/run/ceod', exist_ok=True)
os.putenv('KRB5CCNAME', 'FILE:' + cache_file)
self.kinit()
def kinit(self):
subprocess.run(['kinit', '-k', self.admin_principal], check=True)
def addprinc(self, principal: str, password: str):
subprocess.run([
'kadmin', '-k', '-p', self.admin_principal, 'addprinc',
'-pw', password,
'-policy', 'default',
'+needchange',
principal
], check=True)
def delprinc(self, principal: str):
subprocess.run([
'kadmin', '-k', '-p', self.admin_principal, 'delprinc',
'-force',
principal
], check=True)
def change_password(self, principal: str, password: str):
subprocess.run([
'kadmin', '-k', '-p', self.admin_principal, 'cpw',
'-pw', password,
principal
], check=True)
subprocess.run([
'kadmin', '-k', '-p', self.admin_principal, 'modprinc',
'+needchange',
principal
], check=True)