Skip to content

Commit cabbb75

Browse files
committed
missed lib
1 parent d029b78 commit cabbb75

File tree

4 files changed

+195
-1
lines changed

4 files changed

+195
-1
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ dist/
1414
downloads/
1515
eggs/
1616
.eggs/
17-
lib/
1817
lib64/
1918
parts/
2019
sdist/

src/lib/__init__.py

Whitespace-only changes.

src/lib/ad_utils.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import os.path
2+
import paramiko
3+
from jinja2 import FileSystemLoader
4+
import src.lib.backend_utils as u
5+
import jinja2
6+
7+
__DEBUG__=0
8+
def set_config(config):
9+
u.__CONFIG__ = config
10+
11+
def open_ssh_conn():
12+
pkey = paramiko.Ed25519Key.from_private_key_file('../.ssh/id_ed25519')
13+
client = paramiko.SSHClient()
14+
policy = paramiko.AutoAddPolicy()
15+
client.set_missing_host_key_policy(policy)
16+
host = u.config('host')
17+
user = u.config('user')
18+
try:
19+
client.connect(host, username=user, pkey=pkey)
20+
return client
21+
except paramiko.ssh_exception.SSHException as e:
22+
e_dict = e.args[0]
23+
print(u.returncode(1, "Erreur d'authentification"))
24+
exit(1)
25+
26+
27+
def exec_cmd(command):
28+
client=open_ssh_conn()
29+
stdin, stdout, stderr = client.exec_command(command)
30+
content=stdout.read().decode()
31+
del client, stdin, stdout, stderr
32+
return content
33+
34+
def compose_dn(entity):
35+
rdnValue=u.find_key(entity,'cn')
36+
branchAttr=u.config('branchAttr','')
37+
branch = ''
38+
if branchAttr != '':
39+
branchValue=u.find_key(entity,branchAttr)
40+
41+
match branchValue:
42+
case 'etd':
43+
branch=u.config('branchForEtd','')
44+
case 'esn':
45+
branch = u.config('branchForEsn', '')
46+
case 'adm':
47+
branch = u.config('branchForAdm', '')
48+
if branch != '':
49+
return 'cn=' + rdnValue+ ',' + branch + "," + u.config('base')
50+
else:
51+
return 'cn=' + rdnValue+ "," + u.config('base')
52+
53+
def dn_superior(dn):
54+
tab=dn.split(',')
55+
tab.pop(0)
56+
return ','.join(tab)
57+
58+
59+
def gen_script_from_template(entity,template):
60+
data={
61+
'domain' :u.config('domain'),
62+
'base': u.config('base'),
63+
'dn' : compose_dn(entity),
64+
'path': dn_superior(compose_dn(entity)),
65+
'e': u.make_entry_array(entity)
66+
}
67+
environment = jinja2.Environment(loader=FileSystemLoader("../ps1_templates/"))
68+
template = environment.get_template(template)
69+
content=template.render(data)
70+
return content
71+
72+
def ad_exec_script(entity,template,params=""):
73+
content=gen_script_from_template(entity,template)
74+
client = open_ssh_conn()
75+
sshfile = client.open_sftp()
76+
pid=os.getpid()
77+
if __DEBUG__ == 0 :
78+
scriptName='sesame_script.' + str(pid) + '.ps1'
79+
else:
80+
scriptName='sesame_script.ps1'
81+
with sshfile.open(scriptName, mode="w") as message:
82+
message.write(content)
83+
##execution du script
84+
chan = client.get_transport().open_session()
85+
if params == '':
86+
cmd=scriptName
87+
else:
88+
cmd=scriptName + " " + params
89+
chan.exec_command('powershell -ExecutionPolicy Bypass -NonInteractive -File ' + cmd)
90+
exitCode = chan.recv_exit_status()
91+
content = chan.recv(4096).decode('utf-8')
92+
error = chan.recv_stderr(4096).decode()
93+
if __DEBUG__ == 0:
94+
chan = client.get_transport().open_session()
95+
##supression du script
96+
chan.exec_command('del ' + scriptName)
97+
del client
98+
if exitCode == 0:
99+
print(u.returncode(0,content.rstrip("\n")))
100+
exit(0)
101+
else:
102+
print(u.returncode(1,content.rstrip("\n")))
103+
exit(1)
104+

src/lib/backend_utils.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import configparser
2+
import json
3+
from sys import stdin
4+
5+
__CONFIG__=configparser.RawConfigParser()
6+
7+
8+
def read_config(file):
9+
with open(file) as f:
10+
file_content = '[config]\n' + f.read()
11+
__CONFIG__.read_string(file_content)
12+
return __CONFIG__
13+
14+
def config(key,default=''):
15+
c=__CONFIG__['config']
16+
return c.get(key,default)
17+
18+
19+
def readjsoninput():
20+
input = stdin.read()
21+
return json.loads(input)
22+
23+
24+
def returncode(code,message):
25+
'''
26+
Retourne le code au format json pour le backend
27+
'''
28+
data={}
29+
data['code']=code
30+
data['message']=message
31+
return json.dumps(data)
32+
33+
def is_backend_concerned(entity):
34+
peopleType=find_key(entity,config('branchAttr'))
35+
listBackend=config('backendFor')
36+
if (listBackend.find(peopleType) == -1):
37+
return False
38+
return True
39+
40+
def find_key(element, key):
41+
'''
42+
Check if *keys (nested) exists in `element` (dict).
43+
'''
44+
return _finditem(element,key)
45+
46+
def _finditem(obj, key):
47+
if key in obj: return obj[key]
48+
for k, v in obj.items():
49+
if isinstance(v,dict):
50+
item = _finditem(v, key)
51+
if item is not None:
52+
return item
53+
54+
def make_entry_array(entity):
55+
data={}
56+
if "identity" in entity['payload']:
57+
objectclasses = entity['payload']['identity']['identity']['additionalFields']['objectClasses']
58+
inetOrgPerson=entity['payload']['identity']['identity']['inetOrgPerson']
59+
additionalFields=entity['payload']['identity']['identity']['additionalFields']['attributes']
60+
61+
else:
62+
objectclasses=entity['payload']['additionalFields']['objectClasses']
63+
inetOrgPerson = entity['payload']['inetOrgPerson']
64+
additionalFields = entity['payload']['additionalFields']['attributes']
65+
#inetOrgPerson
66+
for k,v in inetOrgPerson.items():
67+
data[k]=str(v)
68+
69+
for obj in objectclasses:
70+
for k,v in additionalFields[obj].items():
71+
data[k]=str(v)
72+
return data
73+
74+
75+
def make_objectclass(entity):
76+
data = {}
77+
if "identity" in entity['payload']:
78+
objectclasses = entity['payload']['identity']['identity']['additionalFields']['objectClasses']
79+
else:
80+
objectclasses = entity['payload']['additionalFields']['objectClasses']
81+
82+
return ['top', 'inetOrgPerson'] + objectclasses
83+
84+
85+
def make_entry_array_without_empty(entity):
86+
data={}
87+
data1=make_entry_array(entity)
88+
for k,v in data1.items():
89+
if str(v) != "":
90+
data[k]=v
91+
return data

0 commit comments

Comments
 (0)