public void CheckIfCertificateIsprivate(TEntity item, string certificateName, IntegrationAccountDetails iaDetails) { try { string filepath = FileOperations.GetCertificateJsonFilePath(certificateName); string content = File.ReadAllText(filepath); var certificate = JsonConvert.DeserializeObject <JsonCertificate.Rootobject>(content); if (certificate.properties.key != null) { string keyvaultname = iaDetails.KeyVaultName; if (string.IsNullOrEmpty(keyvaultname)) { throw new Exception("Couldn't find the name of the Key Vault to upload the Private key for the Private Certificate. Make sure you selected a Key Vault at the Integration Account Details Screen"); } string certName = FileOperations.GetFileName(certificateName); string keyvaultfilepath = FileOperations.GetKeyVaultJsonFilePath(certName + "Privatekey"); IntegrationAccountContext sclient = new IntegrationAccountContext(); AuthenticationResult authresult = thisApplicationContext.GetProperty("KeyVaultAuthorization") as AuthenticationResult; string kid = sclient.UploadCertificatePrivateKeyToKeyVault(StringOperations.RemoveAllSpecialCharacters(certName) + "Privatekey", keyvaultname, keyvaultfilepath, authresult); certificate.properties.key.keyName = StringOperations.RemoveAllSpecialCharacters(certName) + "Privatekey"; certificate.properties.key.keyVersion = kid; certificate.properties.key.keyVault.id = string.Format(ConfigurationManager.AppSettings["KeyVaultResourceIdTemplate"], iaDetails.SubscriptionId, iaDetails.ResourceGroupName, keyvaultname); //certificate.properties.key.keyVault.name = certName + "Privatekey"; string fileName = FileOperations.GetCertificateJsonFilePath(certName); string partnerJsonFileContent = JsonConvert.SerializeObject(certificate); File.WriteAllText(fileName, partnerJsonFileContent); } } catch (Exception ex) { SetStatus(item, MigrationStatus.Failed, string.Format("Certificate Migration Failed. Reason:{0}", ExceptionHelper.GetExceptionMessage(ex))); TraceProvider.WriteLine(string.Format("Certificate Migration Failed. Reason:{0}", ExceptionHelper.GetExceptionMessage(ex))); TraceProvider.WriteLine(); throw ex; } }
public JsonSchema.RootObject CreateSchemas(SchemaMigrationItemViewModel schemaItem) { var schema = schemaItem.MigrationEntity; string schemaName = schema.fullNameOfSchemaToUpload; JsonSchema.RootObject schemaRootObject = new JsonSchema.RootObject(); try { string directroyPathForJsonFiles = Resources.JsonSchemaFilesLocalPath; string fileName = string.Format(directroyPathForJsonFiles, FileOperations.GetFileName(schemaName), ".json"); string schemaJsonFileContent = Newtonsoft.Json.JsonConvert.SerializeObject(schemaRootObject); FileOperations.CreateFolder(fileName); System.IO.File.WriteAllText(fileName, schemaJsonFileContent); return(schemaRootObject); } catch (Exception ex) { schemaItem.ImportStatus = MigrationStatus.Failed; schemaItem.ImportStatusText = ex.Message; TraceProvider.WriteLine(string.Format("Schema Export to Json Failed:{0}", ExceptionHelper.GetExceptionMessage(ex))); TraceProvider.WriteLine(); throw ex; } }
public void CreateCertificates(string certName, string certThumbprint) { try { X509Certificate2 partnerCert; string partnerCertBase64String; bool certFound = false; X509Certificate2Collection collection = new X509Certificate2Collection(); //BiztalkServerDetails BizTalkServerDetails = this.thisApplicationContext.GetService<BiztalkServerDetails>(); //string serverName = BizTalkServerDetails.RemoteServerName; //if (BizTalkServerDetails.UseDifferentAccount) //{ // string domain = BizTalkServerDetails.RemoteDomainName; // string username = BizTalkServerDetails.RemoteUserName; // string password = BizTalkServerDetails.RemoteUserPassword; // using (UserImpersonation user = new UserImpersonation(username, domain, password)) // { // if (user.ImpersonateValidUser()) // { //collection = GetCertificate(certThumbprint, serverName); //if (collection.Count == 0) //{ // certFound = false; //} //else //{ // certFound = true; //} //} //else //{ // throw new Exception(string.Format(@"Failed to read Certificates from the Server {0}\{1}. Invalid Credentails used to read Certificates.", domain, username)); //} // } // } //else //{ string serverName = System.Environment.MachineName; collection = GetCertificate(certThumbprint, serverName); if (collection.Count == 0) { certFound = false; } else { certFound = true; } // } if (certFound) { partnerCert = collection[0]; partnerCertBase64String = Convert.ToBase64String(partnerCert.GetRawCertData()); JsonCertificate.Rootobject certificateRootObject = new JsonCertificate.Rootobject() { name = FileOperations.GetFileName(certName), properties = new JsonCertificate.Properties() { publicCertificate = partnerCertBase64String, metadata = GenerateMetadata(certName) } }; if (partnerCert.HasPrivateKey) { //CHECK IF IT's EXPORTABLE, and ensure you write traces certificateRootObject.properties.key = new JsonCertificate.Key() { keyName = "<KeyNameHere>", keyVersion = "<KeyVersionHere>", keyVault = new JsonCertificate.KeyVault() { id = "<ResourceUriHere>" } }; //TODO: Create KeyVault Secret JSON. string keyvaultjson = this.GetKeyVaultSecretJsonForPrivateKey(partnerCert); string keyvaultfilename = FileOperations.GetKeyVaultJsonFilePath(certName + "Privatekey"); FileOperations.CreateFolder(keyvaultfilename); System.IO.File.WriteAllText(keyvaultfilename, keyvaultjson); } string fileName = FileOperations.GetCertificateJsonFilePath(certName); string partnerJsonFileContent = Newtonsoft.Json.JsonConvert.SerializeObject(certificateRootObject); FileOperations.CreateFolder(fileName); System.IO.File.WriteAllText(fileName, partnerJsonFileContent); TraceProvider.WriteLine(string.Format("Certificate {0} for partner has been succesfully exported to Json with Name {1}", certName, FileOperations.GetFileName(certName))); } else { TraceProvider.WriteLine(string.Format("Certificate Export to Json Failed. Reason : Certificate {0} Not Found in LocalCertificate store in Server {1}", certName, serverName)); throw new Exception(string.Format("Certificate {0} Not Found in LocalCertificate store in Server {1}", certName, serverName)); } } catch (Exception ex) { throw ex; } }
public async Task ExportToIA(TEntity item, IntegrationAccountDetails iaDetails) { this.iaDetails = iaDetails; var serverPartnerItem = item as PartnerMigrationItemViewModel; var certItem = item as Certificate; string certName = string.Empty; if (serverPartnerItem != null) { certName = serverPartnerItem.MigrationEntity.CertificateName; } else if (certItem != null) { certName = certItem.certificateName; } try { TraceProvider.WriteLine("Migrating certificate : {0}", certName); await Task.Factory.StartNew(() => { try { AuthenticationResult authresult = thisApplicationContext.GetProperty("IntegrationAccountAuthorization") as AuthenticationResult; bool overwrite = Convert.ToBoolean(thisApplicationContext.GetProperty("OverwriteEnabled")); if (!overwrite) { bool exists = CheckIfCertificateExists(certName, this.iaDetails, authresult).Result; if (exists) { SetStatus(item, MigrationStatus.Partial, string.Format("The Certificate {0} already exists on IA with name {1}. Since the Overwrite option was disabled, the certificate was not overwritten.", certName, FileOperations.GetFileName(certName))); TraceProvider.WriteLine(string.Format("The Certificate {0} already exists on IA with name {1}. Since the Overwrite option was disabled, the certificate was not overwritten.", certName, FileOperations.GetFileName(certName))); TraceProvider.WriteLine(); } else { CheckIfCertificateIsprivate(item, certName, iaDetails); MigrateToCloudIA(FileOperations.GetCertificateJsonFilePath(certName), FileOperations.GetFileName(certName), item, iaDetails, authresult).Wait(); SetStatus(item, MigrationStatus.Succeeded, string.Format("Certificate {0} migrated succesfully", certName)); TraceProvider.WriteLine(string.Format("Certificate Migration Successfull: {0}", certName)); TraceProvider.WriteLine(); } } else { CheckIfCertificateIsprivate(item, certName, iaDetails); MigrateToCloudIA(FileOperations.GetCertificateJsonFilePath(certName), FileOperations.GetFileName(certName), item, iaDetails, authresult).Wait(); SetStatus(item, MigrationStatus.Succeeded, string.Format("Certificate {0} migrated succesfully", certName)); TraceProvider.WriteLine(string.Format("Certificate Migration Successfull: {0}", certName)); TraceProvider.WriteLine(); } } catch (Exception) { //throw ex; } }); } catch (Exception ex) { throw ex; } }
public JsonPartner.Rootobject CreatePartners(PartnerMigrationItemViewModel partnerItem) { var partner = partnerItem.MigrationEntity; string partnerName = partner.Name; JsonPartner.Rootobject partnerRootObject = new JsonPartner.Rootobject(); #region Certificates try { if (partner.CertificateName != null && partner.CertificateThumbprint != null) { try { certificateRequired = true; CerificateMigrator <PartnerMigrationItemViewModel> cMigrator = new CerificateMigrator <PartnerMigrationItemViewModel>(thisApplicationContext); cMigrator.CreateCertificates(partner.CertificateName, partner.CertificateThumbprint); succesfullCertificateImport = MigrationStatus.Succeeded; } catch (Exception) { certificateRequired = true; succesfullCertificateImport = MigrationStatus.Failed; } } else { certificateRequired = false; succesfullCertificateImport = MigrationStatus.NotStarted; TraceProvider.WriteLine(string.Format("No certificate is configured with the Partner")); } } catch (Exception ex) { certificateRequired = true; succesfullCertificateImport = MigrationStatus.Failed; TraceProvider.WriteLine(string.Format("Certificate Export to Json Failed:{0}", ExceptionHelper.GetExceptionMessage(ex))); } #endregion #region Partner try { var businessProfile = partner.GetBusinessProfiles().ToList <Server.BusinessProfile>(); List <Microsoft.BizTalk.B2B.PartnerManagement.BusinessIdentity> businessIdentities = new List <Microsoft.BizTalk.B2B.PartnerManagement.BusinessIdentity>(); foreach (var item in businessProfile) { businessIdentities.AddRange(item.GetBusinessIdentities().ToList()); } partnerRootObject = new JsonPartner.Rootobject() { id = "", name = FileOperations.GetFileName(partnerName), type = Resources.JsonPartnerType, properties = new JsonPartner.Properties() { partnerType = Resources.JsonPartnerTypeB2B, content = new JsonPartner.Content() { b2b = new JsonPartner.B2b() { businessIdentities = (businessIdentities.Select(delegate(BizTalk.B2B.PartnerManagement.BusinessIdentity bi) { return(new JsonPartner.Businessidentity() { qualifier = ((Microsoft.BizTalk.B2B.PartnerManagement.QualifierIdentity)bi).Qualifier, value = ((Microsoft.BizTalk.B2B.PartnerManagement.QualifierIdentity)bi).Value }); } )).ToArray() } }, createdTime = DateTime.Now, changedTime = DateTime.Now, metadata = GenerateMetadata(partnerName) } }; string directroyPathForJsonFiles = Resources.JsonPartnerFilesLocalPath; string fileName = string.Format(directroyPathForJsonFiles, FileOperations.GetFileName(partnerName), ".json"); string partnerJsonFileContent = Newtonsoft.Json.JsonConvert.SerializeObject(partnerRootObject); FileOperations.CreateFolder(fileName); System.IO.File.WriteAllText(fileName, partnerJsonFileContent); return(partnerRootObject); } catch (Exception ex) { partnerItem.ImportStatus = MigrationStatus.Failed; partnerItem.ImportStatusText = ex.Message; TraceProvider.WriteLine(string.Format("Partner Export to Json Failed:{0}", ExceptionHelper.GetExceptionMessage(ex))); TraceProvider.WriteLine(); throw ex; } #endregion }
public override async Task ImportAsync(PartnerMigrationItemViewModel serverPartnerItem) { try { var serverPartner = serverPartnerItem.MigrationEntity; TraceProvider.WriteLine(); TraceProvider.WriteLine("Exporting Partner to Json: {0}", serverPartnerItem.MigrationEntity.Name); serverPartnerItem.ImportStatus = MigrationStatus.NotStarted; serverPartnerItem.ExportStatus = MigrationStatus.NotStarted; serverPartnerItem.ImportStatusText = null; serverPartnerItem.ExportStatusText = null; serverPartnerItem.CertificationRequired = false; serverPartnerItem.CertificateImportStatus = MigrationStatus.NotStarted; serverPartnerItem.CertificateExportStatus = MigrationStatus.NotStarted; serverPartnerItem.ExportStatusText = null; await Task.Factory.StartNew(() => { try { CreatePartners(serverPartnerItem); serverPartnerItem.ImportStatus = MigrationStatus.Succeeded; StringBuilder successMessageText = new StringBuilder(); successMessageText.Append(string.Format(Resources.ImportSuccessMessageText, serverPartnerItem.MigrationEntity.Name)); serverPartnerItem.ImportStatusText = successMessageText.ToString(); serverPartnerItem.CertificationRequired = certificateRequired; serverPartnerItem.CertificateImportStatus = succesfullCertificateImport; TraceProvider.WriteLine(string.Format("Partner {0} exported to Json with Name {1}", serverPartnerItem.MigrationEntity.Name, FileOperations.GetFileName(serverPartnerItem.MigrationEntity.Name))); TraceProvider.WriteLine(string.Format("Partner Export to Json Successfull: {0}", serverPartner.Name)); TraceProvider.WriteLine(); } catch (Exception) { //throw ex; } }); } catch (Exception ex) { throw ex; } }
public override async Task ExportToIA(PartnerMigrationItemViewModel serverPartnerItem, IntegrationAccountDetails iaDetails) { this.iaDetails = iaDetails; try { TraceProvider.WriteLine("Migrating partner : {0}", serverPartnerItem.MigrationEntity.Name); await Task.Factory.StartNew(() => { try { AuthenticationResult authresult = thisApplicationContext.GetProperty("IntegrationAccountAuthorization") as AuthenticationResult; bool overwrite = Convert.ToBoolean(thisApplicationContext.GetProperty("OverwriteEnabled")); if (!overwrite) { bool exists = CheckIfArtifactExists(serverPartnerItem.MigrationEntity.Name, "Partner", this.iaDetails, authresult).Result; if (exists) { serverPartnerItem.ExportStatus = MigrationStatus.Partial; serverPartnerItem.ExportStatusText = string.Format("The Partner {0} already exists on IA with name {1}. Since the Overwrite option was disabled, the partner was not overwritten.", serverPartnerItem.MigrationEntity.Name, FileOperations.GetFileName(serverPartnerItem.MigrationEntity.Name)); TraceProvider.WriteLine(serverPartnerItem.ExportStatusText); TraceProvider.WriteLine(); } else { MigrateToCloudIAPartner(FileOperations.GetPartnerJsonFilePath(serverPartnerItem.MigrationEntity.Name), FileOperations.GetFileName(serverPartnerItem.MigrationEntity.Name), serverPartnerItem, iaDetails, authresult).Wait(); serverPartnerItem.ExportStatus = MigrationStatus.Succeeded; StringBuilder successMessageText = new StringBuilder(); successMessageText.Append(string.Format(Resources.ExportSuccessMessageText, serverPartnerItem.MigrationEntity.Name)); serverPartnerItem.ExportStatusText = successMessageText.ToString(); TraceProvider.WriteLine(string.Format("Partner Migration Successfull: {0}", serverPartnerItem.MigrationEntity.Name)); TraceProvider.WriteLine(); } } else { MigrateToCloudIAPartner(FileOperations.GetPartnerJsonFilePath(serverPartnerItem.MigrationEntity.Name), FileOperations.GetFileName(serverPartnerItem.MigrationEntity.Name), serverPartnerItem, iaDetails, authresult).Wait(); serverPartnerItem.ExportStatus = MigrationStatus.Succeeded; StringBuilder successMessageText = new StringBuilder(); successMessageText.Append(string.Format(Resources.ExportSuccessMessageText, serverPartnerItem.MigrationEntity.Name)); serverPartnerItem.ExportStatusText = successMessageText.ToString(); TraceProvider.WriteLine(string.Format("Partner Migration Successfull: {0}", serverPartnerItem.MigrationEntity.Name)); TraceProvider.WriteLine(); } } catch (Exception) { //throw ex; } }); } catch (Exception) { //throw ex; } }
public static string GetMapUrl(string mapname, IntegrationAccountDetails iaDetails) { string url = String.Format(ConfigurationManager.AppSettings["MapUrl"], iaDetails.SubscriptionId, iaDetails.ResourceGroupName, iaDetails.IntegrationAccountName, FileOperations.GetFileName(mapname)); return(url); }
public override async Task ImportAsync(SchemaMigrationItemViewModel serverSchemaItem) { try { var serverSchema = serverSchemaItem.MigrationEntity; TraceProvider.WriteLine(); TraceProvider.WriteLine("Exporting Schema to Json: {0}", serverSchemaItem.MigrationEntity.fullNameOfSchemaToUpload); serverSchemaItem.ImportStatus = MigrationStatus.NotStarted; serverSchemaItem.ExportStatus = MigrationStatus.NotStarted; serverSchemaItem.ImportStatusText = null; serverSchemaItem.ExportStatusText = null; await Task.Factory.StartNew(() => { try { CreateSchemas(serverSchemaItem); serverSchemaItem.ImportStatus = MigrationStatus.Succeeded; StringBuilder successMessageText = new StringBuilder(); successMessageText.Append(string.Format(Resources.ImportSuccessMessageText, serverSchemaItem.MigrationEntity.fullNameOfSchemaToUpload)); serverSchemaItem.ImportStatusText = successMessageText.ToString(); TraceProvider.WriteLine(string.Format("Schema {0} exported to Json with Name {1}", serverSchemaItem.MigrationEntity.fullNameOfSchemaToUpload, FileOperations.GetFileName(serverSchemaItem.MigrationEntity.fullNameOfSchemaToUpload))); TraceProvider.WriteLine(string.Format("Schema Export to Json Successfull: {0}", serverSchemaItem.Name)); TraceProvider.WriteLine(); } catch (Exception) { //throw ex; } }); } catch (Exception ex) { throw ex; } }
public override async Task ExportToIA(SchemaMigrationItemViewModel serverSchemaItem, IntegrationAccountDetails iaDetails) { this.iaDetails = iaDetails; try { TraceProvider.WriteLine("Migrating schema : {0}", serverSchemaItem.MigrationEntity.fullNameOfSchemaToUpload); await Task.Factory.StartNew(() => { try { AuthenticationResult authresult = thisApplicationContext.GetProperty("IntegrationAccountAuthorization") as AuthenticationResult; bool overwrite = Convert.ToBoolean(thisApplicationContext.GetProperty("OverwriteEnabled")); if (!overwrite) { bool exists = CheckIfArtifactExists(serverSchemaItem.MigrationEntity.fullNameOfSchemaToUpload, "Schema", this.iaDetails, authresult).Result; if (exists) { serverSchemaItem.ExportStatus = MigrationStatus.Partial; serverSchemaItem.ExportStatusText = string.Format("The Schema {0} already exists on IA with name {1}. Since the Overwrite option was disabled, the certificate was not overwritten.", serverSchemaItem.MigrationEntity.fullNameOfSchemaToUpload, FileOperations.GetFileName(serverSchemaItem.MigrationEntity.fullNameOfSchemaToUpload)); TraceProvider.WriteLine(serverSchemaItem.ExportStatusText); TraceProvider.WriteLine(); } else { MigrateToCloudIA(FileOperations.GetSchemaJsonFilePath(serverSchemaItem.MigrationEntity.fullNameOfSchemaToUpload), FileOperations.GetFileName(serverSchemaItem.MigrationEntity.fullNameOfSchemaToUpload), serverSchemaItem, iaDetails, authresult).Wait(); serverSchemaItem.ExportStatus = MigrationStatus.Succeeded; serverSchemaItem.ExportStatusText = string.Format("Schema {0} migrated succesfully", serverSchemaItem.MigrationEntity.fullNameOfSchemaToUpload); TraceProvider.WriteLine(string.Format("Schema Migration Successfull: {0}", serverSchemaItem.MigrationEntity.fullNameOfSchemaToUpload)); TraceProvider.WriteLine(); } } else { MigrateToCloudIA(FileOperations.GetSchemaJsonFilePath(serverSchemaItem.MigrationEntity.fullNameOfSchemaToUpload), FileOperations.GetFileName(serverSchemaItem.MigrationEntity.fullNameOfSchemaToUpload), serverSchemaItem, iaDetails, authresult).Wait(); serverSchemaItem.ExportStatus = MigrationStatus.Succeeded; serverSchemaItem.ExportStatusText = string.Format("Schema {0} migrated succesfully", serverSchemaItem.MigrationEntity.fullNameOfSchemaToUpload); TraceProvider.WriteLine(string.Format("Schema Migration Successfull: {0}", serverSchemaItem.MigrationEntity.fullNameOfSchemaToUpload)); TraceProvider.WriteLine(); } } catch (Exception) { //throw ex; } }); } catch (Exception ex) { throw ex; } }