Skip to content

Commit

Permalink
add requests to create and retrieve s3 credentials (#2319)
Browse files Browse the repository at this point in the history
  • Loading branch information
barbaravaldez authored Jan 16, 2024
1 parent 09d2913 commit d2120a4
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

#nullable disable
using Microsoft.SqlTools.Hosting.Protocol.Contracts;
using Microsoft.SqlTools.Utility;

namespace Microsoft.SqlTools.ServiceLayer.ObjectManagement.Contracts
{
public class CreateCredentialRequestParams : GeneralRequestDetails
{
/// <summary>
/// Credential info
/// </summary>
public CredentialInfo CredentialInfo { get; set; }
/// <summary>
/// Connection uri
/// </summary>
public string ConnectionUri { get; set; }
}

public class CreateCredentialRequestResponse { }

public class CreateCredentialRequest
{
public static readonly RequestType<CreateCredentialRequestParams, CreateCredentialRequestResponse> Type = RequestType<CreateCredentialRequestParams, CreateCredentialRequestResponse>.Create("objectManagement/createCredentialRequest");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

#nullable disable
using System.Collections.Generic;
using Microsoft.SqlTools.Hosting.Protocol.Contracts;
using Microsoft.SqlTools.Utility;

namespace Microsoft.SqlTools.ServiceLayer.ObjectManagement.Contracts
{
public class GetCredentialNamesRequestParams : GeneralRequestDetails
{
/// <summary>
/// Connection uri to database
/// </summary>
public string ConnectionUri { get; set; }
}


public class GetCredentialNamesRequest
{
public static readonly RequestType<GetCredentialNamesRequestParams, List<string>> Type = RequestType<GetCredentialNamesRequestParams, List<string>>.Create("objectManagement/getCredentialNamesRequest");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ public void InitializeService(IProtocolEndpoint serviceHost)
this.serviceHost = serviceHost;
this.serviceHost.SetRequestHandler(RenameRequest.Type, HandleRenameRequest, true);
this.serviceHost.SetRequestHandler(DropRequest.Type, HandleDropRequest, true);
this.serviceHost.SetRequestHandler(CreateCredentialRequest.Type, HandleCreateCredentialRequest, true);
this.serviceHost.SetRequestHandler(GetCredentialNamesRequest.Type, HandleGetCredentialNamesRequest, true);
this.serviceHost.SetRequestHandler(InitializeViewRequest.Type, HandleInitializeViewRequest, true);
this.serviceHost.SetRequestHandler(SaveObjectRequest.Type, HandleSaveObjectRequest, true);
this.serviceHost.SetRequestHandler(ScriptObjectRequest.Type, HandleScriptObjectRequest, true);
Expand All @@ -89,6 +91,20 @@ internal async Task HandleDropRequest(DropRequestParams requestParams, RequestCo
await requestContext.SendResult(new DropRequestResponse());
}

internal async Task HandleCreateCredentialRequest(CreateCredentialRequestParams requestParams, RequestContext<CreateCredentialRequestResponse> requestContext)
{
var handler = this.GetObjectTypeHandler(SqlObjectType.Credential) as CredentialHandler;
await handler.Create(requestParams);
await requestContext.SendResult(new CreateCredentialRequestResponse());
}

internal async Task HandleGetCredentialNamesRequest(GetCredentialNamesRequestParams requestParams, RequestContext<List<string>> requestContext)
{
var handler = this.GetObjectTypeHandler(SqlObjectType.Credential) as CredentialHandler;
var credentials = handler.GetCredentialNames(requestParams);
await requestContext.SendResult(credentials);
}

internal async Task HandleInitializeViewRequest(InitializeViewRequestParams requestParams, RequestContext<SqlObjectViewInfo> requestContext)
{
var handler = this.GetObjectTypeHandler(requestParams.ObjectType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
#nullable disable

using System;
using System.Collections.Generic;
using System.Data;
using System.Threading.Tasks;
using Microsoft.Data.SqlClient;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlTools.ServiceLayer.Connection;
using Microsoft.SqlTools.ServiceLayer.Management;
Expand Down Expand Up @@ -60,6 +63,36 @@ public override Task<string> Script(CredentialViewContext context, CredentialInf
throw new NotImplementedException();
}

public async Task Create(Contracts.CreateCredentialRequestParams parameters)
{
await ConfigureCredential(parameters.ConnectionUri, parameters.CredentialInfo, ConfigAction.Create, RunType.RunNow);
}

public List<string> GetCredentialNames(Contracts.GetCredentialNamesRequestParams parameters)
{
List<string> credentials = new List<string>();
ConnectionInfo connectionInfo = this.GetConnectionInfo(parameters.ConnectionUri);
using (SqlConnection sqlConn = ConnectionService.OpenSqlConnection(connectionInfo))
{
if (sqlConn != null)
{
using (var cmd = new SqlCommand { Connection = sqlConn })
{
cmd.CommandText = "SELECT [NAME] FROM sys.credentials";
cmd.ExecuteNonQuery();
using (IDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
credentials.Add(reader.GetString(0));
}
}
}
}
}
return credentials;
}

private Task<Tuple<bool, string>> ConfigureCredential(string ownerUri, CredentialInfo credential, ConfigAction configAction, RunType runType)
{
return Task<Tuple<bool, string>>.Run(() =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ public class CredentialInfo : SqlObject
public DateTime DateLastModified { get; set; }
public DateTime CreateDate { get; set; }
public string ProviderName { get; set; }
public string? Secret { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

using System;
using System.Security;
using Microsoft.SqlServer.Management.HadrModel;
using Microsoft.SqlServer.Management.Sdk.Sfc;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlTools.ServiceLayer.Management;
Expand Down Expand Up @@ -213,7 +214,8 @@ private void SendToServerCreateCredential()
smoCredential.MappedClassType = MappedClassType.CryptographicProvider;
smoCredential.ProviderName = this.providerName;
}
smoCredential.Create(this.CredentialIdentity, this.SecurePassword.ToString());
SecureString secret = this.securePassword.SecureStringToString() == String.Empty ? $"{this.credential.Secret}".StringToSecureString() : this.securePassword;
smoCredential.Create(this.CredentialIdentity, secret);
GC.Collect(); // this.SecurePassword.ToString() just created an immutable string that lives in memory
}

Expand Down

0 comments on commit d2120a4

Please sign in to comment.