SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Stairway to SQLCLR Level 3: Security (General and SAFE Assemblies)


Stairway to SQLCLR Level 3: Security (General and SAFE Assemblies)

Author
Message
Solomon Rutzky
Solomon Rutzky
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12112 Visits: 3304
Comments posted to this topic are about the item Stairway to SQLCLR Level 3: Security (General and SAFE Assemblies)

SQL# - https://SQLsharp.com/ ( SQLCLR library of over 340 Functions and Procedures)
Sql Quantum Lift - https://SqlQuantumLift.com/ ( company )
Sql Quantum Leap - https://SqlQuantumLeap.com/ ( blog )
flatmail
flatmail
SSC Rookie
SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)

Group: General Forum Members
Points: 37 Visits: 10
Solomon, thx for your great articles about CLR.

For quite some time now I'm looking for a way to protect my CLR functions from unauthorized distribution to an arbitrary number of sql servers. Is there a possibility to bring in some sort of license into an assembly that needs to be varified and cannot be re-distributed by script?
I already thought of a license key that is stored inside the master database of the licensed server. It could be queried/validated from inside the assembly, but that would mean a horrible performance loss to the CLR functions. I didn't find any way to validate a license once and then keep it validated for it's next calls.

Does anyone have any further ideas on this?
Solomon Rutzky
Solomon Rutzky
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12112 Visits: 3304
flatmail - Wednesday, April 11, 2018 9:50 AM
Solomon, thx for your great articles about CLR.

For quite some time now I'm looking for a way to protect my CLR functions from unauthorized distribution to an arbitrary number of sql servers. Is there a possibility to bring in some sort of license into an assembly that needs to be varified and cannot be re-distributed by script?
I already thought of a license key that is stored inside the master database of the licensed server. It could be queried/validated from inside the assembly, but that would mean a horrible performance loss to the CLR functions. I didn't find any way to validate a license once and then keep it validated for it's next calls.

You are quite welcome Smile.

I, too, have had similar thoughts with respect to protecting my SQL# project. I have thought that maybe using a static constructor to load a static, readonly, class variable (bool) that could be checked by each function would alleviate the performance hit, though that would still require testing since it is additional steps that need to be take per call to even just check that variable. But the problem with using a static constructor is that there is no SqlContext / Context Connection when it executes since that happens as the class is being loaded. So the timing of when it executes is correct, and happening only once is correct, but no chance of getting data from the DB or even file system. That is, unless you have an EXTERNAL_ACCESS or UNSAFE assembly that can read from the file system or connect back to the host DB via a regular / external connection. However, for my purposes I need something that works with a SAFE Assembly.

One option seems to be that you could reference another Assembly that contains only the license key, but then how to dynamically create and distribute that assembly containing only a single variable?

The only other thought I had been considering was using the application config file (i.e. sqlservr.exe.Config ) as that is readable in a SAFE Assembly, as long as you stick the license key into one of the two pre-defined lists (i.e. ConnectionStrings or AppSettings ). The problem there is that, as far as my testing showed, the config file was not accessible on Linux, and I need something that will work everywhere. Also, now that I think about it, that same concern would be true for the new Managed Instances of Azure SQL Database. So that's 2 of the 3 platforms that SQLCLR works on that wouldn't support this.

So for now I have opted to not worry about it. But it is still an interesting problem that I think about from time to time. If you have any other thoughts about this I would love to hear them.

Take care,
Solomon..


SQL# - https://SQLsharp.com/ ( SQLCLR library of over 340 Functions and Procedures)
Sql Quantum Lift - https://SqlQuantumLift.com/ ( company )
Sql Quantum Leap - https://SqlQuantumLeap.com/ ( blog )
flatmail
flatmail
SSC Rookie
SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)

Group: General Forum Members
Points: 37 Visits: 10
Solomon Rutzky - Wednesday, April 11, 2018 10:35 AM
flatmail - Wednesday, April 11, 2018 9:50 AM
Solomon, thx for your great articles about CLR.

For quite some time now I'm looking for a way to protect my CLR functions from unauthorized distribution to an arbitrary number of sql servers. Is there a possibility to bring in some sort of license into an assembly that needs to be varified and cannot be re-distributed by script?
I already thought of a license key that is stored inside the master database of the licensed server. It could be queried/validated from inside the assembly, but that would mean a horrible performance loss to the CLR functions. I didn't find any way to validate a license once and then keep it validated for it's next calls.

You are quite welcome Smile.

I, too, have had similar thoughts with respect to protecting my SQL# project. I have thought that maybe using a static constructor to load a static, readonly, class variable (bool) that could be checked by each function would alleviate the performance hit, though that would still require testing since it is additional steps that need to be take per call to even just check that variable. But the problem with using a static constructor is that there is no SqlContext / Context Connection when it executes since that happens as the class is being loaded. So the timing of when it executes is correct, and happening only once is correct, but no chance of getting data from the DB or even file system. That is, unless you have an EXTERNAL_ACCESS or UNSAFE assembly that can read from the file system or connect back to the host DB via a regular / external connection. However, for my purposes I need something that works with a SAFE Assembly.

One option seems to be that you could reference another Assembly that contains only the license key, but then how to dynamically create and distribute that assembly containing only a single variable?

The only other thought I had been considering was using the application config file (i.e. sqlservr.exe.Config ) as that is readable in a SAFE Assembly, as long as you stick the license key into one of the two pre-defined lists (i.e. ConnectionStrings or AppSettings ). The problem there is that, as far as my testing showed, the config file was not accessible on Linux, and I need something that will work everywhere. Also, now that I think about it, that same concern would be true for the new Managed Instances of Azure SQL Database. So that's 2 of the 3 platforms that SQLCLR works on that wouldn't support this.

So for now I have opted to not worry about it. But it is still an interesting problem that I think about from time to time. If you have any other thoughts about this I would love to hear them.

Take care,
Solomon..

Thanks you for your quick reply.
You are absolutely right about the static constructor. I also tested this approach by creating a singleton that holds the key, but it forced me to sign the assembly UNSAFE. Since most db admins are not willing to inject UNSAFE assemblies in their sql server, this solution won't work for me either.
Thanks for mentioning the point about Linux and Azure. I didn't think about that at all.
Well, I don't have any breaking new ideas now, but if I should come across some, I will be pleased to post them here.

Thx a lot for sharing your experiences.

Happy coding,
Jens

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search