Skip to content

Conversation

@maxwellL21
Copy link
Collaborator

Description

This PR adds the create, update, and delete tuples event handlers for events such as CreateComponentInstance and DeleteSupportGroup. Please review full list of tuples implemented by handlers listed in the document here

As part of the OpenFGA implementation, when Heureka entities are modified, the relation tuples in OpenFGA should be similarly updated using the event registry logic.

What type of PR is this?

  • 🍕 Feature
  • 🐛 Bug Fix
  • 📝 Documentation Update
  • 🎨 Style
  • 🧑‍💻 Code Refactor
  • 🔥 Performance Improvements
  • ✅ Test
  • 🤖 Build
  • 🔁 CI
  • 📦 Chore (Release)
  • ⏩ Revert

Related Tickets & Documents

Added tests?

  • 👍 yes
  • 🙅 no, because they aren't needed
  • 🙋 no, because I need help
  • Separate ticket for tests # (issue/pr)

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

Added to documentation?

  • 📜 README.md
  • 🤝 Documentation pages updated
  • 🙅 no documentation needed
  • (if applicable) generated OpenAPI docs for CRD changes

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

maxwellL21 and others added 22 commits August 20, 2025 09:05
@maxwellL21 maxwellL21 changed the title Origin/max luong/issue 842/create user resource relationship feat(openfga): add creating, updating, and deleting OpenFGA event handlers Oct 17, 2025
@maxwellL21 maxwellL21 self-assigned this Oct 17, 2025
Copy link
Collaborator

@michalkrzyz michalkrzyz left a comment

Choose a reason for hiding this comment

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

Conditional in tests has to be avoided.

}

for _, rel := range rlist {
authz.AddRelation(rel)
Copy link
Collaborator

Choose a reason for hiding this comment

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

remove for loop and avoid using lists if that is not necessary

})

if updateEvent, ok := e.(*UpdateComponentVersionEvent); ok {
versionId := strconv.FormatInt(updateEvent.ComponentVersion.Id, 10)
Copy link
Collaborator

Choose a reason for hiding this comment

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

use Atoi


if updateEvent, ok := e.(*UpdateComponentVersionEvent); ok {
versionId := strconv.FormatInt(updateEvent.ComponentVersion.Id, 10)
newComponentId := strconv.FormatInt(updateEvent.ComponentVersion.ComponentId, 10)
Copy link
Collaborator

Choose a reason for hiding this comment

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

use Atoi

UserType: "user",
UserId: "userID",
ObjectId: objectId,
ObjectType: "component_version",
Copy link
Collaborator

Choose a reason for hiding this comment

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

repeated code, please use consts to avoid inline duplications, which are source of maintenance problems and slips.

},
}

for _, rel := range relations {
Copy link
Collaborator

Choose a reason for hiding this comment

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

if you want to keep for loop everywhere to follow some logic iterating through relocations, please create helper function for that and use it everywhere (even with single elements)

UserType: "component_instance",
Relation: "component_instance",
ObjectType: "issue_match",
ObjectId: openfga.ObjectId(issueMatchId),
Copy link
Collaborator

Choose a reason for hiding this comment

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

use consts instead of inline strings

ServiceID: serviceFake.Id,
OwnerID: ownerFake.Id,
}
serviceId := openfga.ObjectId(strconv.FormatInt(removeEvent.ServiceID, 10))
Copy link
Collaborator

@michalkrzyz michalkrzyz Oct 22, 2025

Choose a reason for hiding this comment

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

As far as I see the ObjectId has int parameter, but in code mostly we are dealing with string Id, so we convert it all the time and we add unnecessary complexity to the code. consider to add extra function like ObjectId with string parameter (or change the function itself) and hide itoa convert in there. This way we will have great improvement, removing a lot of duplicated code.

},
}

for _, rel := range rlist {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe we could change AddRelocation declaration to accept list parameters instead of doing it all the time in the code. Reducing complexity of for loop is worth it.

}

for _, rel := range relations {
authz.AddRelation(rel)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Consider to create AddRelocations, you may keep AddRelocation calling AddRelocations with single list element, but this will remove many duplications.

objectParts := strings.SplitN(tuple.Key.Object, ":", 2)

for _, r := range filters {
if r.UserType != "" && (len(userParts) < 1 || userParts[0] != string(r.UserType)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

enclose conditionals in helper functions with meaningful name. Anyway why did you create so many if statements with the same body. Please use single if statement, group queries with helper conditional checks. If this is copied behavior from some other part of the code, then please create comment linked refactoring ticket in backlog, we will try to do the best to fix the situation, because it makes the code stink more and more and violates scout rule for sure.

func (a *Authz) RemoveRelationBulk(r []RelationInput) error {
tuples, err := a.ListRelations(r)
if err != nil {
return err
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we could wrap the error, because ListRelocations error might be too meaningless for the user. Especially when this is exported function.

return err
}

func (a *Authz) UpdateRelation(r RelationInput, u RelationInput) error {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please use meaningful names in parameters. I do not understand what is r and u meaning without reading of the function body, this is the sign of poor design.

})

err := a.RemoveRelationBulk([]RelationInput{r})
if err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

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

IMHO This code is wrong by design if RemoveRelocationBulk will return error, we will still call AddRelocation which may hide an error returning nil, so we will return nil even if some function will fail inside. Is it what we would like to have in here?

Anyway is the else part correct? I see 'Added relocation tuple....' log in the case of successful RemoveRelocation. Looks like action-based slip/copy paste error.

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.

feat(authorization): update user resource relationship feat(authorization): create user resource relationships

5 participants