mirror of
https://github.com/trailofbits/algo.git
synced 2025-09-07 20:43:11 +02:00
Updated DigitalOcean UX, proxied requests to it
This commit is contained in:
parent
ca3230c0de
commit
80f04de3ec
4 changed files with 144 additions and 62 deletions
|
@ -1,18 +1,28 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import concurrent.futures
|
||||||
import json
|
import json
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
from os.path import join, dirname, expanduser
|
||||||
|
|
||||||
import yaml
|
import yaml
|
||||||
import boto3
|
|
||||||
from os.path import join, dirname, expanduser
|
|
||||||
from aiohttp import web, ClientSession
|
from aiohttp import web, ClientSession
|
||||||
import concurrent.futures
|
|
||||||
import sys
|
|
||||||
import os
|
|
||||||
|
|
||||||
from google.auth.transport.requests import AuthorizedSession
|
|
||||||
from google.oauth2 import service_account
|
|
||||||
from playbook import PlaybookCLI
|
from playbook import PlaybookCLI
|
||||||
|
|
||||||
|
try:
|
||||||
|
import boto3
|
||||||
|
HAS_BOTO3 = True
|
||||||
|
except ImportError:
|
||||||
|
HAS_BOTO3 = False
|
||||||
|
|
||||||
|
try:
|
||||||
|
from google.auth.transport.requests import AuthorizedSession
|
||||||
|
from google.oauth2 import service_account
|
||||||
|
HAS_GOOGLE_LIBRARIES = True
|
||||||
|
except ImportError:
|
||||||
|
HAS_GOOGLE_LIBRARIES = False
|
||||||
|
|
||||||
routes = web.RouteTableDef()
|
routes = web.RouteTableDef()
|
||||||
PROJECT_ROOT = dirname(dirname(__file__))
|
PROJECT_ROOT = dirname(dirname(__file__))
|
||||||
pool = None
|
pool = None
|
||||||
|
@ -63,7 +73,7 @@ async def playbook_post_handler(request):
|
||||||
|
|
||||||
|
|
||||||
@routes.delete('/playbook')
|
@routes.delete('/playbook')
|
||||||
async def playbook_delete_handler(request):
|
async def playbook_delete_handler(_):
|
||||||
global task_future
|
global task_future
|
||||||
if not task_future:
|
if not task_future:
|
||||||
return web.json_response({'ok': False})
|
return web.json_response({'ok': False})
|
||||||
|
@ -105,6 +115,30 @@ async def post_exit(_):
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
|
@routes.get('/do_config')
|
||||||
|
async def check_do_config(_):
|
||||||
|
return web.json_response({"ok": 'DO_API_TOKEN' in os.environ})
|
||||||
|
|
||||||
|
|
||||||
|
@routes.get('/do_regions')
|
||||||
|
async def do_regions(request):
|
||||||
|
if 'token' in request.query:
|
||||||
|
token = request.query['token']
|
||||||
|
elif 'DO_API_TOKEN' in os.environ:
|
||||||
|
token = os.environ['DO_API_TOKEN']
|
||||||
|
else:
|
||||||
|
return web.json_response({'error': 'no token provided'}, status=400)
|
||||||
|
|
||||||
|
headers = {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'Authorization': 'Bearer {0}'.format(token),
|
||||||
|
}
|
||||||
|
async with ClientSession(headers=headers) as session:
|
||||||
|
async with session.get('https://api.digitalocean.com/v2/regions') as r:
|
||||||
|
json_body = await r.json()
|
||||||
|
return web.json_response(json_body, status=r.status)
|
||||||
|
|
||||||
|
|
||||||
@routes.post('/lightsail_regions')
|
@routes.post('/lightsail_regions')
|
||||||
async def lightsail_regions(request):
|
async def lightsail_regions(request):
|
||||||
data = await request.json()
|
data = await request.json()
|
||||||
|
@ -132,7 +166,7 @@ async def ec2_regions(request):
|
||||||
|
|
||||||
|
|
||||||
@routes.get('/gce_config')
|
@routes.get('/gce_config')
|
||||||
async def check_gce_config(request):
|
async def check_gce_config(_):
|
||||||
gce_file = join(PROJECT_ROOT, 'configs', 'gce.json')
|
gce_file = join(PROJECT_ROOT, 'configs', 'gce.json')
|
||||||
response = {}
|
response = {}
|
||||||
try:
|
try:
|
||||||
|
@ -189,7 +223,7 @@ async def check_vultr_config(request):
|
||||||
|
|
||||||
|
|
||||||
@routes.get('/vultr_regions')
|
@routes.get('/vultr_regions')
|
||||||
async def vultr_regions(request):
|
async def vultr_regions(_):
|
||||||
async with ClientSession() as session:
|
async with ClientSession() as session:
|
||||||
async with session.get('https://api.vultr.com/v1/regions/list') as r:
|
async with session.get('https://api.vultr.com/v1/regions/list') as r:
|
||||||
json_body = await r.json()
|
json_body = await r.json()
|
||||||
|
@ -197,7 +231,7 @@ async def vultr_regions(request):
|
||||||
|
|
||||||
|
|
||||||
@routes.get('/scaleway_config')
|
@routes.get('/scaleway_config')
|
||||||
async def check_scaleway_config(request):
|
async def check_scaleway_config(_):
|
||||||
return web.json_response({"ok": 'SCW_TOKEN' in os.environ})
|
return web.json_response({"ok": 'SCW_TOKEN' in os.environ})
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -5,41 +5,37 @@
|
||||||
Enter your API token. The token must have read and write permissions
|
Enter your API token. The token must have read and write permissions
|
||||||
(<a href="https://cloud.digitalocean.com/settings/api/tokens" target="_blank" rel="noopener noreferrer">https://cloud.digitalocean.com/settings/api/tokens</a>):
|
(<a href="https://cloud.digitalocean.com/settings/api/tokens" target="_blank" rel="noopener noreferrer">https://cloud.digitalocean.com/settings/api/tokens</a>):
|
||||||
</label>
|
</label>
|
||||||
<input
|
<div v-if="ui_token_from_env">
|
||||||
|
<input
|
||||||
|
type="password"
|
||||||
|
class="form-control"
|
||||||
|
v-bind:disabled="ui_loading_check"
|
||||||
|
v-bind:value="'1234567890abcdef'"
|
||||||
|
/>
|
||||||
|
<div v-if="ui_token_from_env" class="form-text alert alert-success" role="alert">
|
||||||
|
The token was read from the environment variable
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div v-else>
|
||||||
|
<input
|
||||||
type="text"
|
type="text"
|
||||||
class="form-control"
|
class="form-control"
|
||||||
id="id_do_token"
|
id="id_do_token"
|
||||||
name="do_token"
|
name="do_token"
|
||||||
|
v-bind:disabled="ui_loading_check"
|
||||||
v-model="do_token"
|
v-model="do_token"
|
||||||
@blur="load_do_regions"
|
@blur="load_regions"
|
||||||
/>
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
<div class="form-group">
|
<region-select v-model="region"
|
||||||
<label
|
v-bind:options="ui_region_options"
|
||||||
v-if="do_regions.length > 0"
|
v-bind:loading="ui_loading_check || ui_loading_regions"
|
||||||
for="id_region"
|
v-bind:error="ui_region_error">
|
||||||
>What region should the server be located in?</label>
|
</region-select>
|
||||||
<label
|
<button v-on:click="submit"
|
||||||
v-if="do_regions.length === 0"
|
v-bind:disabled="!is_valid" class="btn btn-primary" type="button">Next</button>
|
||||||
for="id_region"
|
|
||||||
>Please enter API key above to select region</label>
|
|
||||||
<label v-if="do_region_loading" for="id_region">Loading regions...</label>
|
|
||||||
<select
|
|
||||||
name="region"
|
|
||||||
id="id_region"
|
|
||||||
class="form-control"
|
|
||||||
v-model="do_region"
|
|
||||||
v-bind:disabled="do_region_loading"
|
|
||||||
>
|
|
||||||
<option value disabled>Select region</option>
|
|
||||||
<option
|
|
||||||
v-for="(region, i) in do_regions"
|
|
||||||
v-bind:key="region.slug"
|
|
||||||
v-bind:value="region.slug"
|
|
||||||
>{{region.name}}</option>
|
|
||||||
</select>
|
|
||||||
</div>
|
|
||||||
<button v-on:click="submit" v-bind:disabled="!do_region" class="btn btn-primary" type="button">Next</button>
|
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
|
@ -48,34 +44,76 @@ module.exports = {
|
||||||
data: function() {
|
data: function() {
|
||||||
return {
|
return {
|
||||||
do_token: null,
|
do_token: null,
|
||||||
do_region: null,
|
region: null,
|
||||||
do_regions: [],
|
// helper variables
|
||||||
do_region_loading: false
|
ui_loading_check: false,
|
||||||
|
ui_loading_regions: false,
|
||||||
|
ui_region_error: null,
|
||||||
|
ui_token_from_env: false,
|
||||||
|
ui_region_options: []
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
computed: {
|
||||||
|
is_valid() {
|
||||||
|
return (this.do_config || this.ui_token_from_env) && this.region;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
created: function() {
|
||||||
|
this.check_config();
|
||||||
|
},
|
||||||
methods: {
|
methods: {
|
||||||
load_do_regions: function () {
|
check_config() {
|
||||||
this.do_region_loading = true;
|
this.ui_loading_check = true;
|
||||||
fetch('https://api.digitalocean.com/v2/regions', {
|
return fetch("/do_config")
|
||||||
headers: {
|
.then(r => r.json())
|
||||||
'Content-Type': 'application/json',
|
.then(response => {
|
||||||
'Authorization': `Bearer ${this.do_token}`
|
if (response.ok) {
|
||||||
|
this.ui_token_from_env = true;
|
||||||
|
this.load_regions();
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.then(r => r.json())
|
.finally(() => {
|
||||||
.then(r => {
|
this.ui_loading_check = false;
|
||||||
this.do_regions = r.regions;
|
});
|
||||||
|
},
|
||||||
|
load_regions() {
|
||||||
|
if (this.ui_token_from_env || this.do_token) {
|
||||||
|
this.ui_loading_regions = true;
|
||||||
|
this.ui_region_error = null;
|
||||||
|
const url = this.ui_token_from_env ? "/do_regions" : "/do_regions?token=" + this.do_token;
|
||||||
|
fetch(url)
|
||||||
|
.then((r) => {
|
||||||
|
if (r.status === 200) {
|
||||||
|
return r.json();
|
||||||
|
}
|
||||||
|
throw new Error(r.status);
|
||||||
|
})
|
||||||
|
.then((data) => {
|
||||||
|
this.ui_region_options = data.regions.map(i => ({key: i.slug, value: i.name}));
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
this.ui_region_error = err;
|
||||||
})
|
})
|
||||||
.finally(() => {
|
.finally(() => {
|
||||||
this.do_region_loading = false;
|
this.ui_loading_regions = false;
|
||||||
});
|
});
|
||||||
|
}
|
||||||
},
|
},
|
||||||
submit() {
|
submit() {
|
||||||
this.$emit('submit', {
|
if (this.ui_token_from_env) {
|
||||||
do_token: this.do_token,
|
this.$emit("submit", {
|
||||||
region: this.do_region
|
region: this.region
|
||||||
})
|
});
|
||||||
|
} else {
|
||||||
|
this.$emit("submit", {
|
||||||
|
do_token: this.do_token,
|
||||||
|
region: this.region
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
components: {
|
||||||
|
"region-select": window.httpVueLoader("/static/region-select.vue"),
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|
|
@ -103,6 +103,6 @@ module.exports = {
|
||||||
},
|
},
|
||||||
components: {
|
components: {
|
||||||
"region-select": window.httpVueLoader("/static/region-select.vue"),
|
"region-select": window.httpVueLoader("/static/region-select.vue"),
|
||||||
},
|
}
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
|
@ -8,6 +8,7 @@
|
||||||
<select
|
<select
|
||||||
name="region"
|
name="region"
|
||||||
class="form-control"
|
class="form-control"
|
||||||
|
v-bind:class="{ 'is-invalid': has_error }"
|
||||||
v-bind:value="value"
|
v-bind:value="value"
|
||||||
v-bind:disabled="ui_disabled"
|
v-bind:disabled="ui_disabled"
|
||||||
v-on:input="$emit('input', $event.target.value)"
|
v-on:input="$emit('input', $event.target.value)"
|
||||||
|
@ -20,24 +21,33 @@
|
||||||
>{{ region.value }}</option
|
>{{ region.value }}</option
|
||||||
>
|
>
|
||||||
</select>
|
</select>
|
||||||
|
<div v-if="has_error" class="invalid-feedback">
|
||||||
|
There was an error during fetching regions
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
module.exports = {
|
module.exports = {
|
||||||
props: ["value", "options", "loading"],
|
props: ["value", "options", "loading", "error"],
|
||||||
computed: {
|
computed: {
|
||||||
|
has_options: function() {
|
||||||
|
return this.options && this.options.length;
|
||||||
|
},
|
||||||
ui_disabled: function() {
|
ui_disabled: function() {
|
||||||
return !this.options.length || this.loading;
|
return !this.has_options || this.loading;
|
||||||
},
|
},
|
||||||
ui_show_slot: function() {
|
ui_show_slot: function() {
|
||||||
return !this.loading && !this.options.length
|
return !this.loading && !this.has_options
|
||||||
},
|
},
|
||||||
ui_show_loading: function() {
|
ui_show_loading: function() {
|
||||||
return this.loading;
|
return this.loading;
|
||||||
},
|
},
|
||||||
ui_show_select_prompt: function() {
|
ui_show_select_prompt: function() {
|
||||||
return this.options.length > 0;
|
return this.has_options;
|
||||||
|
},
|
||||||
|
has_error: function() {
|
||||||
|
return !!this.error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Reference in a new issue