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

fix(ca_certificates): invalidate ca store caches when a ca cert is updated and prevent ca_certificates that are still being referenced by other entities from being deleted #11789

Merged
merged 16 commits into from
Nov 22, 2023

Conversation

catbro666
Copy link
Contributor

@catbro666 catbro666 commented Oct 19, 2023

Summary

Currently, when a CA certificate is updated, the related CA certificate store caches won't be invalidated. And a CA certificate can be deleted even if it's still referenced by other entities. This PR fixes these issues.

link to #10120

Checklist

  • The Pull Request has tests
  • A changelog file has been created under changelog/unreleased/kong or skip-changelog label added on PR if changelog is unnecessary. README.md
  • There is a user-facing docs PR against https://github.com/Kong/docs.konghq.com - PUT DOCS PR HERE

Full changelog

  • add :select_by_ca_certificate() method to kong.db.services and kong.db.plugins to select entities referencing a ca certificate.
  • add ca_certificates:delete(). Before deleting a ca certificate, check all the entities and plugins that may reference it and prevent deletion if any one is referencing it.
  • after updating a ca certificate, invalidate all the ca store caches that include this ca certificate.

Issue reference

Fix FTI-2060

@catbro666 catbro666 marked this pull request as draft October 20, 2023 04:38
@catbro666 catbro666 marked this pull request as ready for review October 23, 2023 02:42
@catbro666 catbro666 requested a review from ms2008 October 23, 2023 02:44
@catbro666 catbro666 force-pushed the fit-2060-ca-certificate-cache branch from 7f4192b to ba5ec21 Compare October 24, 2023 08:46
kong/runloop/events.lua Outdated Show resolved Hide resolved
kong/runloop/events.lua Outdated Show resolved Hide resolved
@locao locao self-requested a review October 24, 2023 16:48
@catbro666 catbro666 force-pushed the fit-2060-ca-certificate-cache branch from b4d8a8e to 99d35c7 Compare October 25, 2023 07:21
@catbro666 catbro666 force-pushed the fit-2060-ca-certificate-cache branch from 99d35c7 to 7b28832 Compare October 25, 2023 07:53
@ms2008 ms2008 force-pushed the fit-2060-ca-certificate-cache branch from 40c454b to 22c27b5 Compare October 25, 2023 13:20
Copy link
Contributor

@ms2008 ms2008 left a comment

Choose a reason for hiding this comment

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

LGTM

@tzssangglass
Copy link
Member

LGTM.

Just a question: the reference relationship of ca_certificates is established by traversing the full services data every time ca_certificates is updated. Will it become in the future:

  1. Assuming there is no data in the database;
  2. Incrementally update the reference relationship of ca_certificates every time an entity related to ca_certificates is created/updated/deleted.

@catbro666
Copy link
Contributor Author

@tzssangglass

  1. Assuming there is no data in the database;

Maybe doing an initial traversal at the init phase instead.

  1. Incrementally update the reference relationship of ca_certificates every time an entity related to ca_certificates is created/updated/deleted.

As mentioned in the ticket, this is another optional solution that will complicate the code a lot. Considering the CA certificates won't be updated frequently, not sure if it’s worthwhile.

BTW, the reason why we can't get the entities that reference ca_certificates efficiently is postgres doesn't support foreign key arrays. There was a proposed patch 6 years ago but it never got included because some major surgery is still required.

@hanshuebner
Copy link
Contributor

BTW, the reason why we can't get the entities that reference ca_certificates efficiently is postgres doesn't support foreign key arrays.

This can be implemented with a before commit trigger that checks the relationship. This would be more efficient than traversing all certificates from the application end, in particular in large configurations.

@kikito kikito requested a review from flrgh October 31, 2023 17:57
@flrgh
Copy link
Contributor

flrgh commented Nov 5, 2023

This can be implemented with a before commit trigger that checks the relationship. This would be more efficient than traversing all certificates from the application end, in particular in large configurations.

In the services case, I 100% agree. I would write a DELETE trigger function and call it a day. However, adapting the {{plugin}}.config.ca_certificates case to method is where it quickly gets more convoluted and starts to lose its appeal.


Idea: instead of performing all the filtering in Lua (expensive) or trying to enforce this constraint solely in the DB with schema/trigger changes (leaks plugin-specific business logic into the storage layer), what if we added some specialized :select_by_ca_certificate(cert_id) methods to kong.db.services and kong.db.plugins?

Something kinda like...

-- note: you might even be able to teach `kong/db/dao/init.lua` to
-- auto-generate this code if the type definition for the `ca_certificates`
-- field is annotated to reflect the foreign relationship.

function services:select_by_ca_certificate(cert_id)
  -- idk if this is correct at all
  local rows, err = self.strategy:query("SELECT * FROM services WHERE %s = ANY(ca_certificates)",
                                                  cert_id)
  -- ...
  return services
end

-- alternatively, one could craft a method that uses `SELECT COUNT(*) FROM ...`,
-- which would be more efficient given that we don't actually care about
-- retrieving the matching entities

This would negate the need to iterate and filter through all entities in Lua land, but it also avoids putting too much business logic into the DB.

@catbro666 catbro666 marked this pull request as draft November 8, 2023 06:18
@team-gateway-bot
Copy link
Collaborator

Validation Failed: {"resource":"PullRequest","code":"custom","message":"No commits between master and cherry-pick-11789-to-master-to-upstream"}

@team-gateway-bot
Copy link
Collaborator

Cherry-pick failed for master, because it was unable to cherry-pick the commit(s).

Please cherry-pick the changes locally.

git remote add upstream https://github.com/kong/kong-ee
git fetch upstream master
git worktree add -d .worktree/cherry-pick-11789-to-master-to-upstream upstream/master
cd .worktree/cherry-pick-11789-to-master-to-upstream
git checkout -b cherry-pick-11789-to-master-to-upstream
ancref=$(git merge-base a6d647566991e339ea5126113df4bef21fe0115d 6cb08ab74d3da794493649eaeaf525cf4503edcc)
git cherry-pick -x $ancref..6cb08ab74d3da794493649eaeaf525cf4503edcc

@team-gateway-bot
Copy link
Collaborator

Validation Failed: {"resource":"PullRequest","code":"custom","message":"No commits between master and cherry-pick-11789-to-master-to-upstream"}

@team-gateway-bot
Copy link
Collaborator

Cherry-pick failed for master, because it was unable to cherry-pick the commit(s).

Please cherry-pick the changes locally.

git remote add upstream https://github.com/kong/kong-ee
git fetch upstream master
git worktree add -d .worktree/cherry-pick-11789-to-master-to-upstream upstream/master
cd .worktree/cherry-pick-11789-to-master-to-upstream
git checkout -b cherry-pick-11789-to-master-to-upstream
ancref=$(git merge-base a6d647566991e339ea5126113df4bef21fe0115d 6cb08ab74d3da794493649eaeaf525cf4503edcc)
git cherry-pick -x $ancref..6cb08ab74d3da794493649eaeaf525cf4503edcc

@team-gateway-bot
Copy link
Collaborator

Validation Failed: {"resource":"PullRequest","code":"custom","message":"No commits between master and cherry-pick-11789-to-master-to-upstream"}

@team-gateway-bot
Copy link
Collaborator

Cherry-pick failed for master, because it was unable to cherry-pick the commit(s).

Please cherry-pick the changes locally.

git remote add upstream https://github.com/kong/kong-ee
git fetch upstream master
git worktree add -d .worktree/cherry-pick-11789-to-master-to-upstream upstream/master
cd .worktree/cherry-pick-11789-to-master-to-upstream
git checkout -b cherry-pick-11789-to-master-to-upstream
ancref=$(git merge-base a6d647566991e339ea5126113df4bef21fe0115d 6cb08ab74d3da794493649eaeaf525cf4503edcc)
git cherry-pick -x $ancref..6cb08ab74d3da794493649eaeaf525cf4503edcc

@team-gateway-bot
Copy link
Collaborator

Validation Failed: {"resource":"PullRequest","code":"custom","message":"No commits between master and cherry-pick-11789-to-master-to-upstream"}

@team-gateway-bot
Copy link
Collaborator

Cherry-pick failed for master, because it was unable to cherry-pick the commit(s).

Please cherry-pick the changes locally.

git remote add upstream https://github.com/kong/kong-ee
git fetch upstream master
git worktree add -d .worktree/cherry-pick-11789-to-master-to-upstream upstream/master
cd .worktree/cherry-pick-11789-to-master-to-upstream
git checkout -b cherry-pick-11789-to-master-to-upstream
ancref=$(git merge-base a6d647566991e339ea5126113df4bef21fe0115d 6cb08ab74d3da794493649eaeaf525cf4503edcc)
git cherry-pick -x $ancref..6cb08ab74d3da794493649eaeaf525cf4503edcc

@team-gateway-bot
Copy link
Collaborator

Validation Failed: {"resource":"PullRequest","code":"custom","message":"No commits between master and cherry-pick-11789-to-master-to-upstream"}

@team-gateway-bot
Copy link
Collaborator

Cherry-pick failed for master, because it was unable to cherry-pick the commit(s).

Please cherry-pick the changes locally.

git remote add upstream https://github.com/kong/kong-ee
git fetch upstream master
git worktree add -d .worktree/cherry-pick-11789-to-master-to-upstream upstream/master
cd .worktree/cherry-pick-11789-to-master-to-upstream
git checkout -b cherry-pick-11789-to-master-to-upstream
ancref=$(git merge-base a6d647566991e339ea5126113df4bef21fe0115d 6cb08ab74d3da794493649eaeaf525cf4503edcc)
git cherry-pick -x $ancref..6cb08ab74d3da794493649eaeaf525cf4503edcc

@team-gateway-bot
Copy link
Collaborator

Validation Failed: {"resource":"PullRequest","code":"custom","message":"No commits between master and cherry-pick-11789-to-master-to-upstream"}

@team-gateway-bot
Copy link
Collaborator

Cherry-pick failed for master, because it was unable to cherry-pick the commit(s).

Please cherry-pick the changes locally.

git remote add upstream https://github.com/kong/kong-ee
git fetch upstream master
git worktree add -d .worktree/cherry-pick-11789-to-master-to-upstream upstream/master
cd .worktree/cherry-pick-11789-to-master-to-upstream
git checkout -b cherry-pick-11789-to-master-to-upstream
ancref=$(git merge-base a6d647566991e339ea5126113df4bef21fe0115d 6cb08ab74d3da794493649eaeaf525cf4503edcc)
git cherry-pick -x $ancref..6cb08ab74d3da794493649eaeaf525cf4503edcc

@team-gateway-bot
Copy link
Collaborator

Validation Failed: {"resource":"PullRequest","code":"custom","message":"No commits between master and cherry-pick-11789-to-master-to-upstream"}

@team-gateway-bot
Copy link
Collaborator

Cherry-pick failed for master, because it was unable to cherry-pick the commit(s).

Please cherry-pick the changes locally.

git remote add upstream https://github.com/kong/kong-ee
git fetch upstream master
git worktree add -d .worktree/cherry-pick-11789-to-master-to-upstream upstream/master
cd .worktree/cherry-pick-11789-to-master-to-upstream
git checkout -b cherry-pick-11789-to-master-to-upstream
ancref=$(git merge-base a6d647566991e339ea5126113df4bef21fe0115d 6cb08ab74d3da794493649eaeaf525cf4503edcc)
git cherry-pick -x $ancref..6cb08ab74d3da794493649eaeaf525cf4503edcc

@team-gateway-bot
Copy link
Collaborator

Validation Failed: {"resource":"PullRequest","code":"custom","message":"No commits between master and cherry-pick-11789-to-master-to-upstream"}

@team-gateway-bot
Copy link
Collaborator

Cherry-pick failed for master, because it was unable to cherry-pick the commit(s).

Please cherry-pick the changes locally.

git remote add upstream https://github.com/kong/kong-ee
git fetch upstream master
git worktree add -d .worktree/cherry-pick-11789-to-master-to-upstream upstream/master
cd .worktree/cherry-pick-11789-to-master-to-upstream
git checkout -b cherry-pick-11789-to-master-to-upstream
ancref=$(git merge-base a6d647566991e339ea5126113df4bef21fe0115d 6cb08ab74d3da794493649eaeaf525cf4503edcc)
git cherry-pick -x $ancref..6cb08ab74d3da794493649eaeaf525cf4503edcc

@team-gateway-bot
Copy link
Collaborator

Validation Failed: {"resource":"PullRequest","code":"custom","message":"No commits between master and cherry-pick-11789-to-master-to-upstream"}

@team-gateway-bot
Copy link
Collaborator

Cherry-pick failed for master, because it was unable to cherry-pick the commit(s).

Please cherry-pick the changes locally.

git remote add upstream https://github.com/kong/kong-ee
git fetch upstream master
git worktree add -d .worktree/cherry-pick-11789-to-master-to-upstream upstream/master
cd .worktree/cherry-pick-11789-to-master-to-upstream
git checkout -b cherry-pick-11789-to-master-to-upstream
ancref=$(git merge-base a6d647566991e339ea5126113df4bef21fe0115d 6cb08ab74d3da794493649eaeaf525cf4503edcc)
git cherry-pick -x $ancref..6cb08ab74d3da794493649eaeaf525cf4503edcc

@team-gateway-bot
Copy link
Collaborator

Validation Failed: {"resource":"PullRequest","code":"custom","message":"No commits between master and cherry-pick-11789-to-master-to-upstream"}

@team-gateway-bot
Copy link
Collaborator

Cherry-pick failed for master, because it was unable to cherry-pick the commit(s).

Please cherry-pick the changes locally.

git remote add upstream https://github.com/kong/kong-ee
git fetch upstream master
git worktree add -d .worktree/cherry-pick-11789-to-master-to-upstream upstream/master
cd .worktree/cherry-pick-11789-to-master-to-upstream
git checkout -b cherry-pick-11789-to-master-to-upstream
ancref=$(git merge-base a6d647566991e339ea5126113df4bef21fe0115d 6cb08ab74d3da794493649eaeaf525cf4503edcc)
git cherry-pick -x $ancref..6cb08ab74d3da794493649eaeaf525cf4503edcc

@team-gateway-bot
Copy link
Collaborator

Validation Failed: {"resource":"PullRequest","code":"custom","message":"No commits between master and cherry-pick-11789-to-master-to-upstream"}

@team-gateway-bot
Copy link
Collaborator

Cherry-pick failed for master, because it was unable to cherry-pick the commit(s).

Please cherry-pick the changes locally.

git remote add upstream https://github.com/kong/kong-ee
git fetch upstream master
git worktree add -d .worktree/cherry-pick-11789-to-master-to-upstream upstream/master
cd .worktree/cherry-pick-11789-to-master-to-upstream
git checkout -b cherry-pick-11789-to-master-to-upstream
ancref=$(git merge-base a6d647566991e339ea5126113df4bef21fe0115d 6cb08ab74d3da794493649eaeaf525cf4503edcc)
git cherry-pick -x $ancref..6cb08ab74d3da794493649eaeaf525cf4503edcc

@team-gateway-bot
Copy link
Collaborator

Validation Failed: {"resource":"PullRequest","code":"custom","message":"No commits between master and cherry-pick-11789-to-master-to-upstream"}

@team-gateway-bot
Copy link
Collaborator

Cherry-pick failed for master, because it was unable to cherry-pick the commit(s).

Please cherry-pick the changes locally.

git remote add upstream https://github.com/kong/kong-ee
git fetch upstream master
git worktree add -d .worktree/cherry-pick-11789-to-master-to-upstream upstream/master
cd .worktree/cherry-pick-11789-to-master-to-upstream
git checkout -b cherry-pick-11789-to-master-to-upstream
ancref=$(git merge-base a6d647566991e339ea5126113df4bef21fe0115d 6cb08ab74d3da794493649eaeaf525cf4503edcc)
git cherry-pick -x $ancref..6cb08ab74d3da794493649eaeaf525cf4503edcc

@team-gateway-bot
Copy link
Collaborator

Validation Failed: {"resource":"PullRequest","code":"custom","message":"No commits between master and cherry-pick-11789-to-master-to-upstream"}

@team-gateway-bot
Copy link
Collaborator

Cherry-pick failed for master, because it was unable to cherry-pick the commit(s).

Please cherry-pick the changes locally.

git remote add upstream https://github.com/kong/kong-ee
git fetch upstream master
git worktree add -d .worktree/cherry-pick-11789-to-master-to-upstream upstream/master
cd .worktree/cherry-pick-11789-to-master-to-upstream
git checkout -b cherry-pick-11789-to-master-to-upstream
ancref=$(git merge-base a6d647566991e339ea5126113df4bef21fe0115d 6cb08ab74d3da794493649eaeaf525cf4503edcc)
git cherry-pick -x $ancref..6cb08ab74d3da794493649eaeaf525cf4503edcc

@team-gateway-bot
Copy link
Collaborator

Validation Failed: {"resource":"PullRequest","code":"custom","message":"No commits between master and cherry-pick-11789-to-master-to-upstream"}

@catbro666 catbro666 removed the cherry-pick kong-ee schedule this PR for cherry-picking to kong/kong-ee label Nov 23, 2023
@team-gateway-bot
Copy link
Collaborator

Cherry-pick failed for master, because it was unable to cherry-pick the commit(s).

Please cherry-pick the changes locally.

git remote add upstream https://github.com/kong/kong-ee
git fetch upstream master
git worktree add -d .worktree/cherry-pick-11789-to-master-to-upstream upstream/master
cd .worktree/cherry-pick-11789-to-master-to-upstream
git checkout -b cherry-pick-11789-to-master-to-upstream
ancref=$(git merge-base a6d647566991e339ea5126113df4bef21fe0115d 6cb08ab74d3da794493649eaeaf525cf4503edcc)
git cherry-pick -x $ancref..6cb08ab74d3da794493649eaeaf525cf4503edcc

@team-gateway-bot
Copy link
Collaborator

Validation Failed: {"resource":"PullRequest","code":"custom","message":"No commits between master and cherry-pick-11789-to-master-to-upstream"}

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

Successfully merging this pull request may close these issues.

7 participants