Security: Delegation of Authority

Published Mar 16 2021 11:03 AM 2,588 Views
Microsoft

(part 3 of my series of articles on security principles in Microsoft SQL Servers & Databases)

 

Security concept: Delegation of Authority

To be precise from the beginning: Delegation is a process or concept rather than a principle. But it is a particularly useful practice to keep in mind when designing any security concept and it is strongly connected to the discussed security principles like the Principle of Least Privilege and Separation of Duties as you will find out.

So, what is it about? Delegation (of authority) is the process to pass on certain permissions to other users, often temporarily, without raising their overall privileges to the same level as the delegating account.

There are two slightly different approaches. Delegation can be done either..

1) at the Identity level: allowing Identity A to be used utilized by identity B. Here B assumes A’s identity and powers/permissions. Examples in Operating Systems are the “Runas” and “sudo”-commands in Windows, respectively Unix/Linux.

Or

2) it can be done at the Authorization level: essentially by passing on a specified set of permissions via a form of Role Based Access Control (RBAC*) to another identity.

When you look at two-tier application architectures and the authentication flows, you will probably notice that delegation is somewhat a common pattern.

 

 

OAuth and on-behalf-of token for authentication

 

For example, OAuth is an open standard for access delegation which is also utilized in Azure Active Directory (AAD). Using the OAuth 2.0 On-Behalf-Of flow (OBO) applications can invoke a service API, which in turn needs to call another service API - like SQL Server.
Instead of using the invoking application’s identity, the delegated user’s identity (and permissions) is propagated through the request chain. To do this, the second-tier application authenticates to the resource for example a SQL database, with its own token, a so called “on-behalf-of” token that originated from the first-tier application..
This is an example of delegation at identity level.

 

Delegation of Authority in the SQL realm

 

In SQL Server there are multiple options available to implement Delegation.

 

Azure AD User’s creation

 

One scenario where delegation is being used under the covers is the following:

Assume, an AAD principal, such as the AAD admin account for Azure SQL, wants to create an AAD user in the database (statement: CREATE USER <AADPrincipalName> FROM EXTERNAL PROVIDER).
In this case, the managed service identity (MSI) which is assigned to Azure SQL server is required. Using this MSI, Azure SQL server sends the information about the AAD user which the AAD admin account wants to create as a user inside SQL to AAD graph (in future: MS graph) for verification. Therefore, the MSI (and not the AAD admin account) requires the proper permission in Azure AD (such as the “Directory Readers” role).

 

GRANT WITH GRANT OPTION

 

This one may be a less widely known possibility in SQL: it is possible to Grant a permission to Users and to allow them to pass on this permission. This is what the “WITH GRANT OPTION” of the GRANT statement is for.
In the below example, a User Shakti has been granted the various privileges, INSERT, UPDATE, DELETE and SELECT a Schema named “Sales”. On top of that, by using the WITH GRANT OPTION, she is allowed to pass on those permissions.
This is demoed by impersonating her account using the EXECUTE AS clause (btw: this is also an example of using delegation at the identity level) and now “being” Shakti we can grant permissions to anyone else, Jiao in this case.

 

AndreasWolter_0-1615915638377.png

 

EXECUTE AS and privilege bracketing for temporarily delegating permissions

 

A powerful and commonly used technique is the possibility to run stored procedures under a separate user account for just the task at hand, no matter who the original caller is, and by that means use the privileges of the impersonated account for the runtime of the stored procedure.
This is typically used to delegate tasks that otherwise require high privileges in SQL Server and by that helps to maintain the Principle of Least Privilege.

Strictly speaking, the delegation happens when access to the stored procedure is granted. The use of a stored procedure is technically a separate concept, referred to as “privilege bracketing” which is somewhat in the same space as just-in-time privileges (JIT). JIT is yet another technique which allows the use of certain elevated permissions for a certain period of time (aka “time bound”) only.
Privileged Identity Management in Azure offers these capabilities: What is Privileged Identity Management? - Azure AD | Microsoft Docs

See the below example: ALTER is the minimal (“least”) permission necessary to Update Statistics on tables. But instead of granting ALTER on each table or the whole database, Jiao will only get permission to run this stored procedure which in turn runs under elevated permissions that are assumed for the runtime of the procedure only.

 

AndreasWolter_1-1615915638396.png

 

Note
If you are auditing User’s activities (which in general you should, but this is another topic coming up), you need to be aware that since Impersonation has the effect that the “current user” is not the actual User who is acting, looking only at Server or Database Principal ID’s does not give the right picture. Luckily, SQL Auditing by design always captures the session_server_principal_name. This contains the name of the principal who is connected to the instance of SQL Server originally, no matter how many levels of impersonations are done. This is the same like using the SQL Function ORIGINAL_LOGIN(), which you should use when implementing custom Logging solutions.

Signing Modules for temporarily delegating permissions

 

There is an alternative option when using modules for delegation to EXECUTE AS: signing the module. In SQL Server, modules (such as stored procedures, functions and triggers) can be signed with an asymmetric key or a certificate (technically just another form of asymmetric key).
The trick is that this key or certificate can be mapped to a database user. And when the module is executed, and the signature has been verified, the module will inherit the permissions of the mapped user. And here is the difference to the EXECUTE AS-clause: the permissions of the certificate-mapped user will be added to the permissions of the original caller, not replace them. This is because the execution context actually does not change. Which leads to the second big difference: All the built-in functions that return login and user names return the name of the caller, not the certificate user name.
In the resources you will find a couple of links with various examples.

Here is a diagram of how this can be used in a simple example:

 

AndreasWolter_2-1615917506255.png

 

In this example, Ben alone only has SELECT on the table Orders, but does not have access to the OrderDetails-tables. Instead, the intention is, that he can only access this table by using the specifically prepared stored procedure ProcAccountforInternalCalc.
Only at runtime of this procedure, the permissions of the original caller, Ben, are extended with the permissions of another user, ProcAccountforInternalCalc, which has been specifically created for this use-case: to grant the SELECT permission on the table OrderDetails only when the stored procedure sel_OrderDetails_with_margin is being used. For that, this user is mapped to a certificate, Cert
_ProcAccountforInternalCalc
, which has been used to sign the stored procedure.
Now, anyone who has permission to execute this procedure, will inherit the additional permissions of ProcAccountforInternalCalc and can then see the data from the table – using the business logic from the procedure only.

 

Note
Module signing should only ever be used to GRANT permissions, and not be used as a mechanism to enforce DENY, let alone REVOKE permissions.

 

Most SQL Server environments I have seen already use these concepts one way or the other. Delegation is an immensely powerful and useful technique: As you may realize from the last two examples, those techniques can also help ensuring the Principle of Least Privilege is adhered to. And it also can help implement Separation of Duties, which is the reason to talk about it specifically within the context of the general Security Principles.

The T-SQL code-examples are available as a download below this article.

 

Happy delegating

 

Andreas

 

Thank you to my Reviewer:

Mirek Sztajno, Senior Program Manager in SQL Security and expert in Authentication

 

Resources

 

1 Comment

Not sure if this doc help me or not.

%3CLINGO-SUB%20id%3D%22lingo-sub-2227941%22%20slang%3D%22en-US%22%3ERe%3A%20Security%3A%20Delegation%20of%20Authority%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2227941%22%20slang%3D%22en-US%22%3E%3CP%3ENot%20sure%20if%20this%20doc%20help%20me%20or%20not.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2214708%22%20slang%3D%22en-US%22%3ESecurity%3A%20Delegation%20of%20Authority%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2214708%22%20slang%3D%22en-US%22%3E%3CP%3E%3CEM%3E(part%203%20of%20my%20series%20of%20articles%20on%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fazure-sql%2Fintroduction-into-security-principles-in-the-context-of-database%2Fba-p%2F2067363%22%20target%3D%22_blank%22%3Esecurity%20principles%20in%20Microsoft%20SQL%20Servers%20%26amp%3B%20Databases%3C%2FA%3E)%3C%2FEM%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%227%22%3ESecurity%20concept%3A%20Delegation%20of%20Authority%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3ETo%20be%20precise%20from%20the%20beginning%3A%20Delegation%20is%20a%20process%20or%20concept%20rather%20than%20a%20principle.%20But%20it%20is%20a%20particularly%20useful%20practice%20to%20keep%20in%20mind%20when%20designing%20any%20security%20concept%20and%20it%20is%20strongly%20connected%20to%20the%20discussed%20security%20principles%20like%20the%20Principle%20of%20Least%20Privilege%20and%20Separation%20of%20Duties%20as%20you%20will%20find%20out.%3C%2FP%3E%0A%3CP%3ESo%2C%20what%20is%20it%20about%3F%20%3CU%3EDelegation%20(of%20authority)%20is%20the%20process%20to%20pass%20on%20certain%20permissions%20to%20other%20users%2C%20often%20temporarily%2C%20without%20raising%20their%20overall%20privileges%20to%20the%20same%20level%20as%20the%20delegating%20account.%3C%2FU%3E%3C%2FP%3E%0A%3CP%3EThere%20are%20two%20slightly%20different%20approaches.%20Delegation%20can%20be%20done%20either..%3C%2FP%3E%0A%3CP%3E1)%20at%20the%20%3CSTRONG%3EIdentity%20level%3A%3C%2FSTRONG%3E%20allowing%20Identity%20A%20to%20be%20used%20utilized%20by%20identity%20B.%20Here%20B%20assumes%20A%E2%80%99s%20identity%20and%20powers%2Fpermissions.%20Examples%20in%20Operating%20Systems%20are%20the%20%E2%80%9CRunas%E2%80%9D%20and%20%E2%80%9Csudo%E2%80%9D-commands%20in%20Windows%2C%20respectively%20Unix%2FLinux.%3C%2FP%3E%0A%3CP%3EOr%3C%2FP%3E%0A%3CP%3E2)%20it%20can%20be%20done%20at%20the%20%3CSTRONG%3EAuthorization%20level%3C%2FSTRONG%3E%3A%20essentially%20by%20passing%20on%20a%20specified%20set%20of%20permissions%20via%20a%20form%20of%20Role%20Based%20Access%20Control%20(RBAC*)%20to%20another%20identity.%3C%2FP%3E%0A%3CP%3EWhen%20you%20look%20at%20two-tier%20application%20architectures%20and%20the%20authentication%20flows%2C%20you%20will%20probably%20notice%20that%20delegation%20is%20somewhat%20a%20common%20pattern.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId-1998731805%22%20id%3D%22toc-hId-1998731894%22%3E%26nbsp%3B%3C%2FH3%3E%0A%3CH3%20id%3D%22toc-hId-191277342%22%20id%3D%22toc-hId-191277431%22%3E%3CFONT%20size%3D%226%22%3EOAuth%20and%20on-behalf-of%20token%20for%20authentication%3C%2FFONT%3E%3C%2FH3%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFor%20example%2C%20OAuth%20is%20an%20open%20standard%20for%20access%20delegation%20which%20is%20also%20utilized%20in%20Azure%20Active%20Directory%20(AAD).%20Using%20the%20OAuth%202.0%20%3CSTRONG%3E%3CEM%3EOn-Behalf-Of%3C%2FEM%3E%3C%2FSTRONG%3E%20flow%20(OBO)%20applications%20can%20invoke%20a%20service%20API%2C%20which%20in%20turn%20needs%20to%20call%20another%20service%20API%20-%20like%20SQL%20Server.%3CBR%20%2F%3EInstead%20of%20using%20the%20invoking%20application%E2%80%99s%20identity%2C%20the%20delegated%20user%E2%80%99s%20identity%20(and%20permissions)%20is%20propagated%20through%20the%20request%20chain.%20To%20do%20this%2C%20the%20second-tier%20application%20authenticates%20to%20the%20resource%20for%20example%20a%20SQL%20database%2C%20with%20its%20own%20token%2C%20a%20so%20called%20%E2%80%9Con-behalf-of%E2%80%9D%20token%20that%20originated%20from%20the%20first-tier%20application..%20%3CBR%20%2F%3EThis%20is%20an%20example%20of%20delegation%20at%20identity%20level.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-180774238%22%20id%3D%22toc-hId-180774327%22%3E%3CFONT%20size%3D%226%22%3EDelegation%20of%20Authority%20in%20the%20SQL%20realm%3C%2FFONT%3E%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20SQL%20Server%20there%20are%20multiple%20options%20available%20to%20implement%20Delegation.%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId-871335712%22%20id%3D%22toc-hId-871335801%22%3E%26nbsp%3B%3C%2FH3%3E%0A%3CH3%20id%3D%22toc-hId--936118751%22%20id%3D%22toc-hId--936118662%22%3E%3CFONT%20size%3D%225%22%3EAzure%20AD%20User%E2%80%99s%20creation%3C%2FFONT%3E%3C%2FH3%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOne%20scenario%20where%20delegation%20is%20being%20used%20under%20the%20covers%20is%20the%20following%3A%3C%2FP%3E%0A%3CP%3EAssume%2C%20an%20AAD%20principal%2C%20such%20as%20the%20AAD%20admin%20account%20for%20Azure%20SQL%2C%20wants%20to%20create%20an%20AAD%20user%20in%20the%20database%20(statement%3A%20CREATE%20USER%20%3CAADPRINCIPALNAME%3E%20FROM%20EXTERNAL%20PROVIDER).%3CBR%20%2F%3EIn%20this%20case%2C%20the%20managed%20service%20identity%20(MSI)%20which%20is%20assigned%20to%20Azure%20SQL%20server%20is%20required.%20Using%20this%20MSI%2C%20Azure%20SQL%20server%20sends%20the%20information%20about%20the%20AAD%20user%20which%20the%20AAD%20admin%20account%20wants%20to%20create%20as%20a%20user%20inside%20SQL%20to%20AAD%20graph%20(in%20future%3A%20MS%20graph)%20for%20verification.%20Therefore%2C%20the%20MSI%20(and%20not%20the%20AAD%20admin%20account)%20requires%20the%20proper%20permission%20in%20Azure%20AD%20(such%20as%20the%20%E2%80%9CDirectory%20Readers%E2%80%9D%20role).%3C%2FAADPRINCIPALNAME%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId-1551394082%22%20id%3D%22toc-hId-1551394171%22%3E%3CFONT%20size%3D%225%22%3EGRANT%20WITH%20GRANT%20OPTION%3C%2FFONT%3E%3C%2FH3%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20one%20may%20be%20a%20less%20widely%20known%20possibility%20in%20SQL%3A%20it%20is%20possible%20to%20Grant%20a%20permission%20to%20Users%20and%20to%20allow%20them%20to%20pass%20on%20this%20permission.%20This%20is%20what%20the%20%E2%80%9CWITH%20GRANT%20OPTION%E2%80%9D%20of%20the%20GRANT%20statement%20is%20for.%3CBR%20%2F%3EIn%20the%20below%20example%2C%20a%20User%20%3CEM%3EShakti%3C%2FEM%3E%20has%20been%20granted%20the%20various%20privileges%2C%20INSERT%2C%20UPDATE%2C%20DELETE%20and%20SELECT%20a%20Schema%20named%20%E2%80%9CSales%E2%80%9D.%20On%20top%20of%20that%2C%20by%20using%20the%20WITH%20GRANT%20OPTION%2C%20she%20is%20allowed%20to%20pass%20on%20those%20permissions.%3CBR%20%2F%3EThis%20is%20demoed%20by%20impersonating%20her%20account%20using%20the%20EXECUTE%20AS%20clause%20(btw%3A%20this%20is%20also%20an%20example%20of%20using%20delegation%20at%20the%20identity%20level)%20and%20now%20%E2%80%9Cbeing%E2%80%9D%20%3CEM%3EShakti%3C%2FEM%3E%20we%20can%20grant%20permissions%20to%20anyone%20else%2C%20%3CEM%3EJiao%3C%2FEM%3E%20in%20this%20case.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22AndreasWolter_0-1615915638377.png%22%20style%3D%22width%3A%20450px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F264457iAAD45319A09DF2E9%2Fimage-dimensions%2F450x342%3Fv%3Dv2%22%20width%3D%22450%22%20height%3D%22342%22%20role%3D%22button%22%20title%3D%22AndreasWolter_0-1615915638377.png%22%20alt%3D%22AndreasWolter_0-1615915638377.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId--256060381%22%20id%3D%22toc-hId--256060292%22%3E%3CFONT%20size%3D%225%22%3EEXECUTE%20AS%20and%20privilege%20bracketing%20for%20temporarily%20delegating%20permissions%3C%2FFONT%3E%3C%2FH3%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EA%20powerful%20and%20commonly%20used%20technique%20is%20the%20possibility%20to%20run%20stored%20procedures%20under%20a%20separate%20user%20account%20for%20just%20the%20task%20at%20hand%2C%20no%20matter%20who%20the%20original%20caller%20is%2C%20and%20by%20that%20means%20use%20the%20privileges%20of%20the%20impersonated%20account%20for%20the%20runtime%20of%20the%20stored%20procedure.%3CBR%20%2F%3EThis%20is%20typically%20used%20to%20delegate%20tasks%20that%20otherwise%20require%20high%20privileges%20in%20SQL%20Server%20and%20by%20that%20helps%20to%20maintain%20the%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fazure-sql%2Fsecurity-the-principle-of-least-privilege-polp%2Fba-p%2F2067390%22%20target%3D%22_blank%22%3EPrinciple%20of%20Least%20Privilege%3C%2FA%3E.%3C%2FP%3E%0A%3CP%3EStrictly%20speaking%2C%20the%20delegation%20happens%20when%20access%20to%20the%20stored%20procedure%20is%20granted.%20The%20use%20of%20a%20stored%20procedure%20is%20technically%20a%20separate%20concept%2C%20referred%20to%20as%20%E2%80%9C%3CSTRONG%3Eprivilege%20bracketing%3C%2FSTRONG%3E%E2%80%9D%20which%20is%20somewhat%20in%20the%20same%20space%20as%20%3CSTRONG%3Ejust-in-time%20privileges%3C%2FSTRONG%3E%20(JIT).%20JIT%20is%20yet%20another%20technique%20which%20allows%20the%20use%20of%20certain%20elevated%20permissions%20for%20a%20certain%20period%20of%20time%20(aka%20%E2%80%9Ctime%20bound%E2%80%9D)%20only.%3CBR%20%2F%3EPrivileged%20Identity%20Management%20in%20Azure%20offers%20these%20capabilities%3A%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Factive-directory%2Fprivileged-identity-management%2Fpim-configure%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EWhat%20is%20Privileged%20Identity%20Management%3F%20-%20Azure%20AD%20%7C%20Microsoft%20Docs%3C%2FA%3E%3C%2FP%3E%0A%3CP%3ESee%20the%20below%20example%3A%20ALTER%20is%20the%20minimal%20(%E2%80%9Cleast%E2%80%9D)%20permission%20necessary%20to%20Update%20Statistics%20on%20tables.%20But%20instead%20of%20granting%20ALTER%20on%20each%20table%20or%20the%20whole%20database%2C%20%3CEM%3EJiao%3C%2FEM%3E%20will%20only%20get%20permission%20to%20run%20this%20stored%20procedure%20which%20in%20turn%20runs%20under%20elevated%20permissions%20that%20are%20assumed%20for%20the%20runtime%20of%20the%20procedure%20only.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22AndreasWolter_1-1615915638396.png%22%20style%3D%22width%3A%20450px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F264458i61D1DA4B19129899%2Fimage-dimensions%2F450x342%3Fv%3Dv2%22%20width%3D%22450%22%20height%3D%22342%22%20role%3D%22button%22%20title%3D%22AndreasWolter_1-1615915638396.png%22%20alt%3D%22AndreasWolter_1-1615915638396.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-indent-padding-left-30px%22%3E%3CEM%3ENote%3C%2FEM%3E%3CBR%20%2F%3EIf%20you%20are%20auditing%20User%E2%80%99s%20activities%20(which%20in%20general%20you%20should%2C%20but%20this%20is%20another%20topic%20coming%20up)%2C%20you%20need%20to%20be%20aware%20that%20since%20Impersonation%20has%20the%20effect%20that%20the%20%E2%80%9Ccurrent%20user%E2%80%9D%20is%20not%20the%20actual%20User%20who%20is%20acting%2C%20looking%20only%20at%20Server%20or%20Database%20Principal%20ID%E2%80%99s%20does%20not%20give%20the%20right%20picture.%20Luckily%2C%20SQL%20Auditing%20by%20design%20always%20captures%20the%20%3CEM%3Esession_server_principal_name%3C%2FEM%3E.%20This%20contains%20the%20name%20of%20the%20principal%20who%20is%20connected%20to%20the%20instance%20of%20SQL%20Server%20originally%2C%20no%20matter%20how%20many%20levels%20of%20impersonations%20are%20done.%20This%20is%20the%20same%20like%20using%20the%20SQL%20Function%20%3CEM%3EORIGINAL_LOGIN()%2C%3C%2FEM%3E%20which%20you%20should%20use%20when%20implementing%20custom%20Logging%20solutions.%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId--2063514844%22%20id%3D%22toc-hId--2063514755%22%3E%3CFONT%20size%3D%225%22%3ESigning%20Modules%20for%20temporarily%20delegating%20permissions%3C%2FFONT%3E%3C%2FH3%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThere%20is%20an%20alternative%20option%20when%20using%20modules%20for%20delegation%20to%20EXECUTE%20AS%3A%20signing%20the%20module.%20In%20SQL%20Server%2C%20modules%20(such%20as%20stored%20procedures%2C%20functions%20and%20triggers)%20can%20be%20signed%20with%20an%20asymmetric%20key%20or%20a%20certificate%20(technically%20just%20another%20form%20of%20asymmetric%20key).%3CBR%20%2F%3EThe%20trick%20is%20that%20this%20key%20or%20certificate%20can%20be%20mapped%20to%20a%20database%20user.%20And%20when%20the%20module%20is%20executed%2C%20and%20the%20signature%20has%20been%20verified%2C%20the%20module%20will%20inherit%20the%20permissions%20of%20the%20mapped%20user.%20And%20here%20is%20the%20difference%20to%20the%20EXECUTE%20AS-clause%3A%20the%20permissions%20of%20the%20certificate-mapped%20user%20will%20be%20%3CU%3Eadded%3C%2FU%3E%20to%20the%20permissions%20of%20the%20original%20caller%2C%20not%20replace%20them.%20This%20is%20because%20the%20execution%20context%20actually%20does%20not%20change.%20Which%20leads%20to%20the%20second%20big%20difference%3A%20All%20the%20built-in%20functions%20that%20return%20login%20and%20user%20names%20return%20the%20name%20of%20the%20caller%2C%20not%20the%20certificate%20user%20name.%3CBR%20%2F%3EIn%20the%20resources%20you%20will%20find%20a%20couple%20of%20links%20with%20various%20examples.%3C%2FP%3E%0A%3CP%3EHere%20is%20a%20diagram%20of%20how%20this%20can%20be%20used%20in%20a%20simple%20example%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22AndreasWolter_2-1615917506255.png%22%20style%3D%22width%3A%20680px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F264478i54FAC6DCAF3EA2FC%2Fimage-dimensions%2F680x419%3Fv%3Dv2%22%20width%3D%22680%22%20height%3D%22419%22%20role%3D%22button%22%20title%3D%22AndreasWolter_2-1615917506255.png%22%20alt%3D%22AndreasWolter_2-1615917506255.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20this%20example%2C%20Ben%20alone%20only%20has%20SELECT%20on%20the%20table%20%3CEM%3EOrders%3C%2FEM%3E%2C%20but%20does%20not%20have%20access%20to%20the%20%3CEM%3EOrderDetails%3C%2FEM%3E-tables.%20Instead%2C%20the%20intention%20is%2C%20that%20he%20can%20only%20access%20this%20table%20by%20using%20the%20specifically%20prepared%20stored%20procedure%20%3CEM%3EProcAccountforInternalCalc%3C%2FEM%3E.%3CBR%20%2F%3EOnly%20at%20runtime%20of%20this%20procedure%2C%20the%20permissions%20of%20the%20original%20caller%2C%20Ben%2C%20are%20extended%20with%20the%20permissions%20of%20another%20user%2C%20%3CEM%3EProcAccountforInternalCalc%3C%2FEM%3E%2C%20which%20has%20been%20specifically%20created%20for%20this%20use-case%3A%20to%20grant%20the%20SELECT%20permission%20on%20the%20table%20%3CEM%3EOrderDetails%3C%2FEM%3E%20only%20when%20the%20stored%20procedure%20%3CEM%3Esel_OrderDetails_with_margin%3C%2FEM%3E%20is%20being%20used.%20For%20that%2C%20this%20user%20is%20mapped%20to%20a%20certificate%2C%20%3CEM%3ECert%3CBR%20%2F%3E_ProcAccountforInternalCalc%3C%2FEM%3E%2C%20which%20has%20been%20used%20to%20sign%20the%20stored%20procedure.%3CBR%20%2F%3ENow%2C%20anyone%20who%20has%20permission%20to%20execute%20this%20procedure%2C%20will%20inherit%20the%20additional%20permissions%20of%20%3CEM%3EProcAccountforInternalCalc%3C%2FEM%3E%20and%20can%20then%20see%20the%20data%20from%20the%20table%20%E2%80%93%20using%20the%20business%20logic%20from%20the%20procedure%20only.%3C%2FP%3E%0A%3CDIV%20id%3D%22tinyMceEditorAndreasWolter_0%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%20class%3D%22lia-indent-padding-left-30px%22%3E%3CEM%3ENote%3C%2FEM%3E%3CBR%20%2F%3EModule%20signing%20should%20only%20ever%20be%20used%20to%20GRANT%20permissions%2C%20and%20not%20be%20used%20as%20a%20mechanism%20to%20enforce%20DENY%2C%20let%20alone%20REVOKE%20permissions.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EMost%20SQL%20Server%20environments%20I%20have%20seen%20already%20use%20these%20concepts%20one%20way%20or%20the%20other.%20Delegation%20is%20an%20immensely%20powerful%20and%20useful%20technique%3A%20As%20you%20may%20realize%20from%20the%20last%20two%20examples%2C%20those%20techniques%20can%20also%20help%20ensuring%20the%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fazure-sql%2Fsecurity-the-principle-of-least-privilege-polp%2Fba-p%2F2067390%22%20target%3D%22_blank%22%3EPrinciple%20of%20Least%20Privilege%3C%2FA%3E%20is%20adhered%20to.%20And%20it%20also%20can%20help%20implement%20Separation%20of%20Duties%2C%20which%20is%20the%20reason%20to%20talk%20about%20it%20specifically%20within%20the%20context%20of%20the%20general%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fazure-sql%2Fintro-into-security-principles-in-the-context-of-database%2Fba-p%2F2067363%22%20target%3D%22_blank%22%3ESecurity%20Principles%3C%2FA%3E.%3C%2FP%3E%0A%3CP%3EThe%20T-SQL%20code-examples%20are%20available%20as%20a%20download%20below%20this%20article.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHappy%20delegating%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAndreas%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EThank%20you%20to%20my%20Reviewer%3A%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3EMirek%20Sztajno%2C%20Senior%20Program%20Manager%20in%20SQL%20Security%20and%20expert%20in%20Authentication%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--2074017948%22%20id%3D%22toc-hId--2074017859%22%3EResources%3C%2FH2%3E%0A%3CUL%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FOAuth%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3EOAuth%20-%20Wikipedia%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Factive-directory%2Fdevelop%2Fv2-oauth2-on-behalf-of-flow%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EMicrosoft%20identity%20platform%20and%20OAuth2.0%20On-Behalf-Of%20flow%20-%20Microsoft%20identity%20platform%20%7C%20Microsoft%20Docs%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Fconnect%2Fodbc%2Fusing-azure-active-directory%3Fview%3Dsql-server-ver15%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EUsing%20Azure%20Active%20Directory%20with%20the%20ODBC%20Driver%20-%20SQL%20Server%20%7C%20Microsoft%20Docs%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fazure-sql%2Fdatabase%2Fauthentication-aad-service-principal%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EAzure%20Active%20Directory%20service%20principal%20with%20Azure%20SQL%20-%20Azure%20SQL%20Database%20%7C%20Microsoft%20Docs%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fazure-sql%2Fdatabase%2Fauthentication-aad-service-principal-tutorial%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3ECreate%20Azure%20AD%20users%20using%20service%20principals%20-%20Azure%20SQL%20Database%20%7C%20Microsoft%20Docs%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fwww.microsoft.com%2Fen-us%2Fitshowcase%2Fusing-azure-ad-privileged-identity-management-for-elevated-access%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EUsing%20Azure%20AD%20Privileged%20Identity%20Management%20for%20elevated%20access%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fazure-australia%2Frole-privileged%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EAzure%20role-based%20access%20control%20(Azure%20RBAC)%20and%20Privileged%20Identity%20Management%20-%20Azure%20Australia%20%7C%20Microsoft%20Docs%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Ft-sql%2Fstatements%2Fgrant-transact-sql%23with-grant-option%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EGRANT%20WITH%20GRANT%20OPTION%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Ft-sql%2Fstatements%2Fexecute-as-clause-transact-sql%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EEXECUTE%20AS%20Clause%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Ft-sql%2Ffunctions%2Foriginal-login-transact-sql%3Fview%3Dsql-server-ver15%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EORIGINAL_LOGIN%20(Transact-SQL)%20-%20SQL%20Server%20%7C%20Microsoft%20Docs%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Ft-sql%2Fstatements%2Fadd-signature-transact-sql%3Fview%3Dsql-server-ver15%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EADD%20SIGNATURE%20(Transact-SQL)%20-%20SQL%20Server%20%7C%20Microsoft%20Docs%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fdotnet%2Fframework%2Fdata%2Fadonet%2Fsql%2Fsigning-stored-procedures-in-sql-server%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3ESigning%20Stored%20Procedures%20in%20SQL%20Server%20-%20ADO.NET%20%7C%20Microsoft%20Docs%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fprevious-versions%2Fsql%2Fsql-server-2008%2Fms345102(v%3Dsql.100)%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EModule%20Signing%20(Database%20Engine)%20%7C%20Microsoft%20Docs%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Frelational-databases%2Ftutorial-signing-stored-procedures-with-a-certificate%3Fview%3Dsql-server-ver15%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3ETutorial%3A%20Signing%20Stored%20Procedures%20with%20a%20Certificate%20-%20SQL%20Server%20%7C%20Microsoft%20Docs%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3EWikipedia-Article%20on%20Delegation%3A%20%3CA%20href%3D%22https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FDelegation_(computer_security)%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Ehttps%3A%2F%2Fen.wikipedia.org%2Fwiki%2FDelegation_(computer_security)%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3EWikipedia-Article%20on%20Privilege%20bracketing%3A%20%3CA%20href%3D%22https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FPrivilege_bracketing%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Ehttps%3A%2F%2Fen.wikipedia.org%2Fwiki%2FPrivilege_bracketing%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3EArticle%20by%20a%20Community%20member%20and%20MVP%20on%20Packaging%20Permissions%20in%20Stored%20Procedures%20%3CA%20style%3D%22font-family%3A%20inherit%3B%20background-color%3A%20%23ffffff%3B%22%20href%3D%22http%3A%2F%2Fwww.sommarskog.se%2Fgrantperm.html%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Ehttp%3A%2F%2Fwww.sommarskog.se%2Fgrantperm.html%3C%2FA%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-2214708%22%20slang%3D%22en-US%22%3E%3CP%3ESecurity%20concepts%20in%20database-systems%3A%20Delegation%20of%20Authority%20in%20SQL%20Server%20and%20Azure%20SQL%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22AndreasWolter_2-1615917506255.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F264478i54FAC6DCAF3EA2FC%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22AndreasWolter_2-1615917506255.png%22%20alt%3D%22AndreasWolter_2-1615917506255.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2214708%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAzure%20SQL%20Security%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Co-Authors
Version history
Last update:
‎May 28 2021 10:00 AM
Updated by: