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

Introduce table for session management #136

Merged
merged 2 commits into from
May 29, 2024
Merged

Conversation

ncosta-ic
Copy link
Member

This feature introduces a new session table which is needed by the icinga-notifications-web project.
It stores the current authenticated sessions into the table which allows the background daemon (php-based) to validate a connection and its corresponding browser to an authenticated session.

Read #118 for more information about this addition.

@cla-bot cla-bot bot added the cla/signed CLA is signed by all contributors of a PR label Nov 28, 2023
Copy link
Collaborator

@julianbrost julianbrost left a comment

Choose a reason for hiding this comment

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

Please have a look at the code style / indentation in the rest of the file and keep the additions consistent with that.

schema/pgsql/schema.sql Outdated Show resolved Hide resolved
Comment on lines 290 to 292
id VARCHAR(256) NOT NULL,
username VARCHAR(254) NOT NULL,
device_id VARCHAR(8) NOT NULL,
Copy link
Collaborator

Choose a reason for hiding this comment

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

What will id and device_id store? I can't really make sense of the data types here.

Copy link
Member Author

@ncosta-ic ncosta-ic Nov 28, 2023

Choose a reason for hiding this comment

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

php_session_id stores the session identificator generated by php. We use VARCHAR(256) here as that's the maximum number of allowed characters stated by PHP's documentation.

username's type was defined by double-checking the existing username table definitions found in icingaweb2/schema/pgsql.schema.sql.

device_id stores a device fingerprint generated by creating a jooat hash which consinsts of the authenticated username, concatenated by the full user-agent string. Jooat hashes have a maximum length of 8 bytes, hence the limitation.

Copy link
Collaborator

Choose a reason for hiding this comment

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

This comment is now pretty much a "I think it's strange but if you want it, you get it" as per #118 (comment).

For username, I find it a bit weird that it uses a different type here compared to contact.username. Though that one might be subject to a change to citext in #71 based on feedback from @nilmerg on how Icinga Web handles usernames.

device_id stores a device fingerprint generated by creating a jooat hash which consinsts of the authenticated username, concatenated by the full user-agent string. Jooat hashes have a maximum length of 8 bytes, hence the limitation.

uint32_t vs varchar(8), sounds like you want to store an int value as a hex string? And that hash doesn't really sound like it would give something that would qualify as device ID: if I use the same browser on the same OS on two devices, this should give the same device ID whereas if I update my browser on one device, that would change the device ID.

Copy link
Member Author

@ncosta-ic ncosta-ic Nov 29, 2023

Choose a reason for hiding this comment

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

username uses the type definition from icingaweb2/schema/pgsql.schema as this is the actual type representation of the username that's being passed in the AuthenticationHook parameters. As we're storing the session and its corresponding user in that specific hook, we also want to use the same type definition (I'd say).

You are right with the uint32_t that's used as a return value in jenkins' one at a time hash. We are working with PHP however and a call to hash() returns lowercased hexits by default. It most certainly makes sense to store the uint representation of that value but shouldn't the database reflect the data representation that's also getting used in comparisons and such?
Because in the written PHP code, it will always be compared in its hexadecimal form.

You have a fair point in terms of the device_id column naming convention. It essentially fingerprints a specific browser and its authenticated user. It doesn't really care about the underlying device.
I chose that prefix as other features in the icinga2 suite which also use the user agent string, already refer to it as a device, e.g: icingaweb2/library/Icinga/Web/RememberMeUserDevicesList.php

Copy link
Member Author

Choose a reason for hiding this comment

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

@julianbrost updated the code to include your requested changes.
Is this fine for you now?

schema/pgsql/schema.sql Outdated Show resolved Hide resolved
schema/pgsql/schema.sql Outdated Show resolved Hide resolved
@ncosta-ic
Copy link
Member Author

Please have a look at the code style / indentation in the rest of the file and keep the additions consistent with that.

Yeah, just noticed it as well. It somehow auto-formatted my modifications when I committed the changes. Should be fixed now.

schema/pgsql/schema.sql Outdated Show resolved Hide resolved
schema/pgsql/upgrades/019.sql Outdated Show resolved Hide resolved
schema/pgsql/schema.sql Outdated Show resolved Hide resolved
Comment on lines 290 to 292
id VARCHAR(256) NOT NULL,
username VARCHAR(254) NOT NULL,
device_id VARCHAR(8) NOT NULL,
Copy link
Collaborator

Choose a reason for hiding this comment

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

This comment is now pretty much a "I think it's strange but if you want it, you get it" as per #118 (comment).

For username, I find it a bit weird that it uses a different type here compared to contact.username. Though that one might be subject to a change to citext in #71 based on feedback from @nilmerg on how Icinga Web handles usernames.

device_id stores a device fingerprint generated by creating a jooat hash which consinsts of the authenticated username, concatenated by the full user-agent string. Jooat hashes have a maximum length of 8 bytes, hence the limitation.

uint32_t vs varchar(8), sounds like you want to store an int value as a hex string? And that hash doesn't really sound like it would give something that would qualify as device ID: if I use the same browser on the same OS on two devices, this should give the same device ID whereas if I update my browser on one device, that would change the device ID.

julianbrost
julianbrost previously approved these changes Nov 29, 2023
@julianbrost
Copy link
Collaborator

FYI: I'd wait a bit with merging this until Icinga/icinga-notifications-web#146 seems to get close to finalizing.

@nilmerg
Copy link
Member

nilmerg commented May 28, 2024

It's ready. This isn't, anymore, it seems.

@ncosta-ic ncosta-ic force-pushed the add-sessions-table branch from 735bcb2 to 61c4524 Compare May 29, 2024 08:53
@ncosta-ic
Copy link
Member Author

@nilmerg @julianbrost I rebased it to the current main branch and fixed the colliding file name. It's now ready to be merged as Icinga/icinga-notifications-web#146 waits to be merged and requires this change.

@ncosta-ic ncosta-ic force-pushed the add-sessions-table branch from 61c4524 to 0e3e780 Compare May 29, 2024 08:56
Mainly adapt spacing and character case to be consistent with the rest. Also do
the CREATE EXTENSION at the top of the schema file, it may be used elsewhere in
the future as well. One functional change: rename the primary key constraint to
be consistent with pk_${table_name} used in the rest of the file.
@julianbrost
Copy link
Collaborator

I've pushed a commit that makes the overall style more consistent with the rest of the schema files.

There's one functional change: I renamed the primary key constraint to pk_browser_session so that it is consistent with pk_${table_name} used in the rest of the file. @ncosta-ic Is that fine for you or do you explicitly refer to it by name somewhere? (Our Go code does when generating upsert statements and uses that to generate the constraint name by default. Not super important as that table isn't intended to be used from Go, but I'd still prefer it to be consistent with the rest.)

@ncosta-ic
Copy link
Member Author

ncosta-ic commented May 29, 2024

@julianbrost Consistency is always good, thanks for the fixup. The renaming should not interfere with our code as we do not refer to the primary key constraint (for now, at least).

@julianbrost julianbrost merged commit b6b4a75 into main May 29, 2024
12 checks passed
@julianbrost julianbrost deleted the add-sessions-table branch May 29, 2024 09:42
ncosta-ic added a commit to Icinga/icinga-notifications-web that referenced this pull request Jun 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla/signed CLA is signed by all contributors of a PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Additional session table in the notifications database
3 participants