forked from mendersoftware/deviceadm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
devadm.go
289 lines (242 loc) · 8.08 KB
/
devadm.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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// Copyright 2017 Northern.tech AS
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package devadm
import (
"context"
"time"
"github.com/pkg/errors"
"github.com/mendersoftware/deviceadm/client"
"github.com/mendersoftware/deviceadm/client/deviceauth"
"github.com/mendersoftware/deviceadm/model"
"github.com/mendersoftware/deviceadm/store"
"github.com/mendersoftware/deviceadm/store/mongo"
"github.com/mendersoftware/deviceadm/utils"
"github.com/mendersoftware/deviceadm/utils/clock"
)
var (
ErrAuthNotFound = errors.New("device auth set not found")
ErrNotPreauthorized = errors.New("auth set must be in 'preauthorized' state")
)
// helper for obtaining API clients
type ApiClientGetter func() client.HttpRunner
func simpleApiClientGetter() client.HttpRunner {
return &client.HttpApi{}
}
// this device admission service interface
type App interface {
ListDeviceAuths(ctx context.Context, skip int, limit int, filter store.Filter) ([]model.DeviceAuth, error)
SubmitDeviceAuth(ctx context.Context, d model.DeviceAuth) error
GetDeviceAuth(ctx context.Context, id model.AuthID) (*model.DeviceAuth, error)
AcceptDeviceAuth(ctx context.Context, id model.AuthID) error
RejectDeviceAuth(ctx context.Context, id model.AuthID) error
DeleteDeviceAuth(ctx context.Context, id model.AuthID) error
DeleteDeviceAuthPropagate(ctx context.Context, id model.AuthID, authorizationHeader string) error
AcceptDevicePreAuth(ctx context.Context, id model.AuthID) error
DeleteDeviceData(ctx context.Context, id model.DeviceID) error
ProvisionTenant(ctx context.Context, tenant_id string) error
PreauthorizeDevice(ctx context.Context, authSet model.AuthSet, authorizationHeader string) error
}
var AuthSetConflictError = errors.New("device already exists")
func NewDevAdm(d store.DataStore, authclientconf deviceauth.Config, clock clock.Clock) App {
return &DevAdm{
db: d,
authclientconf: authclientconf,
clientGetter: simpleApiClientGetter,
clock: clock,
}
}
type DevAdm struct {
db store.DataStore
authclientconf deviceauth.Config
clientGetter ApiClientGetter
clock clock.Clock
}
func (d *DevAdm) ListDeviceAuths(ctx context.Context, skip int, limit int, filter store.Filter) ([]model.DeviceAuth, error) {
devs, err := d.db.GetDeviceAuths(ctx, skip, limit, filter)
if err != nil {
return nil, errors.Wrap(err, "failed to fetch devices")
}
return devs, nil
}
func (d *DevAdm) SubmitDeviceAuth(ctx context.Context, dev model.DeviceAuth) error {
now := time.Now()
dev.RequestTime = &now
err := d.db.PutDeviceAuth(ctx, &dev)
if err != nil {
return errors.Wrap(err, "failed to put device")
}
return nil
}
func (d *DevAdm) GetDeviceAuth(ctx context.Context, id model.AuthID) (*model.DeviceAuth, error) {
dev, err := d.db.GetDeviceAuth(ctx, id)
if err != nil {
return nil, err
}
return dev, nil
}
func (d *DevAdm) DeleteDeviceAuth(ctx context.Context, id model.AuthID) error {
err := d.db.DeleteDeviceAuth(ctx, id)
switch err {
case nil:
return nil
case store.ErrNotFound:
return err
default:
return errors.Wrap(err, "failed to delete device")
}
}
func (d *DevAdm) DeleteDeviceAuthPropagate(ctx context.Context, id model.AuthID, authorizationHeader string) error {
err := d.propagateDeviceAuthDeletion(ctx, id, authorizationHeader)
if err != nil {
return err
}
err = d.db.DeleteDeviceAuth(ctx, id)
switch err {
case nil:
return nil
case store.ErrNotFound:
return err
default:
return errors.Wrap(err, "failed to delete device authentication set")
}
}
func (d *DevAdm) AcceptDevicePreAuth(ctx context.Context, id model.AuthID) error {
dev, err := d.db.GetDeviceAuth(ctx, id)
switch err {
case nil:
break
case store.ErrNotFound:
return ErrAuthNotFound
default:
return errors.Wrap(err, "failed to fetch auth set")
}
if dev.Status != model.DevStatusPreauthorized {
return ErrNotPreauthorized
}
err = d.db.UpdateDeviceAuth(ctx, &model.DeviceAuth{
ID: dev.ID,
Status: model.DevStatusAccepted,
})
if err != nil {
return errors.Wrap(err, "failed to update auth set")
}
return nil
}
func (d *DevAdm) propagateDeviceAuthUpdate(ctx context.Context, dev *model.DeviceAuth) error {
// forward device state to auth service
cl := deviceauth.NewClient(d.authclientconf, d.clientGetter())
err := cl.UpdateDevice(ctx, deviceauth.StatusReq{
AuthId: dev.ID.String(),
DeviceId: dev.DeviceId.String(),
Status: dev.Status,
})
if err != nil {
if utils.IsUsageError(err) {
return err
} else {
return errors.Wrap(err, "failed to propagate device status update")
}
}
return nil
}
func (d *DevAdm) propagateDeviceAuthDeletion(
ctx context.Context, authId model.AuthID, authorizationHeader string) error {
devAuth, err := d.db.GetDeviceAuth(ctx, authId)
if err != nil {
if err == store.ErrNotFound {
return err
} else {
return errors.Wrap(err, "failed to get device authentication set")
}
}
if devAuth == nil {
return errors.New("failed to get device authentication set")
}
// forward device authentication set deletion to auth service
cl := deviceauth.NewClient(d.authclientconf, d.clientGetter())
err = cl.DeleteDeviceAuthSet(ctx, devAuth.DeviceId.String(), devAuth.ID.String(), authorizationHeader)
if err != nil {
if utils.IsUsageError(err) {
return err
} else {
return errors.Wrap(err, "failed to propagate device authentication set deletion")
}
}
return nil
}
func (d *DevAdm) updateDeviceAuthStatus(ctx context.Context, id model.AuthID, status string) error {
dev, err := d.db.GetDeviceAuth(ctx, id)
if err != nil {
return err
}
dev.Status = status
err = d.propagateDeviceAuthUpdate(ctx, dev)
if err != nil {
return err
}
// update only status and attributes fields
err = d.db.PutDeviceAuth(ctx, &model.DeviceAuth{
ID: dev.ID,
DeviceId: dev.DeviceId,
Status: dev.Status,
})
if err != nil {
return err
}
return nil
}
func (d *DevAdm) AcceptDeviceAuth(ctx context.Context, id model.AuthID) error {
return d.updateDeviceAuthStatus(ctx, id, model.DevStatusAccepted)
}
func (d *DevAdm) RejectDeviceAuth(ctx context.Context, id model.AuthID) error {
return d.updateDeviceAuthStatus(ctx, id, model.DevStatusRejected)
}
func (d *DevAdm) DeleteDeviceData(ctx context.Context, devid model.DeviceID) error {
return d.db.DeleteDeviceAuthByDevice(ctx, devid)
}
func (d *DevAdm) ProvisionTenant(ctx context.Context, tenant_id string) error {
return d.db.WithAutomigrate().MigrateTenant(ctx, mongo.DbVersion, tenant_id)
}
func (d *DevAdm) PreauthorizeDevice(ctx context.Context, authSet model.AuthSet, authorizationHeader string) error {
deviceAuths, err := d.db.GetDeviceAuthsByIdentityData(ctx, authSet.DeviceId)
if err != nil {
return err
}
if len(deviceAuths) > 0 {
return AuthSetConflictError
}
dev := &model.DeviceAuth{}
dev.DeviceIdentity = authSet.DeviceId
dev.Status = model.DevStatusPreauthorized
dev.Attributes = authSet.Attributes
dev.Key = authSet.Key
now := d.clock.Now()
dev.RequestTime = &now
err = d.db.InsertDeviceAuth(ctx, dev)
if err != nil {
return err
}
return d.propagatePreauthorizeDevice(ctx, dev, authorizationHeader)
}
func (d *DevAdm) propagatePreauthorizeDevice(ctx context.Context, dev *model.DeviceAuth, authorizationHeader string) error {
// forward device preauthorization to auth service
cl := deviceauth.NewClient(d.authclientconf, d.clientGetter())
err := cl.PreauthorizeDevice(ctx, &deviceauth.PreAuthReq{
DeviceId: string(dev.DeviceId),
AuthSetId: string(dev.ID),
IdData: dev.DeviceIdentity,
PubKey: dev.Key,
}, authorizationHeader)
return errors.Wrap(err, "failed to propagate device status update")
}