public async Task GetToken(AzureSubscription azureSubscription) { _LogProvider.WriteLog("GetToken", "Start token request"); if (azureSubscription == null) { _LogProvider.WriteLog("GetToken", "Azure Subscription cannot be null."); throw new ArgumentNullException("Azure Subscription cannot be null."); } _LogProvider.WriteLog("GetToken", "Azure Subscription: " + azureSubscription.ToString()); string authenticationUrl = AzureServiceUrls.GetAzureLoginUrl(azureSubscription.AzureEnvironment) + azureSubscription.AzureAdTenantId.ToString(); _LogProvider.WriteLog("GetToken", "Authentication Url: " + authenticationUrl); _AuthenticationResult = null; AuthenticationContext context = new AuthenticationContext(authenticationUrl); PlatformParameters platformParams = new PlatformParameters(PromptBehavior.Auto, null); _AuthenticationResult = await context.AcquireTokenAsync(AzureServiceUrls.GetASMServiceManagementUrl(azureSubscription.AzureEnvironment), strClientId, new Uri(strReturnUrl), platformParams); _LogProvider.WriteLog("GetToken", "End token request"); }
internal async Task <AuthenticationResult> LoginAzureProvider(AzureEnvironment azureEnvironment) { _LogProvider.WriteLog("LoginAzureProvider", "Start token request"); _LogProvider.WriteLog("LoginAzureProvider", "Azure Environment: " + azureEnvironment.ToString()); string authenticationUrl = AzureServiceUrls.GetAzureLoginUrl(azureEnvironment) + "common"; _LogProvider.WriteLog("LoginAzureProvider", "Authentication Url: " + authenticationUrl); AuthenticationContext context = new AuthenticationContext(authenticationUrl); PlatformParameters platformParams = new PlatformParameters(PromptBehavior.Always, null); AuthenticationResult authenticationResult = await context.AcquireTokenAsync(AzureServiceUrls.GetASMServiceManagementUrl(azureEnvironment), strClientId, new Uri(strReturnUrl), platformParams); if (authenticationResult == null) { _LogProvider.WriteLog("LoginAzureProvider", "Failed to obtain the token (null AuthenticationResult returned)."); } _AuthenticationResult = authenticationResult; _LogProvider.WriteLog("LoginAzureProvider", "End token request for Azure Environment " + azureEnvironment.ToString()); return(_AuthenticationResult); }
public AzureContext(ILogProvider logProvider, IStatusProvider statusProvider, ISettingsProvider settingsProvider) { _LogProvider = logProvider; _StatusProvider = statusProvider; _SettingsProvider = settingsProvider; _AzureServiceUrls = new AzureServiceUrls(this); _TokenProvider = new AzureTokenProvider(this); _AzureRetriever = new AzureRetriever(this); }
internal async static Task <AzureTokenProvider> LoginAzureProvider(AzureEnvironment azureEnvironment) { AuthenticationContext context = new AuthenticationContext(AzureServiceUrls.GetLoginUrl(azureEnvironment) + "common"); PlatformParameters platformParams = new PlatformParameters(PromptBehavior.Always, null); AuthenticationResult authenticationResult = await context.AcquireTokenAsync(AzureServiceUrls.GetASMServiceManagementUrl(azureEnvironment), strClientId, new Uri(strReturnUrl), platformParams); if (authenticationResult == null) { throw new InvalidOperationException("Failed to obtain the token"); } return(new AzureTokenProvider(authenticationResult)); }
public async Task GetToken(AzureSubscription azureSubscription) { _AuthenticationResult = null; if (azureSubscription == null) { return; } AuthenticationContext context = new AuthenticationContext(AzureServiceUrls.GetLoginUrl(azureSubscription.AzureEnvironment) + azureSubscription.AzureAdTenantId.ToString()); PlatformParameters platformParams = new PlatformParameters(PromptBehavior.Auto, null); _AuthenticationResult = await context.AcquireTokenAsync(AzureServiceUrls.GetASMServiceManagementUrl(azureSubscription.AzureEnvironment), strClientId, new Uri(strReturnUrl), platformParams); }
internal async Task <AuthenticationResult> GetGraphToken(AzureEnvironment azureEnvironment, string tenantId) { _LogProvider.WriteLog("GetGraphToken", "Start token request"); _LogProvider.WriteLog("GetGraphToken", "Azure Environment: " + azureEnvironment.ToString()); _LogProvider.WriteLog("GetGraphToken", "Azure Tenant: " + tenantId); string authenticationUrl = AzureServiceUrls.GetAzureLoginUrl(azureEnvironment) + tenantId; _LogProvider.WriteLog("GetGraphToken", "Authentication Url: " + authenticationUrl); AuthenticationContext context = new AuthenticationContext(authenticationUrl); PlatformParameters platformParams = new PlatformParameters(PromptBehavior.Auto, null); AuthenticationResult authenticationResult = await context.AcquireTokenAsync(AzureServiceUrls.GetGraphApiUrl(azureEnvironment), strClientId, new Uri(strReturnUrl), platformParams); if (authenticationResult == null) { _LogProvider.WriteLog("GetGraphToken", "Failed to obtain the token (null AuthenticationResult returned)."); } _LogProvider.WriteLog("GetGraphToken", "End token request"); return(authenticationResult); }
private async Task <JObject> GetAzureARMResources(string resourceType, Hashtable info) { _AzureContext.LogProvider.WriteLog("GetAzureARMResources", "Start"); string methodType = "GET"; string url = null; switch (resourceType) { case "Locations": // https://docs.microsoft.com/en-us/rest/api/resources/subscriptions#Subscriptions_ListLocations url = AzureServiceUrls.GetARMServiceManagementUrl(this._AzureContext.AzureEnvironment) + "subscriptions/" + _AzureSubscription.SubscriptionId + ArmConst.Locations + "?api-version=2016-06-01"; _AzureContext.StatusProvider.UpdateStatus("BUSY: Getting ARM Azure Locations for Subscription ID : " + _AzureSubscription.SubscriptionId + "..."); break; case "VirtualNetworks": // https://msdn.microsoft.com/en-us/library/azure/mt163557.aspx url = AzureServiceUrls.GetARMServiceManagementUrl(this._AzureContext.AzureEnvironment) + "subscriptions/" + _AzureSubscription.SubscriptionId + ArmConst.ProviderVirtualNetwork + "?api-version=2016-03-30"; _AzureContext.StatusProvider.UpdateStatus("BUSY: Getting ARM Virtual Networks for Subscription ID : " + _AzureSubscription.SubscriptionId + "..."); break; case "StorageAccounts": // https://docs.microsoft.com/en-us/rest/api/storagerp/storageaccounts#StorageAccounts_List url = AzureServiceUrls.GetARMServiceManagementUrl(this._AzureContext.AzureEnvironment) + "subscriptions/" + _AzureSubscription.SubscriptionId + ArmConst.ProviderStorageAccounts + "?api-version=2016-01-01"; _AzureContext.StatusProvider.UpdateStatus("BUSY: Getting ARM Storage Accounts for Subscription ID : " + _AzureSubscription.SubscriptionId + "..."); break; case "StorageAccountKeys": // https://docs.microsoft.com/en-us/rest/api/storagerp/storageaccounts#StorageAccounts_ListKeys methodType = "POST"; url = AzureServiceUrls.GetARMServiceManagementUrl(this._AzureContext.AzureEnvironment) + "subscriptions/" + _AzureSubscription.SubscriptionId + "/resourceGroups/" + info["ResourceGroupName"] + ArmConst.ProviderStorageAccounts + info["StorageAccountName"] + "/listKeys?api-version=2016-01-01"; _AzureContext.StatusProvider.UpdateStatus("BUSY: Getting ARM Storage Accounts for Subscription ID : " + _AzureSubscription.SubscriptionId + "..."); break; } _AzureContext.LogProvider.WriteLog("GetAzureARMResources", "GET " + url); if (_armJsonDocumentCache == null) { _armJsonDocumentCache = new Dictionary <string, JObject>(); } if (_armJsonDocumentCache.ContainsKey(url)) { _AzureContext.LogProvider.WriteLog("GetAzureARMResources", "FROM JSON CACHE"); _AzureContext.LogProvider.WriteLog("GetAzureARMResources", "End"); writeRetreiverResultToLog(url, "Cached"); return(_armJsonDocumentCache[url]); } HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); request.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + _AzureContext.TokenProvider.AuthenticationResult.AccessToken); request.ContentType = "application/json"; request.Method = methodType; if (request.Method == "POST") { request.ContentLength = 0; } string webRequesetResult = String.Empty; try { HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync(); webRequesetResult = new StreamReader(response.GetResponseStream()).ReadToEnd(); _AzureContext.LogProvider.WriteLog("GetAzureARMResources", "RESPONSE " + response.StatusCode); } catch (Exception exception) { _AzureContext.LogProvider.WriteLog("GetAzureARMResources", "EXCEPTION " + exception.Message); throw exception; } if (webRequesetResult != String.Empty) { JObject webRequestResultJson = JObject.Parse(webRequesetResult); _AzureContext.LogProvider.WriteLog("GetAzureARMResources", "End"); writeRetreiverResultToLog(url, webRequesetResult); if (!_armJsonDocumentCache.ContainsKey(url)) { _armJsonDocumentCache.Add(url, webRequestResultJson); } return(webRequestResultJson); } else { _AzureContext.LogProvider.WriteLog("GetAzureARMResources", "End"); writeRetreiverResultToLog(url, String.Empty); return(null); } }
private async Task <XmlDocument> GetAzureAsmResources(string resourceType, Hashtable info) { _AzureContext.LogProvider.WriteLog("GetAzureASMResources", "Start"); string url = null; switch (resourceType) { case "Subscriptions": url = AzureServiceUrls.GetASMServiceManagementUrl(this._AzureContext.AzureEnvironment) + "subscriptions"; _AzureContext.StatusProvider.UpdateStatus("BUSY: Getting Subscriptions..."); break; case "VirtualNetworks": url = AzureServiceUrls.GetASMServiceManagementUrl(this._AzureContext.AzureEnvironment) + _AzureSubscription.SubscriptionId + "/services/networking/virtualnetwork"; _AzureContext.StatusProvider.UpdateStatus("BUSY: Getting Virtual Networks for Subscription ID : " + _AzureSubscription.SubscriptionId + "..."); break; case "ClientRootCertificates": url = AzureServiceUrls.GetASMServiceManagementUrl(this._AzureContext.AzureEnvironment) + _AzureSubscription.SubscriptionId + "/services/networking/" + info["virtualnetworkname"] + "/gateway/clientrootcertificates"; _AzureContext.StatusProvider.UpdateStatus("BUSY: Getting Client Root Certificates for Virtual Network : " + info["virtualnetworkname"] + "..."); break; case "ClientRootCertificate": url = AzureServiceUrls.GetASMServiceManagementUrl(this._AzureContext.AzureEnvironment) + _AzureSubscription.SubscriptionId + "/services/networking/" + info["virtualnetworkname"] + "/gateway/clientrootcertificates/" + info["thumbprint"]; _AzureContext.StatusProvider.UpdateStatus("BUSY: Getting certificate data for certificate : " + info["thumbprint"] + "..."); break; case "NetworkSecurityGroup": url = AzureServiceUrls.GetASMServiceManagementUrl(this._AzureContext.AzureEnvironment) + _AzureSubscription.SubscriptionId + "/services/networking/networksecuritygroups/" + info["name"] + "?detaillevel=Full"; _AzureContext.StatusProvider.UpdateStatus("BUSY: Getting Network Security Group : " + info["name"] + "..."); break; case "NetworkSecurityGroups": url = AzureServiceUrls.GetASMServiceManagementUrl(this._AzureContext.AzureEnvironment) + _AzureSubscription.SubscriptionId + "/services/networking/networksecuritygroups"; _AzureContext.StatusProvider.UpdateStatus("BUSY: Getting Network Security Groups"); break; case "RouteTable": url = AzureServiceUrls.GetASMServiceManagementUrl(this._AzureContext.AzureEnvironment) + _AzureSubscription.SubscriptionId + "/services/networking/routetables/" + info["name"] + "?detailLevel=full"; _AzureContext.StatusProvider.UpdateStatus("BUSY: Getting Route Table : " + info["routetablename"] + "..."); break; case "NSGSubnet": url = AzureServiceUrls.GetASMServiceManagementUrl(this._AzureContext.AzureEnvironment) + _AzureSubscription.SubscriptionId + "/services/networking/virtualnetwork/" + info["virtualnetworkname"] + "/subnets/" + info["subnetname"] + "/networksecuritygroups"; _AzureContext.StatusProvider.UpdateStatus("BUSY: Getting NSG for subnet " + info["subnetname"] + "..."); break; case "VirtualNetworkGateway": url = AzureServiceUrls.GetASMServiceManagementUrl(this._AzureContext.AzureEnvironment) + _AzureSubscription.SubscriptionId + "/services/networking/" + info["virtualnetworkname"] + "/gateway"; _AzureContext.StatusProvider.UpdateStatus("BUSY: Getting Virtual Network Gateway : " + info["virtualnetworkname"] + "..."); break; case "VirtualNetworkGatewaySharedKey": url = AzureServiceUrls.GetASMServiceManagementUrl(this._AzureContext.AzureEnvironment) + _AzureSubscription.SubscriptionId + "/services/networking/" + info["virtualnetworkname"] + "/gateway/connection/" + info["localnetworksitename"] + "/sharedkey"; _AzureContext.StatusProvider.UpdateStatus("BUSY: Getting Virtual Network Gateway Shared Key: " + info["localnetworksitename"] + "..."); break; case "StorageAccounts": url = AzureServiceUrls.GetASMServiceManagementUrl(this._AzureContext.AzureEnvironment) + _AzureSubscription.SubscriptionId + "/services/storageservices"; _AzureContext.StatusProvider.UpdateStatus("BUSY: Getting Storage Accounts for Subscription ID : " + _AzureSubscription.SubscriptionId + "..."); break; case "StorageAccount": url = AzureServiceUrls.GetASMServiceManagementUrl(this._AzureContext.AzureEnvironment) + _AzureSubscription.SubscriptionId + "/services/storageservices/" + info["name"]; _AzureContext.StatusProvider.UpdateStatus("BUSY: Getting Storage Account '" + info["name"] + " ' for Subscription ID : " + _AzureSubscription.SubscriptionId + "..."); break; case "StorageAccountKeys": url = AzureServiceUrls.GetASMServiceManagementUrl(this._AzureContext.AzureEnvironment) + _AzureSubscription.SubscriptionId + "/services/storageservices/" + info["name"] + "/keys"; _AzureContext.StatusProvider.UpdateStatus("BUSY: Getting Storage Account '" + info["name"] + "' Keys."); break; case "CloudServices": url = AzureServiceUrls.GetASMServiceManagementUrl(this._AzureContext.AzureEnvironment) + _AzureSubscription.SubscriptionId + "/services/hostedservices"; _AzureContext.StatusProvider.UpdateStatus("BUSY: Getting Cloud Services for Subscription ID : " + _AzureSubscription.SubscriptionId + "..."); break; case "CloudService": url = AzureServiceUrls.GetASMServiceManagementUrl(this._AzureContext.AzureEnvironment) + _AzureSubscription.SubscriptionId + "/services/hostedservices/" + info["name"] + "?embed-detail=true"; _AzureContext.StatusProvider.UpdateStatus("BUSY: Getting Virtual Machines for Cloud Service : " + info["name"] + "..."); break; case "VirtualMachine": url = AzureServiceUrls.GetASMServiceManagementUrl(this._AzureContext.AzureEnvironment) + _AzureSubscription.SubscriptionId + "/services/hostedservices/" + info["cloudservicename"] + "/deployments/" + info["deploymentname"] + "/roles/" + info["virtualmachinename"]; _AzureContext.StatusProvider.UpdateStatus("BUSY: Getting Virtual Machine '" + info["virtualmachinename"] + "' for Cloud Service '" + info["virtualmachinename"] + "'"); break; case "VMImages": url = AzureServiceUrls.GetASMServiceManagementUrl(this._AzureContext.AzureEnvironment) + _AzureSubscription.SubscriptionId + "/services/images"; break; case "ReservedIPs": url = AzureServiceUrls.GetASMServiceManagementUrl(this._AzureContext.AzureEnvironment) + _AzureSubscription.SubscriptionId + "/services/networking/reservedips"; break; case "AffinityGroup": url = AzureServiceUrls.GetASMServiceManagementUrl(this._AzureContext.AzureEnvironment) + _AzureSubscription.SubscriptionId + "/affinitygroups/" + info["affinitygroupname"]; break; case "Locations": url = AzureServiceUrls.GetASMServiceManagementUrl(this._AzureContext.AzureEnvironment) + _AzureSubscription.SubscriptionId + "/locations"; break; } _AzureContext.LogProvider.WriteLog("GetAzureASMResources", "GET " + url); if (_asmXmlDocumentCache == null) { _asmXmlDocumentCache = new Dictionary <string, XmlDocument>(); } if (_asmXmlDocumentCache.ContainsKey(url)) { _AzureContext.LogProvider.WriteLog("GetAzureASMResources", "FROM XML CACHE"); _AzureContext.LogProvider.WriteLog("GetAzureASMResources", "End"); writeRetreiverResultToLog(url, "Cached"); return(_asmXmlDocumentCache[url]); } HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); request.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + _AzureContext.TokenProvider.AuthenticationResult.AccessToken); request.Headers.Add("x-ms-version", "2015-04-01"); request.Method = "GET"; string xml = String.Empty; try { HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync(); xml = new StreamReader(response.GetResponseStream()).ReadToEnd(); _AzureContext.LogProvider.WriteLog("GetAzureASMResources", "RESPONSE " + response.StatusCode); } catch (Exception exception) { _AzureContext.LogProvider.WriteLog("GetAzureASMResources", "EXCEPTION " + exception.Message); throw exception; } if (xml != String.Empty) { XmlDocument xmlDoc = RemoveXmlns(xml); _AzureContext.LogProvider.WriteLog("GetAzureASMResources", "End"); writeRetreiverResultToLog(url, xml); if (!_asmXmlDocumentCache.ContainsKey(url)) { _asmXmlDocumentCache.Add(url, xmlDoc); } return(xmlDoc); } else { _AzureContext.LogProvider.WriteLog("GetAzureASMResources", "End"); writeRetreiverResultToLog(url, String.Empty); return(null); } }