简体   繁体   中英

How can I get my Go (Golang) program to access Cosmos DB (using the MongoDB API) through my company's proxy server?

I'm developing an app (written in Go/Golang and using the "official" MongoDB driver) that must access MongoDB databases in both MongoDB as well as Cosmos/Azure (using the MongoDB API). I use Studio3T to help out with the database tasks and my app runs as a command line utility that I launch from within iTerm running as bash.

I'm able to access the MongoDB databases--one of which is running locally on my Mac, the other 2 are on company servers behind our firewall (my Mac is running nearly always on a company VPN). It's the Cosmos instance in Azure that has been challenging to connect to.

I obtained the necessary Cosmos connection information from the Azure portal and tried to create the connection settings in Studio 3T and my app. At first, neither would work, but when I manually set the proxy settings in Studio3T to use our regular proxy server, it was able to connect to the Cosmos instance just fine. My app, on the other hand, still cannot connect, saying that the server is unknown.

I've tried multiple permutations of the connection string in my app--including one that mirrors the one generated by Studio3T, that looks like this:

mongodb://<user>:<pw>@<host>:<port>/admin?ssl=true&replicaSet=<repSet>&readPreference=primary&maxIdleTimeMS=120000&connectTimeoutMS=10000&authSource=admin&authMechanism=SCRAM-SHA-1

(items in brackets are set with exactly the same values that work in Studio3T)

What makes it confusing is that my Mac is set globally to use the same proxy server with exactly the same settings I put into Studio 3T, and the shell/bash environment where my app gets started has the http_proxy/https_proxy variables set to exactly the same settings and it still doesn't work. Also, I'm able to access all kinds of external Internet-based resources on my Mac, and it's really only this particular Cosmos instance in Azure that is inaccessible (and even then, it's only inaccessible to my Go program, not the Studio3T instance running on exactly the same machine and using exactly the same settings).

This is a classic "say the magic word" problem, the kind of thing that makes me wish I'd gone to cooking school rather than taking up writing software.

So, my questions are:

  1. What does Studio 3T do when it sees settings that enable using a custom proxy server? Is there some kind of internal logic in Studio 3T that allows its MongoDB connection to discover Azure-based instances of Cosmos using those settings? I noticed that the URI that Studio 3T generates has these params: 3t.proxyType=custom, 3t.proxyProtocol=htttp, 3t.proxyHost, 3t.proxyPort, which I assume are Studio3T-specific, but obviously must be something that Studio3T uses to signal its proxy handling behavior

  2. Is there something I can add to my Go program that would "set the stage" for the driver's attempt to connect to Cosmos so that it connects to the Cosmos instance like Studio3T is able to?

  3. Are there any environment settings I can tweak (including anything that's set globally at the MacOS-level in Network Preferences) that can facilitate my app's ability to navigate our proxy server and connect to Cosmos?

Any insight/suggestions/help you can give me would be greatly appreciated...

A proxy address looks like this

http://USER:PASSWORD@COMPANY.EXAMPLE.TLD:PORT

Or something like this

http://aiw1dak1:powalkmd1@my.internal.company.local:8571

That is something you need to set in your mac computer to be able to make connections through a proxy, for example

export http_proxy = http://aiw1dak1:powalkmd1@my.internal.company.local:8571

More info here: https://www.serverlab.ca/tutorials/osx/administration-osx/configuring-a-network-proxy-for-osx/

That should set every network requests through the proxy, including MongoDB.

You can also go to the network settings and configure the network's proxy settings to make requests automatically, and you can request your company to give you a WPAD file .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM