Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add author ID to labels #27055

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open

Conversation

sgress454
Copy link
Contributor

@sgress454 sgress454 commented Mar 11, 2025

For #27035

Checklist for submitter

  • Changes file added for user-visible changes in changes/, orbit/changes/ or ee/fleetd-chrome/changes.
    See Changes files for more information.
  • Input data is properly validated, SELECT * is avoided, SQL injection is prevented (using placeholders for values in statements)
  • If database migrations are included, checked table schema to confirm autoupdate
  • For database migrations:
    • Checked schema for all modified table for columns that will auto-update timestamps during migration.
    • Confirmed that updating the timestamps is acceptable, and will not cause unwanted side effects.
    • Ensured the correct collation is explicitly set for character columns (COLLATE utf8mb4_unicode_ci).
  • Added/updated automated tests
  • A detailed QA plan exists on the associated ticket (if it isn't there, work with the product group's QA engineer to add it)
  • Manual QA for all new/changed functionality

Details

This PR adds an author_id column to the labels table, and adds the associated properties to the Label and LabelSpec types. When a new label is created via the UI or API, an author ID is set on the label if one can be inferred from the context. Otherwise, the author ID is set to null.

Authz and Automated testing

Additional backend authorization logic is introduced in a follow-on PR, #27089, because rconciling all of the test updates between this PR and #27038 was getting complicated.

Manual Testing

  • Tested in the UI by creating a new label on the Hosts page
  • Tested via Gitops by merging this branch with Manage labels in GitOps #27038 and doing fleetctl gitops with a global config with labels: in it.

@sgress454 sgress454 requested a review from a team as a code owner March 11, 2025 23:35
Comment on lines 17 to +21
func (ds *Datastore) ApplyLabelSpecs(ctx context.Context, specs []*fleet.LabelSpec) (err error) {
return ds.ApplyLabelSpecsWithAuthor(ctx, specs, 0)
}

func (ds *Datastore) ApplyLabelSpecsWithAuthor(ctx context.Context, specs []*fleet.LabelSpec, authorID uint) (err error) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no ApplyLabels method that takes Label types as an argument, and there's no reason to add AuthorID to LabelSpec since it should never be set manually. So the cleanest way to do this was to update ApplyLabelSpecs to ApplyLabelSpecsWithAuthor and then create a new ApplyLabelSpecs wrapper with a default. This way we don't have to update the many calls to ApplyLabelSpecs in the codebase.

Copy link

codecov bot commented Mar 12, 2025

Codecov Report

Attention: Patch coverage is 76.47059% with 8 lines in your changes missing coverage. Please review.

Project coverage is 63.97%. Comparing base (25a3835) to head (5f54537).
Report is 28 commits behind head on main.

Files with missing lines Patch % Lines
...tions/tables/20250311132525_AddAuthorIdToLabels.go 52.94% 6 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #27055      +/-   ##
==========================================
- Coverage   63.98%   63.97%   -0.02%     
==========================================
  Files        1706     1708       +2     
  Lines      162797   163100     +303     
  Branches     4326     4326              
==========================================
+ Hits       104172   104346     +174     
- Misses      50513    50617     +104     
- Partials     8112     8137      +25     
Flag Coverage Δ
backend 64.73% <76.47%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Member

@lucasmrod lucasmrod left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a few comments.

A product question: Should Labels also have team_id?

With the new idea of team admins/maintainers being able to create labels, a team admin/maintainer can now create a "global" label query that will run on all hosts from all teams. This is different to how Policies and Queries work [*].

[*] Policies and Queries that have author_id also have team_id.


func Up_20250311132525(tx *sql.Tx) error {
_, err := tx.Exec(
"ALTER TABLE `labels` " +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to author_id in the policies and queries tables, we need a foreign constraint and ON DELETE SET NULL:

  CONSTRAINT `labels_users_fk_1` FOREIGN KEY (`author_id`) REFERENCES `users` (`id`) ON DELETE SET NULL

@@ -15,6 +15,10 @@ import (
)

func (ds *Datastore) ApplyLabelSpecs(ctx context.Context, specs []*fleet.LabelSpec) (err error) {
return ds.ApplyLabelSpecsWithAuthor(ctx, specs, 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the argument be authorID *uint and the default nil?


// Get the user from the context.
user, ok := viewer.FromContext(ctx)
// If we have a user, mark them as the label's author.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to not have a user set at this point?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants