assisted-service icon indicating copy to clipboard operation
assisted-service copied to clipboard

MGMT-19297: Error when querying /events endpoint with ?host_ids=uuid1…

Open rivkyrizel opened this issue 8 months ago • 8 comments

Fixes an SQL ambiguity issue when querying the /events endpoint with host_ids using RHSSO authentication. The ambiguity arises due to joins between infra_envs, hosts, and clusters, causing column name conflicts (e.g., user_name, org_id).

This change ensures the authorization check is applied after the main query by wrapping it in a subquery, preventing conflicts while maintaining access control. Also includes a test case to verify event retrieval by host ID when org tenancy is disabled.

List all the issues related to this PR

  • [ ] New Feature
  • [ ] Enhancement
  • [x] Bug fix
  • [ ] Tests
  • [ ] Documentation
  • [ ] CI/CD

What environments does this code impact?

  • [ ] Automation (CI, tools, etc)
  • [ ] Cloud
  • [ ] Operator Managed Deployments
  • [x] None

How was this code tested?

  • [x] assisted-test-infra environment
  • [ ] dev-scripts environment
  • [ ] Reviewer's test appreciated
  • [ ] Waiting for CI to do a full test run
  • [ ] Manual (Elaborate on how it was tested)
  • [ ] No tests needed

Checklist

  • [x] Title and description added to both, commit and PR.
  • [x] Relevant issues have been associated (see CONTRIBUTING guide)
  • [x] This change does not require a documentation update (docstring, docs, README, etc)
  • [x] Does this change include unit-tests (note that code changes require unit-tests)

Reviewers Checklist

  • Are the title and description (in both PR and commit) meaningful and clear?
  • Is there a bug required (and linked) for this change?
  • Should this PR be backported?

rivkyrizel avatar Mar 23 '25 12:03 rivkyrizel

@rivkyrizel: This pull request references MGMT-19297 which is a valid jira issue.

Warning: The referenced jira issue has an invalid target version for the target branch this PR targets: expected the bug to target the "4.19.0" version, but no target version was set.

In response to this:

Fixes an SQL ambiguity issue when querying the /events endpoint with host_ids using RHSSO authentication. The ambiguity arises due to joins between infra_envs, hosts, and clusters, causing column name conflicts (e.g., user_name, org_id).

This change ensures the authorization check is applied after the main query by wrapping it in a subquery, preventing conflicts while maintaining access control. Also includes a test case to verify event retrieval by host ID when org tenancy is disabled.

List all the issues related to this PR

  • [ ] New Feature
  • [ ] Enhancement
  • [x] Bug fix
  • [ ] Tests
  • [ ] Documentation
  • [ ] CI/CD

What environments does this code impact?

  • [ ] Automation (CI, tools, etc)
  • [ ] Cloud
  • [ ] Operator Managed Deployments
  • [x] None

How was this code tested?

  • [x] assisted-test-infra environment
  • [ ] dev-scripts environment
  • [ ] Reviewer's test appreciated
  • [ ] Waiting for CI to do a full test run
  • [ ] Manual (Elaborate on how it was tested)
  • [ ] No tests needed

Checklist

  • [x] Title and description added to both, commit and PR.
  • [x] Relevant issues have been associated (see CONTRIBUTING guide)
  • [x] This change does not require a documentation update (docstring, docs, README, etc)
  • [x] Does this change include unit-tests (note that code changes require unit-tests)

Reviewers Checklist

  • Are the title and description (in both PR and commit) meaningful and clear?
  • Is there a bug required (and linked) for this change?
  • Should this PR be backported?

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the openshift-eng/jira-lifecycle-plugin repository.

openshift-ci-robot avatar Mar 23 '25 12:03 openshift-ci-robot

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 68.78%. Comparing base (1f52e95) to head (2d7e1f1). Report is 16 commits behind head on master.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #7445      +/-   ##
==========================================
+ Coverage   67.26%   68.78%   +1.52%     
==========================================
  Files         335      335              
  Lines       42621    44719    +2098     
==========================================
+ Hits        28669    30760    +2091     
+ Misses      11360    11316      -44     
- Partials     2592     2643      +51     
Files with missing lines Coverage Δ
internal/events/event.go 84.50% <100.00%> (ø)

... and 10 files with indirect coverage changes

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

codecov[bot] avatar Mar 23 '25 13:03 codecov[bot]

@rccrdpccl please review

gamli75 avatar Mar 25 '25 09:03 gamli75

@rivkyrizel can you please add an example of how the final query looked like before, and how it looks like now? How would this ambiguity generate (i.e. why joinable cluster,infraenv or host would have different org_id or user_name)? Could you expand on the issues that this ambiguity would cause?

rccrdpccl avatar Mar 26 '25 08:03 rccrdpccl

@rccrdpccl The issue was caused by ambiguity between user_name and org_id, which exist in infra_envs, hosts, and clusters. The previous query failed due to SQL ambiguity:

SELECT events.*, infra_envs.user_name, infra_envs.org_id
FROM events
INNER JOIN infra_envs ON infra_envs.id = events.infra_env_id
INNER JOIN hosts ON hosts.id = events.host_id
WHERE hosts.deleted_at IS NULL AND user_name = ?

We now apply filtering in an outer query:

SELECT * FROM (
    SELECT events.*, infra_envs.user_name, infra_envs.org_id
    FROM events
    INNER JOIN infra_envs ON infra_envs.id = events.infra_env_id
    INNER JOIN hosts ON hosts.id = events.host_id
    WHERE hosts.deleted_at IS NULL
) AS s WHERE s.user_name = 'foo' OR s.org_id = 'bar'

Since user_name and org_id exist in multiple tables, they may hold different values due to updates, ownership changes, or historical data inconsistencies.

Resulting Error:

{ 
  "code": "500",  
  "reason": "ERROR: column reference \"user_name\" is ambiguous (SQLSTATE 42702)"  
}

Using a subquery ensures unique column references before filtering, resolving the issue.

rivkyrizel avatar Mar 27 '25 15:03 rivkyrizel

@rivkyrizel thank you. Just nitpicking, but would it be easy to generate a query avoiding a subquery with gorm? Possibly adding the org and username filter on the relevant joins and not on the where clause?

If gorm would make it too convoluted or force us to a manual query we can leave out

rccrdpccl avatar Mar 27 '25 16:03 rccrdpccl

/lgtm

rccrdpccl avatar Apr 29 '25 09:04 rccrdpccl

/approve

rccrdpccl avatar Apr 29 '25 09:04 rccrdpccl

/retest

rccrdpccl avatar Apr 29 '25 10:04 rccrdpccl

/override ci/prow/okd-scos-e2e-aws-ovn

gamli75 avatar May 04 '25 07:05 gamli75

@gamli75: Overrode contexts on behalf of gamli75: ci/prow/okd-scos-e2e-aws-ovn

In response to this:

/override ci/prow/okd-scos-e2e-aws-ovn

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

openshift-ci[bot] avatar May 04 '25 07:05 openshift-ci[bot]

/retest

gamli75 avatar May 04 '25 07:05 gamli75

/retest

rivkyrizel avatar May 04 '25 13:05 rivkyrizel

/retest

rccrdpccl avatar May 06 '25 09:05 rccrdpccl

/override ci/prow/okd-scos-e2e-aws-ovn

rivkyrizel avatar May 08 '25 09:05 rivkyrizel

@rivkyrizel: rivkyrizel unauthorized: /override is restricted to Repo administrators, approvers in top level OWNERS file, and the following github teams:openshift: openshift-release-oversight openshift-staff-engineers.

In response to this:

/override ci/prow/okd-scos-e2e-aws-ovn

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

openshift-ci[bot] avatar May 08 '25 09:05 openshift-ci[bot]

/override ci/prow/okd-scos-e2e-aws-ovn

gamli75 avatar May 08 '25 11:05 gamli75

/test edge-e2e-metal-assisted-4-19

gamli75 avatar May 08 '25 11:05 gamli75

@gamli75: Overrode contexts on behalf of gamli75: ci/prow/okd-scos-e2e-aws-ovn

In response to this:

/override ci/prow/okd-scos-e2e-aws-ovn

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

openshift-ci[bot] avatar May 08 '25 11:05 openshift-ci[bot]

/lgtm /approve

rccrdpccl avatar May 12 '25 14:05 rccrdpccl

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: rccrdpccl, rivkyrizel

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment Approvers can cancel approval by writing /approve cancel in a comment

openshift-ci[bot] avatar May 12 '25 14:05 openshift-ci[bot]

/retest-required

Remaining retests: 0 against base HEAD 91dc61d08376e46710321d2c9dadf09fe263cfd9 and 2 for PR HEAD 2d7e1f1b6693322d4f0c608023cc1aa129f40f20 in total

openshift-ci-robot avatar May 12 '25 20:05 openshift-ci-robot

@rivkyrizel: all tests passed!

Full PR test history. Your PR dashboard.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here.

openshift-ci[bot] avatar May 13 '25 01:05 openshift-ci[bot]

[ART PR BUILD NOTIFIER]

Distgit: ose-agent-installer-api-server This PR has been included in build ose-agent-installer-api-server-container-v4.20.0-202505130412.p0.gb16fd19.assembly.stream.el9. All builds following this will include this PR.

openshift-bot avatar May 13 '25 05:05 openshift-bot