public static void Encode(IByteWriter stream, EnvelopeType value) { XdrEncoding.EncodeInt32((int)value.InnerValue, stream); }
public OvfStorageResource(RASD_Type type, EnvelopeType envelopeType) { rasd = type; envelope = envelopeType; file = OVF.FindFileReferenceByRASD(envelope, rasd); }
/// <summary> /// /// </summary> /// <param name="env"></param> /// <param name="ovfilename"></param> /// <param name="method"></param> /// <param name="compress"></param> public void CompressOvfFiles(EnvelopeType env, string ovfilename, string method, bool compress) { ProcessCompression(env, Path.GetDirectoryName(ovfilename), method, compress); OVF.SaveAs(env, ovfilename); }
// Find a name to use of a VM within an envelope that could have come from any hypervisor. // TODO: Consider refactoring this method because it is very similar to OVF.FindSystemName(). private string FindVMName(EnvelopeType ovfEnv, string systemid) { VirtualSystem_Type vSystem = OVF.FindVirtualSystemById(ovfEnv, systemid); // Use the given name if present and valid. // The given name is Envelope.VirtualSystem.Name specified in the OVF Specification 1.1 clause 7.2. // XenServer sets the name property. // vSphere 4.1 and Virtual Box 4.0.6 do not. if ((Tools.ValidateProperty("Name", vSystem)) && !String.IsNullOrEmpty(vSystem.Name[0].Value)) { return(vSystem.Name[0].Value); } // The VM wasn't given a name. // Build a list of choices from various properties. var choices = new List <string>(); // VirtualSystem.id is next preference because vSphere and Virtual Box typically set this property to the VM name. if (!string.IsNullOrEmpty(vSystem.id)) { choices.Add(vSystem.id); } // VirtualHardwareSection_Type.VirtualSystemIdentifier is next preference because Virtual Box will also set this property to the VM name. VirtualHardwareSection_Type[] vhsList = OVF.FindVirtualHardwareSection(ovfEnv, systemid); foreach (VirtualHardwareSection_Type vhs in vhsList) { if (vhs == null || vhs.System == null) { continue; } if (Tools.ValidateProperty("VirtualSystemIdentifier", vhs.System)) { choices.Add(vhs.System.VirtualSystemIdentifier.Value); } } // Operating system description is next preference. OperatingSystemSection_Type[] ossList = OVF.FindSections <OperatingSystemSection_Type>(vSystem.Items); foreach (OperatingSystemSection_Type oss in ossList) { if (Tools.ValidateProperty("Description", oss)) { choices.Add(oss.Description.Value); } } // Envelope name is the last preference for XenServer that can could be a path in some cases. // vSphere and Virtual Box usually don't set this property. choices.Add(Path.GetFileNameWithoutExtension(ovfEnv.Name)); // Last preference is file name. choices.Add(Path.GetFileNameWithoutExtension(m_pageImportSource.SelectedOvfPackage.PackageSourceFile)); // First choice is one that is not a GUID. foreach (var choice in choices) { if (!String.IsNullOrEmpty(choice) && !IsGUID(choice)) { return(choice); } } // Second choice is the first GUID. foreach (var choice in choices) { if (!String.IsNullOrEmpty(choice)) { return(choice); } } // Last resort is a new GUID. return(Guid.NewGuid().ToString()); }
public OvfStorageResourceContainer(EnvelopeType selectedOvfEnvelope, string sysId) { rasdArray = OVF.FindDiskRasds(selectedOvfEnvelope, sysId); this.selectedOvfEnvelope = selectedOvfEnvelope; }
/// <summary> /// Initializes a new instance of the <see cref="CREnvelope" /> class. /// </summary> /// <param name="envelopeType">The envelope type.</param> public CREnvelope(EnvelopeType envelopeType = EnvelopeType.ADSR) : base(envelopeType) { }
protected override void UpdateWizardContent(XenTabPage page) { Type type = page.GetType(); if (type == typeof(ImportSourcePage)) { #region ImportSourcePage var oldTypeOfImport = m_typeOfImport; //store previous type m_typeOfImport = m_pageImportSource.TypeOfImport; var appliancePages = new XenTabPage[] { m_pageEula, m_pageHost, m_pageStorage, m_pageNetwork, m_pageSecurity, m_pageOptions, m_pageTvmIp }; var imagePages = new XenTabPage[] { m_pageVMconfig, m_pageHost, m_pageStorage, m_pageNetwork, m_pageOptions, m_pageTvmIp }; var xvaPages = new XenTabPage[] { m_pageXvaHost, m_pageXvaStorage, m_pageXvaNetwork }; switch (m_typeOfImport) { case ImportType.Ovf: if (oldTypeOfImport != ImportType.Ovf) { Text = Messages.WIZARD_TEXT_IMPORT_OVF; pictureBoxWizard.Image = Properties.Resources._000_ImportVirtualAppliance_h32bit_32; m_pageFinish.ShowStartVmsGroupBox = false; RemovePages(imagePages); RemovePage(m_pageBootOptions); RemovePages(xvaPages); AddAfterPage(m_pageImportSource, appliancePages); } m_pageEula.SelectedOvfEnvelope = m_pageImportSource.SelectedOvfEnvelope; m_pageSecurity.SelectedOvfPackage = m_pageImportSource.SelectedOvfPackage; CheckDisabledPages(m_pageEula, m_pageSecurity); //decide whether to disable these progress steps ResetVmMappings(m_pageImportSource.SelectedOvfEnvelope); m_pageHost.SelectedOvfEnvelope = m_pageImportSource.SelectedOvfEnvelope; m_pageHost.SetDefaultTarget(m_pageHost.ChosenItem ?? m_selectedObject); m_pageHost.VmMappings = m_vmMappings; m_pageStorage.SelectedOvfEnvelope = m_pageImportSource.SelectedOvfEnvelope; lunPerVdiMappingPage.SelectedOvfEnvelope = m_pageImportSource.SelectedOvfEnvelope; m_pageNetwork.SelectedOvfEnvelope = m_pageImportSource.SelectedOvfEnvelope; NotifyNextPagesOfChange(m_pageEula, m_pageHost, m_pageStorage, m_pageNetwork, m_pageSecurity, m_pageOptions); break; case ImportType.Vhd: if (oldTypeOfImport != ImportType.Vhd) { Text = Messages.WIZARD_TEXT_IMPORT_VHD; pictureBoxWizard.Image = Properties.Resources._000_ImportVM_h32bit_32; m_pageFinish.ShowStartVmsGroupBox = false; RemovePages(appliancePages); RemovePages(xvaPages); AddAfterPage(m_pageImportSource, imagePages); } m_pageVMconfig.IsWim = m_pageImportSource.IsWIM; m_pageHost.SetDefaultTarget(m_pageHost.ChosenItem ?? m_selectedObject); m_pageHost.SelectedOvfEnvelope = null; m_pageHost.VmMappings = m_vmMappings; NotifyNextPagesOfChange(m_pageVMconfig, m_pageHost, m_pageStorage, m_pageNetwork, m_pageOptions); break; case ImportType.Xva: if (oldTypeOfImport != ImportType.Xva) { Text = Messages.WIZARD_TEXT_IMPORT_XVA; pictureBoxWizard.Image = Properties.Resources._000_ImportVM_h32bit_32; m_pageFinish.ShowStartVmsGroupBox = true; RemovePages(imagePages); RemovePage(m_pageBootOptions); RemovePages(appliancePages); AddAfterPage(m_pageImportSource, xvaPages); } m_pageXvaHost.SetDefaultTarget(m_selectedObject); m_pageXvaStorage.FilePath = m_pageImportSource.FilePath; break; } #endregion } else if (type == typeof(ImageVMConfigPage)) { //then use it to create an ovf for the import m_envelopeFromVhd = OVF.CreateOvfEnvelope(m_pageVMconfig.VmName, m_pageVMconfig.CpuCount, m_pageVMconfig.Memory, m_pageBootOptions.BootParams, m_pageBootOptions.PlatformSettings, m_pageImportSource.DiskCapacity, m_pageImportSource.IsWIM, m_pageVMconfig.AdditionalSpace, m_pageImportSource.FilePath, m_pageImportSource.ImageLength); m_pageStorage.SelectedOvfEnvelope = m_envelopeFromVhd; lunPerVdiMappingPage.SelectedOvfEnvelope = m_envelopeFromVhd; m_pageNetwork.SelectedOvfEnvelope = m_envelopeFromVhd; ResetVmMappings(m_envelopeFromVhd); NotifyNextPagesOfChange(m_pageHost, m_pageStorage, m_pageNetwork); } else if (type == typeof(ImportSelectHostPage)) { TargetConnection = m_pageHost.ChosenItem == null ? null : m_pageHost.ChosenItem.Connection; RemovePage(m_pageRbac); ConfigureRbacPage(TargetConnection); m_vmMappings = m_pageHost.VmMappings; m_pageStorage.VmMappings = m_vmMappings; m_pageStorage.Connection = TargetConnection; m_pageNetwork.Connection = TargetConnection; m_pageOptions.Connection = TargetConnection; m_pageTvmIp.Connection = TargetConnection; RemovePage(m_pageBootOptions); if (m_typeOfImport == ImportType.Vhd && BootModesControl.ShowBootModeOptions(TargetConnection)) { AddAfterPage(m_pageNetwork, m_pageBootOptions); m_pageBootOptions.Connection = TargetConnection; } m_pageBootOptions.Connection = TargetConnection; NotifyNextPagesOfChange(m_pageStorage, m_pageNetwork, m_pageOptions, m_pageTvmIp); } else if (type == typeof(ImportSelectStoragePage)) { RemovePage(lunPerVdiMappingPage); lunPerVdiMappingPage.ClearPickerData(); m_vmMappings = m_pageStorage.VmMappings; m_pageNetwork.VmMappings = m_vmMappings; lunPerVdiMappingPage.VmMappings = m_vmMappings; if (lunPerVdiMappingPage.IsAnyPickerDataMappable && lunPerVdiMappingPage.MapLunsToVdisRequired && m_typeOfImport == ImportType.Ovf) { AddAfterPage(m_pageStorage, lunPerVdiMappingPage); } } else if (type == typeof(LunPerVdiImportPage)) { m_vmMappings = lunPerVdiMappingPage.VmMappings; m_pageNetwork.VmMappings = m_vmMappings; } else if (type == typeof(ImportSelectNetworkPage)) { m_vmMappings = m_pageNetwork.VmMappings; m_pageOptions.VmMappings = m_vmMappings; } else if (type == typeof(GlobalSelectHost)) { var con = m_pageXvaHost.SelectedHost == null ? m_pageXvaHost.SelectedConnection : m_pageXvaHost.SelectedHost.Connection; RemovePage(m_pageRbac); ConfigureRbacPage(con); m_pageXvaStorage.SetConnection(con); m_pageXvaStorage.SetTargetHost(m_ignoreAffinitySet ? null : m_pageXvaHost.SelectedHost); m_pageXvaNetwork.SetConnection(con); m_pageXvaNetwork.SetAffinity(m_pageXvaHost.SelectedHost); NotifyNextPagesOfChange(m_pageXvaStorage, m_pageXvaNetwork); } else if (type == typeof(StoragePickerPage)) { m_pageFinish.ShowStartVmsGroupBox = m_pageXvaStorage.ImportedVm != null && !m_pageXvaStorage.ImportedVm.is_a_template; m_pageXvaNetwork.SetVm(m_pageXvaStorage.ImportedVm); NotifyNextPagesOfChange(m_pageXvaNetwork); } if (type != typeof(ImportFinishPage)) { NotifyNextPagesOfChange(m_pageFinish); } }
/// <summary> /// Check the Envelope to see if the files are compressed. /// </summary> /// <param name="ovfFilename">fullpath/filename to OVF</param> /// <param name="method">out method used: Gzip | BZip2</param> /// <returns>True|False</returns> public bool IsCompressed(string ovfFilename, out string method) { EnvelopeType env = OVF.Load(ovfFilename); return(IsCompressed(env, out method)); }
private void ProcessCompression(EnvelopeType env, string path, string method, bool compress) { if (env.References != null && env.References.File != null && env.References.File.Length > 0) { foreach (File_Type file in env.References.File) { if (compress) { if (method.ToLower() != "gzip" && method.ToLower() != "bzip2") { throw new InvalidDataException(string.Format(Messages.COMPRESS_INVALID_METHOD, method)); } } else { if (file.compression == null) { log.InfoFormat("File {0} was not marked as compressed, skipped.", file.href); continue; } if (file.compression.ToLower() != "gzip" && file.compression.ToLower() != "bzip2") { log.ErrorFormat("Invalid compression method File: {0} Method: {1}, must be Gzip or BZip2", file.href, file.compression); continue; } method = file.compression; } int slash = file.href.LastIndexOf('/'); string filename = slash >= 0 ? file.href.Substring(slash + 1) : file.href; filename = Path.Combine(path, filename); string tempfile = Path.Combine(path, Path.GetFileName(Path.GetTempFileName())); try { if (method.ToLower() == "gzip") { using (var inputFile = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite, FileShare.None)) { using (var outputFile = new FileStream(tempfile, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None)) { if (compress) { GZipCompress(inputFile, outputFile); } else { GZipDecompress(inputFile, outputFile); } } inputFile.Flush(); } //the original file should be overwritten only if the process has succeeded, so it should //be done here and not in the finally block, because if compression/decompression fails //for some reason, the original file is lost File.Delete(filename); File.Move(tempfile, filename); } else { throw new InvalidDataException(string.Format(Messages.COMPRESS_INVALID_METHOD, method)); } file.compression = compress ? method : null; } catch (EndOfStreamException eose) { log.ErrorFormat("End of Stream: {0}", eose.Message); } catch (Exception ex) { if (ex is OperationCanceledException) { throw; } var message = string.Format(Messages.COMPRESS_FAILED, filename); log.ErrorFormat("{0} {1}", message, ex.Message); throw new Exception(message, ex); } finally { //in case of failure or cancellation delete the temp file; //in case of success it won't be there, but no exception is thrown File.Delete(tempfile); } FileInfo fi = new FileInfo(filename); file.size = (ulong)fi.Length; } } }
/// <summary> /// Encrypt the files associated with the provided OVF package. /// </summary> /// <param name="pathToOvf">Path to the OVF Package</param> /// <param name="ovfFileName">Filename of the ovf file.</param> /// <param name="password">password to use during encryption.</param> public void Encrypt(string pathToOvf, string ovfFileName, string password) { string filename = Path.Combine(pathToOvf, ovfFileName); EnvelopeType env = Tools.LoadOvfXml(filename); Encrypt(env, filename, password); }
private static void CryptoFileWrapper(EnvelopeType env, string ovffilename, string password, bool encrypt) { bool process = true; if ((env.References == null) || (env.References.File == null) || (env.References.File.Length == 0)) { log.Info("OVF.Security: No files to encrypt/decrypt."); return; } try { List<DataReference> dataReference = new List<DataReference>(); string cryptoclassname = (string)AlgorithmMap((Properties.Settings.Default.encryptAlgorithmURI.Split(new char[] { '#' }))[1].ToLower().Replace('-', '_')); int keysize = Convert.ToInt32(Properties.Settings.Default.encryptKeyLength); string fileuuids = null; string version = null; // // Initial version really only works when there is ONLY ONE SecuritySection::Security // #region GET DECRYPT INFO if (!encrypt) { SecuritySection_Type securitysection = null; foreach (Section_Type section in env.Sections) { if (section is SecuritySection_Type) { securitysection = (SecuritySection_Type)section; break; } } foreach (Security_Type securitytype in securitysection.Security) { foreach (XenOvf.Definitions.XENC.ReferenceType dataref in securitytype.ReferenceList.Items) { if (dataref is DataReference) { fileuuids += ":" + ((DataReference)dataref).ValueType; } } if (securitytype.EncryptionMethod != null && securitytype.EncryptionMethod.Algorithm != null) { string algoname = (securitytype.EncryptionMethod.Algorithm.Split(new char[] { '#' }))[1].ToLower().Replace('-', '_'); object x = Properties.Settings.Default[algoname]; if (x != null) { cryptoclassname = (string)x; keysize = Convert.ToInt32(securitytype.EncryptionMethod.KeySize); } } if (!string.IsNullOrEmpty(securitytype.version)) { version = securitytype.version; } } } #endregion #region PROCESS FILES foreach (File_Type file in env.References.File) { if (encrypt) { version = Properties.Settings.Default.securityVersion; if (file.Id == null) { file.Id = "xen_" + Guid.NewGuid().ToString(); DataReference newDataReference = new DataReference(); newDataReference.ValueType = file.Id; dataReference.Add(newDataReference); process = true; } else { log.InfoFormat("File already encrypted, skipping. [{0}]", file.href); process = false; } } else { if (file.Id != null && fileuuids != null && fileuuids.ToLower().Contains(file.Id.ToLower())) { process = true; file.Id = null; } else { process = false; log.InfoFormat("File is not encrypted, skipping. [{0}]", file.href); } } if (process) { string fullname = string.Format(@"{0}\{1}", Path.GetDirectoryName(ovffilename), file.href); log.DebugFormat(encrypt ? "Encrypt: {0}" : "Decrypt: {0}", fullname); ICryptoTransform trans = CryptoSetup(cryptoclassname, password, encrypt, version); CryptoFile(trans, fullname, fullname + ".tmp", encrypt); if (_cancelEncrypt) { File.Delete(fullname + ".tmp"); } else { File.Delete(fullname); File.Move(fullname + ".tmp", fullname); } } } #endregion #region BUILD SECURITY SECTION if (encrypt && process && !_cancelEncrypt) { List<Section_Type> sections = new List<Section_Type>(); SecuritySection_Type securitySection = null; foreach (Section_Type section in env.Sections) { if (section is SecuritySection_Type) { securitySection = (SecuritySection_Type)section; } else { sections.Add(section); } } if (securitySection == null) { securitySection = new SecuritySection_Type(); securitySection.Info = new Msg_Type(); securitySection.Info.Value = "Encrypted Content Definition"; } List<Security_Type> secType = new List<Security_Type>(); if (securitySection.Security != null && securitySection.Security.Length > 0) { secType.AddRange(securitySection.Security); } Security_Type securityType = new Security_Type(); securityType.version = Properties.Settings.Default.securityVersion; securityType.Id = "xen_" + Guid.NewGuid().ToString(); ReferenceList referenceList = new ReferenceList(); referenceList.Items = dataReference.ToArray(); List<ItemsChoiceType3> ictList = new List<ItemsChoiceType3>(); for (int i = 0; i < dataReference.Count; i++) { ictList.Add(ItemsChoiceType3.DataReference); } referenceList.ItemsElementName = ictList.ToArray(); EncryptionMethodType encryptMethod = new EncryptionMethodType(); encryptMethod.KeySize = Convert.ToString(_KeySize); encryptMethod.Algorithm = Properties.Settings.Default.encryptAlgorithmURI; EncryptedDataType EncryptedData = new EncryptedDataType(); EncryptedData.CipherData = new CipherDataType(); CryptoElement(EncryptedData, KnownEncrypt, cryptoclassname, version, password); securityType.ReferenceList = referenceList; securityType.EncryptionMethod = encryptMethod; securityType.EncryptedData = EncryptedData; secType.Add(securityType); securitySection.Security = secType.ToArray(); sections.Add(securitySection); env.Sections = sections.ToArray(); } #endregion } catch (Exception ex) { log.ErrorFormat("OVF.Security: Cryptography error: {0}", ex.Message); throw; } }
/// <summary> /// Validate password prior do decrypting, depends on sample encrypted section in The SecuritySection. /// </summary> /// <param name="ovfFilename">ovf file name</param> /// <param name="password">password to check</param> /// <returns>true = valid password, false = password failed</returns> public bool CheckPassword(string ovfFilename, string password) { EnvelopeType env = Load(ovfFilename); return CheckPassword(env, password); }