forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugins.go
189 lines (169 loc) · 6.17 KB
/
plugins.go
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
/*
* Copyright (c) 2018. Abstrium SAS <team (at) pydio.com>
* This file is part of Pydio Cells.
*
* Pydio Cells is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio Cells is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio Cells. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com>.
*/
// Package grpc provides the gRPC service to communicate with the Pydio's user persistence layer.
package grpc
import (
"context"
"encoding/base64"
"os"
"strings"
"time"
"github.com/micro/go-micro"
"github.com/micro/go-micro/errors"
"github.com/pydio/cells/common"
"github.com/pydio/cells/common/config"
"github.com/pydio/cells/common/log"
defaults "github.com/pydio/cells/common/micro"
"github.com/pydio/cells/common/plugins"
"github.com/pydio/cells/common/proto/idm"
"github.com/pydio/cells/common/service"
servicecontext "github.com/pydio/cells/common/service/context"
service2 "github.com/pydio/cells/common/service/proto"
"github.com/pydio/cells/idm/user"
"github.com/pydio/cells/scheduler/actions"
)
const (
ENV_PYDIO_ADMIN_USER_LOGIN = "PYDIO_ADMIN_USER_LOGIN"
ENV_PYDIO_ADMIN_USER_PASSWORD = "PYDIO_ADMIN_USER_PASSWORD"
)
func init() {
actions.GetActionsManager().Register(DeleteUsersActionName, func() actions.ConcreteAction {
return &DeleteUsersAction{}
})
plugins.Register(func(ctx context.Context) {
service.NewService(
service.Name(common.ServiceGrpcNamespace_+common.ServiceUser),
service.Context(ctx),
service.Tag(common.ServiceTagIdm),
service.Description("Users persistence layer"),
service.Dependency(common.ServiceGrpcNamespace_+common.ServiceRole, []string{}),
service.Migrations([]*service.Migration{
{
TargetVersion: service.FirstRun(),
Up: InitDefaults,
},
}),
service.WithStorage(user.NewDAO, "idm_user"),
service.WithMicro(func(m micro.Service) error {
idm.RegisterUserServiceHandler(m.Options().Server, new(Handler))
// Register a cleaner for removing a workspace when there are no more ACLs on it.
dao := servicecontext.GetDAO(m.Options().Context).(user.DAO)
cleaner := &RolesCleaner{Dao: dao}
if err := m.Options().Server.Subscribe(m.Options().Server.NewSubscriber(common.TopicIdmEvent, cleaner)); err != nil {
return err
}
return nil
}),
)
})
}
func InitDefaults(ctx context.Context) error {
var login, pwd string
dao := servicecontext.GetDAO(ctx).(user.DAO)
if os.Getenv(ENV_PYDIO_ADMIN_USER_LOGIN) != "" && os.Getenv(ENV_PYDIO_ADMIN_USER_PASSWORD) != "" {
login = os.Getenv(ENV_PYDIO_ADMIN_USER_LOGIN)
pwd = os.Getenv(ENV_PYDIO_ADMIN_USER_PASSWORD)
}
if rootConfig := config.Get("defaults", "root").String(); rootConfig != "" {
sDec, _ := base64.StdEncoding.DecodeString(rootConfig)
parts := strings.Split(string(sDec), "||||")
login = parts[0]
pwd = parts[1]
// Now remove from configs
config.Del("defaults", "root")
config.Save("cli", "First Run / Creating default root user")
}
if login != "" && pwd != "" {
log.Logger(ctx).Info("Initialization: creating admin user: " + login)
// Check if user exists
newUser, err := CreateIfNotExists(ctx, dao, &idm.User{
Login: login,
Password: pwd,
Attributes: map[string]string{"profile": common.PydioProfileAdmin},
})
if err != nil {
return err
} else if newUser != nil {
builder := service2.NewResourcePoliciesBuilder()
builder = builder.WithProfileRead(common.PydioProfileStandard)
builder = builder.WithUserWrite(login)
builder = builder.WithProfileWrite(common.PydioProfileAdmin)
if err2 := dao.AddPolicies(false, newUser.Uuid, builder.Policies()); err2 != nil {
return err2
}
// Create user role
service.Retry(ctx, func() error {
roleClient := idm.NewRoleServiceClient(common.ServiceGrpcNamespace_+common.ServiceRole, defaults.NewClient())
_, e := roleClient.CreateRole(ctx, &idm.CreateRoleRequest{Role: &idm.Role{
Uuid: newUser.Uuid,
Label: newUser.Login + " role",
UserRole: true,
Policies: builder.Policies(),
}})
return e
}, 8*time.Second, 50*time.Second)
}
}
log.Logger(ctx).Info("Initialization: creating s3 anonymous user")
newAnon, err := CreateIfNotExists(ctx, dao, &idm.User{
Login: common.PydioS3AnonUsername,
Password: common.PydioS3AnonUsername,
Attributes: map[string]string{"profile": common.PydioProfileAnon},
})
if err != nil {
return err
}
if newAnon != nil {
builder := service2.NewResourcePoliciesBuilder()
builder = builder.WithUserRead(common.PydioS3AnonUsername)
builder = builder.WithProfileRead(common.PydioProfileAdmin)
builder = builder.WithProfileWrite(common.PydioProfileAdmin)
if err2 := dao.AddPolicies(false, newAnon.Uuid, builder.Policies()); err2 != nil {
return err2
}
// Create user role
service.Retry(ctx, func() error {
roleClient := idm.NewRoleServiceClient(common.ServiceGrpcNamespace_+common.ServiceRole, defaults.NewClient())
_, e := roleClient.CreateRole(ctx, &idm.CreateRoleRequest{Role: &idm.Role{
Uuid: newAnon.Uuid,
Label: newAnon.Login + " role",
UserRole: true,
Policies: builder.Policies(),
}})
return e
}, 8*time.Second, 50*time.Second)
}
return nil
}
// CreateIfNotExists creates a user if DAO.Bind() call returns a 404 error.
func CreateIfNotExists(ctx context.Context, dao user.DAO, user *idm.User) (*idm.User, error) {
if _, err := dao.Bind(user.Login, user.Password); err != nil && errors.Parse(err.Error()).Code != 404 {
return nil, err
} else if err == nil {
log.Logger(ctx).Info("Skipping user " + user.Login + ", already exists")
return nil, nil
}
// User is not created yet, add it now
out, _, err := dao.Add(user)
if err != nil {
return nil, err
}
return out.(*idm.User), nil
}