Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stored procedure ENCRYPTION option depending on build mode

I'm working on stored procedures encryption in enterprise project. We have a bunch of SP's that should be protected on production.
There is no problem to set WITH ENCRYPTION parameter in each SP that is in sqlproj. But I want to make this directive optional: if I'm building project in debug mode - do not apply this procedure option, otherwise - use it. Actually main goal here is to get database for developers without encryption, but on production - encrypted SP's.
Using PowerShell script in build task I can modify generated sql file and as a result get script with encryption parameter, but I'm wondering how it would work with dacpac.
Any suggestions?

Update:

After some time spent playing with msbuild. I decided to stop (at least for now) on solution with PowerShell script task after SqlCore target:

<Import Project="$(ExtensionTasksPath)MSBuild.ExtensionPack.tasks" Condition="Exists('$(ExtensionTasksPath)MSBuild.ExtensionPack.dll')" />
  <UsingTask TaskFactory="PowershellTaskFactory" TaskName="CreateDecryptedScript" AssemblyFile="$(PowerShellTaskAssembly)" Condition="Exists('$(PowerShellTaskAssembly)')">
    <ParameterGroup>
      <File Required="true" ParameterType="System.String" />
      <ResultFile Required="true" ParameterType="System.String" />
    </ParameterGroup>
    <Task>
      <![CDATA[      
      (Get-Content $file) | Foreach-Object {$_ -replace 'WITH ENCRYPTION', '--WITH ENCRYPTION'} | Set-Content $resultfile    
    ]]>
    </Task>
  </UsingTask>
<Target Name="CreateDecryptedScript" AfterTargets="SqlCore">
    <CreateDecryptedScript File="$(OutputPath)$(CreateScriptFileName)" ResultFile="$(OutputPath)$(DecryptedScriptName)" Condition="Exists('$(PowerShellTaskAssembly)')" />
</Target>

As a result, after rebuilding project we have script for creating database without encryption.

But publish that is invoked from project does not force this stuff to happen and we will alter all SP's with encryption.

like image 438
Anatolii Gabuza Avatar asked Nov 03 '22 11:11

Anatolii Gabuza


1 Answers

It is easy to decrypt "encrypted" stored procedures. I suggest you just don't bother encrypting them.

  • Anyway to decrypt an encrypted sql server stored procedure?

  • https://duckduckgo.com/?q=decrypt+sql+server+stored+procedure

If you must encrypt them, I suggest a post-deploy step in DEV which decrypts every encrypted stored procedure, one by one. You could create such a step easily using sp_execsql and the information at the links.

like image 94
Ben Avatar answered Nov 12 '22 17:11

Ben