Skip to content

Commit

Permalink
Merge pull request microsoft#152 from parth-007/CodeMirror/master
Browse files Browse the repository at this point in the history
Mirror ADO to GitHub
  • Loading branch information
may-hartov authored May 23, 2021
2 parents b5bbcba + 3f65ebf commit e518a14
Show file tree
Hide file tree
Showing 13 changed files with 184 additions and 142 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ namespace EncryptCredentials.Controllers
using EncryptCredentials.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Microsoft.PowerBI.Api.Models;
using Microsoft.Rest;
using System;
using Microsoft.PowerBI.Api.Models;
using Microsoft.Rest;
using System;

public class EncryptCredentialsController : Controller
{
Expand Down Expand Up @@ -39,13 +39,13 @@ public IActionResult GetDatasourcesInGroup(GetDatasourceMap getDatasourceMap)
return Ok(datasources);
}
catch (HttpOperationException ex)
{
Console.Error.WriteLine(ex.Message + "\n\n" + ex.StackTrace);
{
Console.Error.WriteLine(ex.Message + "\n\n" + ex.StackTrace);

// Set status code of the response
Response.StatusCode = (Int32)ex.Response.StatusCode;
return Content("Error " + Response.StatusCode + " " + ex.Message);
}
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.Message + "\n\n" + ex.StackTrace);
Expand All @@ -70,20 +70,20 @@ public IActionResult UpdateDatasource(UpdateDatasourceMap updateDatasourceMap)
var dataSourceRequest = new UpdateDatasourceRequest {
CredentialDetails = credentialDetails
};

// Update gateway credentials
powerBIService.UpdateDatasource(updateDatasourceMap.GatewayId, updateDatasourceMap.DatasourceId, dataSourceRequest);

return Ok("Successfully updated data source credentials");
}
catch (HttpOperationException ex)
{
Console.Error.WriteLine(ex.Message + "\n\n" + ex.StackTrace);
{
Console.Error.WriteLine(ex.Message + "\n\n" + ex.StackTrace);

// Set status code of the response
Response.StatusCode = (Int32)ex.Response.StatusCode;
return Content("Error " + Response.StatusCode + " " + ex.Message);
}
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.Message + "\n\n" + ex.StackTrace);
Expand All @@ -101,6 +101,16 @@ public IActionResult AddDatasource(AddDatasourceMap addDatasourceMap)
{
try
{
// Check for cloud gateway
var gateway = powerBIService.GetGateway(addDatasourceMap.GatewayId);

// Name is null for cloud gateway
if (string.IsNullOrWhiteSpace(gateway.Name))
{
var reason = "Add data source is not supported for cloud gateway.";
return Content("Error: " + reason);
}

// Capture Credential Details
var credentialDetails = powerBIService.GetCredentialDetails(addDatasourceMap.GatewayId, addDatasourceMap.CredentialType, addDatasourceMap.Credentials, addDatasourceMap.PrivacyLevel);

Expand All @@ -118,13 +128,13 @@ public IActionResult AddDatasource(AddDatasourceMap addDatasourceMap)
return Ok("Successfully added data source with ID: " + datasource.Id);
}
catch (HttpOperationException ex)
{
Console.Error.WriteLine(ex.Message + "\n\n" + ex.StackTrace);
{
Console.Error.WriteLine(ex.Message + "\n\n" + ex.StackTrace);

// Set status code of the response
Response.StatusCode = (Int32)ex.Response.StatusCode;
return Content("Error " + Response.StatusCode + " " + ex.Message);
}
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.Message + "\n\n" + ex.StackTrace);
Expand All @@ -148,13 +158,13 @@ public IActionResult EncryptCredentials(EncryptCredentialsMap encryptCredentials
return Ok(credentialDetails.Credentials);
}
catch (HttpOperationException ex)
{
Console.Error.WriteLine(ex.Message + "\n\n" + ex.StackTrace);
{
Console.Error.WriteLine(ex.Message + "\n\n" + ex.StackTrace);

// Set status code of the response
Response.StatusCode = (Int32)ex.Response.StatusCode;
return Content("Error " + Response.StatusCode + " " + ex.Message);
}
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.Message + "\n\n" + ex.StackTrace);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ namespace EncryptCredentials.Services
{
using EncryptCredentials.Models;
using Microsoft.PowerBI.Api;
using Microsoft.PowerBI.Api.Extensions;
using Microsoft.PowerBI.Api.Models;
using Microsoft.PowerBI.Api.Extensions;
using Microsoft.PowerBI.Api.Models;
using Microsoft.PowerBI.Api.Models.Credentials;
using Microsoft.Rest;
using System;

public class PowerBIService
public class PowerBIService
{
private readonly AadService aadService;
private readonly string urlPowerBiServiceApiRoot = "https://api.powerbi.com";
private readonly string urlPowerBiServiceApiRoot = "https://api.powerbi.com";

public PowerBIService(AadService aadService)
{
Expand All @@ -30,7 +30,7 @@ public PowerBIService(AadService aadService)
public PowerBIClient GetPowerBIClient()
{
var tokenCredentials = new TokenCredentials(aadService.GetAccessToken(), "Bearer");
return new PowerBIClient(new Uri(urlPowerBiServiceApiRoot ), tokenCredentials);
return new PowerBIClient(new Uri(urlPowerBiServiceApiRoot), tokenCredentials);
}

/// <summary>
Expand All @@ -52,15 +52,13 @@ public Datasources GetDatasourcesInGroup(Guid groupId, Guid datasetId)
/// Get Gateway public key
/// </summary>
/// <param name="gatewayId">Gateway Id of corresponding Dataset</param>
/// <returns>Public key of corresponding gateway</returns>
public GatewayPublicKey GetGatewayPublicKey(Guid gatewayId)
/// <returns>Corresponding gateway</returns>
public Gateway GetGateway(Guid gatewayId)
{
PowerBIClient pbiClient = this.GetPowerBIClient();

// Get gateway info
var gateway = pbiClient.Gateways.GetGateway(gatewayId);

return gateway.PublicKey;
// Get gateway info and return
return pbiClient.Gateways.GetGateway(gatewayId);
}

/// <summary>
Expand All @@ -71,30 +69,30 @@ public GatewayPublicKey GetGatewayPublicKey(Guid gatewayId)
/// <returns>Credentials for updating the datasource</returns>
public CredentialsBase GetCredentials(string credentialType, string[] credentialsArray)
{
CredentialsBase credentials;

// Capture credentials based on credential type selected by the user
switch(credentialType)
{
case Constants.KeyCredentials:
credentials = new KeyCredentials(key: credentialsArray[0]);
break;
case Constants.BasicCredentials:
credentials = new BasicCredentials(username: credentialsArray[0], password: credentialsArray[1]);
break;
case Constants.OAuth2Credentials:
credentials = new OAuth2Credentials(accessToken: credentialsArray[0]);
break;
case Constants.WindowsCredentials:
credentials = new WindowsCredentials(username: credentialsArray[0], password: credentialsArray[1]);
break;
default:
Console.Error.WriteLine(Constants.InvalidCredType);
throw new Exception(Constants.InvalidCredType);
}

return credentials;
}
CredentialsBase credentials;

// Capture credentials based on credential type selected by the user
switch (credentialType)
{
case Constants.KeyCredentials:
credentials = new KeyCredentials(key: credentialsArray[0]);
break;
case Constants.BasicCredentials:
credentials = new BasicCredentials(username: credentialsArray[0], password: credentialsArray[1]);
break;
case Constants.OAuth2Credentials:
credentials = new OAuth2Credentials(accessToken: credentialsArray[0]);
break;
case Constants.WindowsCredentials:
credentials = new WindowsCredentials(username: credentialsArray[0], password: credentialsArray[1]);
break;
default:
Console.Error.WriteLine(Constants.InvalidCredType);
throw new Exception(Constants.InvalidCredType);
}

return credentials;
}

/// <summary>
/// Get credential details
Expand All @@ -111,19 +109,27 @@ public CredentialDetails GetCredentialDetails(Guid gatewayId, string credentialT
// Capture credentials based on credential type selected by the user
var credentials = GetCredentials(credentialType, credentialsArray);

// Get Public Key
var publicKey = GetGatewayPublicKey(gatewayId);
// Get the Getway
var gateway = GetGateway(gatewayId);

// Initialize credentialsEncryptor and encryptedConnection for Cloud gateway
var credentialsEncryptor = (AsymmetricKeyEncryptor)null;
var encryptedConnection = EncryptedConnection.NotEncrypted;

// Create Asymmetric key Encryptor
var credentialsEncryptor = new AsymmetricKeyEncryptor(publicKey);
// Name is present in case of on-premises gateway
if (!string.IsNullOrWhiteSpace(gateway.Name))
{
// Update for On-premises gateway
encryptedConnection = EncryptedConnection.Encrypted;
credentialsEncryptor = new AsymmetricKeyEncryptor(gateway.PublicKey);
}

// Capture Credential Details
var credentialDetails = new CredentialDetails(
credentials,
privacyLevel: privacyLevel,
EncryptedConnection.Encrypted,
credentialsEncryptor
);
credentials,
privacyLevel: privacyLevel,
encryptedConnection,
credentialsEncryptor);

return credentialDetails;
}
Expand All @@ -147,7 +153,7 @@ public void UpdateDatasource(Guid gatewayId, Guid datasourceId, UpdateDatasource
/// Add Datasource in corresponding Power BI Gateway
/// </summary>
/// <param name="gatewayId">Gateway Id of corresponding Dataset</param>
/// <param name="publishDatasourceToGatewayRequest">Request body for Add Datasourcce API</param>
/// <param name="publishDatasourceToGatewayRequest">Request body for Add Datasource API</param>
public GatewayDatasource AddDatasource(Guid gatewayId, PublishDatasourceToGatewayRequest publishDatasourceToGatewayRequest)
{
PowerBIClient pbiClient = this.GetPowerBIClient();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ public ResponseEntity<?> addDataSourceController(@RequestBody AddDatasourceReque

Gateway gateway = GetDatasourceData.getGateway(accessToken, request.gatewayId);

if (gateway.name == null) {
throw new Exception("Error: Add data source is not supported for cloud gateway.");
}
return AddCredentialsService.addDataSource(
accessToken,
request.gatewayId,
Expand Down Expand Up @@ -150,12 +153,19 @@ public ResponseEntity<String> encryptCredentialsController(@RequestBody UpdateDa

// Serialize credentials for encryption
String serializedCredentials = Utils.serializeCredentials(request.credentialsArray, request.credType);

// Encrypt the credentials Asymmetric Key Encryption
AsymmetricKeyEncryptorService credentialsEncryptor = new AsymmetricKeyEncryptorService(selectedGateway.publicKey);
String encryptedCredentialsString = credentialsEncryptor.encodeCredentials(serializedCredentials);

return ResponseEntity.status(HttpStatus.OK).body(encryptedCredentialsString);
String credentials = null;

// On-premises gateway contains name property
if(selectedGateway.name != null) {
// Encrypt the credentials Asymmetric Key Encryption if on-premise gateway is used
AsymmetricKeyEncryptorService credentialsEncryptor = new AsymmetricKeyEncryptorService(selectedGateway.publicKey);
credentials = credentialsEncryptor.encodeCredentials(serializedCredentials);
} else {
// Return serialized data in case of cloud gateway
credentials = serializedCredentials;
}

return ResponseEntity.status(HttpStatus.OK).body(credentials);
} catch (HttpClientErrorException hcex) {
return generateResponseForException(hcex);
} catch (Exception ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ public class CredentialDetails {

public String privacyLevel;

public CredentialDetails(String credentialType, String serializedCredentials, String privacyLevel) {
public CredentialDetails(String credentialType, String serializedCredentials, String encryptedConnection, String privacyLevel) {
this.credentialType = credentialType;
this.credentials = serializedCredentials;
this.encryptedConnection = "Encrypted";
this.encryptedConnection = encryptedConnection;
this.encryptionAlgorithm = "RSA-OAEP";
this.privacyLevel = privacyLevel;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
package com.encryptcredentialsample.encryptcredential.models;

public class Gateway {
public String name;
public GatewayPublicKey publicKey;
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ public static ResponseEntity<String> addDataSource(
// Encrypt the credentials Asymmetric Key Encryption
AsymmetricKeyEncryptorService credentialsEncryptor = new AsymmetricKeyEncryptorService(pubKey);
String encryptedCredentialsString = credentialsEncryptor.encodeCredentials(serializedCredentials);

// Credential Details class object for request body
CredentialDetails credentialDetails = new CredentialDetails(credType, encryptedCredentialsString, privacyLevel);
PublishDatasourceToGatewayRequest requestBodyObjKey = new PublishDatasourceToGatewayRequest(dataSourceType, connectionDetails, credentialDetails, dataSourceName);
CredentialDetails credentialDetails = new CredentialDetails(credType, encryptedCredentialsString, "Encrypted", privacyLevel);

PublishDatasourceToGatewayRequest requestBodyObjKey = new PublishDatasourceToGatewayRequest(dataSourceType, connectionDetails, credentialDetails, dataSourceName);

return makeAddDataSourcePostRequest(gatewayId, requestBodyObjKey, accessToken);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import com.encryptcredentialsample.encryptcredential.models.CredentialDetails;
import com.encryptcredentialsample.encryptcredential.models.CredentialDetailsRequestBody;
import com.encryptcredentialsample.encryptcredential.models.Gateway;
import com.encryptcredentialsample.encryptcredential.models.GatewayPublicKey;

public class UpdateCredentialsService {
Expand All @@ -33,12 +34,26 @@ public static ResponseEntity<String> updateDatasource(
// Serialize credentials for encryption
String serializedCredentials = Utils.serializeCredentials(credentialsArray, credType);

// Encrypt the credentials Asymmetric Key Encryption
AsymmetricKeyEncryptorService credentialsEncryptor = new AsymmetricKeyEncryptorService(pubKey);
String encryptedCredentialsString = credentialsEncryptor.encodeCredentials(serializedCredentials);
String encryptedCredentialsString = null;

// Credential Details class object for request body
CredentialDetails credentialDetails = new CredentialDetails(credType, encryptedCredentialsString, privacyLevel);
Gateway gateway = GetDatasourceData.getGateway(accessToken, gatewayId);
String encryptedConnection = null;

// On-premises gateway contains name property
// Use on-premises gateway
if (gateway.name != null) {
encryptedConnection = "Encrypted";
// Encrypt the credentials Asymmetric Key Encryption
AsymmetricKeyEncryptorService credentialsEncryptor = new AsymmetricKeyEncryptorService(pubKey);
encryptedCredentialsString = credentialsEncryptor.encodeCredentials(serializedCredentials);
} else {
// Use cloud gateway
encryptedConnection = "NotEncrypted";
encryptedCredentialsString = serializedCredentials;
}

// Credential Details class object for request body
CredentialDetails credentialDetails = new CredentialDetails(credType, encryptedCredentialsString, encryptedConnection, privacyLevel);

// Converting CredentialDetails class object to json string
CredentialDetailsRequestBody requestBodyObj = new CredentialDetailsRequestBody(credentialDetails);
Expand Down
Loading

0 comments on commit e518a14

Please sign in to comment.