Running SQL queries on a linked server using impersonation

Mando 1 Reputation point
2021-04-22T18:10:45.187+00:00

Okay, I am finally posting this here because every other Q&A I've found is talking about using impersonation on a SQL Agent Job and I don't need that (yet). I simply just trying to execute SQL queries in an SSMS window using impersonation to view data on a linked server.

I've gone through the following link extensively:
https://learn.microsoft.com/en-us/previous-versions/sql/sql-server-2008-r2/ms188304(v=sql.105)?redirectedfrom=MSDN

And this blog post explains the conundrum perfectly, but it's like 12 years old... so surely SOMEONE has gotten this to work, right?

Anyways, here's a rundown of my config:

Local Server: (LocalSQLUser & LocalDomainUser)
DB Trustworthy ON
SPNs created
Delegation Enabled
Linked Server connection created (Mapped LocalSQLUser to RemoteDomainUser, With all connections using current login's security context)

Remote Server: (RemoteDomainUser)
DB Trustworthy ON (RemoteDBOwner granted AUTHORIZE)
SPNs created
Delegation Enabled
Permissions granted to a specific view for RemoteDomainUser

Currently getting anonymous login error message when trying to run SELECT on view after successfully running EXECUTE AS [RemoteDomainUser].

Developer technologies | Transact-SQL
SQL Server | Other
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Erland Sommarskog 121.4K Reputation points MVP Volunteer Moderator
    2021-04-22T21:31:01.393+00:00

    I am not sure that I get things together here. First of all, having the databases trustworthy is not going to help, but it is a possible security risk.

    You say:

    Linked Server connection created (Mapped LocalSQLUser to > RemoteDomainUser, With all connections using current login's security context)

    This is kind of contradictory. Have you set up login-mapping, or are you relying on self-mapping? I realise that you talk from the UI, but the UI is more confusing than useful. Please use the scripting button in the UI and post the script, so we can see how the server is set up. If you mask actual data, please make sure that you do it consistently.

    If you have self-mapping, this means that you log in on the remote server as yourself. You cannot do EXECUTE AS to impersonate a different login and access the other server with that login. The impersonation you do is only valid in your own SQL Server, but not in Windows or any remote data sources - that would be a major security hole, as that you would permit you to impersonate an admin to elevate your permissions.

    If you have set up a login mapping for a specific login, you can impersonate that login, but I stop here, until I understand your configuration better.


  2. CarrinWu-MSFT 6,891 Reputation points
    2021-04-23T08:12:55.277+00:00

    Hi @Mando ,

    Welcome to Microsoft Q&A!

    Please refer to this link and this blog to get more information. They are might be helped.

    Best regards,
    Carrin


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  3. Erland Sommarskog 121.4K Reputation points MVP Volunteer Moderator
    2021-04-23T15:12:30.067+00:00

    EXEC master.dbo.sp_addlinkedsvrlogin @rmtsrvname=N'[Remote >Server]',@useself=N'True',@locallogin=NULL,@rmtuser=NULL,@rmt>password=NULL

    So you have self-mapping.

    To re-iterate of what I said in the last post, if the desire is that access to the linked server should be by the Windows login REMOTE\User by preceding the access to the view with

    EXECUTE AS LOGIN = 'REMOTE\User'
    

    this not going to work. The impersonation you make with EXECUTE AS LOGIN is only valid inside your SQL Server instance. Imaging that REMOTE\User is an admin account on the remote server on which you at most you have humble permission. By impersonating this login on your local server where you admin you could take over the remote server.

    If self-mapping is not an option to you, that is, the users on the local machine do not have access on their own to the remote server, you must set up login-mapping in one way or another and that login-mapping must be to an SQL-login - it cannot be a Windows-login.

    It could help if you could spell out your requirements in more detail. I may have more tips, but I don't want to drop them now, as they may be off the mark.


  4. CarrinWu-MSFT 6,891 Reputation points
    2021-04-26T05:14:42.55+00:00

    Hi @Mando , I saw your discussion with Erland, any attempt to access resources outside of the database will cause EXECUTE AS statement to fail. Please refer to EXECUTE AS (Transact-SQL) to get more information.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.