Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions pkg/tenancy/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ type Ownership struct {
type DeviceLookup struct {
DeviceId primitive.ObjectID
DeviceKey string
CloudKey string
OrganisationId primitive.ObjectID
ProjectId primitive.ObjectID
LegacyOwnerId primitive.ObjectID
Expand Down Expand Up @@ -213,10 +214,12 @@ func (r *DeviceResolver) LoadScopedDevice(ctx context.Context, lookup DeviceLook

func scopedDeviceFilter(lookup DeviceLookup) (bson.M, error) {
if !lookup.DeviceId.IsZero() {
return bson.M{
filter := bson.M{
properties.DeviceId: lookup.DeviceId,
properties.DeviceKey: lookup.DeviceKey,
}, nil
}
addCloudKeyFilter(filter, lookup.CloudKey)
return filter, nil
}

legacyOwnerId := lookup.LegacyOwnerId
Expand All @@ -236,6 +239,7 @@ func scopedDeviceFilter(lookup DeviceLookup) (bson.M, error) {
)
for _, arm := range filter["$or"].([]bson.M) {
arm[properties.DeviceKey] = lookup.DeviceKey
addCloudKeyFilter(arm, lookup.CloudKey)
}
return filter, nil
}
Expand All @@ -248,6 +252,7 @@ func scopedDeviceFilter(lookup DeviceLookup) (bson.M, error) {
)
for _, arm := range filter["$or"].([]bson.M) {
arm[properties.DeviceKey] = lookup.DeviceKey
addCloudKeyFilter(arm, lookup.CloudKey)
}
return filter, nil
}
Expand All @@ -260,12 +265,19 @@ func scopedDeviceFilter(lookup DeviceLookup) (bson.M, error) {
for field, test := range database.MissingCanonicalOrganisation() {
filter[field] = test
}
addCloudKeyFilter(filter, lookup.CloudKey)
return filter, nil
}

return nil, fmt.Errorf("%w for key %q", ErrDeviceScopeRequired, lookup.DeviceKey)
}

func addCloudKeyFilter(filter bson.M, cloudKey string) {
if cloudKey != "" {
filter["analytics.cloudpublickey"] = cloudKey
}
}

// ResolveDeviceByKey loads a device and resolves its ownership in one step.
func (r *DeviceResolver) ResolveDeviceByKey(ctx context.Context, deviceKey string) (models.Device, Ownership, error) {
device, err := r.LoadDevice(ctx, deviceKey)
Expand Down
3 changes: 2 additions & 1 deletion pkg/tenancy/device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ func TestLoadScopedDeviceAllowsSameKeyInDifferentProjects(t *testing.T) {

device, err := resolver.LoadScopedDevice(context.Background(), DeviceLookup{
DeviceKey: "device-1",
CloudKey: "cloud-key-1",
OrganisationId: organisationId,
ProjectId: projectId,
})
Expand All @@ -344,7 +345,7 @@ func TestLoadScopedDeviceAllowsSameKeyInDifferentProjects(t *testing.T) {

filter := mock.FindCalls[0].Filter.(bson.M)
for _, arm := range filter["$or"].([]bson.M) {
if arm[properties.DeviceKey] != "device-1" || arm[properties.DeviceProjectId] != projectId {
if arm[properties.DeviceKey] != "device-1" || arm[properties.DeviceProjectId] != projectId || arm["analytics.cloudpublickey"] != "cloud-key-1" {
t.Fatalf("project-scoped arm = %#v", arm)
}
}
Expand Down
Loading