Esempio n. 1
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req,
            ILogger log)
        {
            string token = string.Empty;

            log.LogInformation("GenerateBearerToken Function is called");

            try
            {
                string  resourceUri = req.Query["ResourceUri"];
                string  requestBody = await new StreamReader(req.Body).ReadToEndAsync();
                dynamic data        = JsonConvert.DeserializeObject(requestBody);
                resourceUri = resourceUri ?? data?.ResourceUri;

                if (!string.IsNullOrEmpty(resourceUri))
                {
                    log.LogInformation("Fetching details from KeyVault");
                    //Fetching UAMI Client Id and Tenant Id from Key Vaults
                    string clientId_UAMI = await KeyVaultHelper.FetchKeyVaultSecret(ConstantsHelper.GetEnvironmentVariable(ConstantsHelper.clientId_UAMI), log);

                    string tenantId = await KeyVaultHelper.FetchKeyVaultSecret(ConstantsHelper.GetEnvironmentVariable(ConstantsHelper.tenantId), log);

                    token = await TokenHelper.GetToken(clientId_UAMI, tenantId, resourceUri, log);


                    if (!string.IsNullOrEmpty(token))
                    {
                        return(new OkObjectResult("Bearer " + token));
                    }
                    else
                    {
                        return(new OkObjectResult("[Error] Exception has been occured in generating token.Please check Function logs under Monitor"));
                    }
                }
                else
                {
                    return(new BadRequestObjectResult("[Warning] Resource Uri is missing in request"));
                }
            }
            catch (Exception ex)
            {
                return(new NotFoundObjectResult($"\n GenerateBearerToken got an exception \n Time: { DateTime.Now} \n Exception{ ex.Message}"));
            }
        }
        public static async Task <HttpResponseMessage> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequestMessage req,
            ILogger log)
        {
            try
            {
                log.LogInformation("PushLogsToLogAnalytics Function Called");
                //Get Request Data
                dynamic data = await req.Content.ReadAsAsync <object>();

                string customLogFile  = data.LogFileName;
                string automationName = data.AutomationName;
                string moduleName     = data.ModuleName;
                string logData        = Convert.ToString(data.LogData);

                //Parsing provided logData Json
                JObject logDataObj  = JObject.Parse(logData);
                string  logDataJson = logDataObj.ToString(Newtonsoft.Json.Formatting.Indented);

                //Preparing Final Json for Log Analytics Injection
                dynamic obj = new JObject();
                obj.AutomationName = automationName;
                obj.ModuleName     = moduleName;
                obj.Log            = logDataJson;
                string myJson = obj.ToString(Newtonsoft.Json.Formatting.Indented);
                log.LogInformation("PreparedFinalJson : " + myJson);

                //Validating Json - User provided Log Data Json and prepared final Json
                bool isChildJsonValid  = LogAnalyticsHelper.IsValidJson(logDataJson, log);
                bool isParentJsonValid = LogAnalyticsHelper.IsValidJson(myJson, log);

                if (isChildJsonValid && isParentJsonValid)
                {
                    log.LogInformation("Fetching details from KeyVault");
                    log.LogInformation("Invoking FetchKeyVaultSecret method");
                    string workspaceId = await KeyVaultHelper.FetchKeyVaultSecret(ConstantsHelper.GetEnvironmentVariable(ConstantsHelper.logAnalyticsWorkspaceID), log);

                    string primaryKey = await KeyVaultHelper.FetchKeyVaultSecret(ConstantsHelper.GetEnvironmentVariable(ConstantsHelper.logAnalyticsWorkspaceSharedKey), log);

                    log.LogInformation("FetchKeyVaultSecret executed successfully");

                    //Invoking PushLogsToLogAnalytics method to ingest the logs into workspace
                    bool status = await LogAnalyticsHelper.PushLogsToLogAnalytics(myJson, customLogFile, workspaceId, primaryKey, log);

                    if (status)
                    {
                        log.LogInformation("Ingestion of log analytics is completed.");
                        return(req.CreateResponse(HttpStatusCode.OK, "[Info] Ingestion of log analytics is completed."));
                    }
                    else
                    {
                        log.LogInformation("Ingestion of log analytics is failed");
                        return(req.CreateResponse(HttpStatusCode.BadRequest, "[Error] Ingestion of log analytics is failed"));
                    }
                }
                else
                {
                    return(req.CreateResponse(HttpStatusCode.BadRequest, $"[Warning] Invalid Json Provided"));
                }
            }
            catch (System.Exception ex)
            {
                return(req.CreateResponse(HttpStatusCode.NotFound, $"{ex.Message}"));
            }
        }