forked from privacyidea/privacyidea
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pi-manage
executable file
·1003 lines (886 loc) · 34.6 KB
/
pi-manage
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# 2017-10-08 Cornelius Kölbel <cornelius.koelbel@netknights.it>
# Allow cleaning up different actions with different
# retention times.
# 2017-07-12 Cornelius Kölbel <cornelius.koelbel@netknights.it>
# Add generation of PGP keys
# 2017-02-23 Cornelius Kölbel <cornelius.koelbel@netknights.it>
# Add CA sub commands
# 2017-01-27 Diogenes S. Jesus
# Cornelius Kölbel <cornelius.koelbel@netknights.it>
# Add creation of more detailed policy
# 2016-04-15 Cornelius Kölbel <cornelius@privacyidea.org>
# Add backup for pymysql driver
# 2016-01-29 Cornelius Kölbel <cornelius@privacyidea.org>
# Add profiling
# 2015-10-09 Cornelius Kölbel <cornelius@privacyidea.org>
# Set file permissions
# 2015-09-24 Cornelius Kölbel <cornelius@privacyidea.org>
# Add validate call
# 2015-06-16 Cornelius Kölbel <cornelius@privacyidea.org>
# Add creation of JWT token
# 2015-03-27 Cornelius Kölbel, cornelius@privacyidea.org
# Add sub command for policies
# 2014-12-15 Cornelius Kölbel, info@privacyidea.org
# Initial creation
#
# (c) Cornelius Kölbel
# Info: http://www.privacyidea.org
#
# This code is free software; you can redistribute it and/or
# modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
# License as published by the Free Software Foundation; either
# version 3 of the License, or any later version.
#
# This code is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU AFFERO GENERAL PUBLIC LICENSE for more details.
#
# You should have received a copy of the GNU Affero General Public
# License along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# ./manage.py db init
# ./manage.py db migrate
# ./manage.py createdb
#
import os
import sys
import datetime
from datetime import timedelta
import re
from subprocess import call, Popen, PIPE
from getpass import getpass
import gnupg
import yaml
import flask
from privacyidea.lib.security.default import DefaultSecurityModule
from privacyidea.lib.crypto import geturandom
from privacyidea.lib.auth import (create_db_admin, list_db_admin,
delete_db_admin)
from privacyidea.lib.policy import (delete_policy, enable_policy,
PolicyClass, set_policy)
from privacyidea.lib.caconnector import (get_caconnector_list,
get_caconnector_class,
get_caconnector_object,
save_caconnector)
from privacyidea.app import create_app
from privacyidea.lib.auth import ROLE
from flask_script import Manager, Command
from privacyidea.app import db
from flask_migrate import MigrateCommand
# Wee need to import something, so that the models will be created.
from privacyidea.models import Audit
from sqlalchemy import create_engine, desc, MetaData
from sqlalchemy.orm import sessionmaker
from privacyidea.lib.auditmodules.sqlaudit import LogEntry
from Crypto.PublicKey import RSA
import jwt
import ast
SILENT = True
MYSQL_DIALECTS = ["mysql", "pymysql", "mysql+pymysql"]
app = create_app(config_name='production', silent=SILENT)
manager = Manager(app)
admin_manager = Manager(usage='Create new administrators or modify existing '
'ones.')
backup_manager = Manager(usage='Create database backup and restore')
realm_manager = Manager(usage='Create new realms')
resolver_manager = Manager(usage='Create new resolver')
policy_manager = Manager(usage='Manage policies')
api_manager = Manager(usage="Manage API keys")
ca_manager = Manager(usage="Manage Certificate Authorities")
manager.add_command('db', MigrateCommand)
manager.add_command('admin', admin_manager)
manager.add_command('backup', backup_manager)
manager.add_command('realm', realm_manager)
manager.add_command('resolver', resolver_manager)
manager.add_command('policy', policy_manager)
manager.add_command('api', api_manager)
manager.add_command('ca', ca_manager)
@ca_manager.command
def list(verbose=False):
l = get_caconnector_list()
for ca in l:
print("{ca!s} (type {typ!s})".format(ca=ca.get("connectorname"),
typ=ca.get("type")))
if verbose:
for k, v in ca.get("data").iteritems():
print("\t{key!s:20}: {value!s}".format(key=k, value=v))
@ca_manager.command
def create_crl(ca, force=False):
ca_obj = get_caconnector_object(ca)
r = ca_obj.create_crl(check_validity=not force)
if not r:
print("The CRL was not created.")
else:
print("The CRL {name!s} was created.".format(name=r))
@ca_manager.option('name', help='The name of the new CA')
@ca_manager.option('-t', '--type', help='The type of the new CA. The default '
'is "local"')
def create(name, type):
"""
Create a new CA connector. In case of the "localca" also the directory
structure, the openssl.cnf and the key pair is created.
"""
ca = get_caconnector_object(name)
if ca:
print("A CA connector with the name '{0!s}' already exists.".format(
name))
sys.exit(1)
if not type:
type = "local"
print("Creating CA connector of type {0!s}.".format(type))
ca_class = get_caconnector_class(type)
ca_params = ca_class.create_ca(name)
r = save_caconnector(ca_params)
if r:
print("Saved CA Connector with ID {0!s}.".format(r))
else:
print("Error saving CA connector.")
@admin_manager.command
def add(username, email=None, password=None):
"""
Register a new administrator in the database.
"""
db.create_all()
if not password:
password = getpass()
password2 = getpass(prompt='Confirm: ')
if password != password2:
import sys
sys.exit('Error: passwords do not match.')
create_db_admin(app, username, email, password)
print('Admin {0} was registered successfully.'.format(username))
@admin_manager.command
def list():
"""
List all administrators.
"""
list_db_admin()
@admin_manager.command
def delete(username):
"""
Delete an existing administrator.
"""
delete_db_admin(username)
@admin_manager.command
def change(username, email=None, password_prompt=False):
"""
Change the email address or the password of an existing administrator.
"""
if password_prompt:
password = getpass()
password2 = getpass(prompt='Confirm: ')
if password != password2:
import sys
sys.exit('Error: passwords do not match.')
else:
password = None
create_db_admin(app, username, email, password)
@backup_manager.command
def create(directory="/var/lib/privacyidea/backup/",
conf_dir="/etc/privacyidea/",
enckey=False):
"""
Create a new backup of the database and the configuration. The default
does not include the encryption key. Use the 'enckey' option to also
backup the encryption key. Then you should make sure, that the backups
are stored safely.
"""
CONF_DIR = conf_dir
DATE = datetime.datetime.now().strftime("%Y%m%d-%H%M")
BASE_NAME = "privacyidea-backup"
call(["mkdir", "-p", directory])
# set correct owner, if possible
if os.geteuid() == 0:
encfile_stat = os.stat(app.config.get("PI_ENCFILE"))
os.chown(directory, encfile_stat.st_uid, encfile_stat.st_gid)
sqlfile = "%s/dbdump-%s.sql" % (directory, DATE)
backup_file = "%s/%s-%s.tgz" % (directory, BASE_NAME, DATE)
sqluri = app.config.get("SQLALCHEMY_DATABASE_URI")
sqltype = sqluri.split(":")[0]
if sqltype == "sqlite":
productive_file = sqluri[len("sqlite:///"):]
print("Backup SQLite %s" % productive_file)
sqlfile = "%s/db-%s.sqlite" % (directory, DATE)
call(["cp", productive_file, sqlfile])
elif sqltype in MYSQL_DIALECTS:
m = re.match(".*mysql://(.*):(.*)@(.*)/(.*)", sqluri)
username = m.groups()[0]
password = m.groups()[1]
datahost = m.groups()[2]
database = m.groups()[3]
defaults_file = "{0!s}/mysql.cnf".format(conf_dir)
_write_mysql_defaults(defaults_file, username, password)
call("mysqldump --defaults-file=%s -h %s %s > %s" % (
defaults_file,
datahost,
database, sqlfile), shell=True)
else:
print("unsupported SQL syntax: %s" % sqltype)
sys.exit(2)
enc_file = app.config.get("PI_ENCFILE")
backup_call = ["tar", "-zcf",
backup_file, CONF_DIR, sqlfile]
if not enckey:
# Exclude enckey from backup
backup_call.append("--exclude={0!s}".format(enc_file))
call(backup_call)
os.unlink(sqlfile)
os.chmod(backup_file, 0600)
def _write_mysql_defaults(filename, username, password):
"""
Write the defaults_file for mysql commands
:param filename: THe name of the file
:param username: The username to connect to the database
:param password: The password to connect to the database
:return:
"""
f = open(filename, "w")
f.write("""[client]
user={0!s}
password={1!s}""".format(username, password))
f.close()
os.chmod(filename, 0600)
# set correct owner, if possible
if os.geteuid() == 0:
directory_stat = os.stat(os.path.dirname(filename))
os.chown(filename, directory_stat.st_uid, directory_stat.st_gid)
@backup_manager.command
def restore(backup_file):
"""
Restore a previously made backup. You need to specify the tgz file.
"""
sqluri = None
config_file = None
sqlfile = None
enckey_contained = False
p = Popen(["tar", "-ztf", backup_file], stdout=PIPE)
std_out, err_out = p.communicate()
for line in std_out.split("\n"):
if re.search("/pi.cfg$", line):
config_file = "/{0!s}".format(line.strip())
elif re.search("\.sql", line):
sqlfile = "/{0!s}".format(line.strip())
elif re.search("/enckey", line):
enckey_contained = True
if not config_file:
raise Exception("Missing config file pi.cfg in backup file.")
if not sqlfile:
raise Exception("Missing database dump in backup file.")
if enckey_contained:
print("Also restoring encryption key 'enckey'")
else:
print("NO FILE 'enckey' CONTAINED! BE SURE TO RESTORE THE ENCRYPTION "
"KEY MANUALLY!")
print("Restoring to {0!s} with data from {1!s}".format(config_file,
sqlfile))
call(["tar", "-zxf", backup_file, "-C", "/"])
print(60*"=")
f = open(config_file, "r")
# Determine the SQLAlchemy URI
for line in f:
if re.search("^SQLALCHEMY_DATABASE_URI", line):
key, value = line.split("=", 1)
# Strip whitespaces, and ' "
sqluri = value.strip().strip("'").strip('"')
f.close()
if sqluri is None:
print("No SQLALCHEMY_DATABASE_URI found in {0!s}".format(config_file))
sys.exit(2)
sqltype = sqluri.split(":")[0]
if sqltype == "sqlite":
productive_file = sqluri[len("sqlite:///"):]
print("Restore SQLite %s" % productive_file)
call(["cp", sqlfile, productive_file])
os.unlink(sqlfile)
elif sqltype in MYSQL_DIALECTS:
m = re.match(".*mysql://(.*):(.*)@(.*)/(.*)", sqluri)
username = m.groups()[0]
password = m.groups()[1]
datahost = m.groups()[2]
database = m.groups()[3]
defaults_file = "/etc/privacyidea/mysql.cnf"
_write_mysql_defaults(defaults_file, username, password)
# Rewriting database
print("Restoring database.")
call("mysql --defaults-file=%s -h %s %s < %s" % (defaults_file,
datahost,
database,
sqlfile), shell=True)
os.unlink(sqlfile)
else:
print("unsupported SQL syntax: %s" % sqltype)
sys.exit(2)
@manager.command
def test():
"""
Run all nosetests.
"""
call(['nosetests', '-v',
'--with-coverage', '--cover-package=privacyidea', '--cover-branches',
'--cover-erase', '--cover-html', '--cover-html-dir=cover'])
@manager.command
def encrypt_enckey(encfile):
"""
You will be asked for a password and the encryption key in the specified
file will be encrypted with an AES key derived from your password.
The encryption key in the file is a 96 bit binary key.
The password based encrypted encryption key is a hex combination of an IV
and the encrypted data.
The result can be piped to a new enckey file.
"""
password = getpass()
password2 = getpass(prompt='Confirm: ')
if password != password2:
import sys
sys.exit('Error: passwords do not match.')
f = open(encfile)
enckey = f.read()
f.close()
res = DefaultSecurityModule.password_encrypt(enckey, password)
print(res)
@manager.command
def create_enckey():
"""
If the key of the given configuration does not exist, it will be created
"""
print
filename = app.config.get("PI_ENCFILE")
if os.path.isfile(filename):
print("The file \n\t%s\nalready exist. We do not overwrite it!" %
filename)
sys.exit(1)
f = open(filename, "w")
f.write(DefaultSecurityModule.random(96))
f.close()
print("Encryption key written to %s" % filename)
os.chmod(filename, 0400)
print("The file permission of %s was set to 400!" % filename)
print("Please ensure, that it is owned by the right user. ")
@manager.command
def create_pgp_keys(keysize=2048, force=False):
"""
Generate PGP keys to allow encrypted token import.
"""
GPG_HOME = app.config.get("PI_GNUPG_HOME", "/etc/privacyidea/gpg")
gpg = gnupg.GPG(gnupghome=GPG_HOME)
keys = gpg.list_keys(True)
if len(keys) and not force:
print("There are already private keys. If you want to "
"generate a new private key, use the parameter --force.")
print(keys)
sys.exit(1)
input_data = gpg.gen_key_input(key_type="RSA", key_length=keysize,
name_real="privacyIDEA Server",
name_comment="Import")
gpg.gen_key(input_data)
@manager.command
def create_audit_keys(keysize=2048):
"""
Create the RSA signing keys for the audit log.
You may specify an additional keysize.
The default keysize is 2048 bit.
"""
filename = app.config.get("PI_AUDIT_KEY_PRIVATE")
if os.path.isfile(filename):
print("The file \n\t%s\nalready exist. We do not overwrite it!" %
filename)
sys.exit(1)
new_key = RSA.generate(keysize, e=65537)
public_key = new_key.publickey().exportKey("PEM")
private_key = new_key.exportKey("PEM")
f = open(filename, "w")
f.write(private_key)
f.close()
f = open(app.config.get("PI_AUDIT_KEY_PUBLIC"), "w")
f.write(public_key)
f.close()
print("Signing keys written to %s and %s" %
(filename, app.config.get("PI_AUDIT_KEY_PUBLIC")))
os.chmod(filename, 0400)
print("The file permission of %s was set to 400!" % filename)
print("Please ensure, that it is owned by the right user.")
@manager.command
def createdb():
"""
Initially create the tables in the database. The database must exist.
(SQLite database will be created)
"""
print(db)
db.create_all()
db.session.commit()
@manager.command
def dropdb(dropit=None):
"""
This drops all the privacyIDEA database tables (except audit table).
Use with caution! All data will be lost!
For safety reason you need to pass
--dropit==yes
Otherwise the command will not drop anything.
"""
if dropit == "yes":
print("Dropping all database tables!")
db.drop_all()
else:
print("Not dropping anything!")
@manager.command
def validate(user, password, realm=None, client=None):
"""
Do an authentication request
"""
from privacyidea.lib.user import get_user_from_param
from privacyidea.lib.token import check_user_pass
try:
user = get_user_from_param({"user": user, "realm": realm})
auth, details = check_user_pass(user, password)
print("RESULT=%s" % auth)
print("DETAILS=%s" % details)
except Exception as exx:
print("RESULT=Error")
print("ERROR=%s" % exx)
class CommandOutsideRequestContext(Command):
"""
In contrast to flask_script's `Command`, this command class does
not push a request context before running the command.
"""
def __call__(self, app=None, *args, **kwargs):
return self.run(*args, **kwargs)
def profile(length=30, profile_dir=None):
"""
Start the application in profiling mode.
"""
from werkzeug.contrib.profiler import ProfilerMiddleware
if flask.has_request_context():
print("WARNING: The app may behave unrealistically during profiling.")
app.wsgi_app = ProfilerMiddleware(app.wsgi_app, restrictions=[length],
profile_dir=profile_dir)
app.run()
# If flask_script's `Command` is used here instead of `CommandOutsideRequestContext`,
# the request context will persist over requests. Thus, `g` is not
# cleared properly between requests, which makes privacyIDEA behave unrealistically.
try:
manager.add_command('profile', CommandOutsideRequestContext(profile))
except TypeError:
# Apparently, we are using Flask-Script 0.6.7, which does not support the API used above.
# So we just add the `profile` command without using `CommandOutsideRequestContext`.
profile = manager.command(profile)
@manager.option('--highwatermark', '--hw', help="If entries exceed this value, "
"old entries are deleted.")
@manager.option('--lowwatermark', '--lw', help="Keep this number of entries.")
@manager.option('--age', help="Delete audit entries older than these number "
"of days.")
@manager.option('--config', help="Read config from the specified yaml file.")
@manager.option('--dryrun', help="Do not actually delete, only show "
"what would be done.", action="store_true")
def rotate_audit(highwatermark=10000, lowwatermark=5000, age=0, config=None,
dryrun=False):
"""
Clean the SQL audit log.
You can either clean the audit log based on the number of entries of
based on the age of the entries.
Cleaning based on number of entries:
If more than 'highwatermark' entries are in the audit log old entries
will be deleted, so that 'lowwatermark' entries remain.
Cleaning based on age:
Entries older than the specified number of days are deleted.
Cleaning based on config file:
You can clean different type of entries with different ages or watermark.
See the documentation for the format of the config file
"""
metadata = MetaData()
highwatermark = int(highwatermark or 10000)
lowwatermark = int(lowwatermark or 5000)
default_module = "privacyidea.lib.auditmodules.sqlaudit"
token_db_uri = app.config.get("SQLALCHEMY_DATABASE_URI")
audit_db_uri = app.config.get("PI_AUDIT_SQL_URI", token_db_uri)
audit_module = app.config.get("PI_AUDIT_MODULE", default_module)
actions = []
if audit_module != default_module:
raise Exception("We only rotate SQL audit module. You are using %s" %
audit_module)
if config:
print("Cleaning up with config file.")
elif age:
age = int(age)
print("Cleaning up with age: %s. %s" % (age, audit_db_uri))
else:
print("Cleaning up with high: %s, low: %s. %s" % (highwatermark,
lowwatermark,
audit_db_uri))
engine = create_engine(audit_db_uri)
# create a configured "Session" class
session = sessionmaker(bind=engine)()
# create a Session
metadata.create_all(engine)
if config:
with open(config, 'r') as f:
Config = yaml.load(f)
auditlogs = session.query(LogEntry).all()
delete_list = []
for log in auditlogs:
print("investigating log entry {0!s}".format(log.id))
for rule in Config:
age = int(rule.get("rotate"))
rotate_date = datetime.datetime.now() - datetime.timedelta(days=age)
match = False
for key in rule.keys():
if key not in ["rotate"]:
search_value = rule.get(key)
print(" + searching for {0!r} in {1!s}".format(search_value, getattr(LogEntry, key)))
audit_value = getattr(log, key) or ""
m = re.search(search_value, audit_value)
if m:
# it matches!
print(" + -- found {0!r}".format(audit_value))
match = True
else:
# It does not match, we continue to next rule
print(" + NO MATCH - SKIPPING rest of conditions!")
match = False
break
if match:
if log.date < rotate_date:
# Delete it!
print(" + Deleting {0!s} due to rule {1!s}".format(log.id, rule))
# Delete it
delete_list.append(log.id)
# skip all other rules and go to the next log entry
break
if dryrun:
print("If you only would let me I would clean up {0!s} entries!".format(len(delete_list)))
else:
print("Cleaning up {0!s} entries.".format(len(delete_list)))
engine.execute(LogEntry.__table__.delete().where(LogEntry.id.in_(delete_list)))
session.commit()
elif age:
now = datetime.datetime.now() - datetime.timedelta(days=age)
print("Deleting entries older than {0!s}".format(now))
sqlquery = session.query(LogEntry.date).filter(LogEntry.date < now)
if dryrun:
r = len(sqlquery.all())
else:
r = session.query(LogEntry.date).filter(LogEntry.date < now).delete()
session.commit()
print("{0!s} entries deleted.".format(r))
else:
count = session.query(LogEntry.id).count()
for l in session.query(LogEntry.id).order_by(desc(LogEntry.id)).limit(1):
last_id = l[0]
print("The log audit log has %i entries, the last one is %i" % (count,
last_id))
# deleting old entries
if count > highwatermark:
print("More than %i entries, deleting..." % highwatermark)
cut_id = last_id - lowwatermark
# delete all entries less than cut_id
print("Deleting entries smaller than %i" % cut_id)
sqlquery = session.query(LogEntry.id).filter(LogEntry.id < cut_id)
if dryrun:
r = len(sqlquery.all())
else:
r = session.query(LogEntry.id).filter(LogEntry.id < cut_id).delete()
session.commit()
print("{0!s} entries deleted.".format(r))
@resolver_manager.command
def create(name, rtype, filename):
"""
Create a new resolver with name and type (ldapresolver, sqlresolver).
Read the necessary resolver parameters from the filename. The file should
contain a python dictionary.
:param name: The name of the resolver
:param rtype: The type of the resolver like ldapresolver or sqlresolver
:param filename: The name of the config file.
:return:
"""
from privacyidea.lib.resolver import save_resolver
f = open(filename, 'r')
contents = f.read()
f.close()
params = ast.literal_eval(contents)
params["resolver"] = name
params["type"] = rtype
save_resolver(params)
@resolver_manager.command
def create_internal(name):
"""
This creates a new internal, editable sqlresolver. The users will be
stored in the token database in a table called 'users_<name>'. You can then
add this resolver to a new real using the command 'pi-manage.py realm'.
"""
from privacyidea.lib.resolver import save_resolver
sqluri = app.config.get("SQLALCHEMY_DATABASE_URI")
sqlelements = sqluri.split("/")
# mysql://user:password@localhost/pi
# sqlite:////home/cornelius/src/privacyidea/data.sqlite
sql_driver = sqlelements[0][:-1]
user_pw_host = sqlelements[2]
database = "/".join(sqlelements[3:])
username = ""
password = ""
# determine host and user
hostparts = user_pw_host.split("@")
if len(hostparts) > 2:
print("Invalid database URI: %s" % sqluri)
sys.exit(2)
elif len(hostparts) == 1:
host = hostparts[0] or "/"
elif len(hostparts) == 2:
host = hostparts[1] or "/"
# split hostname and password
userparts = hostparts[0].split(":")
if len(userparts) == 2:
username = userparts[0]
password = userparts[1]
elif len(userparts) == 1:
username = userparts[0]
else:
print("Invalid username and password in database URI: %s" % sqluri)
sys.exit(3)
# now we can create the resolver
params = {'resolver': name,
'type': "sqlresolver",
'Server': host,
'Driver': sql_driver,
'User': username,
'Password': password,
'Database': database,
'Table': 'users_' + name,
'Limit': '500',
'Editable': '1',
'Map': '{"userid": "id", "username": "username", '
'"email":"email", "password": "password", '
'"phone":"phone", "mobile":"mobile", "surname":"surname", '
'"givenname":"givenname", "description": "description"}'}
save_resolver(params)
# Now we create the database table
from sqlalchemy import create_engine
from sqlalchemy import Table, MetaData, Column
from sqlalchemy import Integer, String
engine = create_engine(sqluri)
metadata = MetaData()
Table('users_%s' % name,
metadata,
Column('id', Integer, primary_key=True),
Column('username', String(40), unique=True),
Column('email', String(80)),
Column('password', String(255)),
Column('phone', String(40)),
Column('mobile', String(40)),
Column('surname', String(40)),
Column('givenname', String(40)),
Column('description', String(255)))
metadata.create_all(engine)
@resolver_manager.command
def list():
"""
list the available resolvers and the type
"""
from privacyidea.lib.resolver import get_resolver_list
resolver_list = get_resolver_list()
for name, resolver in resolver_list.iteritems():
print("%16s - (%s)" % (name, resolver.get("type")))
@realm_manager.command
def list():
"""
list the available realms
"""
from privacyidea.lib.realm import get_realms
realm_list = get_realms()
for name, realm_data in realm_list.iteritems():
resolvernames = [x.get("name") for x in realm_data.get("resolver")]
print("%16s: %s" % (name, resolvernames))
@realm_manager.command
def create(name, resolver):
"""
Create a new realm.
This will create a new realm with the given resolver.
*restriction*: The new realm will only contain one resolver!
:return:
"""
from privacyidea.lib.realm import set_realm
set_realm(name, [resolver])
# Policy interface
@policy_manager.command
def list():
"""
list the policies
"""
P = PolicyClass()
policies = P.get_policies()
print("Active \t Name \t Scope")
print(40*"=")
for policy in policies:
print("%s \t %s \t %s" % (policy.get("active"), policy.get("name"),
policy.get("scope")))
@policy_manager.command
def enable(name):
"""
enable a policy by name
"""
r = enable_policy(name)
print(r)
@policy_manager.command
def disable(name):
"""
disable a policy by name
"""
r = enable_policy(name, False)
print(r)
@policy_manager.command
def delete(name):
"""
delete a policy by name
"""
r = delete_policy(name)
print(r)
@policy_manager.command
def p_export(filename=None):
"""
Export all policies to a file as a JSON format, list of dictionaries.
If the filename is omitted, the policies are written to stdout.
"""
import pprint
pp = pprint.PrettyPrinter(indent=4)
P = PolicyClass()
policies = P.get_policies()
pol_str = pp.pformat(policies)
if filename:
f = open(filename, 'w')
f.write(pol_str)
f.close()
else:
print(pol_str)
@policy_manager.command
def p_import(filename, cleanup=False):
"""
Import the policies from a file.
If 'cleanup' is specified the existing policies are deleted before the
policies from the file are imported.
"""
if cleanup:
print("Cleanup old policies.")
P = PolicyClass()
policies = P.get_policies()
for policy in policies:
name = policy.get("name")
r = delete_policy(name)
print("Deleted policy {0!s} with result {1!s}".format(name, r))
f = open(filename, 'r')
contents = f.read()
f.close()
policies = ast.literal_eval(contents)
for policy in policies:
name = policy.get("name")
r = set_policy(name,
action=policy.get("action"),
active=policy.get("active", True),
adminrealm=policy.get("adminrealm"),
check_all_resolvers=policy.get(
"check_all_resolvers", False),
client=policy.get("client"),
#condition=policy.get("condition"),
realm=policy.get("realm"),
resolver=policy.get("resolver"),
scope=policy.get("scope"),
time=policy.get("time"),
user=policy.get("user"))
print("Added policy {0!s} with result {1!s}".format(name, r))
@policy_manager.command
def create(name, scope, action, filename=None):
"""
create a new policy. 'FILENAME' must contain a dictionary and its content
takes precedence over CLI parameters.
I.e. if you are specifying a FILENAME,
the parameters name, scope and action need to be specified, but are ignored.
Note: This will only create one policy per file.
"""
if filename:
try:
f = open(filename, 'r')
contents = f.read()
f.close()
params = ast.literal_eval(contents)
if params.get("scope") and params.get("scope")!=scope:
print("Found scope '{0!s}' in file, will use that instead of "
"'{1!s}'.".format(params.get("scope"), scope))
else:
print("scope not defined in file, will use the cli value "
"{0!s}.".format(scope))
params["scope"] = scope
if params.get("action") and params.get("action")!=action:
print("Found action in file: '{0!s}', will use that instead "
"of: '{1!s}'.".format(params.get("action"), action))
else:
print("action not defined in file, will use the cli value "
"{0!s}.".format(action))
params["action"]=action
r = set_policy(name, scope=params.get("scope"),
action=params.get("action"),
realm=params.get("realm"),
resolver=params.get("resolver"),
user=params.get("user"),
time=params.get("time"),
client=params.get("client"),
active=params.get("active", True),
adminrealm=params.get("adminrealm"),
check_all_resolvers=params.get(
"check_all_resolvers", False))
return r
except Exception:
print("Unexpected error: {0!s}".format(sys.exc_info()[1]))
else:
r = set_policy(name, scope, action)
return r
@api_manager.option('-r', '--role',
help="The role of the API key can either be "
"'admin' or 'validate' to access the admin "
"API or the validate API.",
default=ROLE.ADMIN)
@api_manager.option('-d', '--days',
help='The number of days the access token should be valid.'
' Defaults to 365.',
default=365)
@api_manager.option('-R', '--realm',
help='The realm of the admin. Defaults to "API"',
default="API")
@api_manager.option('-u', '--username',
help='The username of the admin.')
def createtoken(role, days, realm, username):
"""
Create an API authentication token
for administrative or validate use.
Possible roles are "admin" or "validate".
"""
if role not in ["admin", "validate"]:
print("ERROR: The role must be 'admin' or 'validate'!")
sys.exit(1)
username = username or geturandom(hex=True)
secret = app.config.get("SECRET_KEY")
authtype = "API"
validity = timedelta(days=int(days))
token = jwt.encode({"username": username,
"realm": realm,
"nonce": geturandom(hex=True),
"role": role,
"authtype": authtype,
"exp": datetime.datetime.utcnow() + validity,
"rights": "TODO"},
secret)
print("Username: {0!s}".format(username))
print("Realm: {0!s}".format(realm))
print("Role: {0!s}".format(role))
print("Validity: {0!s} days".format(days))
print("Auth-Token: {0!s}".format(token))
if __name__ == '__main__':
# We add one blank line, to separate the messages from the initialization
print("""
_ _______ _______
___ ____(_) _____ _______ __/ _/ _ \/ __/ _ |
/ _ \/ __/ / |/ / _ `/ __/ // // // // / _// __ |
/ .__/_/ /_/|___/\_,_/\__/\_, /___/____/___/_/ |_|