Skip to content

Commit

Permalink
api-cli: Added a new service
Browse files Browse the repository at this point in the history
This service does not replace the old ways of adding information
for the database, the binaries in the /bin folder they still
continue to develop the same function as the new service, so that
backward compatibility can be maintained.

Signed-off-by: asakiz <asakizin@gmail.com>
  • Loading branch information
Asakiz authored and gustavosbarreto committed Apr 19, 2021
1 parent 54a17cf commit 06db347
Show file tree
Hide file tree
Showing 23 changed files with 1,134 additions and 191 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/docker-build.yml
Expand Up @@ -19,7 +19,7 @@ jobs:
strategy:
fail-fast: false
matrix:
project: [ api, ssh, gateway, ui ]
project: [ api, ssh, gateway, ui, cli ]

runs-on: ubuntu-20.04

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/docker-publish.yml
Expand Up @@ -13,7 +13,7 @@ jobs:
strategy:
fail-fast: true
matrix:
project: [ api, ssh, gateway, ui ]
project: [ api, ssh, gateway, ui, cli ]

runs-on: ubuntu-20.04

Expand Down
5 changes: 4 additions & 1 deletion .github/workflows/qa.yml
Expand Up @@ -15,7 +15,7 @@ jobs:
strategy:
fail-fast: false
matrix:
project: [ api, agent, pkg, ssh, ui ]
project: [ api, agent, pkg, ssh, ui, cli ]
include:
- project: api
extra_args: ""
Expand All @@ -27,6 +27,8 @@ jobs:
extra_args: "-tags internal_api"
- project: ui
extra_args: ""
- project: cli
extra_args: ""

runs-on: ubuntu-latest

Expand All @@ -47,6 +49,7 @@ jobs:
- 'agent/**'
- 'pkg/**'
- 'ssh/**'
- 'cli/**'
- name: Set up Go 1.x [Go]
if: matrix.project != 'ui' && steps.filter.outputs.go == 'true'
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -11,3 +11,4 @@ api/api
ssh/ssh
agent/agent
agent/shellhub.key
cli/cli
36 changes: 1 addition & 35 deletions bin/add-namespace
Expand Up @@ -10,38 +10,4 @@ fi
NAMESPACE=$1
OWNER=$2

[ -z "$TENANT_ID" ] && TENANT_ID=`uuidgen`

if [ $(docker inspect --format='{{.State.Running}}' $(docker-compose ps -q mongo)) = false ]; then
echo "ERROR: mongo container is not running"
exit 1
fi

if ! echo $NAMESPACE | grep -q -E "^([a-zA-Z0-9]{1}[a-zA-Z0-9_-]{0,62}){1}(\.[a-zA-Z0-9_]{1}[a-zA-Z0-9_-]{0,62})*?$"; then
echo "ERROR: namespace is not a valid RFC1123 hostname format"
exit 1
fi

EXISTS=$(docker-compose exec -T mongo mongo main --quiet --eval "db.namespaces.find({ name: '$NAMESPACE' })")

if [ -n "$EXISTS" ]; then
echo "ERROR: namespace already exists!"
exit 1
fi

OWNER_ID=`docker-compose exec -T mongo mongo main --quiet --eval "JSON.stringify(db.users.findOne({ username:'$OWNER' }))" | docker run --rm -i imega/jq -r '._id."$oid" // empty'`

if [ -z "$OWNER_ID" ]; then
echo "ERROR: owner not found!"
exit 1
fi

INSERTED=`docker-compose exec -T mongo mongo main --quiet --eval "db.namespaces.insert({ name: '$NAMESPACE', owner: '$OWNER_ID', tenant_id: '$TENANT_ID', members: [ '$OWNER_ID' ], settings: {session_record: true}, max_devices: -1}).nInserted"`

if [ $INSERTED -eq 1 ]; then
echo "Namespace added: $NAMESPACE"
echo "Owner: $OWNER"
echo "Tenant ID: $TENANT_ID"
else
echo "ERROR: Failed to add namespace"
fi
docker-compose exec cli ./cli add-namespace $NAMESPACE $OWNER
27 changes: 2 additions & 25 deletions bin/add-user
Expand Up @@ -2,31 +2,8 @@

[ $# -ne 3 ] && echo "Usage: $0 <username> <password> <email>" && exit 1

if [ ! -f "$(which uuidgen 2> /dev/null)" ]; then
echo "$0 requires uuidgen but it's not installed. Aborting!"
exit 1
fi

USERNAME=$1
PASSWORD=`printf $2 | sha256sum | awk '{ print $1 }'`
PASSWORD=$2
EMAIL=$3

if [ $(docker inspect --format='{{.State.Running}}' $(docker-compose ps -q mongo)) = false ]; then
echo "ERROR: mongo container is not running"
exit 1
fi

EXISTS=$(docker-compose exec -T mongo mongo main --quiet --eval "db.users.find({ username: '$USERNAME' })")

if [ -n "$EXISTS" ]; then
echo "ERROR: user already exists!"
exit 1
fi

INSERTED=`docker-compose exec -T mongo mongo main --quiet --eval "db.users.insert({ name: '$USERNAME', username: '$USERNAME', password: '$PASSWORD', email: '$EMAIL' }).nInserted"`

if [ $INSERTED -eq 1 ]; then
echo "User added: $USERNAME"
else
echo "ERROR: Failed to add user"
fi
docker-compose exec cli ./cli add-user $USERNAME $PASSWORD $EMAIL
27 changes: 1 addition & 26 deletions bin/add-user-namespace
Expand Up @@ -5,29 +5,4 @@
USERNAME=$1
NAMESPACE=$2

if [ $(docker inspect --format='{{.State.Running}}' $(docker-compose ps -q mongo)) = false ]; then
echo "ERROR: mongo container is not running"
exit 1
fi

ID=`docker-compose exec -T mongo mongo main --quiet --eval "JSON.stringify(db.users.findOne({ name:'$USERNAME' }))" | docker run --rm -i imega/jq -r '._id."$oid" // empty'`

if [ -z "$ID" ]; then
echo "ERROR: user does not exists!"
exit 1
fi

TENANT_ID=`docker-compose exec -T mongo mongo main --quiet --eval "JSON.stringify(db.namespaces.findOne({ name:'$NAMESPACE' }))" | docker run --rm -i imega/jq -r '.tenant_id // empty'`

if [ -z "$TENANT_ID" ]; then
echo "ERROR: namespace does not exists!"
exit 1
fi

MODIFIED=`docker-compose exec -T mongo mongo main --quiet --eval "db.namespaces.updateOne({ tenant_id: '$TENANT_ID' }, { \\$addToSet: { members: '$ID' } }).modifiedCount"`

if [ $MODIFIED -eq 1 ]; then
echo "User $USERNAME added to namespace $NAMESPACE"
else
echo "ERROR: Failed to add user to namespace"
fi
docker-compose exec cli ./cli add-user-namespace $USERNAME $NAMESPACE
26 changes: 1 addition & 25 deletions bin/del-namespace
Expand Up @@ -4,28 +4,4 @@

NAMESPACE=$1

if [ $(docker inspect --format='{{.State.Running}}' $(docker-compose ps -q mongo)) = false ]; then
echo "ERROR: mongo container is not running"
exit 1
fi

EXISTS=$(docker-compose exec -T mongo mongo main --quiet --eval "db.namespaces.find({ name: '$NAMESPACE' })")

if [ -z "$EXISTS" ]; then
echo "ERROR: namespace does not exists!"
exit 1
fi

TENANT_ID=`docker-compose exec -T mongo mongo main --quiet --eval "JSON.stringify(db.namespaces.findOne({ name:'$NAMESPACE' }))" | docker run --rm -i imega/jq -r '.tenant_id // empty'`

for COL in devices sessions connected_devices firewall_rules public_keys recorded_sessions; do
docker-compose exec -T mongo mongo main --quiet --eval "db.$COL.remove({ tenant_id: '$TENANT_ID' }).nRemoved" > /dev/null
done

REMOVED=`docker-compose exec -T mongo mongo main --quiet --eval "db.namespaces.remove({name: '$NAMESPACE'}).nRemoved"`

if [ $REMOVED -gt 0 ]; then
echo "Namespace deleted"
else
echo "ERROR: Failed to delete namespace"
fi
docker-compose exec cli ./cli del-namespace $NAMESPACE
30 changes: 1 addition & 29 deletions bin/del-user
Expand Up @@ -4,32 +4,4 @@

USERNAME=$1

if [ $(docker inspect --format='{{.State.Running}}' $(docker-compose ps -q mongo)) = false ]; then
echo "ERROR: mongo container is not running"
exit 1
fi

EXISTS=$(docker-compose exec -T mongo mongo main --quiet --eval "db.users.find({ username: '$USERNAME' })")

if [ -z "$EXISTS" ]; then
echo "ERROR: user does not exists!"
exit 1
fi

OWNER_ID=`docker-compose exec -T mongo mongo main --quiet --eval "JSON.stringify(db.users.findOne({ username:'$USERNAME' }))" | docker run --rm -i imega/jq -r '._id."$oid" // empty'`

docker-compose exec -T mongo \
mongo main --quiet \
--eval "JSON.stringify(db.namespaces.find({ owner: '$OWNER_ID' }).toArray())" \
| docker run --rm -i imega/jq -c -r '.[] .name // empty' \
| while read NAMESPACE; do
./bin/del-namespace $NAMESPACE
done

REMOVED=`docker-compose exec -T mongo mongo main --quiet --eval "db.users.remove({username: '$USERNAME'}).nRemoved"`

if [ $REMOVED -gt 0 ]; then
echo "User deleted"
else
echo "ERROR: Failed to delete user"
fi
docker-compose exec cli ./cli del-user $USERNAME
27 changes: 1 addition & 26 deletions bin/del-user-namespace
Expand Up @@ -5,29 +5,4 @@
USERNAME=$1
NAMESPACE=$2

if [ $(docker inspect --format='{{.State.Running}}' $(docker-compose ps -q mongo)) = false ]; then
echo "ERROR: mongo container is not running"
exit 1
fi

ID=`docker-compose exec -T mongo mongo main --quiet --eval "JSON.stringify(db.users.findOne({ name:'$USERNAME' }))" | docker run --rm -i imega/jq -r '._id."$oid" // empty'`

if [ -z "$ID" ]; then
echo "ERROR: user does not exists!"
exit 1
fi

TENANT_ID=`docker-compose exec -T mongo mongo main --quiet --eval "JSON.stringify(db.namespaces.findOne({ name:'$NAMESPACE' }))" | docker run --rm -i imega/jq -r '.tenant_id // empty'`

if [ -z "$TENANT_ID" ]; then
echo "ERROR: namespace does not exists!"
exit 1
fi

MODIFIED=`docker-compose exec -T mongo mongo main --quiet --eval "db.namespaces.updateOne({ tenant_id: '$TENANT_ID' }, { \\$pull: { members: '$ID' } }).modifiedCount"`

if [ $MODIFIED -eq 1 ]; then
echo "User $USERNAME removed from namespace $NAMESPACE"
else
echo "ERROR: Failed to remove user from namespace"
fi
docker-compose exec cli ./cli del-user-namespace $USERNAME $NAMESPACE
22 changes: 2 additions & 20 deletions bin/reset-user-password
Expand Up @@ -3,24 +3,6 @@
[ -z $1 ] || [ -z $2 ] && echo "Usage: $0 <username> <password>" && exit 1

USERNAME=$1
PASSWORD=`printf $2 | sha256sum | awk '{ print $1 }'`
PASSWORD=$2

if [ $(docker inspect --format='{{.State.Running}}' $(docker-compose ps -q mongo)) = false ]; then
echo "ERROR: mongo container is not running"
exit 1
fi

EXISTS=$(docker-compose exec -T mongo mongo main --quiet --eval "db.users.find({ username: '$USERNAME' })")

if [ -z "$EXISTS" ]; then
echo "ERROR: user does not exists!"
exit 1
fi

MODIFIED=`docker-compose exec -T mongo mongo main --quiet --eval "db.users.update({ username: '$USERNAME' }, { \\$set: { password: '$PASSWORD' } }).nModified"`

if [ $MODIFIED -eq 1 ]; then
echo "Password changed"
else
echo "User password not changed"
fi
docker-compose exec cli ./cli reset-user-password $USERNAME $PASSWORD
56 changes: 56 additions & 0 deletions cli/Dockerfile
@@ -0,0 +1,56 @@
# base stage
FROM golang:1.13-alpine3.11 AS base

RUN apk add --no-cache git ca-certificates

WORKDIR $GOPATH/src/github.com/shellhub-io/shellhub

COPY ./go.mod ./
COPY ./api/go.mod ./api/go.mod

WORKDIR $GOPATH/src/github.com/shellhub-io/shellhub/cli

COPY ./cli/go.mod ./cli/go.sum ./

RUN go mod download

# builder stage
FROM base AS builder

COPY ./api $GOPATH/src/github.com/shellhub-io/shellhub/api
COPY ./pkg $GOPATH/src/github.com/shellhub-io/shellhub/pkg
COPY ./cli .

WORKDIR $GOPATH/src/github.com/shellhub-io/shellhub

RUN go mod download

WORKDIR $GOPATH/src/github.com/shellhub-io/shellhub/cli

RUN go build

# development stage
FROM base AS development

RUN apk add --update openssl build-base docker-cli
RUN go get github.com/markbates/refresh && \
go get github.com/golangci/golangci-lint/cmd/golangci-lint@v1.37.1 && \
go get github.com/vektra/mockery/v2/.../

WORKDIR $GOPATH/src/github.com/shellhub-io/shellhub

RUN go mod download

COPY ./cli/entrypoint-dev.sh /entrypoint.sh
COPY ./.revive.toml /

WORKDIR $GOPATH/src/github.com/shellhub-io/shellhub/cli

ENTRYPOINT ["/entrypoint.sh"]

# production stage
FROM alpine:3.11 AS production

COPY --from=builder /go/src/github.com/shellhub-io/shellhub/cli/cli /cli

ENTRYPOINT /cli
3 changes: 3 additions & 0 deletions cli/entrypoint-dev.sh
@@ -0,0 +1,3 @@
#!/bin/sh

refresh run
5 changes: 5 additions & 0 deletions cli/entrypoint.sh
@@ -0,0 +1,5 @@
#!/bin/sh

gomplate -f /etc/nginx/conf.d/shellhub.conf -o /etc/nginx/conf.d/shellhub.conf

exec "$@"
19 changes: 19 additions & 0 deletions cli/go.mod
@@ -0,0 +1,19 @@
module github.com/shellhub-io/shellhub/cli

go 1.14

require (
github.com/kelseyhightower/envconfig v1.4.0
github.com/satori/go.uuid v1.2.0
github.com/shellhub-io/shellhub v0.5.2
github.com/shellhub-io/shellhub/api v0.5.2
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.1.3
github.com/stretchr/testify v1.7.0
go.mongodb.org/mongo-driver v1.5.1
gopkg.in/go-playground/validator.v9 v9.31.0
)

replace github.com/shellhub-io/shellhub => ../

replace github.com/shellhub-io/shellhub/api => ../api

0 comments on commit 06db347

Please sign in to comment.