Question

Dynamic IPs and Trusted Sources

Context: During development it’s helpful to access my MongoDB database and run it on local host. I secure the database by adding my IP to the “trusted sources” on the DO dashboard.

Problem: The problem is that my IP is dynamic. This forces me to remove the old IP and replace it with my new one. There has got to be a better way to do this…

Possible Solution: One of my ideas is to write a script that appends and removes firewall rules using ‘doctl.’ But if someone has a better idea, I would love to hear it!

thanks


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
February 1, 2024
Accepted Answer

Hello there!

Great question, and it’s a common challenge for developers working with dynamic IP addresses, especially when securing access to your databases. Let’s explore a couple of solutions to make your life easier!

Using a VPN

A very effective way to bypass the dynamic IP issue is to use a VPN. This approach allows you to maintain a consistent IP address for your development environment, regardless of changes by your ISP. DigitalOcean’s Marketplace offers a solution called Pi-hole + OpenVPN, which you can set up with a single click. This not only helps with secure and private browsing but can also simplify your access management to MongoDB by having a static IP.

Check out the Pi-hole + OpenVPN on DigitalOcean’s Marketplace here: Pi-hole VPN.

Automating with doctl

For your idea of automating the update of your trusted sources rules, doctl is indeed a powerful tool in your arsenal. Here’s a basic example of how you could use doctl to update your firewall rules, specifically for managing access to your MongoDB database:

  1. Install doctl: First, ensure you have doctl installed. You can find the installation instructions on the DigitalOcean documentation.

  2. Authenticate doctl: Make sure you’ve authenticated doctl with your DigitalOcean account. You’ll typically do this with an API token:

    doctl auth init
    
  3. Script for Updating your Rules: You can write a simple script that fetches your current IP and updates the trusted sources rule. Here’s a very basic example:

#!/bin/bash

# Fetch your current public IP
MY_IP=$(curl -s https://ifconfig.io/)

# Specify your managed database ID
DATABASE_ID="your-database-id"

# Update the database firewall rules (trusted sources)
# Note: Adjust --uuid with your actual DATABASE_ID and --rule with the appropriate IP
doctl databases firewalls update $DATABASE_ID --rule type:ip, value:$MY_IP --force

echo "Database trusted sources rules updated to allow access from $MY_IP"

For more information you can check out the docs here:

https://docs.digitalocean.com/reference/doctl/reference/databases/firewalls/list/

I would personally go for using a VPN with a static IP simplifies access management without needing to adjust firewall rules constantly. However, automating updates with doctl provides a flexible, immediate solution as well. Depending on your workflow and security requirements, you might find one option more suitable than the other.

Happy coding, and stay secure!

Best,

Bobby

KFSys
Site Moderator
Site Moderator badge
January 30, 2024

Heya @58215a70856347b792e785248632d2,

My idea would be using a VPN which has a static IP address. It’s not perfect as well but that is how I do it.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.