I'm trying to fetch MongoDB Atlas logs via a function app, but unable to get correct nonce value.

Aron M 0 Reputation points
2024-03-01T12:24:27.8166667+00:00

I've encountered a problem that I haven't been able to solve for a while now.

Purpose
----------

An azure function that downloads audit logs from MongoDB Atlas and stores it in an azure storage container.

Setup
----------

Mongo DB Atlas cluster and function are only accessible when connected to a VPN. MongoDB requires additional auth otherwise returns access denied. I'm using digest authentication. I've tested downloading the file via postman, and sending it to be stored in a storage container. Both work as expected.

Issue
----------

  1. My code makes a request which is rejected.
  2. The headers are parsed to extract the realm and nonce.
  3. Values are taken in by a helper method that generates a digest header.
  4. Digest header is added to DefaultRequestHeaders.
  5. 2nd request is made but auth fails. Nonce returned is different to nonce supplied (presumably because each request generates a unique value).

Question
----------

What am I doing incorrectly? My guess is that making the second request is somehow wrong and I need to return header during the 1st request - otherwise I don't understand how I get the nonce value from an api request before calling it (which is impossible afaik)

Code
----------

using System.Collections.Generic;
using System;
using System.Net.Http.Headers;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Functions.Worker;
namespace mongoDB_Atlas.logs.ExternalApiFunction
{
    public class Class1
    {
        // Client.
        private static readonly HttpClient client = new HttpClient();
        //--------------------------------------------------------------------------------------//
        // No Auth Function
        //--------------------------------------------------------------------------------------//
        [Function("no-auth")]
        public static async Task
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,004 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.