-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SUSTAIN-632] Do not update the users table to contain the public key.
The keys table should be the only source of truth New insert/updates to the users table will insert a sentinel value in the public_key column. The old values are left as is. Triggers will act only on the values in the users.public_key that are not the sentinel value. Signed-off-by: Prajakta Purohit <[email protected]>
- Loading branch information
1 parent
3c916eb
commit a1496c5
Showing
16 changed files
with
509 additions
and
70 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
omnibus/files/private-chef-upgrades/001/034_create_and_update_user.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
define_upgrade do | ||
|
||
if Partybus.config.bootstrap_server | ||
must_be_data_master | ||
|
||
# schema update no longer insert public_key in users table. | ||
# the keys table is the only source of truth | ||
run_sqitch('@sentinel_public_key_for_users', 'oc_erchef') | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
src/oc_erchef/schema/deploy/create_and_update_users.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
-- Deploy enterprise_chef:create_and_update_users to pg | ||
-- Inserts a sentinel value into the public_key field of a user table. | ||
-- Ensures keys table is the only source of truth for public_key | ||
|
||
BEGIN; | ||
|
||
DROP FUNCTION If EXISTS add_user(character,character,text,text,text,integer,text,text,password_hash_type,character,timestamp without time zone,timestamp without time zone,text,boolean,text,boolean); | ||
|
||
CREATE OR REPLACE FUNCTION add_user(p_id users.id%TYPE, | ||
p_authz_id users.authz_id%TYPE, | ||
p_username users.username%TYPE, | ||
p_email users.email%TYPE, | ||
p_public_key users.public_key%TYPE, | ||
p_pubkey_version users.pubkey_version%TYPE, | ||
p_hashed_password users.hashed_password%TYPE, | ||
p_salt users.salt%TYPE, | ||
p_hash_type users.hash_type%TYPE, | ||
p_last_updated_by users.last_updated_by%TYPE, | ||
p_created_at users.created_at%TYPE, | ||
p_updated_at users.updated_at%TYPE, | ||
p_external_authentication_uid users.external_authentication_uid%TYPE, | ||
p_recovery_authentication_enabled users.recovery_authentication_enabled%TYPE, | ||
p_serialized_object users.serialized_object%TYPE, | ||
p_admin users.admin%TYPE) RETURNS integer AS $$ | ||
DECLARE | ||
inserteduser integer; | ||
BEGIN | ||
-- Need this insert despite of the trigger since the trigger updates the keys table | ||
-- from the value inserted in the users table. But we insert sentinel in the users table. | ||
IF p_public_key != 'this_is_not_a_key' THEN | ||
INSERT INTO keys | ||
(id, key_name, public_key, key_version, created_at, expires_at) | ||
VALUES | ||
(p_id, 'default', p_public_key, p_pubkey_version, now(), 'infinity'::timestamp); | ||
END IF; | ||
WITH createduser AS | ||
(INSERT INTO users | ||
(id, authz_id, username, email, public_key, hashed_password, salt, hash_type, | ||
last_updated_by, created_at, updated_at, external_authentication_uid, | ||
recovery_authentication_enabled, serialized_object, admin, | ||
pubkey_version) | ||
VALUES (p_id, p_authz_id, p_username, p_email, 'this_is_not_a_key', | ||
p_hashed_password, p_salt, p_hash_type, p_last_updated_by, p_created_at, p_updated_at, | ||
p_external_authentication_uid, p_recovery_authentication_enabled, | ||
p_serialized_object, p_admin, p_pubkey_version) RETURNING 1) | ||
SELECT count(*) FROM createduser INTO inserteduser; | ||
RETURN inserteduser; | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
|
||
DROP FUNCTION IF EXISTS update_user(integer,text,text,text,password_hash_type,text,text,boolean,text,text,character,timestamp without time zone,boolean,character); | ||
|
||
CREATE OR REPLACE FUNCTION update_user(p_pubkey_version users.pubkey_version%TYPE, | ||
p_public_key users.public_key%TYPE, | ||
p_hashed_password users.hashed_password%TYPE, | ||
p_salt users.salt%TYPE, | ||
p_hash_type users.hash_type%TYPE, | ||
p_serialized_object users.serialized_object%TYPE, | ||
p_external_authentication_uid users.external_authentication_uid%TYPE, | ||
p_recovery_authentication_enabled users.recovery_authentication_enabled%TYPE, | ||
p_email users.email%TYPE, | ||
p_username users.username%TYPE, | ||
p_last_updated_by users.last_updated_by%TYPE, | ||
p_updated_at users.updated_at%TYPE, | ||
p_admin users.admin%TYPE, | ||
p_id character(32)) RETURNS integer AS $$ | ||
DECLARE | ||
updateduser integer; | ||
BEGIN | ||
IF p_public_key IS NULL THEN | ||
DELETE FROM keys WHERE id = p_id AND key_name = 'default'; | ||
END IF; | ||
-- Need this insert despite of the trigger since the trigger updates the keys table | ||
-- from the value inserted in the users table. But we insert sentinel in the users table. | ||
IF p_public_key != 'this_is_not_a_key' THEN | ||
UPDATE keys SET public_key = p_public_key, | ||
key_version = p_pubkey_version, | ||
expires_at = 'infinity'::timestamp | ||
WHERE id = p_id AND key_name = 'default'; | ||
INSERT INTO keys (id, key_name, public_key, key_version, created_at, expires_at) | ||
SELECT p_id, 'default', p_public_key, p_pubkey_version, now(), 'infinity'::timestamp | ||
WHERE NOT EXISTS (SELECT 1 FROM keys WHERE id = p_id AND key_name = 'default'); | ||
END IF; | ||
WITH changeduser AS | ||
(UPDATE users SET | ||
username = p_username, | ||
email = p_email, | ||
public_key = 'this_is_not_a_key', | ||
hashed_password = p_hashed_password, | ||
salt = p_salt, | ||
hash_type = p_hash_type, | ||
last_updated_by = p_last_updated_by, | ||
updated_at = p_updated_at, | ||
external_authentication_uid = p_external_authentication_uid, | ||
recovery_authentication_enabled = p_recovery_authentication_enabled, | ||
serialized_object = p_serialized_object, | ||
admin = p_admin, | ||
pubkey_version = p_pubkey_version | ||
WHERE id = p_id RETURNING 1) | ||
SELECT count(*) FROM changeduser INTO updateduser; | ||
RETURN updateduser; | ||
END | ||
$$ LANGUAGE plpgsql; | ||
|
||
COMMIT; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/oc_erchef/schema/deploy/keys_update_trigger@users_email_functional_index.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
-- Deploy keys_update_trigger | ||
-- | ||
-- This keeps the keys table consistent when the clients and user tables update their keys | ||
-- | ||
BEGIN; | ||
|
||
CREATE OR REPLACE FUNCTION add_key() RETURNS TRIGGER AS $add_key$ | ||
BEGIN | ||
IF NEW.public_key IS NOT NULL THEN | ||
INSERT INTO keys | ||
(id, key_name, public_key, key_version, created_at, expires_at) VALUES | ||
(NEW.id, 'default', NEW.public_key, NEW.pubkey_version, now(), 'infinity'::timestamp); | ||
END IF; | ||
RETURN NULL; -- result is ignored since this is an AFTER trigger | ||
END; | ||
$add_key$ LANGUAGE plpgsql; | ||
|
||
CREATE OR REPLACE FUNCTION update_key() RETURNS TRIGGER AS $update_key$ | ||
BEGIN | ||
IF NEW.public_key IS NOT NULL THEN | ||
UPDATE keys SET public_key = NEW.public_key, key_version = NEW.pubkey_version, expires_at = 'infinity'::timestamp | ||
WHERE id = NEW.id AND key_name = 'default'; | ||
INSERT INTO keys (id, key_name, public_key, key_version, created_at, expires_at) | ||
SELECT NEW.id, 'default', NEW.public_key, NEW.pubkey_version, now(), 'infinity'::timestamp | ||
WHERE NOT EXISTS (SELECT 1 FROM keys WHERE id = NEW.id AND key_name = 'default'); | ||
ELSE | ||
DELETE FROM keys WHERE id = NEW.id AND key_name = 'default'; | ||
END IF; | ||
RETURN NULL; -- result is ignored since this is an AFTER trigger | ||
END; | ||
$update_key$ LANGUAGE plpgsql; | ||
|
||
DROP TRIGGER IF EXISTS add_key ON clients; | ||
DROP TRIGGER IF EXISTS update_key ON clients; | ||
|
||
DROP TRIGGER IF EXISTS add_key ON users; | ||
DROP TRIGGER IF EXISTS update_key ON users; | ||
|
||
CREATE TRIGGER add_key AFTER INSERT ON clients FOR EACH ROW EXECUTE PROCEDURE add_key(); | ||
CREATE TRIGGER update_key AFTER UPDATE ON clients FOR EACH ROW EXECUTE PROCEDURE update_key(); | ||
|
||
CREATE TRIGGER add_key AFTER INSERT ON users FOR EACH ROW EXECUTE PROCEDURE add_key(); | ||
CREATE TRIGGER update_key AFTER UPDATE ON users FOR EACH ROW EXECUTE PROCEDURE update_key(); | ||
|
||
COMMIT; |
Oops, something went wrong.