@@ -1294,26 +1294,31 @@ func (q *querier) GetTailnetTunnelPeerIDs(ctx context.Context, srcID uuid.UUID)
1294
1294
}
1295
1295
1296
1296
func (q * querier ) GetTemplateAppInsights (ctx context.Context , arg database.GetTemplateAppInsightsParams ) ([]database.GetTemplateAppInsightsRow , error ) {
1297
- for _ , templateID := range arg .TemplateIDs {
1298
- template , err := q .db .GetTemplateByID (ctx , templateID )
1299
- if err != nil {
1300
- return nil , err
1301
- }
1297
+ // Used by TemplateAppInsights endpoint
1298
+ // For auditors, check read template_insights, and fall back to update template.
1299
+ if err := q .authorizeContext (ctx , rbac .ActionRead , rbac .ResourceTemplateInsights ); IsNotAuthorizedError (err ) {
1300
+ for _ , templateID := range arg .TemplateIDs {
1301
+ template , err := q .db .GetTemplateByID (ctx , templateID )
1302
+ if err != nil {
1303
+ return nil , err
1304
+ }
1302
1305
1303
- if err := q .authorizeContext (ctx , rbac .ActionUpdate , template ); err != nil {
1304
- return nil , err
1306
+ if err := q .authorizeContext (ctx , rbac .ActionUpdate , template ); err != nil {
1307
+ return nil , err
1308
+ }
1305
1309
}
1306
- }
1307
- if len ( arg . TemplateIDs ) == 0 {
1308
- if err := q . authorizeContext ( ctx , rbac . ActionUpdate , rbac . ResourceTemplate . All ()); err != nil {
1309
- return nil , err
1310
+ if len ( arg . TemplateIDs ) == 0 {
1311
+ if err := q . authorizeContext ( ctx , rbac . ActionUpdate , rbac . ResourceTemplate . All ()); err != nil {
1312
+ return nil , err
1313
+ }
1310
1314
}
1311
1315
}
1312
1316
return q .db .GetTemplateAppInsights (ctx , arg )
1313
1317
}
1314
1318
1315
1319
func (q * querier ) GetTemplateAppInsightsByTemplate (ctx context.Context , arg database.GetTemplateAppInsightsByTemplateParams ) ([]database.GetTemplateAppInsightsByTemplateRow , error ) {
1316
- if err := q .authorizeContext (ctx , rbac .ActionUpdate , rbac .ResourceTemplate .All ()); err != nil {
1320
+ // Only used by prometheus metrics, so we don't strictly need to check update template perms.
1321
+ if err := q .authorizeContext (ctx , rbac .ActionRead , rbac .ResourceTemplateInsights ); err != nil {
1317
1322
return nil , err
1318
1323
}
1319
1324
return q .db .GetTemplateAppInsightsByTemplate (ctx , arg )
@@ -1344,64 +1349,77 @@ func (q *querier) GetTemplateDAUs(ctx context.Context, arg database.GetTemplateD
1344
1349
}
1345
1350
1346
1351
func (q * querier ) GetTemplateInsights (ctx context.Context , arg database.GetTemplateInsightsParams ) (database.GetTemplateInsightsRow , error ) {
1347
- for _ , templateID := range arg .TemplateIDs {
1348
- template , err := q .db .GetTemplateByID (ctx , templateID )
1349
- if err != nil {
1350
- return database.GetTemplateInsightsRow {}, err
1351
- }
1352
+ // Used by TemplateInsights endpoint
1353
+ // For auditors, check read template_insights, and fall back to update template.
1354
+ if err := q .authorizeContext (ctx , rbac .ActionRead , rbac .ResourceTemplateInsights ); IsNotAuthorizedError (err ) {
1355
+ for _ , templateID := range arg .TemplateIDs {
1356
+ template , err := q .db .GetTemplateByID (ctx , templateID )
1357
+ if err != nil {
1358
+ return database.GetTemplateInsightsRow {}, err
1359
+ }
1352
1360
1353
- if err := q .authorizeContext (ctx , rbac .ActionUpdate , template ); err != nil {
1354
- return database.GetTemplateInsightsRow {}, err
1361
+ if err := q .authorizeContext (ctx , rbac .ActionUpdate , template ); err != nil {
1362
+ return database.GetTemplateInsightsRow {}, err
1363
+ }
1355
1364
}
1356
- }
1357
- if len ( arg . TemplateIDs ) == 0 {
1358
- if err := q . authorizeContext ( ctx , rbac . ActionUpdate , rbac . ResourceTemplate . All ()); err != nil {
1359
- return database. GetTemplateInsightsRow {}, err
1365
+ if len ( arg . TemplateIDs ) == 0 {
1366
+ if err := q . authorizeContext ( ctx , rbac . ActionUpdate , rbac . ResourceTemplate . All ()); err != nil {
1367
+ return database. GetTemplateInsightsRow {}, err
1368
+ }
1360
1369
}
1361
1370
}
1362
1371
return q .db .GetTemplateInsights (ctx , arg )
1363
1372
}
1364
1373
1365
1374
func (q * querier ) GetTemplateInsightsByInterval (ctx context.Context , arg database.GetTemplateInsightsByIntervalParams ) ([]database.GetTemplateInsightsByIntervalRow , error ) {
1366
- for _ , templateID := range arg .TemplateIDs {
1367
- template , err := q .db .GetTemplateByID (ctx , templateID )
1368
- if err != nil {
1369
- return nil , err
1370
- }
1375
+ // Used by TemplateInsights endpoint
1376
+ // For auditors, check read template_insights, and fall back to update template.
1377
+ if err := q .authorizeContext (ctx , rbac .ActionRead , rbac .ResourceTemplateInsights ); IsNotAuthorizedError (err ) {
1378
+ for _ , templateID := range arg .TemplateIDs {
1379
+ template , err := q .db .GetTemplateByID (ctx , templateID )
1380
+ if err != nil {
1381
+ return nil , err
1382
+ }
1371
1383
1372
- if err := q .authorizeContext (ctx , rbac .ActionUpdate , template ); err != nil {
1373
- return nil , err
1384
+ if err := q .authorizeContext (ctx , rbac .ActionUpdate , template ); err != nil {
1385
+ return nil , err
1386
+ }
1374
1387
}
1375
- }
1376
- if len ( arg . TemplateIDs ) == 0 {
1377
- if err := q . authorizeContext ( ctx , rbac . ActionUpdate , rbac . ResourceTemplate . All ()); err != nil {
1378
- return nil , err
1388
+ if len ( arg . TemplateIDs ) == 0 {
1389
+ if err := q . authorizeContext ( ctx , rbac . ActionUpdate , rbac . ResourceTemplate . All ()); err != nil {
1390
+ return nil , err
1391
+ }
1379
1392
}
1380
1393
}
1381
1394
return q .db .GetTemplateInsightsByInterval (ctx , arg )
1382
1395
}
1383
1396
1384
1397
func (q * querier ) GetTemplateInsightsByTemplate (ctx context.Context , arg database.GetTemplateInsightsByTemplateParams ) ([]database.GetTemplateInsightsByTemplateRow , error ) {
1385
- if err := q .authorizeContext (ctx , rbac .ActionUpdate , rbac .ResourceTemplate .All ()); err != nil {
1398
+ // Only used by prometheus metrics collector. No need to check update template perms.
1399
+ if err := q .authorizeContext (ctx , rbac .ActionRead , rbac .ResourceTemplateInsights ); err != nil {
1386
1400
return nil , err
1387
1401
}
1388
1402
return q .db .GetTemplateInsightsByTemplate (ctx , arg )
1389
1403
}
1390
1404
1391
1405
func (q * querier ) GetTemplateParameterInsights (ctx context.Context , arg database.GetTemplateParameterInsightsParams ) ([]database.GetTemplateParameterInsightsRow , error ) {
1392
- for _ , templateID := range arg .TemplateIDs {
1393
- template , err := q .db .GetTemplateByID (ctx , templateID )
1394
- if err != nil {
1395
- return nil , err
1396
- }
1406
+ // Used by both insights endpoint and prometheus collector.
1407
+ // For auditors, check read template_insights, and fall back to update template.
1408
+ if err := q .authorizeContext (ctx , rbac .ActionRead , rbac .ResourceTemplateInsights ); IsNotAuthorizedError (err ) {
1409
+ for _ , templateID := range arg .TemplateIDs {
1410
+ template , err := q .db .GetTemplateByID (ctx , templateID )
1411
+ if err != nil {
1412
+ return nil , err
1413
+ }
1397
1414
1398
- if err := q .authorizeContext (ctx , rbac .ActionUpdate , template ); err != nil {
1399
- return nil , err
1415
+ if err := q .authorizeContext (ctx , rbac .ActionUpdate , template ); err != nil {
1416
+ return nil , err
1417
+ }
1400
1418
}
1401
- }
1402
- if len ( arg . TemplateIDs ) == 0 {
1403
- if err := q . authorizeContext ( ctx , rbac . ActionUpdate , rbac . ResourceTemplate . All ()); err != nil {
1404
- return nil , err
1419
+ if len ( arg . TemplateIDs ) == 0 {
1420
+ if err := q . authorizeContext ( ctx , rbac . ActionUpdate , rbac . ResourceTemplate . All ()); err != nil {
1421
+ return nil , err
1422
+ }
1405
1423
}
1406
1424
}
1407
1425
return q .db .GetTemplateParameterInsights (ctx , arg )
@@ -1559,19 +1577,22 @@ func (q *querier) GetUnexpiredLicenses(ctx context.Context) ([]database.License,
1559
1577
}
1560
1578
1561
1579
func (q * querier ) GetUserActivityInsights (ctx context.Context , arg database.GetUserActivityInsightsParams ) ([]database.GetUserActivityInsightsRow , error ) {
1562
- for _ , templateID := range arg .TemplateIDs {
1563
- template , err := q .db .GetTemplateByID (ctx , templateID )
1564
- if err != nil {
1565
- return nil , err
1566
- }
1580
+ // Used by insights endpoints. Need to check both for auditors and for regular users with template acl perms.
1581
+ if err := q .authorizeContext (ctx , rbac .ActionRead , rbac .ResourceTemplateInsights ); IsNotAuthorizedError (err ) {
1582
+ for _ , templateID := range arg .TemplateIDs {
1583
+ template , err := q .db .GetTemplateByID (ctx , templateID )
1584
+ if err != nil {
1585
+ return nil , err
1586
+ }
1567
1587
1568
- if err := q .authorizeContext (ctx , rbac .ActionUpdate , template ); err != nil {
1569
- return nil , err
1588
+ if err := q .authorizeContext (ctx , rbac .ActionUpdate , template ); err != nil {
1589
+ return nil , err
1590
+ }
1570
1591
}
1571
- }
1572
- if len ( arg . TemplateIDs ) == 0 {
1573
- if err := q . authorizeContext ( ctx , rbac . ActionUpdate , rbac . ResourceTemplate . All ()); err != nil {
1574
- return nil , err
1592
+ if len ( arg . TemplateIDs ) == 0 {
1593
+ if err := q . authorizeContext ( ctx , rbac . ActionUpdate , rbac . ResourceTemplate . All ()); err != nil {
1594
+ return nil , err
1595
+ }
1575
1596
}
1576
1597
}
1577
1598
return q .db .GetUserActivityInsights (ctx , arg )
@@ -1593,19 +1614,22 @@ func (q *querier) GetUserCount(ctx context.Context) (int64, error) {
1593
1614
}
1594
1615
1595
1616
func (q * querier ) GetUserLatencyInsights (ctx context.Context , arg database.GetUserLatencyInsightsParams ) ([]database.GetUserLatencyInsightsRow , error ) {
1596
- for _ , templateID := range arg .TemplateIDs {
1597
- template , err := q .db .GetTemplateByID (ctx , templateID )
1598
- if err != nil {
1599
- return nil , err
1600
- }
1617
+ // Used by insights endpoints. Need to check both for auditors and for regular users with template acl perms.
1618
+ if err := q .authorizeContext (ctx , rbac .ActionRead , rbac .ResourceTemplateInsights ); IsNotAuthorizedError (err ) {
1619
+ for _ , templateID := range arg .TemplateIDs {
1620
+ template , err := q .db .GetTemplateByID (ctx , templateID )
1621
+ if err != nil {
1622
+ return nil , err
1623
+ }
1601
1624
1602
- if err := q .authorizeContext (ctx , rbac .ActionUpdate , template ); err != nil {
1603
- return nil , err
1625
+ if err := q .authorizeContext (ctx , rbac .ActionUpdate , template ); err != nil {
1626
+ return nil , err
1627
+ }
1604
1628
}
1605
- }
1606
- if len ( arg . TemplateIDs ) == 0 {
1607
- if err := q . authorizeContext ( ctx , rbac . ActionUpdate , rbac . ResourceTemplate . All ()); err != nil {
1608
- return nil , err
1629
+ if len ( arg . TemplateIDs ) == 0 {
1630
+ if err := q . authorizeContext ( ctx , rbac . ActionUpdate , rbac . ResourceTemplate . All ()); err != nil {
1631
+ return nil , err
1632
+ }
1609
1633
}
1610
1634
}
1611
1635
return q .db .GetUserLatencyInsights (ctx , arg )
0 commit comments