-
Notifications
You must be signed in to change notification settings - Fork 21
FM-2236 Add with_grant_option for user permissions #71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,44 +13,54 @@ | |
# [database] | ||
# The databaser you would like the permission managed on. | ||
# | ||
# [permission] | ||
# The permission you would like managed. i.e. 'SELECT', 'INSERT', 'UPDATE', 'DELETE' | ||
# [permissions] | ||
# An array of permissions you would like managed. i.e. ['SELECT', 'INSERT', 'UPDATE', 'DELETE'] | ||
# | ||
# [state] | ||
# The state you would like the permission in. Accepts 'GRANT', 'DENY', 'REVOKE' Please note that REVOKE equates to absent and will default to database and system level permissions. | ||
# | ||
# [with_grant_option] | ||
# Whether to give the user the option to grant this permission to other users, accepts true or false, defaults to false | ||
# | ||
# [instance] | ||
# The name of the instance where the user and database exists. Defaults to 'MSSQLSERVER' | ||
# | ||
## | ||
define sqlserver::user::permission ( | ||
$user, | ||
$database, | ||
$permission = $title, | ||
$state = 'GRANT', | ||
$instance = 'MSSQLSERVER', | ||
$permissions, | ||
$state = 'GRANT', | ||
$with_grant_option = false, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
$instance = 'MSSQLSERVER', | ||
){ | ||
sqlserver_validate_instance_name($instance) | ||
|
||
## Validate Permissions | ||
$_permission = upcase($permission) | ||
sqlserver_validate_range($_permission, 4, 128, 'Permission must be between 4 and 128 characters') | ||
validate_re($_permission, '^([A-Z]|\s)+$','Permissions must be alphabetic only') | ||
sqlserver_validate_range($permissions, 4, 128, 'Permission must be between 4 and 128 characters') | ||
validate_array($permissions) | ||
|
||
## Validate state | ||
$_state = upcase($state) | ||
validate_re($_state,'^(GRANT|REVOKE|DENY)$',"State can only be of 'GRANT', 'REVOKE' or 'DENY' you passed ${state}") | ||
|
||
validate_bool($with_grant_option) | ||
if $with_grant_option and $_state != 'GRANT' { | ||
fail("Can not use with_grant_option and state ${_state}, must be 'GRANT'") | ||
} | ||
|
||
sqlserver_validate_range($database, 1, 128, 'Database must be between 1 and 128 characters') | ||
|
||
sqlserver_validate_range($user, 1, 128, 'User must be between 1 and 128 characters') | ||
|
||
if $with_grant_option { | ||
$grant_option = "-WITH_GRANT_OPTION" | ||
} | ||
sqlserver_tsql{ | ||
"user-permissions-${instance}-${database}-${user}-${$_state}-${_permission}": | ||
"user-permissions-${instance}-${database}-${user}-${_state}${grant_option}": | ||
instance => $instance, | ||
command => template("sqlserver/create/user_permission.sql.erb"), | ||
onlyif => template('sqlserver/query/user_permission_exists.sql.erb'), | ||
command => template("sqlserver/create/user/permission.sql.erb"), | ||
onlyif => template('sqlserver/query/user/permission_exists.sql.erb'), | ||
require => Sqlserver::Config[$instance], | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
USE [<%= @database %>]; | ||
DECLARE @perm_state varchar(250), @error_msg varchar(250), @permission varchar(250); | ||
<% @permissions.each do |permission| | ||
permission.upcase! | ||
%> | ||
SET @permission = '<%= permission %>' | ||
BEGIN | ||
<% if @with_grant_option == false %> | ||
IF 'GRANT_WITH_GRANT_OPTION' = <%= scope.function_template(['sqlserver/snippets/user/permission/get_perm_state.sql.erb']) %> | ||
REVOKE GRANT OPTION FOR <%= permission %> TO [<%= @user %>] CASCADE; | ||
<% end %> | ||
<%= @_state %> <%= permission %> TO [<%= @user %>]<% if @with_grant_option == true %> WITH GRANT OPTION<% end %>; | ||
END | ||
BEGIN | ||
<%= scope.function_template(['sqlserver/snippets/user/permission/exists.sql.erb']) %> | ||
END | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So this is going to set all perms, then check each individually after and fail on the first one that doesn't exist? I'd almost prefer for it to be atomic. Set one permission, check one permission, fail there. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or one loop. |
||
<% end %> |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
USE [<%= @database %>]; | ||
|
||
DECLARE @perm_state varchar(250), @error_msg varchar(250), @permission varchar(250); | ||
<% @permissions.each do |permission| | ||
permission.upcase! %> | ||
SET @permission = '<%= permission %>' | ||
<%= scope.function_template(['sqlserver/snippets/user/permission/exists.sql.erb']) %> | ||
<% end %> |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
SET @perm_state = <%= scope.function_template(['sqlserver/snippets/user/permission/get_perm_state.sql.erb']) %>; | ||
SET @error_msg = 'EXPECTED user [<%= @user %>] to have permission [' + @permission + '] with <%= @_state %> but got ' + @perm_state; | ||
IF @perm_state != '<% if @with_grant_option == true %>GRANT_WITH_GRANT_OPTION<% else %><%= @_state %><% end %>' | ||
THROW 51000, @error_msg, 10 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
ISNULL( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like this refactor should be it's own commit as well IMHO. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The file renames? I had to work to follow what actually changed there, but it would be helpful to rename the files in one commit and then adjust the values in another. It will make it easier to follow the changes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was a thing that happened durnning development and having a, we may want to not have a growing folder of snippets/ and instead put them into logical folders. |
||
(SELECT perm.state_desc FROM sys.database_principals princ | ||
JOIN sys.database_permissions perm ON perm.grantee_principal_id = princ.principal_id | ||
WHERE princ.type in ('U','S','G') AND name = '<%= @user %>' AND permission_name = @permission), | ||
'REVOKE') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Permissions needs updated to show that it is an array.