Catch exception when querying sys.logins without permissions (#1984)

This commit is contained in:
Karl Burtram
2023-04-03 19:27:05 -07:00
committed by GitHub
parent 0b2eb26d45
commit 539ad243c1

View File

@@ -294,28 +294,39 @@ namespace Microsoft.SqlTools.ServiceLayer.Utility
public static string[] LoadItems(ServerConnection serverConnection, string urn) public static string[] LoadItems(ServerConnection serverConnection, string urn)
{ {
List<string> items = new List<string>(); try
Request req = new Request();
req.Urn = urn;
req.ResultType = ResultType.IDataReader;
req.Fields = new string[] { "Name" };
Enumerator en = new Enumerator();
using (IDataReader reader = en.Process(serverConnection, req).Data as IDataReader)
{ {
if (reader != null) List<string> items = new List<string>();
Request req = new Request();
req.Urn = urn;
req.ResultType = ResultType.IDataReader;
req.Fields = new string[] { "Name" };
Enumerator en = new Enumerator();
using (IDataReader reader = en.Process(serverConnection, req).Data as IDataReader)
{ {
string name; if (reader != null)
while (reader.Read())
{ {
// Get the permission name string name;
name = reader.GetString(0); while (reader.Read())
items.Add(name); {
// Get the permission name
name = reader.GetString(0);
items.Add(name);
}
} }
} }
items.Sort();
return items.ToArray();
}
catch (Microsoft.SqlServer.Management.Sdk.Sfc.EnumeratorException)
{
// reading Logins can fail when trying to create a contained/SQL DB user
// when the current session does not have permissions to master
// we can return an empty existing login list in this scenario
// no need to log here since this is an expected non-blocking exception that is recoverable
return new string[0];
} }
items.Sort();
return items.ToArray();
} }
} }
} }