• SQL Server
  • Log Shipping Tricks Demo
  • SQLCruise Alaska 2012 Pics
SQLSoldier News From the Frontlines

Cross-database Queries by Contained Users

December 13, 2012 8:38 pm / Leave a Comment / SQLSoldier

Cross-database Queries by Contained Users

Image source: http://www.necromaticmedia.com/2007/11/containment/

http://www.necromaticmedia.com/2007/11/containment/


I presented to the Pacific Northwest SQL Server User Group / PASS Chapter (PNWSQL) last night on the subject of contained databases. The slide deck and demo code from my presentation, Implementing Contained Databases, can be downloaded below or will be uploaded to the User Group site in the near future, if not already. the presentation showed the basic implementation and administration tasks as well as covering the pitfalls and gotchas that you need to watch out for.

There was a question brought up last night my good friend Meher (@MeherSQL) for a scenario I had not yet tested. Since we know that contained users have no permissions external to the database, what would happen if the same contained user exists in more than 1 database on the server? Would they be able to access multiple databases without re-logging in? I postulated that the only way that would work is if the security identifiers (SIDs) were the same. If they’re not the same, it definitely won’t work. If they are the same, it may work.

I set out to test that theory today. I created 2 contained databases on one of my SQL Server 2012 instances and used the same Create user statement to create a contained user (user with password) in both databases.

-- Create contained database #1
Create Database CDTest
    Containment = Partial;
Go

-- Create contained database #2
Create Database CDTest2
    Containment = Partial;
Go

-- Create contained user in database #1
Use CDTest;

Create User TestCDUser
    With Password = N'12345',
    Default_Schema = dbo;
Go

-- Create contained user in database #2
Use CDTest2;

Create User TestCDUser
    With Password = N'12345',
    Default_Schema = dbo;
Go

Then I open a query window and log in to database CDTest as the contained user. The first thing I notice is that I can only see the database I logged into plus tempdb and master in the dropdown list. Then I run just a basic query against the other database, and I get an error stating that I cannot access the other database. In another query window, I log in to the other contained database and not that exact same behavior from the second attempt though this time, the error reported a different SID for the account.

The error statement:

Msg 916, Level 14, State 1, Line 1
The server principal "S-1-9-3-3229113722-1181156168-1455050630-361674451." is not able to access the database "CDTest2" under the current security context.

Another friend, Nic Cain (blog|@SirSQL), suggested creating the second user with the same SID as the first. I dropped the User in one of the databases and then recreated it using the same SID as the other. Note that to do this, i don’t use the SID specified in the error message. I use the binary SID stored in sys.database_principals.

Create User TestCDUser
    With Password = N'12345',
    Default_Schema = dbo,
    SID = 0x0105000000000009030000007A5D78C048036746864FBA56D3B68E15;
Go

I run the same test as before and get the same results except this time the error messages report the same SID. No luck. On a hunch, I try enabling the TRUSTWORTHY property on both databases. I refresh my connection in both query windows. I note that the database selection dropdown still shows only the current database plus master and tempdb. However, when I attempt to query the other database in each window, I get vastly different results. With the TRUSTWORTHY option set, I am able to run cross-database queries as the same user.

I dropped the second user again and recreated it without specifying the SID. Now that the SIDs no longer matched, attempts to run cross-database queries fail with the same error as before. If you want to use contained users but need to be able to perform cross-database queries, this method will allow you to accomplish it.

As a side-note, I must remind you that using TRUSTWORTHY opens your database up to security holes, and it is recommend to not use it unless you have no other option.

Session Files From My Presentation

Implementing Contained Databases: ImplementingContainedDatabases.zip (494 KB)

Posted in: SQL Server / Tagged: Security, T-SQL, Tips & Tricks

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Post Navigation

← Previous Post
Next Post →
<

Remote DBA Services
- serious SQL Server expertise for less than a full-time DBA
My Articles
 
My Book
Check out my interview on

Extreme Data Recovery (with Argenis Fernandez)
10 Things all BI System Administrators Should Know
Upcoming Events
    All events shown in Pacific Time

    No events to show

RSS My SQL Server Magazine Articles

  • Database Mirroring for Disaster Recovery September 16, 2011
  • Comparative Review: Database Schema Comparison Tools August 24, 2011
  • 3 Log Shipping Techniques June 22, 2011
  • Hardening SQL Server June 20, 2011
  • Review: ScriptLogic Security Explorer for SQL Server February 8, 2011

Tags

31 Days of Disaster Recovery Architecture Automation CDC & Change Tracking Data Architecture VC Database Mirroring DBCC Denali Disaster Recovery Dynamic Management Views Extended Events Gamers & Geeks General Discussion High Availability How do I ... ? Humor Idera ACE Program Internals MCM Meme Monday Performance & Optimization PowerShell Professional Development Replication Security SQLBits SQL PASS SQL PASS Summit SQLRally SQL Saturday SQL Server Magazine SQL University SSAS & BI SSIS SSMS SSRS T-SQL T-SQL Tuesday tempDB Tips & Tricks Travel Troubleshooting Undocumented Stuff Whitepapers XML in SQL

News

Download my Powershell Scripts

The following scripts can be downloaded as text files. You will need to change the file extension to .ps1 in order to execute them.

Backup a database
Restore a database
Scan a server to find a free port
Query DNS to get the FQDN of a server


To see some examples of my other forms of writing, please visit my page on WritersCafe.org. It is almost exclusively horror fiction, but I sometimes throw other things in there too from time to time. There's one science fiction story, a couple of poems, and quite a few humor pieces as well.


Look for me in the SQL Q&A section of the August, 2007 issue of TechNet Magazine.
August issue of TechNet Magazine's SQL Q&A column

Protect our Heroes

© Copyright 2012 - Robert L Davis
Infinity Theme by DesignCoral / WordPress

Twitter Twitter 
LinkedIn LinkedIn 
TLF TLF RSS RSS 
WritersCafe WritersCafe 
SQLPASS SQLPASS 
Facebook Facebook
grab this