Skip to content

Commit babf3ac

Browse files
committed
Change logic in handler,db,events and align rhsso_test to new function headers
1 parent b75034e commit babf3ac

File tree

4 files changed

+53
-29
lines changed

4 files changed

+53
-29
lines changed

Diff for: internal/events/event.go

+11-9
Original file line numberDiff line numberDiff line change
@@ -355,12 +355,12 @@ func (e Events) prepareEventsTable(ctx context.Context, tx *gorm.DB, clusterID *
355355
//host bound to a cluster or registered to a bound infra-env) check the access permission
356356
//relative to the cluster ownership
357357
if clusterBoundEvents() {
358-
tx = tx.Model(&common.Event{}).Select("events.*, clusters.user_name, clusters.org_id").
358+
tx = tx.Model(&common.Event{}).Select("events.*, clusters.user_name, clusters.org_id, clusters.openshift_cluster_id").
359359
Joins("INNER JOIN clusters ON clusters.id = events.cluster_id")
360360

361361
// if deleted hosts flag is true, we need to add 'deleted_at' to know whether events are related to a deleted host
362362
if swag.BoolValue(deletedHosts) {
363-
tx = tx.Select("events.*, clusters.user_name, clusters.org_id, hosts.deleted_at").
363+
tx = tx.Select("events.*, clusters.user_name, clusters.org_id, clusters.openshift_cluster_id, hosts.deleted_at").
364364
Joins("LEFT JOIN hosts ON hosts.id = events.host_id")
365365
}
366366
return tx
@@ -369,14 +369,16 @@ func (e Events) prepareEventsTable(ctx context.Context, tx *gorm.DB, clusterID *
369369
//for unbound events that are searched with infra-env id (whether events on hosts or the
370370
//infra-env level itself) check the access permission relative to the infra-env ownership
371371
if nonBoundEvents() {
372-
return tx.Model(&common.Event{}).Select("events.*, infra_envs.user_name, infra_envs.org_id").
372+
return tx.Model(&common.Event{}).Select("events.*, infra_envs.user_name, infra_envs.org_id, clusters.openshift_cluster_id").
373+
Joins("LEFT JOIN clusters ON clusters.id = events.cluster_id").
373374
Joins("INNER JOIN infra_envs ON infra_envs.id = events.infra_env_id")
374375
}
375376

376377
// Events must be linked to the infra_envs table and then to the hosts table
377378
// The hosts table does not hold an org_id, so permissions related fields must be supplied by the infra_env
378379
if hostOnlyEvents() {
379-
return tx.Model(&common.Event{}).Select("events.*, infra_envs.user_name, infra_envs.org_id").
380+
return tx.Model(&common.Event{}).Select("events.*, infra_envs.user_name, infra_envs.org_id, clusters.openshift_cluster_id").
381+
Joins("LEFT JOIN clusters ON clusters.id = events.cluster_id").
380382
Joins("INNER JOIN infra_envs ON infra_envs.id = events.infra_env_id").
381383
Joins("INNER JOIN hosts ON hosts.id = events.host_id"). // This join is here to ensure that only events for a host that exists are fetched
382384
Where("hosts.deleted_at IS NULL") // Only interested in active hosts
@@ -427,11 +429,6 @@ func (e Events) queryEvents(ctx context.Context, params *common.V2GetEventsParam
427429

428430
events := []*common.Event{}
429431

430-
// add authorization check to query
431-
if e.authz != nil {
432-
tx, _ = e.authz.OwnedBy(ctx, tx, auth.EventsResource)
433-
}
434-
435432
tx = e.prepareEventsTable(ctx, tx, params.ClusterID, params.HostIds, params.InfraEnvID, params.Severities, params.Message, params.DeletedHosts)
436433
if tx == nil {
437434
return make([]*common.Event, 0), &common.EventSeverityCount{}, swag.Int64(0), nil
@@ -470,6 +467,11 @@ func (e Events) queryEvents(ctx context.Context, params *common.V2GetEventsParam
470467
return make([]*common.Event, 0), eventSeverityCount, &eventCount, nil
471468
}
472469

470+
// if we need to apply authorization check then repackage tx as a subquery
471+
// this is to ensure that user_name and org_id are unambiguous
472+
if e.authz != nil {
473+
tx, _ = e.authz.OwnedBy(ctx, cleanQuery.Table("(?) as s", tx), auth.EventsResource)
474+
}
473475
err = tx.Offset(int(*params.Offset)).Limit(int(*params.Limit)).Find(&events).Error
474476
if err != nil {
475477
return nil, nil, nil, err

Diff for: pkg/auth/rhsso_authz_handler.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,19 @@ func (a *AuthzHandler) OwnedBy(ctx context.Context, db *gorm.DB, resource Resour
8080
if err != nil {
8181
return nil, err
8282
}
83-
if resource != ClusterResource {
84-
return db.Where("cluster_id IN ?", allowedClusterID), nil
83+
84+
query := ""
85+
switch resource {
86+
case InfraEnvResource:
87+
query = "user_name = ? OR cluster_id IN (?) OR openshift_cluster_id in (?)"
88+
db = db.Joins("LEFT JOIN clusters on infra_envs.cluster_id = clusters.id")
89+
case EventsResource:
90+
query = "user_name = ? OR cluster_id IN (?) OR openshift_cluster_id in (?)"
91+
default:
92+
query = "user_name = ? OR id IN (?) OR openshift_cluster_id in (?)"
8593
}
8694

87-
return db.Where("id IN ? OR openshift_cluster_id IN ?", allowedClusterID, allowedClusterUuids), nil
95+
return db.Where(query, ocm.UserNameFromContext(ctx), allowedClusterID, allowedClusterUuids), nil
8896
}
8997
if a.isTenancyEnabled() {
9098
return db.Where("org_id = ?", ocm.OrgIDFromContext(ctx)), nil

Diff for: pkg/auth/rhsso_authz_handler_test.go

+26-13
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ var _ = Describe("OwnedBy", func() {
157157
ctx = context.WithValue(ctx, restapi.AuthKey, payload)
158158

159159
var records []common.Cluster
160-
results := handler.OwnedBy(ctx, db).Find(&records)
160+
results, _ := handler.OwnedBy(ctx, db, "")
161+
results.Find(&records)
161162
Expect(results.RowsAffected, 4)
162163
})
163164
It("admin user - non-empty query", func() {
@@ -166,7 +167,8 @@ var _ = Describe("OwnedBy", func() {
166167
ctx = context.WithValue(ctx, restapi.AuthKey, payload)
167168

168169
var records []common.Cluster
169-
results := handler.OwnedBy(ctx, db).Where("name = ?", "A").Find(&records)
170+
results, _ := handler.OwnedBy(ctx, db, "")
171+
results.Where("name = ?", "A").Find(&records)
170172
Expect(results.RowsAffected, 3)
171173
Expect(AllRecordsHasName(records, "A")).To(BeTrue())
172174
})
@@ -176,7 +178,8 @@ var _ = Describe("OwnedBy", func() {
176178
ctx = context.WithValue(ctx, restapi.AuthKey, payload)
177179

178180
var records []common.Cluster
179-
results := handler.OwnedByUser(ctx, db, "user1").Find(&records)
181+
results, _ := handler.OwnedByUser(ctx, db, "", "user1")
182+
results.Find(&records)
180183
Expect(results.RowsAffected, 2)
181184
Expect(AllRecordsHasUserName(records, "user1")).To(BeTrue())
182185
})
@@ -187,7 +190,8 @@ var _ = Describe("OwnedBy", func() {
187190
ctx = context.WithValue(ctx, restapi.AuthKey, payload)
188191

189192
var records []common.Cluster
190-
results := handler.OwnedBy(ctx, db).Find(&records)
193+
results, _ := handler.OwnedBy(ctx, db, "")
194+
results.Find(&records)
191195
Expect(results.RowsAffected, 2)
192196
Expect(AllRecordsHasUserName(records, "user1")).To(BeTrue())
193197
})
@@ -197,7 +201,8 @@ var _ = Describe("OwnedBy", func() {
197201
ctx = context.WithValue(ctx, restapi.AuthKey, payload)
198202

199203
var records []common.Cluster
200-
results := handler.OwnedBy(ctx, db).Where("name = ?", "A").Find(&records)
204+
results, _ := handler.OwnedBy(ctx, db, "")
205+
results.Where("name = ?", "A").Find(&records)
201206
Expect(results.RowsAffected, 2)
202207
Expect(AllRecordsHasName(records, "A")).To(BeTrue())
203208
Expect(AllRecordsHasUserName(records, "user1")).To(BeTrue())
@@ -208,7 +213,8 @@ var _ = Describe("OwnedBy", func() {
208213
ctx = context.WithValue(ctx, restapi.AuthKey, payload)
209214

210215
var records []common.Cluster
211-
results := handler.OwnedByUser(ctx, db, "user1").Find(&records)
216+
results, _ := handler.OwnedByUser(ctx, db, "", "user1")
217+
results.Find(&records)
212218
Expect(results.RowsAffected, 2)
213219
Expect(AllRecordsHasUserName(records, "user1")).To(BeTrue())
214220
})
@@ -218,7 +224,8 @@ var _ = Describe("OwnedBy", func() {
218224
ctx = context.WithValue(ctx, restapi.AuthKey, payload)
219225

220226
var records []common.Cluster
221-
results := handler.OwnedByUser(ctx, db, "user2").Find(&records)
227+
results, _ := handler.OwnedByUser(ctx, db, "", "user2")
228+
results.Find(&records)
222229
Expect(results.RowsAffected, 0)
223230
})
224231
})
@@ -233,7 +240,8 @@ var _ = Describe("OwnedBy", func() {
233240
ctx = context.WithValue(ctx, restapi.AuthKey, payload)
234241

235242
var records []common.Cluster
236-
results := handler.OwnedBy(ctx, db).Find(&records)
243+
results, _ := handler.OwnedBy(ctx, db, "")
244+
results.Find(&records)
237245
Expect(results.RowsAffected, 4)
238246
})
239247
It("admin user - non-empty query", func() {
@@ -242,7 +250,8 @@ var _ = Describe("OwnedBy", func() {
242250
ctx = context.WithValue(ctx, restapi.AuthKey, payload)
243251

244252
var records []common.Cluster
245-
results := handler.OwnedBy(ctx, db).Where("name = ?", "A").Find(&records)
253+
results, _ := handler.OwnedBy(ctx, db, "")
254+
results.Where("name = ?", "A").Find(&records)
246255
Expect(results.RowsAffected, 3)
247256
Expect(AllRecordsHasName(records, "A")).To(BeTrue())
248257
})
@@ -252,7 +261,8 @@ var _ = Describe("OwnedBy", func() {
252261
ctx = context.WithValue(ctx, restapi.AuthKey, payload)
253262

254263
var records []common.Cluster
255-
results := handler.OwnedBy(ctx, db).Find(&records)
264+
results, _ := handler.OwnedBy(ctx, db, "")
265+
results.Find(&records)
256266
Expect(results.RowsAffected, 2)
257267
Expect(AllRecordsHasOrgId(records, "org1")).To(BeTrue())
258268
})
@@ -262,7 +272,8 @@ var _ = Describe("OwnedBy", func() {
262272
ctx = context.WithValue(ctx, restapi.AuthKey, payload)
263273

264274
var records []common.Cluster
265-
results := handler.OwnedBy(ctx, db).Where("name = ?", "A").Find(&records)
275+
results, _ := handler.OwnedBy(ctx, db, "")
276+
results.Find(&records).Where("name = ?", "A").Find(&records)
266277
Expect(results.RowsAffected, 1)
267278
Expect(AllRecordsHasName(records, "A")).To(BeTrue())
268279
Expect(AllRecordsHasOrgId(records, "org1")).To(BeTrue())
@@ -274,7 +285,8 @@ var _ = Describe("OwnedBy", func() {
274285
ctx = context.WithValue(ctx, restapi.AuthKey, payload)
275286

276287
var records []common.Cluster
277-
results := handler.OwnedByUser(ctx, db, "user1").Find(&records)
288+
results, _ := handler.OwnedByUser(ctx, db, "", "user1")
289+
results.Find(&records)
278290
Expect(results.RowsAffected, 1)
279291
Expect(AllRecordsHasUserName(records, "user1")).To(BeTrue())
280292
})
@@ -285,7 +297,8 @@ var _ = Describe("OwnedBy", func() {
285297
ctx = context.WithValue(ctx, restapi.AuthKey, payload)
286298

287299
var records []common.Cluster
288-
results := handler.OwnedByUser(ctx, db, "user2").Find(&records)
300+
results, _ := handler.OwnedByUser(ctx, db, "", "user2")
301+
results.Find(&records)
289302
Expect(results.RowsAffected, 0)
290303
})
291304
})

Diff for: pkg/ocm/mock_authorization.go

+5-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)