Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 0 additions & 44 deletions reserved.py

This file was deleted.

125 changes: 0 additions & 125 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from tornado.wsgi import WSGIContainer
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
from reserved import RESERVED_USERNAMES

app = Flask(__name__)
app.config['RESTFUL_JSON'] = {"indent": 4}
Expand All @@ -25,130 +24,6 @@
https_port = 4443
http_port = 8080


def validate_pubkey(value):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs to be added into userdb

if len(value) > 8192 or len(value) < 80:
raise ValueError("Expected length to be between 80 and 8192 characters")

value = value.replace("\"", "").replace("'", "").replace("\\\"", "")
value = value.split(' ')
types = [ 'ecdsa-sha2-nistp256', 'ecdsa-sha2-nistp384',
'ecdsa-sha2-nistp521', 'ssh-rsa', 'ssh-dss', 'ssh-ed25519' ]
if value[0] not in types:
raise ValueError(
"Expected " + ', '.join(types[:-1]) + ', or ' + types[-1]
)

return "%s %s" % (value[0], value[1])


def validate_username(value):

# Regexp must be kept in sync with
# https://github.com/hashbang/hashbang.sh/blob/master/src/hashbang.sh#L186-196
if re.compile(r"^[a-z][a-z0-9]{,30}$").match(value) is None:
raise ValueError('Username is invalid')

if value in RESERVED_USERNAMES:
raise ValueError('Username is reserved')

return value

@api.representation('text/plain')
def output_plain(data, code, headers=None):
lines = []
for server, stats in data.items():
line = [server]
for key, val in stats.items():
line.append(str(val))
lines.append("|".join(line))
data = "\n".join(lines)
resp = make_response(data, code)
resp.headers.extend(headers or {})
return resp


class UserCreate(Resource):
def __init__(self):
self.reqparse = RequestParser()
self.reqparse.add_argument(
'user',
type = validate_username,
required = True,
location = 'json'
)
self.reqparse.add_argument(
'key',
type = validate_pubkey,
required = True,
location = 'json'
)
self.reqparse.add_argument(
'host',
type = str,
required = True,
location = 'json'
)
super(UserCreate, self).__init__()

def post(self):
args = self.reqparse.parse_args()

try:
data = requests.post(
"https://userdb.hashbang.sh/passwd",
data = {
"name": str(args["user"]),
"host": args["host"],
"data": {
"shell": "/bin/bash",
"ssh_keys": [
args["key"]
]
}
}
).json()
if 'passwd_name_key' in data['message']:
return {'message': 'User already exists'}, 400
if 'passwd_host_fkey' in data['message']:
return {'message': 'Unknown shell server'}, 400
except:
(typ, value, tb) = sys.exc_info()
sys.stderr.write("Unexpected Error: %s\n" % typ)
sys.stderr.write("\t%s\n" % value)
traceback.print_tb(tb)
return {'message': 'User creation script failed'}, 500

return {'message': 'success'}


class ServerStats(Resource):
LOCATIONS = {
"da1.hashbang.sh": {"lat": 32.8, "lon": -96.8},
"ny1.hashbang.sh": {"lat": 40.7, "lon": -74},
"sf1.hashbang.sh": {"lat": 37.8, "lon": -122.4},
"to1.hashbang.sh": {"lat": 43.7, "lon": -79.4},
"de1.hashbang.sh": {"lat": 49.4478, "lon": 11.0683},
}

def get(self, out_format='json'):
try:
data = requests.get("https://userdb.hashbang.sh/hosts").json()
except:
return {'message': 'Unable to connect to server'}, 500

for idx, s in enumerate(data):
server_host = s['name']
if server_host in self.LOCATIONS.keys():
data[idx].update({'coordinates': self.LOCATIONS[server_host]})

return data


api.add_resource(UserCreate, '/user/create')
api.add_resource(ServerStats, '/server/stats')


def security_headers(response, secure=False):
csp = "default-src 'none'; " \
"style-src https://fonts.googleapis.com 'self'; " \
Expand Down
4 changes: 1 addition & 3 deletions src/hashbang.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@
As an advanced alternative you can bypass using this script and hit our user
provisioning API directly with an SSH Public Key and desired username:

> curl https://hashbang.sh/server/stats
{ "server.hashbang.sh": { infos ...}, ... }
> curl -d '{"user":"someuser","key":"'"$(cat ~/.ssh/id_rsa.pub)"'","host":"someHost.hashbang.sh"}' -H 'Content-Type: application/json' https://hashbang.sh/user/create
> curl https://userdb.hashbang.sh/passwd -H Content-Type:application/json -d '{"name": "'"${USER}"'", "host": "de1.hashbang.sh", "data": {"shell": "/bin/bash", "ssh_keys": ["'"$(cat .ssh/id_rsa.pub)"'"]}}'


We look forward to seeing you on the other side.
Expand Down