Allow github codespace access to digitalocean database via trusted source

36 Views Asked by At

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?

0

There are 0 best solutions below