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

mendersoftware / deviceauth / 826658230

pending completion
826658230

Pull #638

gitlab-ci

Peter Grzybowski
chore: moving to single db
Pull Request #638: chore: moving to single db

334 of 405 new or added lines in 5 files covered. (82.47%)

38 existing lines in 3 files now uncovered.

4669 of 5588 relevant lines covered (83.55%)

75.19 hits per line

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

80.58
/store/mongo/datastore_mongo_cleanup.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 mongo
16

17
import (
18
        "context"
19

20
        "github.com/pkg/errors"
21
        "go.mongodb.org/mongo-driver/bson"
22
        "go.mongodb.org/mongo-driver/mongo"
23

24
        "github.com/mendersoftware/deviceauth/model"
25
        "github.com/mendersoftware/deviceauth/store"
26
)
27

28
// Retrieves devices with decommissioning flag set
29
func (db *DataStoreMongo) GetDevicesBeingDecommissioned(tenantId string) ([]model.Device, error) {
4✔
30
        c := db.client.Database(DbName).Collection(DbDevicesColl)
4✔
31

4✔
32
        devices := []model.Device{}
4✔
33

4✔
34
        cursor, err := c.Find(
4✔
35
                context.Background(),
4✔
36
                model.Device{
4✔
37
                        Decommissioning: true,
4✔
38
                        TenantID:        tenantId,
4✔
39
                },
4✔
40
        )
4✔
41
        if err != nil {
4✔
42
                return nil, err
×
43
        }
×
44
        if err = cursor.All(context.Background(), &devices); err != nil {
4✔
45
                if err == mongo.ErrNoDocuments {
×
46
                        return nil, store.ErrDevNotFound
×
47
                }
×
48
                return nil, errors.Wrap(err, "failed to fetch devices")
×
49
        }
50

51
        return devices, nil
4✔
52
}
53

54
// Retrieves Ids of the auth sets owned by devices that are in decommissioning state or not owned by
55
// any device.
56
func (db *DataStoreMongo) GetBrokenAuthSets(tenantId string) ([]string, error) {
18✔
57
        c := db.client.Database(DbName).Collection(DbAuthSetColl)
18✔
58

18✔
59
        deviceIds := []string{}
18✔
60
        brokenAuthSets := []string{}
18✔
61

18✔
62
        ctx := context.Background()
18✔
63

18✔
64
        // get all auth sets; group by device id
18✔
65
        match := bson.D{
18✔
66
                {
18✔
67
                        Key: "$match", Value: bson.D{
18✔
68
                                {Key: dbFieldTenantID, Value: tenantId}},
18✔
69
                },
18✔
70
        }
18✔
71
        group := bson.D{
18✔
72
                {
18✔
73
                        Key: "$group", Value: bson.D{
18✔
74
                                {Key: "_id", Value: "$device_id"}},
18✔
75
                },
18✔
76
        }
18✔
77
        pipeline := []bson.D{
18✔
78
                match,
18✔
79
                group,
18✔
80
        }
18✔
81
        var result []struct {
18✔
82
                DeviceId string `bson:"_id"`
18✔
83
        }
18✔
84

18✔
85
        cursor, err := c.Aggregate(ctx, pipeline)
18✔
86
        if err != nil {
18✔
87
                return nil, err
×
88
        }
×
89
        if err := cursor.All(ctx, &result); err != nil {
18✔
90
                if err == mongo.ErrNoDocuments {
×
91
                        return nil, nil
×
92
                }
×
93
                return nil, err
×
94
        }
95

96
        for _, res := range result {
48✔
97
                deviceIds = append(deviceIds, res.DeviceId)
30✔
98
        }
30✔
99

100
        //check if devices exists
101
        nonexistentDevices, err := db.filterNonExistentDevices(tenantId, deviceIds)
18✔
102
        if err != nil {
18✔
103
                return nil, err
×
104
        }
×
105

106
        // fetch auth sets for non exisitent devices
107
        for _, dev := range nonexistentDevices {
30✔
108

12✔
109
                authSets := []model.AuthSet{}
12✔
110

12✔
111
                cursor, err := c.Find(ctx, bson.M{"device_id": dev, dbFieldTenantID: tenantId})
12✔
112
                if err != nil {
12✔
113
                        return nil, errors.Wrap(err, "failed to fetch authentication sets")
×
114
                }
×
115
                if err = cursor.All(ctx, &authSets); err != nil {
12✔
116
                        if err != mongo.ErrNoDocuments {
×
117
                                return nil, errors.Wrap(err, "failed to fetch authentication sets")
×
118
                        }
×
119
                }
120

121
                for _, authSet := range authSets {
24✔
122
                        brokenAuthSets = append(brokenAuthSets, authSet.Id)
12✔
123
                }
12✔
124
        }
125

126
        return brokenAuthSets, nil
18✔
127
}
128

129
// Deletes devices with decommissioning flag set
130
func (db *DataStoreMongo) DeleteDevicesBeingDecommissioned(tenantId string) error {
4✔
131
        c := db.client.Database(DbName).Collection(DbDevicesColl)
4✔
132

4✔
133
        _, err := c.DeleteMany(
4✔
134
                context.Background(),
4✔
135
                model.Device{
4✔
136
                        Decommissioning: true,
4✔
137
                        TenantID:        tenantId,
4✔
138
                },
4✔
139
        )
4✔
140
        if err != nil {
4✔
141
                return errors.Wrap(err, "failed to remove decommissioned devices")
×
142
        }
×
143

144
        return nil
4✔
145
}
146

147
// Deletes auth sets owned by devices that are in decommissioning state and auth sets not
148
// owned by any device.
149
func (db *DataStoreMongo) DeleteBrokenAuthSets(tenantId string) error {
6✔
150
        database := db.client.Database(DbName)
6✔
151
        collAuthSets := database.Collection(DbAuthSetColl)
6✔
152
        collTokens := database.Collection(DbTokensColl)
6✔
153

6✔
154
        authSets, err := db.GetBrokenAuthSets(tenantId)
6✔
155
        if err != nil {
6✔
156
                return err
×
157
        }
×
158

159
        // delete authsets for non-existent devices
160
        ctx := context.Background()
6✔
161
        for _, as := range authSets {
12✔
162
                _, err := collAuthSets.DeleteOne(ctx, model.AuthSet{Id: as, TenantID: tenantId})
6✔
163
                if err != nil {
6✔
NEW
164
                        return errors.Wrapf(err, "tenant %s, failed to delete authentication sets", tenantId)
×
165
                }
×
166
                // Attempt to delete token (may have already expired).
167
                _, _ = collTokens.DeleteOne(ctx, bson.M{"_id": as, dbFieldTenantID: tenantId})
6✔
168
        }
169

170
        return nil
6✔
171
}
172

173
// Filters list of device ids.
174
// Result is the list of ids of non-existent devices and devices with decommissioning flag set.
175
func (db *DataStoreMongo) filterNonExistentDevices(
176
        tenantId string,
177
        devIds []string,
178
) ([]string, error) {
18✔
179
        c := db.client.Database(DbName).Collection(DbDevicesColl)
18✔
180

18✔
181
        nonexistentDevices := []string{}
18✔
182

18✔
183
        //check if device exists
18✔
184
        for _, devId := range devIds {
48✔
185
                res := model.Device{}
30✔
186
                err := c.FindOne(
30✔
187
                        context.Background(),
30✔
188
                        bson.M{
30✔
189
                                "_id":           devId,
30✔
190
                                dbFieldTenantID: tenantId,
30✔
191
                        },
30✔
192
                ).Decode(&res)
30✔
193

30✔
194
                if err != nil {
38✔
195
                        if err == mongo.ErrNoDocuments {
16✔
196
                                nonexistentDevices = append(nonexistentDevices, devId)
8✔
197
                        } else {
8✔
NEW
198
                                return nil, errors.Wrapf(err, "tenant %s, failed to fetch device", tenantId)
×
199
                        }
×
200
                }
201

202
                if res.Decommissioning {
34✔
203
                        nonexistentDevices = append(nonexistentDevices, devId)
4✔
204
                }
4✔
205
        }
206

207
        return nonexistentDevices, nil
18✔
208
}
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