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

huangapple go评论89阅读模式
英文:

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

问题

我正在开发一个应用程序(使用Go/Golang编写,并使用“官方”MongoDB驱动程序),该应用程序必须同时访问MongoDB和Cosmos/Azure(使用MongoDB API)。我使用Studio3T来帮助处理数据库任务,我的应用程序作为命令行实用程序运行,我从其中一个bash中的iTerm中启动。

我能够访问MongoDB数据库,其中一个在我的Mac上本地运行,另外两个在公司服务器上,位于我们的防火墙后面(我的Mac几乎始终在公司VPN上运行)。但是,连接到Azure中的Cosmos实例一直是个挑战。

我从Azure门户获取了必要的Cosmos连接信息,并尝试在Studio 3T和我的应用程序中创建连接设置。起初,两者都无法工作,但是当我在Studio3T中手动设置代理设置以使用我们的常规代理服务器时,它能够成功连接到Cosmos实例。然而,我的应用程序仍然无法连接,显示服务器未知的错误。

我在我的应用程序中尝试了多种连接字符串的排列组合,包括与Studio3T生成的连接字符串完全相同的一个,如下所示:

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

(方括号中的项目设置为与Studio3T中有效的值完全相同)

令人困惑的是,我的Mac全局设置为使用与我在Studio 3T中输入的完全相同的代理服务器和设置,而启动我的应用程序的shell/bash环境中的http_proxy/https_proxy变量设置为完全相同的设置,但仍然无法工作。此外,我能够访问我的Mac上的各种外部基于互联网的资源,而且只有这个特定的Azure中的Cosmos实例是无法访问的(即使是在完全相同的机器上运行并使用完全相同的设置的Studio3T实例也是如此)。

这是一个典型的“说出魔法词”的问题,这让我希望我去烹饪学校而不是写软件。

所以,我的问题是:

  1. 当Studio 3T看到启用使用自定义代理服务器的设置时,它会做什么?在Studio 3T中是否有一些内部逻辑,允许其MongoDB连接使用这些设置来发现基于Azure的Cosmos实例?我注意到Studio 3T生成的URI具有以下参数:3t.proxyType=custom、3t.proxyProtocol=htttp、3t.proxyHost、3t.proxyPort,我认为这些是Studio3T特定的参数,但显然必须是Studio3T用来指示其代理处理行为的东西。

  2. 是否有一些我可以添加到我的Go程序中的东西,可以为驱动程序连接到Cosmos的尝试“设置舞台”,使其像Studio3T一样连接到Cosmos实例?

  3. 是否有任何环境设置我可以调整(包括在网络首选项中全局设置的任何设置),以促进我的应用程序通过代理服务器并连接到Cosmos?

非常感谢您能给我提供的任何见解、建议和帮助...

英文:

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://&lt;user&gt;:&lt;pw&gt;@&lt;host&gt;:&lt;port&gt;/admin?ssl=true&amp;replicaSet=&lt;repSet&gt;&amp;readPreference=primary&amp;maxIdleTimeMS=120000&amp;connectTimeoutMS=10000&amp;authSource=admin&amp;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...

答案1

得分: 1

代理地址的格式如下:

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

或者像这样:

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

这是你需要在你的 Mac 电脑上设置的内容,以便通过代理进行连接,例如:

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

更多信息请参考:https://www.serverlab.ca/tutorials/osx/administration-osx/configuring-a-network-proxy-for-osx/

这样设置后,所有的网络请求都会通过代理进行,包括 MongoDB。

你也可以进入网络设置并配置网络的代理设置,以便自动进行请求,或者你可以向你的公司请求提供一个 WPAD 文件

英文:

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.

huangapple
  • 本文由 发表于 2021年12月16日 02:50:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/70368899.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定