Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api,gateway,pkg): add and fit user created on setup route on new rules #4236

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions api/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ func NewRouter(service services.Service, opts ...Option) *echo.Echo {
publicAPI.GET(GetSessionRecordURL, gateway.Handler(handler.GetSessionRecord))
publicAPI.PUT(EditSessionRecordStatusURL, gateway.Handler(handler.EditSessionRecordStatus), routesmiddleware.BlockAPIKey, routesmiddleware.RequiresPermission(authorizer.NamespaceEnableSessionRecord))

internalAPI.POST(SetupEndpoint, gateway.Handler(handler.Setup))

// NOTE: Rewrite requests to containers to devices, as they are the same thing under the hood, using it as an alias.
e.Pre(echoMiddleware.Rewrite(map[string]string{
"/api/containers": "/api/devices?connector=true",
Expand Down
5 changes: 5 additions & 0 deletions api/services/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ var (
ErrAPIKeyDuplicated = errors.New("APIKey duplicated", ErrLayer, ErrCodeDuplicated)
ErrAuthForbidden = errors.New("user is authenticated but cannot access this resource", ErrLayer, ErrCodeForbidden)
ErrRoleInvalid = errors.New("role is invalid", ErrLayer, ErrCodeForbidden)
ErrUserDelete = errors.New("user couldn't be deleted", ErrLayer, ErrCodeInvalid)
)

func NewErrRoleInvalid() error {
Expand Down Expand Up @@ -477,3 +478,7 @@ func NewErrDeviceMaxDevicesReached(count int) error {
func NewErrAuthForbidden() error {
return NewErrForbidden(ErrAuthForbidden, nil)
}

func NewErrUserDelete(err error) error {
return NewErrInvalid(ErrUserDelete, nil, err)
}
8 changes: 6 additions & 2 deletions api/services/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (s *service) Setup(ctx context.Context, req requests.Setup) error {
}

namespace := &models.Namespace{
Name: req.Namespace,
Name: req.Username,
Owner: insertedID,
MaxDevices: 0,
Members: []models.Member{
Expand All @@ -60,11 +60,15 @@ func (s *service) Setup(ctx context.Context, req requests.Setup) error {
CreatedAt: clock.Now(),
Settings: &models.NamespaceSettings{
SessionRecord: false,
ConnectionAnnouncement: "",
ConnectionAnnouncement: models.DefaultAnnouncementMessage,
},
}

if _, err = s.store.NamespaceCreate(ctx, namespace); err != nil {
if err := s.store.UserDelete(ctx, insertedID); err != nil {
return NewErrUserDelete(err)
}

return NewErrNamespaceDuplicated(err)
}

Expand Down
107 changes: 82 additions & 25 deletions api/services/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,10 @@ func TestSetup(t *testing.T) {
{
description: "Fail when cannot create the user",
req: requests.Setup{
Email: "teste@google.com",
Name: "userteste",
Username: "userteste",
Password: "secret",
Namespace: "teste-space",
Email: "teste@google.com",
Name: "userteste",
Username: "userteste",
Password: "secret",
},
requiredMocks: func() {
hashMock.
Expand All @@ -54,11 +53,10 @@ func TestSetup(t *testing.T) {
{
description: "Fail when cannot create the user",
req: requests.Setup{
Email: "teste@google.com",
Name: "userteste",
Username: "userteste",
Password: "secret",
Namespace: "teste-space",
Email: "teste@google.com",
Name: "userteste",
Username: "userteste",
Password: "secret",
},
requiredMocks: func() {
clockMock.On("Now").Return(now).Once()
Expand Down Expand Up @@ -88,11 +86,10 @@ func TestSetup(t *testing.T) {
{
description: "Fail when cannot create namespace",
req: requests.Setup{
Email: "teste@google.com",
Name: "userteste",
Username: "userteste",
Password: "secret",
Namespace: "teste-space",
Email: "teste@google.com",
Name: "userteste",
Username: "userteste",
Password: "secret",
},
requiredMocks: func() {
clockMock.On("Now").Return(now).Twice()
Expand All @@ -118,8 +115,11 @@ func TestSetup(t *testing.T) {
Hash: "$2a$10$V/6N1wsjheBVvWosPfv02uf4WAOb9lmp8YVVCIa2UYuFV4OJby7Yi",
},
}

storeMock.On("UserCreate", ctx, user).Return("000000000000000000000000", nil).Once()

namespace := &models.Namespace{
Name: "teste-space",
Name: "userteste",
Owner: "000000000000000000000000",
MaxDevices: 0,
Members: []models.Member{
Expand All @@ -130,23 +130,80 @@ func TestSetup(t *testing.T) {
},
Settings: &models.NamespaceSettings{
SessionRecord: false,
ConnectionAnnouncement: "",
ConnectionAnnouncement: models.DefaultAnnouncementMessage,
},
CreatedAt: now,
}
storeMock.On("UserCreate", ctx, user).Return("000000000000000000000000", nil).Once()

storeMock.On("NamespaceCreate", ctx, namespace).Return(namespace, errors.New("error", "", 0)).Once()
storeMock.On("UserDelete", ctx, "000000000000000000000000").Return(nil).Once()
},
expected: NewErrNamespaceDuplicated(errors.New("error", "", 0)),
},
{
description: "Fail when cannot create namespace, and user deletion fails",
req: requests.Setup{
Email: "teste@google.com",
Name: "userteste",
Username: "userteste",
Password: "secret",
},
requiredMocks: func() {
clockMock.On("Now").Return(now).Twice()

uuidMock := &uuid_mocks.Uuid{}
uuidMock.On("Generate").Return("random_uuid").Once()

hashMock.
On("Do", "secret").
Return("$2a$10$V/6N1wsjheBVvWosPfv02uf4WAOb9lmp8YVVCIa2UYuFV4OJby7Yi", nil).
Once()

user := &models.User{
Status: models.UserStatusConfirmed,
CreatedAt: now,
UserData: models.UserData{
Name: "userteste",
Email: "teste@google.com",
Username: "userteste",
},
Password: models.UserPassword{
Plain: "secret",
Hash: "$2a$10$V/6N1wsjheBVvWosPfv02uf4WAOb9lmp8YVVCIa2UYuFV4OJby7Yi",
},
}

storeMock.On("UserCreate", ctx, user).Return("000000000000000000000000", nil).Once()

namespace := &models.Namespace{
Name: "userteste",
Owner: "000000000000000000000000",
MaxDevices: 0,
Members: []models.Member{
{
ID: "000000000000000000000000",
Role: authorizer.RoleOwner,
},
},
Settings: &models.NamespaceSettings{
SessionRecord: false,
ConnectionAnnouncement: models.DefaultAnnouncementMessage,
},
CreatedAt: now,
}

storeMock.On("NamespaceCreate", ctx, namespace).Return(namespace, errors.New("error", "", 0)).Once()
storeMock.On("UserDelete", ctx, "000000000000000000000000").Return(errors.New("error", "", 0)).Once()
},
expected: NewErrUserDelete(errors.New("error", "", 0)),
},
{
description: "Success to create the user and namespace",
req: requests.Setup{
Email: "teste@google.com",
Name: "userteste",
Username: "userteste",
Password: "secret",
Namespace: "teste-space",
Email: "teste@google.com",
Name: "userteste",
Username: "userteste",
Password: "secret",
},
requiredMocks: func() {
clockMock.On("Now").Return(now).Twice()
Expand All @@ -172,7 +229,7 @@ func TestSetup(t *testing.T) {
},
}
namespace := &models.Namespace{
Name: "teste-space",
Name: "userteste",
Owner: "000000000000000000000000",
MaxDevices: 0,
Members: []models.Member{
Expand All @@ -183,7 +240,7 @@ func TestSetup(t *testing.T) {
},
Settings: &models.NamespaceSettings{
SessionRecord: false,
ConnectionAnnouncement: "",
ConnectionAnnouncement: models.DefaultAnnouncementMessage,
},
CreatedAt: now,
}
Expand Down
1 change: 1 addition & 0 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ services:
- ./pkg:/go/src/github.com/shellhub-io/shellhub/pkg
- ./.golangci.yaml:/.golangci.yaml
- ./gateway/nginx:/templates
- ./gateway/setup:/srv/setup
environment:
- SHELLHUB_VERSION=latest
- SHELLHUB_ENV=${SHELLHUB_ENV}
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ services:
depends_on:
- api
- ui
- mongo
ports:
- ${SHELLHUB_BIND_ADDRESS}:${SHELLHUB_HTTP_PORT}:80
networks:
Expand Down
2 changes: 2 additions & 0 deletions gateway/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ COPY ./gateway/go.mod ./gateway/go.sum ./

RUN go mod download

COPY ./gateway/setup /srv/setup

# builder stage
FROM base AS builder

Expand Down
4 changes: 3 additions & 1 deletion gateway/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package main
import (
"context"
"fmt"
"runtime"

"github.com/go-playground/validator/v10"
"github.com/sethvargo/go-envconfig"
"runtime"
)

// GatewayConfig holds the configuration settings for the gateway.
Expand All @@ -21,6 +22,7 @@ type GatewayConfig struct {
EnableProxyProtocol bool `env:"SHELLHUB_PROXY"`
EnableEnterprise bool `env:"SHELLHUB_ENTERPRISE"`
EnableCloud bool `env:"SHELLHUB_CLOUD"`
MongoURI string `env:"MONGO_URI,default=mongodb://mongo:27017/main"`
}

var validate = validator.New()
Expand Down
39 changes: 37 additions & 2 deletions gateway/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,55 @@ require (
github.com/go-playground/validator/v10 v10.22.0
github.com/pkg/errors v0.9.1
github.com/sethvargo/go-envconfig v0.9.0
github.com/shellhub-io/shellhub/api v0.16.3
golang.org/x/sys v0.22.0
)

require (
github.com/andybalholm/brotli v1.1.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/go-redis/cache/v8 v8.4.4 // indirect
github.com/go-redis/redis/v8 v8.11.5 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/klauspost/compress v1.17.8 // indirect
github.com/klauspost/pgzip v1.2.5 // indirect
github.com/labstack/echo/v4 v4.12.0 // indirect
github.com/labstack/gommon v0.4.2 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mholt/archiver/v3 v3.5.1 // indirect
github.com/montanaflynn/stats v0.7.1 // indirect
github.com/nwaples/rardecode v1.1.3 // indirect
github.com/oschwald/geoip2-golang v1.8.0 // indirect
github.com/oschwald/maxminddb-golang v1.10.0 // indirect
github.com/pierrec/lz4/v4 v4.1.17 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/shellhub-io/shellhub v0.13.4 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/square/mongo-lock v0.0.0-20230808145049-cfcf499f6bf0 // indirect
github.com/ulikunitz/xz v0.5.11 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
github.com/vmihailenco/go-tinylfu v0.2.2 // indirect
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
github.com/xakep666/mongo-migrate v0.3.2 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.2 // indirect
github.com/xdg-go/stringprep v1.0.4 // indirect
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect
go.mongodb.org/mongo-driver v1.16.0 // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/text v0.16.0 // indirect
)

Expand Down
Loading
Loading