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

Create user in envtool for PostgreSQL #4133

Closed
wants to merge 24 commits into from

Conversation

chilagrow
Copy link
Contributor

@chilagrow chilagrow commented Feb 29, 2024

Description

To test it, I added --test-enable-new-auth flag in task file run: and run-secured:.
It does not create user for SQLite, that will be handled in a separate PR.

Closes #1877.

Readiness checklist

  • I added/updated unit tests (and they pass).
  • I added/updated integration/compatibility tests (and they pass).
  • I added/updated comments and checked rendering.
  • I made spot refactorings.
  • I updated user documentation.
  • I ran task all, and it passed.
  • I ensured that PR title is good enough for the changelog.
  • (for maintainers only) I set Reviewers (@FerretDB/core), Milestone (Next), Labels, Project and project's Sprint fields.
  • I marked all done items in this checklist.

Copy link

codecov bot commented Feb 29, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 73.80%. Comparing base (5a5ca29) to head (0b57bbe).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #4133      +/-   ##
==========================================
- Coverage   74.31%   73.80%   -0.51%     
==========================================
  Files         353      353              
  Lines       22738    22739       +1     
==========================================
- Hits        16897    16783     -114     
- Misses       4552     4686     +134     
+ Partials     1289     1270      -19     
Files Coverage Δ
integration/setup/setup.go 81.25% <100.00%> (ø)
internal/handler/commands.go 100.00% <100.00%> (ø)

... and 12 files with indirect coverage changes

Flag Coverage Δ
filter-true 67.51% <100.00%> (-0.31%) ⬇️
hana-1 ?
integration 67.51% <100.00%> (-0.31%) ⬇️
mongodb-1 4.75% <25.00%> (+<0.01%) ⬆️
postgresql-1 48.72% <100.00%> (+0.53%) ⬆️
postgresql-2 50.07% <100.00%> (+1.48%) ⬆️
postgresql-3 48.07% <100.00%> (-1.02%) ⬇️
sqlite-1 47.66% <75.00%> (+0.32%) ⬆️
sqlite-2 48.99% <75.00%> (+1.21%) ⬆️
sqlite-3 47.47% <75.00%> (-0.71%) ⬇️
unit 32.59% <100.00%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

//
// Without this, once the first user is created, the authentication fails
// as username/password does not exist in admin.system.users collection.
func setupUser(ctx context.Context, logger *zap.SugaredLogger, postgreSQLPort uint16) error {
Copy link
Contributor Author

@chilagrow chilagrow Feb 29, 2024

Choose a reason for hiding this comment

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

This does not replace setupUser() in integration test.
Because this function creates user in ferretdb database which postgreSQL schema, postgres://username@127.0.0.1:5432/ferretdb?pool_max_conns=50.

Each integration test create its own schema like so, postgres://username@127.0.0.1:5432/testauthentication?pool_max_conns=50.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for the clarification! I was worried this could jeopardize running tests directly with go test for a moment 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's still there for SQLite. It only has handler level authentication, and once the first user is created without this code, auth would fail.

It will be handled in a separate PR.

@@ -419,7 +419,7 @@ tasks:
--proxy-addr=127.0.0.1:47017
--mode=diff-normal
--handler=pg
--postgresql-url='postgres://127.0.0.1:5433/ferretdb?search_path='
--postgresql-url='postgres://username:password@127.0.0.1:5433/ferretdb?search_path='
Copy link
Contributor Author

Choose a reason for hiding this comment

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

For run-secured to work, what other better thing can I do?

Copy link
Member

Choose a reason for hiding this comment

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

That target was supposed to be for the "old" authentication that does not need credentials in the PostgreSQL URI. Why are they needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's because postgres started on port 5433 is created from postgres_secured docker https://github.com/FerretDB/FerretDB/blob/main/docker-compose.yml#L28.
Unlike postgres docker which has POSTGRES_HOST_AUTH_METHOD=trust, postgres_secured doesn't have trust so username/password is required to connect to it.

@chilagrow chilagrow added the code/chore Code maintenance improvements label Feb 29, 2024
@@ -27,12 +33,55 @@ import (
func TestCommandsAuthenticationLogout(t *testing.T) {
Copy link
Contributor Author

@chilagrow chilagrow Mar 1, 2024

Choose a reason for hiding this comment

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

Logout does not fail on authentication check, if user is not authenticated, it just returns success.
This test was updated to use newly created user, because if it uses default username/password user and logs out in this test, t.Cleanup to drop collection fails due to not authenticate.

@@ -334,7 +334,8 @@ func insertBenchmarkProvider(tb testtb.TB, ctx context.Context, collection *mong
func setupUser(tb testtb.TB, ctx context.Context, client *mongo.Client) {
tb.Helper()

if IsMongoDB(tb) {
if IsPostgreSQL(tb) || IsMongoDB(tb) {
// the user is created in env tool for PostgreSQL
Copy link
Contributor Author

Choose a reason for hiding this comment

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

For SQLite we need something similar to have a database with default user. Will be handle in a separate PR.

Copy link
Contributor

@henvic henvic Mar 1, 2024

Choose a reason for hiding this comment

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

And also for MySQL.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes that's true. MySQL hasn't been handled. let me see the status of MySQL backend

Copy link
Member

Choose a reason for hiding this comment

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

MySQL and HANA backends are not yet supported, no need to do anything for them

@chilagrow chilagrow marked this pull request as ready for review March 1, 2024 07:42
@chilagrow chilagrow requested review from AlekSi and a team as code owners March 1, 2024 07:42
@chilagrow chilagrow requested review from rumyantseva, a team, henvic and noisersup March 1, 2024 07:42
henvic
henvic previously approved these changes Mar 1, 2024
@AlekSi AlekSi changed the title Create user in env tool for PostgreSQL Create user in envtool for PostgreSQL Mar 1, 2024
@chilagrow chilagrow marked this pull request as draft March 1, 2024 09:48
auto-merge was automatically disabled March 1, 2024 09:48

Pull request was converted to draft

@chilagrow chilagrow marked this pull request as ready for review March 5, 2024 04:09
@chilagrow chilagrow enabled auto-merge (squash) March 5, 2024 04:13
@chilagrow chilagrow requested a review from henvic March 5, 2024 05:33
cmd/envtool/user.json Outdated Show resolved Hide resolved
henvic
henvic previously approved these changes Mar 5, 2024
henvic
henvic previously approved these changes Mar 5, 2024
@chilagrow chilagrow mentioned this pull request Mar 6, 2024
9 tasks
Copy link
Member

@rumyantseva rumyantseva left a comment

Choose a reason for hiding this comment

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

LGTM

@chilagrow chilagrow requested a review from henvic March 6, 2024 09:31
@@ -419,7 +419,7 @@ tasks:
--proxy-addr=127.0.0.1:47017
--mode=diff-normal
--handler=pg
--postgresql-url='postgres://127.0.0.1:5433/ferretdb?search_path='
--postgresql-url='postgres://username:password@127.0.0.1:5433/ferretdb?search_path='
Copy link
Member

Choose a reason for hiding this comment

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

That target was supposed to be for the "old" authentication that does not need credentials in the PostgreSQL URI. Why are they needed?

Comment on lines +181 to +186
err = setupUserInPostgres(ctx, logger, "postgres://username:password@127.0.0.1:5433/ferretdb")
if err != nil {
return err
}

return setupUserInPostgres(ctx, logger, "postgres://username:password@127.0.0.1:5433/template1")
Copy link
Member

Choose a reason for hiding this comment

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

I don't understand why those calls are not inside setupAnyPostgres and what we are doing with credentials there

Comment on lines +300 to +304
if _, err = dbPool.Exec(ctx, q); err != nil && (!errors.As(err, &pgErr) || pgErr.Code != pgerrcode.DuplicateSchema) {
return err
}

if err == nil {
Copy link
Member

Choose a reason for hiding this comment

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

I think we could simply that by:

if errors.As(err, &pgErr) && pgErr.Code == pgerrcode.DuplicateSchema {
  err = nil
}
if err != nil {
  return err
}

}

if err == nil {
q = `CREATE UNIQUE INDEX "system_users_aff2f7ce__id__67399184_idx" ON admin.system_users_aff2f7ce (((_jsonb->'_id')))`
Copy link
Member

Choose a reason for hiding this comment

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

Yikes!..
I wonder if we should step away from that and just implement the localhost exception that we are already working on.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes unfortunately this is something needed to be exposed.
It was copy and paste from log output, logic is too difficult to copy accurately 🙈

You know what, let me leave this PR for now and progress with local host exception.

@@ -334,7 +334,8 @@ func insertBenchmarkProvider(tb testtb.TB, ctx context.Context, collection *mong
func setupUser(tb testtb.TB, ctx context.Context, client *mongo.Client) {
tb.Helper()

if IsMongoDB(tb) {
if IsPostgreSQL(tb) || IsMongoDB(tb) {
// the user is created in env tool for PostgreSQL
Copy link
Member

Choose a reason for hiding this comment

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

MySQL and HANA backends are not yet supported, no need to do anything for them

@chilagrow chilagrow mentioned this pull request Mar 7, 2024
9 tasks
@chilagrow chilagrow closed this Mar 7, 2024
auto-merge was automatically disabled March 7, 2024 09:50

Pull request was closed

Handler: h.MsgLogout,
Help: "Logs out from the current session.",
Handler: h.MsgLogout,
anonymous: true,
Copy link
Member

Choose a reason for hiding this comment

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

Let's not forget to port fixes from that PR

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks yep that has been included in #4156

@AlekSi AlekSi removed this from the Next milestone Mar 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code/chore Code maintenance improvements
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Enforce new authentication
4 participants