Skip to content
This repository has been archived by the owner on Feb 16, 2021. It is now read-only.

Migrates SSH Observatory datastore from MongoDB to Postgres #121

Merged
merged 52 commits into from
Jan 19, 2018

Conversation

claudijd
Copy link
Contributor

@claudijd claudijd commented Jan 9, 2018

This is a work in progress to put together a postgres backend replacement for mongodb. It's far from done, but a PR is the best place to share with others and get their feedback.


# Creates a database
def create(name="ssh_scan")
@client.exec("CREATE DATABASE #{name}")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

All these SQL calls need to be parameterized for safety

# "scan" => nil,
# "queue_time" => Time.now,
# "worker_id" => nil,
# )
Copy link
Contributor Author

Choose a reason for hiding this comment

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

All these interfaces need populating/porting into postgres capable

@claudijd claudijd changed the title [WIP] First pass on postgres prototype [WIP] postgres prototype Jan 10, 2018
@claudijd
Copy link
Contributor Author

With the latest push, this should start passing again. I will also be doing some bench sanity checks to make sure it still works end to end before landing this change.

@claudijd
Copy link
Contributor Author

Ok, so finally able to task scans properly, so we're getting there, next up, troubleshooting the minor changes we made but forgot to adjust the interfaces for.

@caggle
Copy link

caggle commented Jan 17, 2018

Instructions worked great for me, no issues with or without jq.

I did notice though, when you scan a system with 22/tcp is closed, currently the scan goes on forever? And worker instance borks some time when this happens.

@SCMGuruLLC
Copy link

SCMGuruLLC commented Jan 17, 2018

I'm running Ubuntu Server 16.04.3 LTS 32-bit.

I ran:
3.) docker-compose up --build

And got:
ERROR: Couldn't connect to Docker daemon at http+docker://localunixsocket - is it running?
If it's at a non-standard location, specify the URL with the DOCKER_HOST environment variable.

I ran:
sudo docker-compose up --build

And got:
Creating network "sshscanapi_default" with the default driver
Building base
Step 1/8 : FROM ruby:latest AS base
ERROR: Service 'base' failed to build: Error parsing reference: "ruby:latest AS base" is not a valid repository/tag: invalid reference format

@claudijd
Copy link
Contributor Author

claudijd commented Jan 17, 2018

@cag I believe ssh_scan has logic to handle the socket timeout and will drop the scan result into an error state. Unfortunately, the clients are very unsophisticated, mainly because they are for demo purposes and don't have logic to capture that error condition. That said, you should be able to verify this by accessing this route after attempting the scan...

https://sshscan.rubidus.com/api/v1/scan/results?uuid=db49ba74-8482-4c30-9cbe-66f4ae62683b

You just need to replace the UUID in the url to the UUID the client says it got for that scan. We will then return everything we have for that scan, but if the socket timed out, it will be very little, but it should be there. In other words, I believe this is working as expected, please let me know if it doesn't behave this way for you.

@claudijd
Copy link
Contributor Author

claudijd commented Jan 17, 2018

@scmaccal the sudo/non-sudo behavior is simply a function of your current user not having docker privs. If you add that user to the docker group, I think that will solve the problem.

As for the second issue, I'm wondering if it's a docker version issue, would you mind sharing the output of the following:

docker-compose version
docker version

I'm assuming your version of docker doesn't support the AS capability, but I'd like to compare to be sure.

@SCMGuruLLC
Copy link

SCMGuruLLC commented Jan 17, 2018 via email

@claudijd
Copy link
Contributor Author

@scmaccal I'm running docker-compose 1.18.0 and docker 17.12.0-ce. I'm wondering if upgrading both of those on your system will fulfill the FROM/AS behavior and start working.

@SCMGuruLLC
Copy link

SCMGuruLLC commented Jan 17, 2018 via email

@claudijd claudijd changed the title [ALPHA] Migrates SSH Observatory datastore from MongoDB to Postgres [BETA] Migrates SSH Observatory datastore from MongoDB to Postgres Jan 17, 2018
@SCMGuruLLC
Copy link

SCMGuruLLC commented Jan 18, 2018 via email

@claudijd
Copy link
Contributor Author

@scmaccal I have a doc for the typical docker infra setup, which is maintained on the GitHub wiki here (https://github.com/mozilla/ssh_scan_api/wiki/Deploying-ssh_scan_api-using-docker-compose). I'll probably add these additions there and then just ask people to follow that guide (since this PR will likely be deployed by EOW). Thanks for the feedback, that's really helpful to avoid future issues for people trying it out.

@claudijd
Copy link
Contributor Author

claudijd commented Jan 18, 2018

@scmaccal the wiki instructions have been updated. For anyone wishing to test out this branch, they simply need to follow the instructions here and replace the git checkout master with git checkout postgres_sql_prototype and to let me know if they have any problems.

@claudijd
Copy link
Contributor Author

claudijd commented Jan 18, 2018

@scmaccal you may have gotten a collaboration request on this repo. It was my attempt to list you as a named reviewer on this PR, but I guess that's not possible without giving you write access. I ended up canceling it because it didn't give me the desirer effect. I think your comments and help here have been excellent, thanks again for the help with this!

@SCMGuruLLC
Copy link

SCMGuruLLC commented Jan 18, 2018 via email

@claudijd claudijd changed the title [BETA] Migrates SSH Observatory datastore from MongoDB to Postgres Migrates SSH Observatory datastore from MongoDB to Postgres Jan 19, 2018
@claudijd claudijd merged commit b3372f3 into master Jan 19, 2018
@caggle
Copy link

caggle commented Jan 22, 2018

@claudijd Thanks. Yes I didn't realise the logic to handle socket timeout was actually in the proper client (ssh_scan).

Viewing the scan result for a scan attempted on a closed port (e.g. http://127.0.0.1:8000/api/v1/scan/results?uuid=0c435d0d-ddae-494d-a01d-b8c78bb5a01d) returns "status": "ERRORED", which is probably the expected result.

@claudijd
Copy link
Contributor Author

Just an FYI: although this PR worked on the bench, in practice under a multi-threaded load, this PG port doesn't operate very well and is entirely too unstable to run in any predictable way. That said, I'm probably going to be rolling back these changes on the production server today and I'll be changing my strategy to leverage a better supported SQL abstraction, such as ActiveRecord. I've got the first commit of that effort started in #137 and I'll be shooting for a rough target of EOW to land the ActiveRecord port.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants