Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
OSUG
RESIF
ws-eidaauth
Commits
47ca7b04
Commit
47ca7b04
authored
Jul 10, 2020
by
Jonathan Schaeffer
Browse files
Logging through Flask app
parent
78825940
Changes
1
Hide whitespace changes
Inline
Side-by-side
eidawsauth/eidawsauth.py
View file @
47ca7b04
...
...
@@ -12,23 +12,20 @@ from config import Configurator
from
version
import
__version__
logging
.
basicConfig
(
format
=
'%(asctime)s %(levelname)s %(message)s'
)
logger
=
logging
.
getLogger
(
__name__
)
application
=
Flask
(
__name__
)
if
os
.
getenv
(
'RUNMODE'
)
==
'production'
:
logger
.
setLevel
(
logging
.
INFO
)
application
.
logger
.
setLevel
(
logging
.
INFO
)
else
:
logger
.
setLevel
(
logging
.
DEBUG
)
application
.
logger
.
setLevel
(
logging
.
DEBUG
)
# Loglevel can be overrinden by LOGLEVEL env var :
if
os
.
getenv
(
'DEBUG'
)
==
'true'
:
logger
.
setLevel
(
logging
.
DEBUG
)
application
.
logger
.
setLevel
(
logging
.
DEBUG
)
else
:
logger
.
setLevel
(
logging
.
INFO
)
application
=
Flask
(
__name__
)
application
.
logger
.
setLevel
(
logging
.
INFO
)
application
.
config
.
from_object
(
Configurator
)
def
wsshash
(
login
,
password
):
"""
Compute a hash suitable for the IRIS wss stack.
...
...
@@ -45,10 +42,10 @@ def verify_token_signature(data, gpg_homedir):
def
parse_input_data
(
data
):
# Then we get the token :
token
=
re
.
search
(
r
'{(?P<token>.*)}'
,
str
(
data
)).
groupdict
()[
'token'
]
logg
ing
.
debug
(
token
)
application
.
logg
er
.
debug
(
token
)
d
=
dict
([
i
for
i
in
kv
.
split
(
':'
,
1
)]
for
kv
in
token
.
replace
(
'"'
,
''
).
replace
(
' '
,
''
).
split
(
','
))
logg
ing
.
debug
(
"Transformed to dictionary : %s"
,
d
)
application
.
logg
er
.
debug
(
"Transformed to dictionary : %s"
,
d
)
return
d
def
register_login
(
login
,
password
):
...
...
@@ -65,9 +62,9 @@ def register_login(login, password):
user
=
application
.
config
[
'RESIFAUTH_PGUSER'
],
password
=
application
.
config
[
'RESIFAUTH_PGPASSWORD'
])
cur
=
conn
.
cursor
()
logg
ing
.
debug
(
"Connected to users database"
)
application
.
logg
er
.
debug
(
"Connected to users database"
)
except
Exception
as
e
:
logg
ing
.
error
(
"Unable to connect to database %s as %s@%s:%s"
,
application
.
config
[
'RESIFAUTH_PGDATABASE'
],
application
.
logg
er
.
error
(
"Unable to connect to database %s as %s@%s:%s"
,
application
.
config
[
'RESIFAUTH_PGDATABASE'
],
application
.
config
[
'RESIFAUTH_PGUSER'
],
application
.
config
[
'RESIFAUTH_PGHOST'
],
application
.
config
[
'RESIFAUTH_PGPORT'
])
...
...
@@ -99,9 +96,9 @@ def register_privileges(login, fdsn_refs):
user
=
application
.
config
[
'RESIFINV_PGUSER'
],
password
=
application
.
config
[
'RESIFINV_PGPASSWORD'
])
cur
=
conn
.
cursor
()
logg
ing
.
debug
(
"Connected to privileges database"
)
application
.
logg
er
.
debug
(
"Connected to privileges database"
)
except
Exception
as
e
:
logg
ing
.
error
(
"Unable to connect to database %s as %s@%s:%s"
,
application
.
config
[
'RESIFINV_PGDATABASE'
],
application
.
logg
er
.
error
(
"Unable to connect to database %s as %s@%s:%s"
,
application
.
config
[
'RESIFINV_PGDATABASE'
],
application
.
config
[
'RESIFINV_PGUSER'
],
application
.
config
[
'RESIFINV_PGHOST'
],
application
.
config
[
'RESIFINV_PGPORT'
])
...
...
@@ -111,19 +108,19 @@ def register_privileges(login, fdsn_refs):
for
ref
in
fdsn_refs
:
ref
[
'login'
]
=
login
ref
[
'expires_at'
]
=
datetime
.
datetime
.
now
()
+
datetime
.
timedelta
(
days
=
1
)
logg
ing
.
info
(
ref
)
application
.
logg
er
.
info
(
ref
)
sql_request
=
"select network_id from networks where start_year=%(startyear)s and end_year=%(endyear)s and network=%(networkcode)s"
try
:
cur
.
execute
(
sql_request
,
ref
)
except
psycopg2
.
Error
as
e
:
logg
ing
.
error
(
e
.
pgerror
)
application
.
logg
er
.
error
(
e
.
pgerror
)
else
:
if
cur
.
rowcount
!=
1
:
logg
ing
.
info
(
cur
.
mogrify
(
sql_request
,
ref
))
logg
ing
.
error
(
"%d networks found for %s"
,
cur
.
rowcount
,
ref
)
application
.
logg
er
.
info
(
cur
.
mogrify
(
sql_request
,
ref
))
application
.
logg
er
.
error
(
"%d networks found for %s"
,
cur
.
rowcount
,
ref
)
raise
NameError
(
f
"
{
cur
.
rowcount
}
networks found for
{
ref
}
"
)
ref
[
'networkid'
]
=
cur
.
fetchone
()[
0
]
logg
ing
.
info
(
"Inserting tupple in %s.eida_temp_users: %s"
,
application
.
config
[
'
PRIVILEGEDBNAM
E'
],
ref
)
application
.
logg
er
.
info
(
"Inserting tupple in %s.eida_temp_users: %s"
,
application
.
config
[
'
RESIFINV_PGDATABAS
E'
],
ref
)
cur
.
execute
(
"""
insert into eida_temp_users (network_id, network, start_year, end_year, name, expires_at) values (%(networkid)s, %(networkcode)s, %(startyear)s, %(endyear)s, %(login)s, %(expires_at)s);
"""
,
ref
)
...
...
@@ -140,7 +137,7 @@ def cleanup():
"""
Clean old temporary logins and passwords in both databases.
"""
logg
ing
.
info
(
"Cleaning up expired temporary accounts"
)
application
.
logg
er
.
info
(
"Cleaning up expired temporary accounts"
)
rows_deleted
=
0
try
:
conn
=
psycopg2
.
connect
(
dbname
=
application
.
config
[
'RESIFAUTH_PGDATABASE'
],
...
...
@@ -149,14 +146,14 @@ def cleanup():
user
=
application
.
config
[
'RESIFAUTH_PGUSER'
],
password
=
application
.
config
[
'RESIFAUTH_PGPASSWORD'
])
cur
=
conn
.
cursor
()
logg
ing
.
debug
(
"Connected to users database"
)
application
.
logg
er
.
debug
(
"Connected to users database"
)
cur
.
execute
(
"delete from credentials where expires_at < now();"
)
cur
.
execute
(
"delete from users where expires_at < now();"
)
rows_deleted
=
cur
.
rowcount
conn
.
commit
()
conn
.
close
()
except
psycopg2
.
Error
as
e
:
logg
ing
.
error
(
e
.
pgerror
)
application
.
logg
er
.
error
(
e
.
pgerror
)
raise
e
try
:
...
...
@@ -166,13 +163,13 @@ def cleanup():
user
=
application
.
config
[
'RESIFINV_PGUSER'
],
password
=
application
.
config
[
'RESIFINV_PGPASSWORD'
])
cur
=
conn
.
cursor
()
logg
ing
.
debug
(
"Connected to privlieges database"
)
logg
ing
.
debug
(
"Deleting from privileges database"
)
application
.
logg
er
.
debug
(
"Connected to privlieges database"
)
application
.
logg
er
.
debug
(
"Deleting from privileges database"
)
cur
.
execute
(
"delete from eida_temp_users where expires_at < now();"
)
conn
.
commit
()
conn
.
close
()
except
Exception
as
e
:
logg
ing
.
error
(
e
.
pgerror
)
application
.
logg
er
.
error
(
e
.
pgerror
)
raise
e
return
Response
(
"Deleted %d expired accounts."
%
(
rows_deleted
),
status
=
200
)
...
...
@@ -180,23 +177,23 @@ def cleanup():
def
auth
():
login
=
''
password
=
''
logg
ing
.
debug
(
request
.
mimetype
)
application
.
logg
er
.
debug
(
request
.
mimetype
)
data
=
request
.
get_data
()
logg
ing
.
debug
(
"Data: %s"
,
data
)
application
.
logg
er
.
debug
(
"Data: %s"
,
data
)
try
:
verify_token_signature
(
data
,
application
.
config
[
'GNUPG_HOMEDIR'
])
tokendict
=
parse_input_data
(
data
)
logg
ing
.
info
(
"Token signature OK: %s"
%
str
(
tokendict
))
application
.
logg
er
.
info
(
"Token signature OK: %s"
%
str
(
tokendict
))
except
ValueError
as
e
:
logg
ing
.
info
(
"Token signature could not be checked: %s"
%
str
(
data
))
application
.
logg
er
.
info
(
"Token signature could not be checked: %s"
%
str
(
data
))
return
Response
(
str
(
e
),
status
=
415
)
# Now we have a dictionary corresponding to the token's content.
# Verify validity
expiration_ts
=
datetime
.
datetime
.
strptime
(
tokendict
[
'valid_until'
],
'%Y-%m-%dT%H:%M:%S.%fZ'
)
if
(
expiration_ts
-
datetime
.
datetime
.
now
()).
total_seconds
()
<
0
:
logg
ing
.
info
(
"Token is expired"
)
application
.
logg
er
.
info
(
"Token is expired"
)
return
Response
(
'Token is expired. Please generate a new one at https://geofon.gfz-potsdam.de/eas/'
,
status
=
400
)
logg
ing
.
info
(
"Token is valid"
)
application
.
logg
er
.
info
(
"Token is valid"
)
# Compute a random login and password
login
=
''
.
join
(
random
.
choices
(
string
.
ascii_uppercase
+
string
.
digits
,
k
=
14
))
...
...
@@ -209,13 +206,13 @@ def auth():
# Check membership and get FDSN references
fdsn_memberships
=
[]
for
em
in
tokendict
[
'memberof'
].
split
(
';'
):
logg
ing
.
debug
(
"EPOS membership: "
+
em
)
application
.
logg
er
.
debug
(
"EPOS membership: "
+
em
)
if
em
in
application
.
config
[
'EPOS_FDSN_MAP'
]:
logg
ing
.
debug
(
" ... is in epos fdsn map"
)
application
.
logg
er
.
debug
(
" ... is in epos fdsn map"
)
fdsn_memberships
.
append
(
application
.
config
[
'EPOS_FDSN_MAP'
][
em
])
if
len
(
fdsn_memberships
)
>
0
:
logg
ing
.
debug
(
"FDSN memberships: %s"
%
(
fdsn_memberships
))
application
.
logg
er
.
debug
(
"FDSN memberships: %s"
%
(
fdsn_memberships
))
try
:
register_privileges
(
login
,
fdsn_memberships
)
except
NameError
as
n
:
...
...
@@ -224,5 +221,5 @@ def auth():
return
"%s:%s"
%
(
login
,
password
)
if
__name__
==
"__main__"
:
logg
ing
.
info
(
"Running in %s mode"
%
(
application
.
config
[
'ENVIRONMENT'
]))
application
.
logg
er
.
info
(
"Running in %s mode"
%
(
application
.
config
[
'ENVIRONMENT'
]))
application
.
run
(
host
=
'0.0.0.0'
)
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment