forked from canonical/iot-identity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
device.go
205 lines (180 loc) · 5.83 KB
/
device.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
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* This file is part of the IoT Identity Service
* Copyright 2019 Canonical Ltd.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License version 3, as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
* SATISFACTORY QUALITY, 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 web
import (
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/everactive/iot-identity/service"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"github.com/snapcore/snapd/asserts"
)
type devicesField struct {
ID string
Serial string
}
// DeviceList fetches device registrations
func (wb IdentityService) DeviceList(context *gin.Context) {
devices, err := wb.Identity.DeviceList(context.Param("orgid"))
if err != nil {
log.Println("Error fetching devices:", err)
formatStandardResponse("DeviceList", err.Error(), context.Writer)
return
}
devicesFields := []devicesField{}
for _, d := range devices {
devicesFields = append(devicesFields, devicesField{
ID: d.ID,
Serial: d.Device.SerialNumber,
})
}
wb.logger.WithField("devices", devicesFields)
formatDevicesResponse(devices, context.Writer)
}
// DeviceGet fetches a device registration
func (wb IdentityService) DeviceGet(context *gin.Context) {
en, err := wb.Identity.DeviceGet(context.Param("orgid"), context.Param("device"))
if err != nil {
log.Printf("Error fetching device `%s`: %v\n", context.Param("device"), err)
formatStandardResponse("DeviceGet", err.Error(), context.Writer)
return
}
formatEnrollResponse(*en, context.Writer)
}
// DeviceUpdate updates a device registration
func (wb IdentityService) DeviceUpdate(context *gin.Context) {
orgid := context.Param("orgid")
device := context.Param("device")
request := &service.DeviceUpdateRequest{}
err := decodeRequest(context.Writer, context.Request, request)
if err != nil {
return
}
err = wb.Identity.DeviceUpdate(orgid, device, request)
if err != nil {
log.Printf("Error updating device `%s`: %v\n", device, err)
formatStandardResponse("DeviceUpdate", err.Error(), context.Writer)
return
}
formatStandardResponse("", "", context.Writer)
}
// DeleteDevice unregisters a new device with the identity service
func (wb IdentityService) DeleteDevice(context *gin.Context) {
id, err := wb.Identity.DeleteDevice(context.Param("deviceid"))
if err != nil {
log.Println("Error deleting device:", err)
formatStandardResponse("DeleteDevice", err.Error(), context.Writer)
return
}
formatRegisterResponse(id, context.Writer)
}
// RegisterDevice registers a new device with the identity service
func (wb IdentityService) RegisterDevice(context *gin.Context) {
// Decode the JSON body
request := &service.RegisterDeviceRequest{}
err := decodeRequest(context.Writer, context.Request, request)
if err != nil {
return
}
id, err := wb.Identity.RegisterDevice(request)
if err != nil {
log.Println("Error registering device:", err)
formatStandardResponse("RegDevice", err.Error(), context.Writer)
return
}
formatRegisterResponse(id, context.Writer)
}
// EnrollDevice connects an IoT device with the identity service
func (wb IdentityService) EnrollDevice(c *gin.Context) {
// Decode the assertions from the request
assertion1, assertion2, err := decodeEnrollRequest(c.Request)
if err != nil {
formatStandardResponse("EnrollDevice", err.Error(), c.Writer)
return
}
if assertion1 == nil || assertion2 == nil {
formatStandardResponse("EnrollDevice", "A model and serial assertion is required", c.Writer)
return
}
req := service.EnrollDeviceRequest{}
if assertion1.Type().Name == asserts.ModelType.Name && assertion2.Type().Name == asserts.SerialType.Name {
req.Model = assertion1
req.Serial = assertion2
} else if assertion1.Type().Name == asserts.SerialType.Name && assertion2.Type().Name == asserts.ModelType.Name {
req.Model = assertion2
req.Serial = assertion1
}
if req.Model == nil || req.Serial == nil {
log.Println("A model and serial assertion must be provided")
}
en, err := wb.Identity.EnrollDevice(&req)
if err != nil {
log.Println("Error enrolling device:", err)
formatStandardResponse("EnrollDevice", err.Error(), c.Writer)
return
}
formatEnrollResponse(*en, c.Writer)
}
func decodeEnrollRequest(r *http.Request) (asserts.Assertion, asserts.Assertion, error) {
// Use snapd assertion module to decode the assertions in the request stream
dec := asserts.NewDecoder(r.Body)
assertion1, err := dec.Decode()
if err == io.EOF {
return nil, nil, fmt.Errorf("no data supplied")
}
if err != nil {
return nil, nil, err
}
// Decode the second assertion
assertion2, err := dec.Decode()
if err != nil && err != io.EOF {
return nil, nil, err
}
// Stream must be ended now
_, err = dec.Decode()
if err != io.EOF {
if err == nil {
return nil, nil, fmt.Errorf("unexpected assertion in the request stream")
}
return nil, nil, err
}
return assertion1, assertion2, nil
}
func decodeRequest(w http.ResponseWriter, r *http.Request, i interface{}) error {
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
Logger.Error(err)
}
}(r.Body)
// Decode the JSON body
err := json.NewDecoder(r.Body).Decode(i)
switch {
// Check we have some data
case err == io.EOF:
formatStandardResponse("NoData", "No data supplied.", w)
log.Println("No data supplied.")
// Check for parsing errors
case err != nil:
formatStandardResponse("BadData", err.Error(), w)
log.Println(err)
}
return err
}