How to implement row-level security in serverless SQL pools

Published 05-18-2021 08:17 AM 2,698 Views
Microsoft

Serverless Synapse SQL pools enable you to read Parquet/CSV files or Cosmos DB collections and return their content as a set of rows. In some scenarios, you would need to ensure that a reader cannot access some rows in the underlying data source. This way, you are limiting the result set that will be returned to the users based on some security rules. In this scenario, called Row-level security, you would like to return a subset of data depending on the reader’s identity or role.

Row-level security is supported in dedicated SQL pools, but it is not supported in serverless pools (you can propose this feature in Azure feedback site). In some cases, you can implement your own custom row-level security rules using standard T-SQL code.

In this post you will see how to implement RLS by specifying the security rules in the WHERE clause or by using inline table-value function (iTVF).

Scenario

Let’s imagine that we have a view or external table created on top of the COVID data set placed in the Azure Data Lake storage:

create or alter view dbo.covid as
select *
from openrowset(
    bulk 'https://pandemicdatalake.blob.core.windows.net/public/curated/covid-19/ecdc_cases/latest/ecdc_cases.parquet',
    format = 'parquet') as rows

 

The readers will get all COVID cases that are written in the Parquet file. Let’s imagine that the requirement is to restrict the user access and allow them to see just a subset of data based on the following rules:

  • Azure AD user jovan@adventureworks.com can see only the COVID cases reported in Serbia.
  • The users in ‘AfricaAnalyst’ role can see only the COVID cases reported in Africa.

We can represent these security rules using the following predicate (this is T-SQL pseudo-syntax):

( USER_NAME = 'jovan@adventureworks.com' AND geo_id = 'RS' )
OR
( USER IS IN ROLE 'AfricaAnalyst' AND continent_exp = 'Africa' )

 

You can use the system functions like SUSER_SNAME() or IS_ROLEMEMBER() to identify the caller and easily check should you return some rows to the current user. We just need to express this condition in T-SQL and add it as a filtering condition in the view.

In this post you will see two methods for filtering the rows based on security rules:

  • Filtering rows directly in the WHERE condition.
  • Applying security predicated coded in iTVF.

As a prerequisite, you should setup a database role that should have limited access to the underlying data set.

CREATE ROLE AfricaAnalyst;

CREATE USER [jovan@contoso.com] FROM EXTERNAL PROVIDER
CREATE USER [petar@contoso.com] FROM EXTERNAL PROVIDER
CREATE USER [petar@contoso.com] FROM EXTERNAL PROVIDER

ALTER ROLE AfricaAnalyst ADD MEMBER [jovan@contoso.com];
ALTER ROLE AfricaAnalyst ADD MEMBER [petar@contoso.com];
ALTER ROLE AfricaAnalyst ADD MEMBER [nikola@contoso.com];

In this code we have added a role and added three users to this role.

Filtering rows based on security rule

The easiest way to implement row-level security is to directly embed security predicates in the WHERE condition of the view:

create or alter view dbo.covid as
select *
from openrowset(
    bulk 'https://pandemicdatalake.blob.core.windows.net/public/curated/covid-19/ecdc_cases/latest/ecdc_cases.parquet',
    format = 'parquet') as rows
WHERE
( SUSER_SNAME() = 'jovan@adventureworks.com' AND geo_id = 'RS')
OR
( IS_ROLEMEMBER('AfricaAnalyst', SUSER_SNAME()) = 1 AND continent_exp = 'Africa')

The Azure AD principal jovan@adventureworks.com will see only the COVID cases reported in Serbia:

JovanPop_0-1620998381411.png

 

Azure AD principals that belong to ‘AfricaAnalysts’ role will see the cases reported in Africa when running the same query:

JovanPop_1-1620998381426.png

 

Without any change in the query, the readers will be able to access only the rows that they are allowed to see based on their identity.

Operationalizing row-level-security with iTVF

In some cases, it would be hard to maintain security rules if you put them directly in the view definition. The security rules might change, and you don’t want to track all possible views to update them. The WHERE condition in the view may contain other predicates, and some bug in AND/OR logic might change or disable the security rules.

The better idea would be to put the security predicates in a separate function and just apply the function on the views that should return limited data.

We can create a separate schema called secure, and put the security rules in the inline table value function (iTVF):

create schema secure
go

create or alter function secure.geo_predicate(@geo_id varchar(20),
                                              @continent_exp varchar(20))
returns table
return (
    select condition = 1
    WHERE
    (SUSER_SNAME() = 'jovanpop@adventureworks.com' AND @Geo_id = 'RS')
    OR
    (IS_ROLEMEMBER('AfricaAnalyst', SUSER_SNAME())=1 AND continent_exp='Africa')
)

This predicate is very similar to the predicates used in native row-level security in the T-SQL language. This predicate evaluates a security rule based on geo_id and continent_exp values. These values are provided as the function parameters. The function will internally use SUSER_SNAME() and IS_ROLEMEMBER() functions and evaluate security rules based on geo_id and continent_exp values. If the current user can see a row with the provided values, the function fill return value 1. Otherwise, it will not return any value.

Instead of modifying every view by adding new conditions in the WHERE clause, we can create the secure wrapper views where we will apply this predicate. In the following example, I’m creating a view secure.covid where I will apply the predicate and pass the columns that will be used in the predicate to evaluate should this row be returned or not:

create or alter view secure.covid
as
select *
from dbo.covid
    cross apply security.geo_predicate(geo_id, continent_exp)
go

 

Note that in this case the view dbo.covid will not have the WHERE clause that contains the security rule. Security rules are filtering rows by applying security.geo_predicate() iTVF on the original view. For every row in the dbo.covid view. The query will provide the values of geo_id and continent_exp columns to the iTVF predicate. The predicate will remove the row from the output if these values do not satisfy criterion.

 A user jovanpop@adventureworks.com will see the filtered results based on his context:

JovanPop_2-1620998381435.png

 

Placing the security rules in a separate iTVFs and creating the secure wrapper views will make your code more maintainable.

Conclusion

Although native row-level-security is not available in serverless SQL pool, you can easily implement the similar security rules using the standard T-SQL functionalities.

You just need to ensure that a reader cannot bypass the security rules.

  • You need to ensure that the readers cannot directly query the files or collections in the underlying data source using the OPENROWSET function or the views/external tables. Make sure that you restrict access to the OPENROWSET/credentials and DENY SELECT on the base views and external tables that read original un-filtered data.
  • You need to ensure that users cannot directly access the underlying data source and bypass serverless SQL pool. You would need to make sure that underlying storage is protected from random access using private endpoints.
  • Make sure that the readers cannot use some other tool like other Synapse workspace, Apache Spark, or other application or service to directly access the data that you secured on your workspace.

This method uses the core T-SQL functionalities and will not introduce additional overhead compared to the native row-level security. If this workaround doesn’t work in your scenario, you can propose this feature in Azure feedback site.

%3CLINGO-SUB%20id%3D%22lingo-sub-2354759%22%20slang%3D%22en-US%22%3EHow%20to%20implement%20row-level%20security%20in%20serverless%20SQL%20pools%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2354759%22%20slang%3D%22en-US%22%3E%3CP%3EServerless%20Synapse%20SQL%20pools%20enable%20you%20to%20read%20Parquet%2FCSV%20files%20or%20Cosmos%20DB%20collections%20and%20return%20their%20content%20as%20a%20set%20of%20rows.%20In%20some%20scenarios%2C%20you%20would%20need%20to%20ensure%20that%20a%20reader%20cannot%20access%20some%20rows%20in%20the%20underlying%20data%20source.%20This%20way%2C%20you%20are%20limiting%20the%20result%20set%20that%20will%20be%20returned%20to%20the%20users%20based%20on%20some%20security%20rules.%20In%20this%20scenario%2C%20called%20%3CSTRONG%3ERow-level%20security%3C%2FSTRONG%3E%2C%20you%20would%20like%20to%20return%20a%20subset%20of%20data%20depending%20on%20the%20reader%E2%80%99s%20identity%20or%20role.%3C%2FP%3E%0A%3CP%3ERow-level%20security%20is%20supported%20in%20dedicated%20SQL%20pools%2C%20but%20it%20is%20not%20supported%20in%20serverless%20pools%20(you%20can%20propose%20this%20feature%20in%20%3CA%20href%3D%22https%3A%2F%2Ffeedback.azure.com%2Fforums%2F307516-azure-synapse-analytics%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3EAzure%20feedback%20site%3C%2FA%3E).%20In%20some%20cases%2C%20you%20can%20implement%20your%20own%20custom%20row-level%20security%20rules%20using%20standard%20T-SQL%20code.%3C%2FP%3E%0A%3CP%3EIn%20this%20post%20you%20will%20see%20how%20to%20implement%20RLS%20by%20specifying%20the%20security%20rules%20in%20the%20WHERE%20clause%20or%20by%20using%20inline%20table-value%20function%20(iTVF).%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--466960741%22%20id%3D%22toc-hId--466960741%22%3EScenario%3C%2FH2%3E%0A%3CP%3ELet%E2%80%99s%20imagine%20that%20we%20have%20a%20view%20or%20external%20table%20created%20on%20top%20of%20the%20COVID%20data%20set%20placed%20in%20the%20Azure%20Data%20Lake%20storage%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3Ecreate%26nbsp%3Bor%26nbsp%3Balter%26nbsp%3Bview%26nbsp%3Bdbo.covid%26nbsp%3Bas%0Aselect%26nbsp%3B*%0Afrom%26nbsp%3Bopenrowset(%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Bbulk%26nbsp%3B'https%3A%2F%2Fpandemicdatalake.blob.core.windows.net%2Fpublic%2Fcurated%2Fcovid-19%2Fecdc_cases%2Flatest%2Fecdc_cases.parquet'%2C%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Bformat%26nbsp%3B%3D%26nbsp%3B'parquet')%26nbsp%3Bas%26nbsp%3Brows%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20readers%20will%20get%20all%20COVID%20cases%20that%20are%20written%20in%20the%20Parquet%20file.%20Let%E2%80%99s%20imagine%20that%20the%20requirement%20is%20to%20restrict%20the%20user%20access%20and%20allow%20them%20to%20see%20just%20a%20subset%20of%20data%20based%20on%20the%20following%20rules%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EAzure%20AD%20user%20jovan%40adventureworks.com%20can%20see%20only%20the%20COVID%20cases%20reported%20in%20Serbia.%3C%2FLI%3E%0A%3CLI%3EThe%20users%20in%20%E2%80%98AfricaAnalyst%E2%80%99%20role%20can%20see%20only%20the%20COVID%20cases%20reported%20in%20Africa.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3EWe%20can%20represent%20these%20security%20rules%20using%20the%20following%20predicate%20(this%20is%20T-SQL%20pseudo-syntax)%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E(%20USER_NAME%26nbsp%3B%3D%26nbsp%3B'jovan%40adventureworks.com'%26nbsp%3BAND%26nbsp%3Bgeo_id%26nbsp%3B%3D%26nbsp%3B'RS'%20)%0AOR%0A(%20USER%20IS%20IN%20ROLE%26nbsp%3B'AfricaAnalyst'%26nbsp%3BAND%26nbsp%3Bcontinent_exp%26nbsp%3B%3D%26nbsp%3B'Africa'%20)%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYou%20can%20use%20the%20system%20functions%20like%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Ft-sql%2Ffunctions%2Fsuser-sname-transact-sql%3Fview%3Dsql-server-ver15%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3ESUSER_SNAME()%3C%2FA%3E%20or%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Ft-sql%2Ffunctions%2Fis-rolemember-transact-sql%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EIS_ROLEMEMBER()%3C%2FA%3E%20to%20identify%20the%20caller%20and%20easily%20check%20should%20you%20return%20some%20rows%20to%20the%20current%20user.%20We%20just%20need%20to%20express%20this%20condition%20in%20T-SQL%20and%20add%20it%20as%20a%20filtering%20condition%20in%20the%20view.%3C%2FP%3E%0A%3CP%3EIn%20this%20post%20you%20will%20see%20two%20methods%20for%20filtering%20the%20rows%20based%20on%20security%20rules%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EFiltering%20rows%20directly%20in%20the%20WHERE%20condition.%3C%2FLI%3E%0A%3CLI%3EApplying%20security%20predicated%20coded%20in%20iTVF.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3EAs%20a%20prerequisite%2C%20you%20should%20setup%20a%20database%20role%20that%20should%20have%20limited%20access%20to%20the%20underlying%20data%20set.%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3ECREATE%26nbsp%3BROLE%26nbsp%3BAfricaAnalyst%3B%0A%0ACREATE%26nbsp%3BUSER%26nbsp%3B%5Bjovan%40contoso.com%5D%26nbsp%3BFROM%26nbsp%3BEXTERNAL%26nbsp%3BPROVIDER%0ACREATE%26nbsp%3BUSER%26nbsp%3B%5Bpetar%40contoso.com%5D%26nbsp%3BFROM%26nbsp%3BEXTERNAL%26nbsp%3BPROVIDER%0ACREATE%26nbsp%3BUSER%26nbsp%3B%5Bpetar%40contoso.com%5D%26nbsp%3BFROM%26nbsp%3BEXTERNAL%26nbsp%3BPROVIDER%0A%0AALTER%26nbsp%3BROLE%26nbsp%3BAfricaAnalyst%26nbsp%3BADD%26nbsp%3BMEMBER%26nbsp%3B%5Bjovan%40contoso.com%5D%3B%0AALTER%26nbsp%3BROLE%26nbsp%3BAfricaAnalyst%26nbsp%3BADD%26nbsp%3BMEMBER%26nbsp%3B%5Bpetar%40contoso.com%5D%3B%0AALTER%26nbsp%3BROLE%26nbsp%3BAfricaAnalyst%26nbsp%3BADD%26nbsp%3BMEMBER%26nbsp%3B%5Bnikola%40contoso.com%5D%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EIn%20this%20code%20we%20have%20added%20a%20role%20and%20added%20three%20users%20to%20this%20role.%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-2020552092%22%20id%3D%22toc-hId-2020552092%22%3EFiltering%20rows%20based%20on%20security%20rule%3C%2FH2%3E%0A%3CP%3EThe%20easiest%20way%20to%20implement%20row-level%20security%20is%20to%20directly%20embed%20security%20predicates%20in%20the%20WHERE%20condition%20of%20the%20view%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3Ecreate%26nbsp%3Bor%26nbsp%3Balter%26nbsp%3Bview%26nbsp%3Bdbo.covid%26nbsp%3Bas%0Aselect%26nbsp%3B*%0Afrom%26nbsp%3Bopenrowset(%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Bbulk%26nbsp%3B'https%3A%2F%2Fpandemicdatalake.blob.core.windows.net%2Fpublic%2Fcurated%2Fcovid-19%2Fecdc_cases%2Flatest%2Fecdc_cases.parquet'%2C%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Bformat%26nbsp%3B%3D%26nbsp%3B'parquet')%26nbsp%3Bas%26nbsp%3Brows%0AWHERE%0A(%20SUSER_SNAME()%26nbsp%3B%3D%26nbsp%3B'jovan%40adventureworks.com'%26nbsp%3BAND%26nbsp%3Bgeo_id%26nbsp%3B%3D%26nbsp%3B'RS')%0AOR%0A(%20IS_ROLEMEMBER('AfricaAnalyst'%2C%26nbsp%3BSUSER_SNAME())%26nbsp%3B%3D%26nbsp%3B1%26nbsp%3BAND%26nbsp%3Bcontinent_exp%26nbsp%3B%3D%26nbsp%3B'Africa')%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EThe%20Azure%20AD%20principal%20jovan%40adventureworks.com%20will%20see%20only%20the%20COVID%20cases%20reported%20in%20Serbia%3A%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22JovanPop_0-1620998381411.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F280733i19A84B7BEF151E5E%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22JovanPop_0-1620998381411.png%22%20alt%3D%22JovanPop_0-1620998381411.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAzure%20AD%20principals%20that%20belong%20to%20%E2%80%98AfricaAnalysts%E2%80%99%20role%20will%20see%20the%20cases%20reported%20in%20Africa%20when%20running%20the%20same%20query%3A%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22JovanPop_1-1620998381426.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F280734i8B1F021C7E97F469%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22JovanPop_1-1620998381426.png%22%20alt%3D%22JovanPop_1-1620998381426.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWithout%20any%20change%20in%20the%20query%2C%20the%20readers%20will%20be%20able%20to%20access%20only%20the%20rows%20that%20they%20are%20allowed%20to%20see%20based%20on%20their%20identity.%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-213097629%22%20id%3D%22toc-hId-213097629%22%3EOperationalizing%20row-level-security%20with%20iTVF%3C%2FH2%3E%0A%3CP%3EIn%20some%20cases%2C%20it%20would%20be%20hard%20to%20maintain%20security%20rules%20if%20you%20put%20them%20directly%20in%20the%20view%20definition.%20The%20security%20rules%20might%20change%2C%20and%20you%20don%E2%80%99t%20want%20to%20track%20all%20possible%20views%20to%20update%20them.%20The%20WHERE%20condition%20in%20the%20view%20may%20contain%20other%20predicates%2C%20and%20some%20bug%20in%20AND%2FOR%20logic%20might%20change%20or%20disable%20the%20security%20rules.%3C%2FP%3E%0A%3CP%3EThe%20better%20idea%20would%20be%20to%20put%20the%20security%20predicates%20in%20a%20separate%20function%20and%20just%20apply%20the%20function%20on%20the%20views%20that%20should%20return%20limited%20data.%3C%2FP%3E%0A%3CP%3EWe%20can%20create%20a%20separate%20schema%20called%20secure%2C%20and%20put%20the%20security%20rules%20in%20the%20inline%20table%20value%20function%20(iTVF)%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3Ecreate%26nbsp%3Bschema%26nbsp%3Bsecure%0Ago%0A%0Acreate%26nbsp%3Bor%26nbsp%3Balter%26nbsp%3Bfunction%26nbsp%3Bsecure.geo_predicate(%40geo_id%26nbsp%3Bvarchar(20)%2C%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%40continent_exp%26nbsp%3Bvarchar(20))%0Areturns%26nbsp%3Btable%0Areturn%26nbsp%3B(%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20select%26nbsp%3Bcondition%26nbsp%3B%3D%26nbsp%3B1%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20WHERE%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B(SUSER_SNAME()%26nbsp%3B%3D%26nbsp%3B'jovanpop%40adventureworks.com'%26nbsp%3BAND%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F226084%22%20target%3D%22_blank%22%3E%40Geo%3C%2FA%3E_id%26nbsp%3B%3D%26nbsp%3B'RS')%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BOR%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20(IS_ROLEMEMBER('AfricaAnalyst'%2C%26nbsp%3BSUSER_SNAME())%3D1%26nbsp%3BAND%26nbsp%3Bcontinent_exp%3D'Africa')%0A)%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EThis%20predicate%20is%20very%20similar%20to%20the%20predicates%20used%20in%20native%20row-level%20security%20in%20the%20T-SQL%20language.%20This%20predicate%20evaluates%20a%20security%20rule%20based%20on%20geo_id%20and%20continent_exp%20values.%20These%20values%20are%20provided%20as%20the%20function%20parameters.%20The%20function%20will%20internally%20use%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Ft-sql%2Ffunctions%2Fsuser-sname-transact-sql%3Fview%3Dsql-server-ver15%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3ESUSER_SNAME()%3C%2FA%3E%20and%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Ft-sql%2Ffunctions%2Fis-rolemember-transact-sql%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EIS_ROLEMEMBER()%3C%2FA%3E%20functions%20and%20evaluate%20security%20rules%20based%20on%20geo_id%20and%20continent_exp%20values.%20If%20the%20current%20user%20can%20see%20a%20row%20with%20the%20provided%20values%2C%20the%20function%20fill%20return%20value%201.%20Otherwise%2C%20it%20will%20not%20return%20any%20value.%3C%2FP%3E%0A%3CP%3EInstead%20of%20modifying%20every%20view%20by%20adding%20new%20conditions%20in%20the%20WHERE%20clause%2C%20we%20can%20create%20the%20secure%20wrapper%20views%20where%20we%20will%20apply%20this%20predicate.%20In%20the%20following%20example%2C%20I%E2%80%99m%20creating%20a%20view%20secure.covid%20where%20I%20will%20apply%20the%20predicate%20and%20pass%20the%20columns%20that%20will%20be%20used%20in%20the%20predicate%20to%20evaluate%20should%20this%20row%20be%20returned%20or%20not%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3Ecreate%26nbsp%3Bor%26nbsp%3Balter%26nbsp%3Bview%26nbsp%3Bsecure.covid%0Aas%0Aselect%26nbsp%3B*%0Afrom%26nbsp%3Bdbo.covid%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Bcross%26nbsp%3Bapply%26nbsp%3Bsecurity.geo_predicate(geo_id%2C%26nbsp%3Bcontinent_exp)%0Ago%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENote%20that%20in%20this%20case%20the%20view%20dbo.covid%20will%20not%20have%20the%20WHERE%20clause%20that%20contains%20the%20security%20rule.%20Security%20rules%20are%20filtering%20rows%20by%20applying%20security.geo_predicate()%20iTVF%20on%20the%20original%20view.%20For%20every%20row%20in%20the%20dbo.covid%20view.%20The%20query%20will%20provide%20the%20values%20of%20geo_id%20and%20continent_exp%20columns%20to%20the%20iTVF%20predicate.%20The%20predicate%20will%20remove%20the%20row%20from%20the%20output%20if%20these%20values%20do%20not%20satisfy%20criterion.%3C%2FP%3E%0A%3CP%3E%26nbsp%3BA%20user%20%3CA%20href%3D%22mailto%3Ajovanpop%40adventureworks.com%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ejovanpop%40adventureworks.com%3C%2FA%3E%20will%20see%20the%20filtered%20results%20based%20on%20his%20context%3A%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22JovanPop_2-1620998381435.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F280735iCCE8763830657299%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22JovanPop_2-1620998381435.png%22%20alt%3D%22JovanPop_2-1620998381435.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EPlacing%20the%20security%20rules%20in%20a%20separate%20iTVFs%20and%20creating%20the%20secure%20wrapper%20views%20will%20make%20your%20code%20more%20maintainable.%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--1594356834%22%20id%3D%22toc-hId--1594356834%22%3EConclusion%3C%2FH2%3E%0A%3CP%3EAlthough%20native%20row-level-security%20is%20not%20available%20in%20serverless%20SQL%20pool%2C%20you%20can%20easily%20implement%20the%20similar%20security%20rules%20using%20the%20standard%20T-SQL%20functionalities.%3C%2FP%3E%0A%3CP%3EYou%20just%20need%20to%20ensure%20that%20a%20reader%20cannot%20bypass%20the%20security%20rules.%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EYou%20need%20to%20ensure%20that%20the%20readers%20cannot%20directly%20query%20the%20files%20or%20collections%20in%20the%20underlying%20data%20source%20using%20the%20OPENROWSET%20function%20or%20the%20views%2Fexternal%20tables.%20Make%20sure%20that%20you%20restrict%20access%20to%20the%20OPENROWSET%2Fcredentials%20and%20DENY%20SELECT%20on%20the%20base%20views%20and%20external%20tables%20that%20read%20original%20un-filtered%20data.%3C%2FLI%3E%0A%3CLI%3EYou%20need%20to%20ensure%20that%20users%20cannot%20directly%20access%20the%20underlying%20data%20source%20and%20bypass%20serverless%20SQL%20pool.%20You%20would%20need%20to%20make%20sure%20that%20underlying%20storage%20is%20protected%20from%20random%20access%20using%20private%20endpoints.%3C%2FLI%3E%0A%3CLI%3EMake%20sure%20that%20the%20readers%20cannot%20use%20some%20other%20tool%20like%20other%20Synapse%20workspace%2C%20Apache%20Spark%2C%20or%20other%20application%20or%20service%20to%20directly%20access%20the%20data%20that%20you%20secured%20on%20your%20workspace.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3EThis%20method%20uses%20the%20core%20T-SQL%20functionalities%20and%20will%20not%20introduce%20additional%20overhead%20compared%20to%20the%20native%20row-level%20security.%20If%20this%20workaround%20doesn%E2%80%99t%20work%20in%20your%20scenario%2C%20you%20can%20propose%20this%20feature%20in%20%3CA%20href%3D%22https%3A%2F%2Ffeedback.azure.com%2Fforums%2F307516-azure-synapse-analytics%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3EAzure%20feedback%20site%3C%2FA%3E.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-2354759%22%20slang%3D%22en-US%22%3E%3CP%3ERow-level%20security%20enables%20you%20to%20configure%20your%20views%20to%20return%20different%20subsets%20of%20rows%20to%20the%20users%20with%20difference%20permissions.%20This%20way%2C%20the%20readers%20will%20get%20limited%20results%20based%20on%20their%20access%20rights%20or%20roles.%20Row-level%20security%20can%20be%20easily%20implemented%20in%20serverless%20SQL%20pools%20using%20the%20instructions%20in%20this%20post.%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2354759%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ESynapse%20Security%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ESynapse%20SQL%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Co-Authors
Version history
Last update:
‎May 18 2021 08:17 AM
Updated by: