forked from gravitational/teleport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
199 lines (160 loc) · 5.81 KB
/
service.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
190
191
192
193
194
195
196
197
198
199
/*
* Teleport
* Copyright (C) 2023 Gravitational, Inc.
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
package userloginstate
import (
"context"
"github.com/gravitational/trace"
"github.com/jonboulle/clockwork"
"github.com/sirupsen/logrus"
"google.golang.org/protobuf/types/known/emptypb"
userloginstatev1 "github.com/gravitational/teleport/api/gen/proto/go/teleport/userloginstate/v1"
"github.com/gravitational/teleport/api/types"
conv "github.com/gravitational/teleport/api/types/userloginstate/convert/v1"
"github.com/gravitational/teleport/lib/authz"
"github.com/gravitational/teleport/lib/services"
)
// ServiceConfig is the service config for the Access Lists gRPC service.
type ServiceConfig struct {
// Logger is the logger to use.
Logger logrus.FieldLogger
// Authorizer is the authorizer to use.
Authorizer authz.Authorizer
// UserLoginStates is the user login state service to use.
UserLoginStates services.UserLoginStates
// Clock is the clock.
Clock clockwork.Clock
}
func (c *ServiceConfig) checkAndSetDefaults() error {
if c.Authorizer == nil {
return trace.BadParameter("authorizer is missing")
}
if c.UserLoginStates == nil {
return trace.BadParameter("user login states service is missing")
}
if c.Logger == nil {
c.Logger = logrus.WithField(trace.Component, "user_login_state_crud_service")
}
if c.Clock == nil {
c.Clock = clockwork.NewRealClock()
}
return nil
}
type Service struct {
userloginstatev1.UnimplementedUserLoginStateServiceServer
log logrus.FieldLogger
authorizer authz.Authorizer
userLoginStates services.UserLoginStates
clock clockwork.Clock
}
// NewService creates a new User Login State gRPC service.
func NewService(cfg ServiceConfig) (*Service, error) {
if err := cfg.checkAndSetDefaults(); err != nil {
return nil, trace.Wrap(err)
}
return &Service{
log: cfg.Logger,
authorizer: cfg.Authorizer,
userLoginStates: cfg.UserLoginStates,
clock: cfg.Clock,
}, nil
}
// GetUserLoginStates returns a list of all user login states.
func (s *Service) GetUserLoginStates(ctx context.Context, _ *userloginstatev1.GetUserLoginStatesRequest) (*userloginstatev1.GetUserLoginStatesResponse, error) {
authCtx, err := s.authorizer.Authorize(ctx)
if err != nil {
return nil, trace.Wrap(err)
}
if err := authCtx.CheckAccessToKind(types.KindUserLoginState, types.VerbRead, types.VerbList); err != nil {
return nil, trace.Wrap(err)
}
results, err := s.userLoginStates.GetUserLoginStates(ctx)
if err != nil {
return nil, trace.Wrap(err)
}
ulsList := make([]*userloginstatev1.UserLoginState, len(results))
for i, r := range results {
ulsList[i] = conv.ToProto(r)
}
return &userloginstatev1.GetUserLoginStatesResponse{
UserLoginStates: ulsList,
}, nil
}
// GetUserLoginState returns the specified user login state resource.
func (s *Service) GetUserLoginState(ctx context.Context, req *userloginstatev1.GetUserLoginStateRequest) (*userloginstatev1.UserLoginState, error) {
authCtx, err := s.authorizer.Authorize(ctx)
if err != nil {
return nil, trace.Wrap(err)
}
if err := authCtx.CheckAccessToKind(types.KindUserLoginState, types.VerbRead); err != nil {
return nil, trace.Wrap(err)
}
result, err := s.userLoginStates.GetUserLoginState(ctx, req.GetName())
if err != nil {
return nil, trace.Wrap(err)
}
return conv.ToProto(result), nil
}
// UpsertUserLoginState creates or updates a user login state resource.
func (s *Service) UpsertUserLoginState(ctx context.Context, req *userloginstatev1.UpsertUserLoginStateRequest) (*userloginstatev1.UserLoginState, error) {
authCtx, err := s.authorizer.Authorize(ctx)
if err != nil {
return nil, trace.Wrap(err)
}
if err := authCtx.CheckAccessToKind(types.KindUserLoginState, types.VerbCreate, types.VerbUpdate); err != nil {
return nil, trace.Wrap(err)
}
newUls, err := conv.FromProto(req.GetUserLoginState())
if err != nil {
return nil, trace.Wrap(err)
}
responseUls, err := s.userLoginStates.UpsertUserLoginState(ctx, newUls)
if err != nil {
return nil, trace.Wrap(err)
}
return conv.ToProto(responseUls), nil
}
// DeleteUserLoginState removes the specified user login state resource.
func (s *Service) DeleteUserLoginState(ctx context.Context, req *userloginstatev1.DeleteUserLoginStateRequest) (*emptypb.Empty, error) {
authCtx, err := s.authorizer.Authorize(ctx)
if err != nil {
return nil, trace.Wrap(err)
}
if err := authCtx.CheckAccessToKind(types.KindUserLoginState, types.VerbDelete); err != nil {
return nil, trace.Wrap(err)
}
err = s.userLoginStates.DeleteUserLoginState(ctx, req.GetName())
if err != nil {
return nil, trace.Wrap(err)
}
return &emptypb.Empty{}, nil
}
// DeleteAllUserLoginStates removes all user login states.
func (s *Service) DeleteAllUserLoginStates(ctx context.Context, _ *userloginstatev1.DeleteAllUserLoginStatesRequest) (*emptypb.Empty, error) {
authCtx, err := s.authorizer.Authorize(ctx)
if err != nil {
return nil, trace.Wrap(err)
}
if err := authCtx.CheckAccessToKind(types.KindUserLoginState, types.VerbDelete); err != nil {
return nil, trace.Wrap(err)
}
err = s.userLoginStates.DeleteAllUserLoginStates(ctx)
if err != nil {
return nil, trace.Wrap(err)
}
return &emptypb.Empty{}, nil
}