Skip to content

Commit 915a3bc

Browse files
heiytorgustavosbarreto
authored andcommitted
feat(api): enable setup endpoint for Enterprise environments
Enable the setup flow for both Community and Enterprise environments by changing the route condition from `IsCommunity()` to `!IsCloud()`. Add migration 117 to set the `setup` field in the system collection based on existing users count for non-cloud environments.
1 parent 5a175e9 commit 915a3bc

File tree

5 files changed

+50
-3
lines changed

5 files changed

+50
-3
lines changed

api/routes/routes.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ func NewRouter(service services.Service, opts ...Option) *echo.Echo {
177177
publicAPI.GET(GetSessionRecordURL, gateway.Handler(handler.GetSessionRecord))
178178
publicAPI.PUT(EditSessionRecordStatusURL, gateway.Handler(handler.EditSessionRecordStatus), routesmiddleware.RequiresPermission(authorizer.NamespaceEnableSessionRecord))
179179

180-
if envs.IsCommunity() {
180+
if !envs.IsCloud() {
181181
publicAPI.POST(SetupEndpoint, gateway.Handler(handler.Setup))
182182
}
183183

api/routes/setup_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ func TestSetup(t *testing.T) {
2121
envs.DefaultBackend = envMock
2222

2323
envMock.On("Get", "SHELLHUB_CLOUD").Return("false")
24-
envMock.On("Get", "SHELLHUB_ENTERPRISE").Return("false")
2524

2625
servicesMock := new(serviceMocks.Service)
2726

api/store/mongo/migrations/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ func GenerateMigrations() []migrate.Migration {
126126
migration114,
127127
migration115,
128128
migration116,
129+
migration117,
129130
}
130131
}
131132

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package migrations
2+
3+
import (
4+
"context"
5+
6+
"github.com/shellhub-io/shellhub/pkg/envs"
7+
log "github.com/sirupsen/logrus"
8+
migrate "github.com/xakep666/mongo-migrate"
9+
"go.mongodb.org/mongo-driver/bson"
10+
"go.mongodb.org/mongo-driver/mongo"
11+
)
12+
13+
var migration117 = migrate.Migration{
14+
Version: 117,
15+
Description: "Set setup field in system collection for non-cloud environments",
16+
Up: migrate.MigrationFunc(func(ctx context.Context, db *mongo.Database) error {
17+
log.WithFields(log.Fields{
18+
"component": "migration",
19+
"version": 117,
20+
"action": "Up",
21+
}).Info("Applying migration up")
22+
23+
if envs.IsCloud() {
24+
return nil
25+
}
26+
27+
usersCount, err := db.Collection("users").CountDocuments(ctx, bson.M{})
28+
if err != nil {
29+
return err
30+
}
31+
32+
_, err = db.Collection("system").UpdateOne(ctx, bson.M{}, bson.M{"$set": bson.M{"setup": usersCount > 0}})
33+
34+
return err
35+
}),
36+
Down: migrate.MigrationFunc(func(ctx context.Context, db *mongo.Database) error {
37+
log.WithFields(log.Fields{
38+
"component": "migration",
39+
"version": 117,
40+
"action": "Down",
41+
}).Info("Applying migration down")
42+
43+
log.Info("Unable to undo setup field changes")
44+
45+
return nil
46+
}),
47+
}

gateway/nginx/conf.d/shellhub.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ server {
4242
server_tokens off;
4343
resolver 127.0.0.11 ipv6=off;
4444

45-
{{ if and (not $cfg.EnableCloud) (not $cfg.EnableEnterprise) }}
45+
{{ if not $cfg.EnableCloud }}
4646

4747
location = /api/setup {
4848
set $upstream api:8080;

0 commit comments

Comments
 (0)