private void AddEventHubPrivateConfig(XDocument privateConfig) { var eventHubUrl = DiagnosticsHelper.GetConfigValueFromPrivateConfig(this.DiagnosticsConfigurationPath, DiagnosticsHelper.EventHubElemStr, DiagnosticsHelper.EventHubUrlAttr); var eventHubSharedAccessKeyName = DiagnosticsHelper.GetConfigValueFromPrivateConfig(this.DiagnosticsConfigurationPath, DiagnosticsHelper.EventHubElemStr, DiagnosticsHelper.EventHubSharedAccessKeyNameAttr); var eventHubSharedAccessKey = DiagnosticsHelper.GetConfigValueFromPrivateConfig(this.DiagnosticsConfigurationPath, DiagnosticsHelper.EventHubElemStr, DiagnosticsHelper.EventHubSharedAccessKeyAttr); if (!string.IsNullOrEmpty(eventHubUrl) || !string.IsNullOrEmpty(eventHubSharedAccessKeyName) || !string.IsNullOrEmpty(eventHubSharedAccessKey)) { var privateConfigElem = privateConfig.Descendants().FirstOrDefault(d => d.Name.LocalName == DiagnosticsHelper.PrivateConfigElemStr); XNamespace XmlNamespace = DiagnosticsHelper.XmlNamespace; privateConfigElem.Add(new XElement(XmlNamespace + DiagnosticsHelper.EventHubElemStr, new XAttribute(DiagnosticsHelper.EventHubUrlAttr, eventHubUrl), new XAttribute(DiagnosticsHelper.EventHubSharedAccessKeyNameAttr, eventHubSharedAccessKeyName), new XAttribute(DiagnosticsHelper.EventHubSharedAccessKeyAttr, eventHubSharedAccessKey) )); } }
private bool IsSasTokenProvided() { return(!string.IsNullOrEmpty(DiagnosticsHelper.GetConfigValueFromPrivateConfig(this.DiagnosticsConfigurationPath, DiagnosticsHelper.PrivateConfigElemStr, DiagnosticsHelper.PrivConfSasKeyAttr))); }
protected override void ValidateConfiguration() { using (StreamReader sr = new StreamReader(DiagnosticsConfigurationPath)) { string header = sr.ReadLine(); // make sure it is the header if (!header.Trim().StartsWith("<?xml")) { throw new ArgumentException(Resources.DiagnosticsExtensionWrongHeader); } } var doc = XDocument.Load(DiagnosticsConfigurationPath); var publicConfigElement = doc.Descendants().FirstOrDefault(d => d.Name.LocalName == "PublicConfig"); if (publicConfigElement == null) { throw new ArgumentException(Resources.DiagnosticsExtensionNullPublicConfig); } var wadCfgElement = doc.Descendants().FirstOrDefault(d => d.Name.LocalName == "WadCfg"); var wadCfgBlobElement = doc.Descendants().FirstOrDefault(d => d.Name.LocalName == "WadCfgBlob"); if (wadCfgElement == null && wadCfgBlobElement == null) { throw new ArgumentException(Resources.DiagnosticsExtensionPaaSConfigElementNotDefined); } if (wadCfgElement != null) { DiagnosticsHelper.AutoFillMetricsConfig(wadCfgElement, GetResourceId(), cmdlet: this); } // The element <StorageAccount> is not meant to be set by the user in the public config. // Make sure it matches the storage account in the private config. var storageAccountElement = publicConfigElement.Elements().FirstOrDefault(d => d.Name.LocalName == "StorageAccount"); if (storageAccountElement == null) { // The StorageAccount element is not there, we create one XNamespace ns = XmlNamespace; storageAccountElement = new XElement(ns + "StorageAccount", StorageAccountName); publicConfigElement.Add(storageAccountElement); } else { if (!string.IsNullOrEmpty(storageAccountElement.Value) && string.Compare(storageAccountElement.Value, StorageAccountName, true) != 0) { WriteWarning(Resources.DiagnosticsExtensionNoMatchStorageAccount); } storageAccountElement.SetValue(StorageAccountName); } PublicConfiguration = publicConfigElement.ToString(); // Make sure the storage account name in PrivateConfig matches. var privateConfigStorageAccountName = DiagnosticsHelper.GetConfigValueFromPrivateConfig(this.DiagnosticsConfigurationPath, DiagnosticsHelper.StorageAccountElemStr, DiagnosticsHelper.PrivConfNameAttr); if (!string.IsNullOrEmpty(privateConfigStorageAccountName) && !string.Equals(StorageAccountName, privateConfigStorageAccountName, StringComparison.OrdinalIgnoreCase)) { WriteWarning(Resources.DiagnosticsExtensionNoMatchPrivateStorageAccount); } // Look for the PrivateConfig element in the user provided config file. // If it doesn't exist (e.g., a pure PublicConfig.xml), we create the private config for the user. var privateConfigElement = doc.Descendants().FirstOrDefault(d => d.Name.LocalName == "PrivateConfig"); if (privateConfigElement == null) { XNamespace configNameSpace = DiagnosticsHelper.XmlNamespace; privateConfigElement = new XElement(configNameSpace + PrivateConfigStr, new XElement(configNameSpace + DiagnosticsHelper.StorageAccountElemStr, new XAttribute(DiagnosticsHelper.PrivConfNameAttr, string.Empty), new XAttribute(DiagnosticsHelper.PrivConfKeyAttr, string.Empty), new XAttribute(DiagnosticsHelper.PrivConfEndpointAttr, string.Empty) )); } PrivateConfigurationXml = new XDocument( new XDeclaration("1.0", "utf-8", null), privateConfigElement ); SetPrivateConfigAttribute(DiagnosticsHelper.StorageAccountElemStr, DiagnosticsHelper.PrivConfNameAttr, StorageAccountName); SetPrivateConfigAttribute(DiagnosticsHelper.StorageAccountElemStr, DiagnosticsHelper.PrivConfKeyAttr, StorageAccountKey); SetPrivateConfigAttribute(DiagnosticsHelper.StorageAccountElemStr, DiagnosticsHelper.PrivConfEndpointAttr, StorageAccountEndpoint); PrivateConfiguration = PrivateConfigurationXml.ToString(); }