Skip to content

Add migration to fix signature keys inconsistency#11113

Merged
mshaposhnik merged 5 commits intomasterfrom
keysFixMigration
Sep 10, 2018
Merged

Add migration to fix signature keys inconsistency#11113
mshaposhnik merged 5 commits intomasterfrom
keysFixMigration

Conversation

@mshaposhnik
Copy link
Copy Markdown
Contributor

What does this PR do?

Adds migration that makes sure there is all workspace public & private pairs exists, check that all workspaces have unique key pairs, and adds appropriate constraints to the DB schema.

What issues does this PR fix or reference?

#11087

Release Notes

N/A

Docs PR

N/A

@mshaposhnik mshaposhnik changed the title Add migration to fix signature keys incosistency Add migration to fix signature keys inconsistency Sep 7, 2018
@benoitf benoitf added status/code-review This issue has a pull request posted for it and is awaiting code review completion by the community. kind/bug Outline of a bug - must adhere to the bug report template. labels Sep 7, 2018
Copy link
Copy Markdown
Member

@sleshchenko sleshchenko left a comment

Choose a reason for hiding this comment

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

Take a look my inlined comments



-- remove key pair records which linked to non-existing keys
DELETE FROM che_sign_key_pair kp
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can't it be simplified like the following?

DELETE FROM che_sign_key_pair kp
WHERE NOT EXISTS (
   SELECT id
   FROM   che_sign_key k
   WHERE  k.id = kp.private_key OR k.id = kp.public_key
   )

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Nope. That query will work only if both private_ and public_ key not exists.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I understand. Thanks.

There is a possible case when key par A exists, and it has public key but not private one. Key pair is going to be removed. But private key won't be removed. As result adding foreign key will fail. Am I right? If yes, please consider adding cleaning private and public keys which are referencing non-existing key pair.

Copy link
Copy Markdown
Contributor Author

@mshaposhnik mshaposhnik Sep 7, 2018

Choose a reason for hiding this comment

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

I don't see reason why foreign key shoud fail? After that query executed there will be correct data with both keys exists. But i agree that if one key is exists and other is not, the existing key can be left in DB forewer. Need to think is it can became a problem.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't see reason why foreign key shoud fail?

You're right. I missed that foreign keys are from pair to keys but not wise versa.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added extra unused keys cleanup query

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

CREATE UNIQUE INDEX index_sign_public_private_key_id ON che_sign_key_pair (public_key, private_key);

-- add keys table constraints
ALTER TABLE che_sign_key_pair ADD CONSTRAINT fk_sign_public_key_id FOREIGN KEY (public_key) REFERENCES che_sign_key (id);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Make sure that there are indexes for fk_sign_public_key_id and fk_sign_private_key_id

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Didn't get the idea. Explain, pls.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please make sure that

--indexes
CREATE INDEX index_sign_public_key_id ON che_sign_key_pair (public_key);
CREATE INDEX index_sign_private_key_id ON che_sign_key_pair (private_key);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

What is the reason of adding that? We never do seatch on that fields, and i'ts seems not needed for the FK constraint.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We never do seatch on that fields

I would say that we don't do such searches explicitly. But I think JPA uses such search to fetch keys for a requested pair.

We have to add indexes on foreign keys, and we do it like here https://github.com/eclipse/che/blob/6c53555fded7621014501ab44d2c4bbca869b4db/wsmaster/che-core-sql-schema/src/main/resources/che-schema/5.8.0/1__add_foreigh_key_indexes.sql#L18-L17

Correct me If I'm wrong.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ok, i didn't remember the reason, but seems it's needed for JPA. Will add separate indexes.

WHERE (
SELECT count(*) FROM che_sign_key_pair kp2
WHERE kp1.private_key = kp2.private_key
OR kp1.private_key = kp2.public_key
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I guess you wanted to write kp1.public_key = kp2.public_key here

@skabashnyuk
Copy link
Copy Markdown
Contributor

ci-test

@riuvshin
Copy link
Copy Markdown
Contributor

riuvshin commented Sep 7, 2018

ci-test build report:
Build details
Test report
selenium tests report data
docker image: eclipseche/che-server:11113
https://github.com/orgs/eclipse/teams/eclipse-che-qa please check this report.

@skabashnyuk
Copy link
Copy Markdown
Contributor

ci-test

@riuvshin
Copy link
Copy Markdown
Contributor

riuvshin commented Sep 8, 2018

ci-test build report:
Build details
Test report
selenium tests report data
docker image: eclipseche/che-server:11113
https://github.com/orgs/eclipse/teams/eclipse-che-qa please check this report.

@mshaposhnik mshaposhnik merged commit b237197 into master Sep 10, 2018
@benoitf benoitf removed the status/code-review This issue has a pull request posted for it and is awaiting code review completion by the community. label Sep 10, 2018
@benoitf benoitf added this to the 6.11.0 milestone Sep 10, 2018
@mshaposhnik mshaposhnik deleted the keysFixMigration branch November 29, 2018 08:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bug Outline of a bug - must adhere to the bug report template.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants