• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / iot-manager / 1314235538

01 Jun 2024 04:16AM UTC coverage: 87.602%. Remained the same
1314235538

Pull #286

gitlab-ci

web-flow
chore: bump the golang-dependencies group with 7 updates

Bumps the golang-dependencies group with 7 updates:

| Package | From | To |
| --- | --- | --- |
| [github.com/aws/aws-sdk-go-v2](https://github.com/aws/aws-sdk-go-v2) | `1.26.1` | `1.27.0` |
| [github.com/aws/aws-sdk-go-v2/config](https://github.com/aws/aws-sdk-go-v2) | `1.27.11` | `1.27.16` |
| [github.com/aws/aws-sdk-go-v2/credentials](https://github.com/aws/aws-sdk-go-v2) | `1.17.11` | `1.17.16` |
| [github.com/aws/aws-sdk-go-v2/service/iot](https://github.com/aws/aws-sdk-go-v2) | `1.53.3` | `1.53.7` |
| [github.com/aws/aws-sdk-go-v2/service/iotdataplane](https://github.com/aws/aws-sdk-go-v2) | `1.22.4` | `1.22.8` |
| [github.com/gin-gonic/gin](https://github.com/gin-gonic/gin) | `1.9.1` | `1.10.0` |
| [golang.org/x/sys](https://github.com/golang/sys) | `0.19.0` | `0.20.0` |


Updates `github.com/aws/aws-sdk-go-v2` from 1.26.1 to 1.27.0
- [Release notes](https://github.com/aws/aws-sdk-go-v2/releases)
- [Commits](https://github.com/aws/aws-sdk-go-v2/compare/v1.26.1...v1.27.0)

Updates `github.com/aws/aws-sdk-go-v2/config` from 1.27.11 to 1.27.16
- [Release notes](https://github.com/aws/aws-sdk-go-v2/releases)
- [Commits](https://github.com/aws/aws-sdk-go-v2/compare/config/v1.27.11...config/v1.27.16)

Updates `github.com/aws/aws-sdk-go-v2/credentials` from 1.17.11 to 1.17.16
- [Release notes](https://github.com/aws/aws-sdk-go-v2/releases)
- [Commits](https://github.com/aws/aws-sdk-go-v2/compare/config/v1.17.11...credentials/v1.17.16)

Updates `github.com/aws/aws-sdk-go-v2/service/iot` from 1.53.3 to 1.53.7
- [Release notes](https://github.com/aws/aws-sdk-go-v2/releases)
- [Commits](https://github.com/aws/aws-sdk-go-v2/compare/service/iot/v1.53.3...service/iot/v1.53.7)

Updates `github.com/aws/aws-sdk-go-v2/service/iotdataplane` from 1.22.4 to 1.22.8
- [Release notes](https://github.com/aws/aws-sdk-go-v2/releases)
- [Commits](https://github.com/aws/aws-sdk-go-v2/compare/service/pi/v1.22.4...se... (continued)
Pull Request #286: chore: bump the golang-dependencies group with 7 updates

3229 of 3686 relevant lines covered (87.6%)

11.46 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.86
/api/http/internal.go
1
// Copyright 2023 Northern.tech AS
2
//
3
//    Licensed under the Apache License, Version 2.0 (the "License");
4
//    you may not use this file except in compliance with the License.
5
//    You may obtain a copy of the License at
6
//
7
//        http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//    Unless required by applicable law or agreed to in writing, software
10
//    distributed under the License is distributed on an "AS IS" BASIS,
11
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//    See the License for the specific language governing permissions and
13
//    limitations under the License.
14

15
package http
16

17
import (
18
        "encoding/json"
19
        "net/http"
20
        "strings"
21

22
        "github.com/mendersoftware/iot-manager/app"
23
        "github.com/mendersoftware/iot-manager/model"
24

25
        "github.com/gin-gonic/gin"
26
        "github.com/mendersoftware/go-lib-micro/identity"
27
        "github.com/mendersoftware/go-lib-micro/rest.utils"
28
        "github.com/pkg/errors"
29
)
30

31
const (
32
        ParamTenantID = "tenant_id"
33
        ParamDeviceID = "device_id"
34
)
35

36
type InternalHandler APIHandler
37

38
type internalDevice model.DeviceEvent
39

40
func (dev *internalDevice) UnmarshalJSON(b []byte) error {
7✔
41
        type deviceAlias struct {
7✔
42
                // device_id kept for backward compatibility
7✔
43
                ID string `json:"device_id"`
7✔
44
                model.DeviceEvent
7✔
45
        }
7✔
46
        var aDev deviceAlias
7✔
47
        err := json.Unmarshal(b, &aDev)
7✔
48
        if err != nil {
8✔
49
                return err
1✔
50
        }
1✔
51
        if aDev.ID != "" {
7✔
52
                aDev.DeviceEvent.ID = aDev.ID
1✔
53
        }
1✔
54
        *dev = internalDevice(aDev.DeviceEvent)
6✔
55
        return nil
6✔
56
}
57

58
// POST /tenants/:tenant_id/devices
59
// code: 204 - device provisioned to iothub
60
//
61
//        500 - internal server error
62
func (h *InternalHandler) ProvisionDevice(c *gin.Context) {
8✔
63
        tenantID := c.Param(ParamTenantID)
8✔
64
        var device internalDevice
8✔
65
        if err := c.ShouldBindJSON(&device); err != nil {
10✔
66
                rest.RenderError(c,
2✔
67
                        http.StatusBadRequest,
2✔
68
                        errors.Wrap(err, "malformed request body"))
2✔
69
                return
2✔
70
        }
2✔
71
        if device.ID == "" {
7✔
72
                rest.RenderError(c, http.StatusBadRequest, errors.New("missing device ID"))
1✔
73
                return
1✔
74
        }
1✔
75

76
        ctx := identity.WithContext(c.Request.Context(), &identity.Identity{
5✔
77
                Subject: device.ID,
5✔
78
                Tenant:  tenantID,
5✔
79
        })
5✔
80
        err := h.app.ProvisionDevice(ctx, model.DeviceEvent(device))
5✔
81
        switch cause := errors.Cause(err); cause {
5✔
82
        case nil, app.ErrNoCredentials:
3✔
83
                c.Status(http.StatusAccepted)
3✔
84
        case app.ErrDeviceAlreadyExists:
1✔
85
                rest.RenderError(c, http.StatusConflict, cause)
1✔
86
        default:
1✔
87
                rest.RenderError(c, http.StatusInternalServerError, err)
1✔
88
        }
89
}
90

91
func (h *InternalHandler) DecommissionDevice(c *gin.Context) {
5✔
92
        deviceID := c.Param(ParamDeviceID)
5✔
93
        tenantID := c.Param(ParamTenantID)
5✔
94

5✔
95
        ctx := identity.WithContext(c.Request.Context(), &identity.Identity{
5✔
96
                Subject: deviceID,
5✔
97
                Tenant:  tenantID,
5✔
98
        })
5✔
99
        err := h.app.DecommissionDevice(ctx, deviceID)
5✔
100
        switch errors.Cause(err) {
5✔
101
        case nil, app.ErrNoCredentials:
3✔
102
                c.Status(http.StatusAccepted)
3✔
103
        case app.ErrDeviceNotFound:
1✔
104
                rest.RenderError(c, http.StatusNotFound, err)
1✔
105
        default:
1✔
106
                rest.RenderError(c, http.StatusInternalServerError, err)
1✔
107
        }
108
}
109

110
const (
111
        maxBulkItems = 100
112
)
113

114
// PUT /tenants/:tenant_id/devices/status/{status}
115
func (h *InternalHandler) BulkSetDeviceStatus(c *gin.Context) {
7✔
116
        var schema []struct {
7✔
117
                DeviceID string `json:"id"`
7✔
118
        }
7✔
119
        status := model.Status(c.Param("status"))
7✔
120
        if err := status.Validate(); err != nil {
8✔
121
                rest.RenderError(c, http.StatusBadRequest, err)
1✔
122
                return
1✔
123
        }
1✔
124
        if err := c.ShouldBindJSON(&schema); err != nil {
7✔
125
                rest.RenderError(c,
1✔
126
                        http.StatusBadRequest,
1✔
127
                        errors.Wrap(err, "invalid request body"),
1✔
128
                )
1✔
129
                return
1✔
130
        } else if len(schema) > maxBulkItems {
7✔
131
                rest.RenderError(c,
1✔
132
                        http.StatusBadRequest,
1✔
133
                        errors.New("too many bulk items: max 100 items per request"),
1✔
134
                )
1✔
135
                return
1✔
136
        }
1✔
137
        ctx := identity.WithContext(
4✔
138
                c.Request.Context(),
4✔
139
                &identity.Identity{
4✔
140
                        Tenant: c.Param("tenant_id"),
4✔
141
                },
4✔
142
        )
4✔
143
        for _, item := range schema {
11✔
144
                _ = h.app.SetDeviceStatus(ctx, item.DeviceID, status)
7✔
145
        }
7✔
146
        c.Status(http.StatusAccepted)
4✔
147
}
148

149
// POST /tenants/:tenant_id/auth
150
func (h *InternalHandler) PreauthorizeHandler(c *gin.Context) {
3✔
151
        tenantID, okTenant := c.Params.Get("tenant_id")
3✔
152
        if !(okTenant) {
3✔
153
                (*APIHandler)(h).NoRoute(c)
×
154
                return
×
155
        }
×
156
        var req model.PreauthRequest
3✔
157
        if err := c.BindJSON(&req); err != nil {
3✔
158
                _ = c.AbortWithError(http.StatusBadRequest, err)
×
159
                return
×
160
        }
×
161
        sepIdx := strings.Index(req.DeviceID, " ")
3✔
162
        if sepIdx < 0 {
3✔
163
                _ = c.AbortWithError(http.StatusBadRequest, errors.New("invalid parameter `external_id`"))
×
164
                return
×
165
        }
×
166
        // DeviceID is formatted accordingly: {provider:[iot-hub]}
167
        provider := req.DeviceID[:sepIdx]
3✔
168
        req.DeviceID = req.DeviceID[sepIdx+1:]
3✔
169

3✔
170
        ctx := identity.WithContext(c.Request.Context(), &identity.Identity{
3✔
171
                IsDevice: true,
3✔
172
                Subject:  req.DeviceID,
3✔
173
                Tenant:   tenantID,
3✔
174
        })
3✔
175
        var err error
3✔
176
        switch provider {
3✔
177
        case string(model.ProviderIoTHub):
2✔
178
                err = h.app.VerifyDeviceTwin(ctx, req)
2✔
179
        default:
1✔
180
                _ = c.AbortWithError(http.StatusBadRequest, errors.New("external provider not supported"))
1✔
181
                return
1✔
182
        }
183
        if err != nil {
3✔
184
                _ = c.Error(err)
1✔
185
                c.Status(http.StatusUnauthorized)
1✔
186
                return
1✔
187
        }
1✔
188
        c.Status(http.StatusNoContent)
1✔
189
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc