Question

Allow github codespace access to digitalocean database via trusted source

I’m trying to connect my app inside codespace to digitalocean database, I know I need to add ip’s through the trusted source so from the codespace I can access my database.

I created a digitalocean function script that will constantly update the database trusted source with github ips from here api.github.com/meta. Below is the script

const axios = require('axios');

function main(args) {

  const do_token = "PERSONAL_ACCESS_TOKEN";
  const db_cluster = "DATABASE_CLUSTER";
  const api_url = `https://api.digitalocean.com/v2/databases/${db_cluster}/firewall`;

  const app = async function(){
    let meta = await axios.get('https://api.github.com/meta')
    .then( res =>{
      return res.data;
    })
    .catch(err=>{
      console.log(err);
      return [];
    });

    let firewall_list = await axios.get(api_url,{
      headers: {
        'Content-Type' : 'text/plain',
        'Authorization': `Bearer ${do_token}`
      }
    })
    .then( res =>{
      return res.data.rules;
    })
    .catch(err=>{
      console.log(err);
      return [];
    });

    let meta_web = meta.web.filter(i => !i.includes(':') ).map(i=>{
      return {
        "type": "ip_addr",
        "value": i
      }
    });

    let rules = [...firewall_list,...meta_web];

    let new_rules = { "rules": [] };

    rules.forEach(item=>{
        if(!new_rules.rules.find(i=>i.value === item.value)){
           new_rules.rules.push(item);
       }
    });

    await axios({
      url: api_url,
      method: 'put',
      data: new_rules,
      headers: {
        'Content-Type' : 'text/plain',
        "Authorization": `Bearer ${do_token}`
      }
    })
    .then( res =>{
      return res.data;
    })
    .catch(err=>{
      console.log(err);
      return [];
    });

    return { "message": true, "new_rules": new_rules }
  }

  return app();
}

The above code works but unfortunately I’m still not able to connect to the digitalocean database. I tried the git, api, web ip’s on the trusted but seems none of them are correct. Any help?


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
September 7, 2023
Accepted Answer

Hi there,

While GitHub publishes IP ranges for several products on its Meta API, IP addresses for codespaces are dynamically assigned, meaning your codespace is not guaranteed to have the same IP address day to day. For more information about the Meta API, see “Meta.”

Someone has raised this to the GitHub team as well here:

https://github.com/orgs/community/discussions/5505

I could suggest adding a comment to that discussion mentioning that you are also interested in that feature.

Allowlisting an entire IP range would give overly broad access to your database anyway, so for the time being, you might have to disable the trusted sources for your database while the GitHub team works on that feature request.

Best,

Bobby

Try DigitalOcean for free

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

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

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

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel