Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
spacelock
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
External wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
stustanet
spacelock
Commits
0940e1a6
Commit
0940e1a6
authored
Jul 11, 2019
by
Jonas Jelten
Browse files
Options
Downloads
Patches
Plain Diff
database: require username for various actions
parent
fd3da0c2
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
database/setup.sql
+36
-12
36 additions, 12 deletions
database/setup.sql
with
36 additions
and
12 deletions
database/setup.sql
+
36
−
12
View file @
0940e1a6
...
@@ -85,7 +85,7 @@ end $$;
...
@@ -85,7 +85,7 @@ end $$;
-- permission verification is done here:
-- permission verification is done here:
create
or
replace
function
can_access
(
permission_key
text
,
what
access_class
)
create
or
replace
function
can_access
(
user_name
text
,
permission_key
text
,
what
access_class
)
returns
bigint
as
$$
returns
bigint
as
$$
declare
entry
permissions
%
ROWTYPE
;
declare
entry
permissions
%
ROWTYPE
;
declare
now_time
timestamp
with
time
zone
;
declare
now_time
timestamp
with
time
zone
;
...
@@ -95,6 +95,7 @@ begin
...
@@ -95,6 +95,7 @@ begin
select
*
into
entry
select
*
into
entry
from
permissions
from
permissions
where
where
name
=
user_name
and
key
=
crypt
(
permission_key
,
key
)
and
key
=
crypt
(
permission_key
,
key
)
and
active
is
true
and
active
is
true
and
valid_from
<=
now_time
and
valid_from
<=
now_time
and
...
@@ -117,7 +118,7 @@ security definer;
...
@@ -117,7 +118,7 @@ security definer;
-- this function will be executed in setuid-mode!
-- this function will be executed in setuid-mode!
-- to grant access, use:
-- to grant access, use:
-- grant execute on function gen_token to some_insecure_user;
-- grant execute on function gen_token to some_insecure_user;
create
or
replace
function
gen_token
(
permission_key
text
)
create
or
replace
function
gen_token
(
user_name
text
,
permission_key
text
)
returns
text
as
$$
returns
text
as
$$
declare
entry_id
bigint
;
declare
entry_id
bigint
;
declare
entry
permissions
%
ROWTYPE
;
declare
entry
permissions
%
ROWTYPE
;
...
@@ -128,7 +129,7 @@ declare token_duration int;
...
@@ -128,7 +129,7 @@ declare token_duration int;
begin
begin
select
now
()
into
now_time
;
select
now
()
into
now_time
;
select
can_access
(
permission_key
,
'token'
)
into
entry_id
;
select
can_access
(
user_name
,
permission_key
,
'token'
)
into
entry_id
;
select
*
into
entry
from
permissions
where
id
=
entry_id
;
select
*
into
entry
from
permissions
where
id
=
entry_id
;
if
entry
is
NULL
then
if
entry
is
NULL
then
...
@@ -195,17 +196,18 @@ security definer;
...
@@ -195,17 +196,18 @@ security definer;
-- change a user's validity times
-- change a user's validity times
create
or
replace
function
user_mod
(
create
or
replace
function
user_mod
(
admin_name
text
,
admin_token
text
,
admin_token
text
,
target_name
text
,
target_name
text
,
_valid_from
timestamp
with
time
zone
,
_valid_from
timestamp
with
time
zone
,
_valid_to
timestamp
with
time
zone
,
_valid_to
timestamp
with
time
zone
,
_token_validity_time
int
,
_token_validity_time
int
,
_usermod
boolean
default
false
enable
_usermod
boolean
default
false
)
returns
text
as
$$
)
returns
text
as
$$
declare
entry_id
bigint
;
declare
entry_id
bigint
;
declare
entry
permissions
%
ROWTYPE
;
declare
entry
permissions
%
ROWTYPE
;
begin
begin
select
can_access
(
admin_token
,
'usermod'
)
into
entry_id
;
select
can_access
(
admin_name
,
admin_token
,
'usermod'
)
into
entry_id
;
select
*
into
entry
from
permissions
where
id
=
entry_id
;
select
*
into
entry
from
permissions
where
id
=
entry_id
;
if
entry
is
null
then
if
entry
is
null
then
...
@@ -217,7 +219,7 @@ begin
...
@@ -217,7 +219,7 @@ begin
valid_from
=
_valid_from
,
valid_from
=
_valid_from
,
valid_to
=
_valid_to
,
valid_to
=
_valid_to
,
token_validity_time
=
_token_validity_time
,
token_validity_time
=
_token_validity_time
,
usermod
=
_usermod
usermod
=
enable
_usermod
where
where
name
=
target_name
;
name
=
target_name
;
...
@@ -234,6 +236,7 @@ security definer;
...
@@ -234,6 +236,7 @@ security definer;
-- set the user active flag
-- set the user active flag
create
or
replace
function
user_set_active
(
create
or
replace
function
user_set_active
(
admin_name
text
,
admin_token
text
,
admin_token
text
,
target_name
text
,
target_name
text
,
_active
boolean
_active
boolean
...
@@ -242,7 +245,7 @@ declare entry_id bigint;
...
@@ -242,7 +245,7 @@ declare entry_id bigint;
declare
entry
permissions
%
ROWTYPE
;
declare
entry
permissions
%
ROWTYPE
;
declare
previous_state
bool
;
declare
previous_state
bool
;
begin
begin
select
can_access
(
admin_token
,
'usermod'
)
into
entry_id
;
select
can_access
(
admin_name
,
admin_token
,
'usermod'
)
into
entry_id
;
select
*
into
entry
from
permissions
where
id
=
entry_id
;
select
*
into
entry
from
permissions
where
id
=
entry_id
;
if
entry
is
null
then
if
entry
is
null
then
...
@@ -276,19 +279,34 @@ security definer;
...
@@ -276,19 +279,34 @@ security definer;
-- grant access to a newly created user
-- grant access to a newly created user
create
or
replace
function
user_grant_access
(
create
or
replace
function
user_grant_access
(
admin_name
text
,
admin_token
text
,
admin_token
text
,
target_name
text
,
target_name
text
,
valid_from
timestamp
with
time
zone
default
now
(),
valid_from
timestamp
with
time
zone
default
now
(),
valid_to
timestamp
with
time
zone
default
(
now
()
+
interval
'31'
day
),
valid_to
timestamp
with
time
zone
default
(
now
()
+
interval
'31'
day
),
token_validity_time
int
default
86400
-- 24 hours
token_validity_time
int
default
86400
-- 24 hours
)
returns
text
as
$$
)
returns
text
as
$$
declare
entry_id
bigint
;
begin
begin
select
can_access
(
admin_name
,
admin_token
,
'usermod'
)
into
entry_id
;
if
entry_id
is
null
then
return
null
;
end
if
;
if
user_mod
(
admin_token
,
target_name
,
valid_from
,
valid_to
,
token_validity_time
)
is
null
then
if
user_mod
(
admin_token
,
target_name
,
valid_from
,
valid_to
,
token_validity_time
)
is
null
then
return
null
;
return
null
;
end
if
;
end
if
;
if
user_enable
(
admin_token
,
target_name
)
is
null
then
if
user_enable
(
admin_name
,
admin_token
,
target_name
)
is
null
then
return
null
;
return
null
;
end
if
;
end
if
;
update
permissions
set
granted_by
=
entry_id
where
name
=
target_name
;
return
'ok'
;
return
'ok'
;
end
;
end
;
$$
language
plpgsql
$$
language
plpgsql
...
@@ -325,6 +343,7 @@ $$ language plpgsql;
...
@@ -325,6 +343,7 @@ $$ language plpgsql;
-- update a user password, requires the old password
-- update a user password, requires the old password
create
or
replace
function
user_new_password
(
create
or
replace
function
user_new_password
(
user_name
text
,
permission_key
text
permission_key
text
)
returns
text
as
$$
)
returns
text
as
$$
declare
entry
permissions
%
ROWTYPE
;
declare
entry
permissions
%
ROWTYPE
;
...
@@ -333,6 +352,7 @@ begin
...
@@ -333,6 +352,7 @@ begin
select
*
into
entry
select
*
into
entry
from
permissions
from
permissions
where
where
name
=
user_name
and
key
=
crypt
(
permission_key
,
key
);
key
=
crypt
(
permission_key
,
key
);
if
entry
is
null
then
if
entry
is
null
then
...
@@ -360,11 +380,12 @@ security definer;
...
@@ -360,11 +380,12 @@ security definer;
-- disable an existing user
-- disable an existing user
create
or
replace
function
user_disable
(
create
or
replace
function
user_disable
(
admin_name
text
,
admin_token
text
,
admin_token
text
,
target_name
text
target_name
text
)
returns
text
as
$$
)
returns
text
as
$$
begin
begin
return
user_set_active
(
admin_token
,
target_name
,
false
);
return
user_set_active
(
admin_name
,
admin_token
,
target_name
,
false
);
end
;
end
;
$$
language
plpgsql
$$
language
plpgsql
security
definer
;
security
definer
;
...
@@ -372,11 +393,12 @@ security definer;
...
@@ -372,11 +393,12 @@ security definer;
-- enable an existing user
-- enable an existing user
create
or
replace
function
user_enable
(
create
or
replace
function
user_enable
(
admin_name
text
,
admin_token
text
,
admin_token
text
,
target_name
text
target_name
text
)
returns
text
as
$$
)
returns
text
as
$$
begin
begin
return
user_set_active
(
admin_token
,
target_name
,
true
);
return
user_set_active
(
admin_name
,
admin_token
,
target_name
,
true
);
end
;
end
;
$$
language
plpgsql
$$
language
plpgsql
security
definer
;
security
definer
;
...
@@ -384,13 +406,14 @@ security definer;
...
@@ -384,13 +406,14 @@ security definer;
-- remove a user's account
-- remove a user's account
create
or
replace
function
user_del
(
create
or
replace
function
user_del
(
admin_name
text
,
admin_token
text
,
admin_token
text
,
target_name
text
target_name
text
)
returns
text
as
$$
)
returns
text
as
$$
declare
entry_id
bigint
;
declare
entry_id
bigint
;
declare
entry
permissions
%
ROWTYPE
;
declare
entry
permissions
%
ROWTYPE
;
begin
begin
select
can_access
(
admin_token
,
'usermod'
)
into
entry_id
;
select
can_access
(
admin_name
,
admin_token
,
'usermod'
)
into
entry_id
;
select
*
into
entry
from
permissions
where
id
=
entry_id
;
select
*
into
entry
from
permissions
where
id
=
entry_id
;
if
entry
is
null
then
if
entry
is
null
then
...
@@ -413,6 +436,7 @@ security definer;
...
@@ -413,6 +436,7 @@ security definer;
-- list all users
-- list all users
create
or
replace
function
user_list
(
create
or
replace
function
user_list
(
user_name
text
,
permission_key
text
permission_key
text
)
)
returns
table
(
returns
table
(
...
@@ -428,7 +452,7 @@ returns table (
...
@@ -428,7 +452,7 @@ returns table (
declare
entry_id
bigint
;
declare
entry_id
bigint
;
declare
entry
permissions
%
ROWTYPE
;
declare
entry
permissions
%
ROWTYPE
;
begin
begin
select
can_access
(
permission_key
,
'usermod'
)
into
entry_id
;
select
can_access
(
user_name
,
permission_key
,
'usermod'
)
into
entry_id
;
select
*
into
entry
from
permissions
where
id
=
entry_id
;
select
*
into
entry
from
permissions
where
id
=
entry_id
;
if
entry
is
null
then
if
entry
is
null
then
...
...
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
sign in
to comment