-
-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathnamespace.go
More file actions
269 lines (226 loc) · 8.55 KB
/
namespace.go
File metadata and controls
269 lines (226 loc) · 8.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package services
import (
"context"
"errors"
"strings"
"github.com/shellhub-io/shellhub/api/store"
"github.com/shellhub-io/shellhub/pkg/api/authorizer"
"github.com/shellhub-io/shellhub/pkg/api/requests"
"github.com/shellhub-io/shellhub/pkg/clock"
"github.com/shellhub-io/shellhub/pkg/envs"
"github.com/shellhub-io/shellhub/pkg/models"
"github.com/shellhub-io/shellhub/pkg/uuid"
)
type NamespaceService interface {
ListNamespaces(ctx context.Context, req *requests.NamespaceList) ([]models.Namespace, int, error)
CreateNamespace(ctx context.Context, namespace *requests.NamespaceCreate) (*models.Namespace, error)
GetNamespace(ctx context.Context, tenantID string) (*models.Namespace, error)
DeleteNamespace(ctx context.Context, tenantID string) error
EditSessionRecordStatus(ctx context.Context, sessionRecord bool, tenantID string) error
GetSessionRecord(ctx context.Context, tenantID string) (bool, error)
}
// CreateNamespace creates a new namespace.
func (s *service) CreateNamespace(ctx context.Context, req *requests.NamespaceCreate) (*models.Namespace, error) {
user, err := s.store.UserResolve(ctx, store.UserIDResolver, req.UserID)
if err != nil || user == nil {
return nil, NewErrUserNotFound(req.UserID, err)
}
// When MaxNamespaces is less than zero, it means that the user has no limit
// of namespaces. If the value is zero, it means he has no right to create a new namespace
if user.MaxNamespaces == 0 {
return nil, NewErrNamespaceCreationIsForbidden(user.MaxNamespaces, nil)
} else if user.MaxNamespaces > 0 {
info, err := s.store.UserGetInfo(ctx, req.UserID)
switch {
case err != nil:
return nil, err
case len(info.OwnedNamespaces) >= user.MaxNamespaces:
return nil, NewErrNamespaceLimitReached(user.MaxNamespaces, nil)
}
}
conflictsTarget := &models.NamespaceConflicts{Name: strings.ToLower(req.Name)}
if _, has, err := s.store.NamespaceConflicts(ctx, conflictsTarget); has || err != nil {
return nil, NewErrNamespaceDuplicated(err)
}
ns := &models.Namespace{
Name: strings.ToLower(req.Name),
Owner: user.ID,
DevicesAcceptedCount: 0,
DevicesPendingCount: 0,
DevicesRejectedCount: 0,
DevicesRemovedCount: 0,
Members: []models.Member{
{
ID: user.ID,
Role: authorizer.RoleOwner,
AddedAt: clock.Now(),
},
},
Settings: &models.NamespaceSettings{
SessionRecord: true,
ConnectionAnnouncement: "",
AllowPassword: true,
AllowPublicKey: true,
AllowRoot: true,
AllowEmptyPasswords: true,
AllowTTY: true,
AllowTCPForwarding: true,
AllowWebEndpoints: true,
AllowSFTP: true,
AllowAgentForwarding: true,
},
TenantID: req.TenantID,
Type: models.NewDefaultType(),
}
if envs.IsCommunity() {
ns.Settings.ConnectionAnnouncement = models.DefaultAnnouncementMessage
}
if models.IsTypeTeam(req.Type) {
ns.Type = models.TypeTeam
} else if models.IsTypePersonal(req.Type) {
ns.Type = models.TypePersonal
}
if req.TenantID == "" {
ns.TenantID = uuid.Generate()
}
// Set limits according to ShellHub instance type
if envs.IsCloud() {
// cloud free plan is limited only by the max of devices
ns.MaxDevices = 3
} else {
// we don't set limits on enterprise and community instances
ns.MaxDevices = -1
}
if _, err := s.store.NamespaceCreate(ctx, ns); err != nil {
return nil, NewErrNamespaceCreateStore(err)
}
return ns, nil
}
func (s *service) ListNamespaces(ctx context.Context, req *requests.NamespaceList) ([]models.Namespace, int, error) {
opts := []store.QueryOption{s.store.Options().Match(&req.Filters), s.store.Options().Paginate(&req.Paginator)}
if req.UserID != "" {
opts = append(opts, s.store.Options().WithMember(req.UserID))
}
namespaces, count, err := s.store.NamespaceList(ctx, opts...)
if err != nil {
return nil, 0, NewErrNamespaceList(err)
}
return namespaces, count, nil
}
// GetNamespace gets a namespace.
//
// It receives a context, used to "control" the request flow and the tenant ID from models.Namespace.
//
// GetNamespace returns a models.Namespace and an error. When error is not nil, the models.Namespace is nil.
func (s *service) GetNamespace(ctx context.Context, tenantID string) (*models.Namespace, error) {
namespace, err := s.store.NamespaceResolve(ctx, store.NamespaceTenantIDResolver, tenantID)
if err != nil || namespace == nil {
return nil, NewErrNamespaceNotFound(tenantID, err)
}
return namespace, nil
}
// DeleteNamespace deletes a namespace.
//
// It receives a context, used to "control" the request flow and the tenant ID from models.Namespace.
//
// When cloud and billing is enabled, it will try to delete the namespace's billing information from the billing
// service if it exists.
func (s *service) DeleteNamespace(ctx context.Context, tenantID string) error {
n, err := s.store.NamespaceResolve(ctx, store.NamespaceTenantIDResolver, tenantID)
if err != nil {
return NewErrNamespaceNotFound(tenantID, err)
}
ableToReportDeleteNamespace := func(ns *models.Namespace) bool {
return !ns.Billing.IsNil() && ns.Billing.HasCutomer() && ns.Billing.HasSubscription()
}
if envs.IsCloud() && ableToReportDeleteNamespace(n) {
if err := s.reportBilling(ctx, tenantID, BillingActionNamespaceDelete); err != nil {
return NewErrBillingReportNamespaceDelete(err)
}
}
if err := fireNamespaceDelete(ctx, n); err != nil {
return err
}
return s.store.NamespaceDelete(ctx, n)
}
func (s *service) EditNamespace(ctx context.Context, req *requests.NamespaceEdit) (*models.Namespace, error) {
namespace, err := s.store.NamespaceResolve(ctx, store.NamespaceTenantIDResolver, req.Tenant)
if err != nil {
return nil, NewErrNamespaceNotFound(req.Tenant, err)
}
if req.Name != "" && !strings.EqualFold(req.Name, namespace.Name) {
namespace.Name = strings.ToLower(req.Name)
}
if req.Settings.SessionRecord != nil {
namespace.Settings.SessionRecord = *req.Settings.SessionRecord
}
if req.Settings.ConnectionAnnouncement != nil {
namespace.Settings.ConnectionAnnouncement = *req.Settings.ConnectionAnnouncement
}
if req.Settings.AllowPassword != nil {
namespace.Settings.AllowPassword = *req.Settings.AllowPassword
}
if req.Settings.AllowPublicKey != nil {
namespace.Settings.AllowPublicKey = *req.Settings.AllowPublicKey
}
if req.Settings.AllowRoot != nil {
namespace.Settings.AllowRoot = *req.Settings.AllowRoot
}
if req.Settings.AllowEmptyPasswords != nil {
namespace.Settings.AllowEmptyPasswords = *req.Settings.AllowEmptyPasswords
}
if req.Settings.AllowTTY != nil {
namespace.Settings.AllowTTY = *req.Settings.AllowTTY
}
if req.Settings.AllowTCPForwarding != nil {
namespace.Settings.AllowTCPForwarding = *req.Settings.AllowTCPForwarding
}
if req.Settings.AllowWebEndpoints != nil {
namespace.Settings.AllowWebEndpoints = *req.Settings.AllowWebEndpoints
}
if req.Settings.AllowSFTP != nil {
namespace.Settings.AllowSFTP = *req.Settings.AllowSFTP
}
if req.Settings.AllowAgentForwarding != nil {
namespace.Settings.AllowAgentForwarding = *req.Settings.AllowAgentForwarding
}
if err := s.store.NamespaceUpdate(ctx, namespace); err != nil {
return nil, err
}
return s.store.NamespaceResolve(ctx, store.NamespaceTenantIDResolver, req.Tenant)
}
// EditSessionRecordStatus defines if the sessions will be recorded.
//
// It receives a context, used to "control" the request flow, a boolean to define if the sessions will be recorded and
// the tenant ID from models.Namespace.
//
// This method is deprecated, use [NamespaceService#EditNamespace] instead.
func (s *service) EditSessionRecordStatus(ctx context.Context, sessionRecord bool, tenantID string) error {
n, err := s.store.NamespaceResolve(ctx, store.NamespaceTenantIDResolver, tenantID)
if err != nil {
switch {
case errors.Is(err, store.ErrNoDocuments):
return NewErrNamespaceNotFound(tenantID, err)
default:
return err
}
}
n.Settings.SessionRecord = sessionRecord
if err := s.store.NamespaceUpdate(ctx, n); err != nil { // nolint:revive
return err
}
return nil
}
// GetSessionRecord gets the session record data.
//
// It receives a context, used to "control" the request flow, the tenant ID from models.Namespace.
//
// GetSessionRecord returns a boolean indicating the session record status and an error. When error is not nil,
// the boolean is false.
func (s *service) GetSessionRecord(ctx context.Context, tenantID string) (bool, error) {
n, err := s.store.NamespaceResolve(ctx, store.NamespaceTenantIDResolver, tenantID)
if err != nil {
return false, NewErrNamespaceNotFound(tenantID, err)
}
return n.Settings.SessionRecord, nil
}