-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Add option to customize admin schema for PostgreSQL auto user provisioning #43338
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -38,15 +38,23 @@ import ( | |||||
) | ||||||
|
||||||
// connectAsAdmin connect to the database from db route as admin user. | ||||||
// If useDefaultDatabase is true and a default database is configured for the admin user, it will be used instead. | ||||||
func (e *Engine) connectAsAdmin(ctx context.Context, sessionCtx *common.Session, useDefaultDatabase bool) (*pgx.Conn, error) { | ||||||
// If useDefaultValues is true and a default values are configured for the admin user, they will be used instead. | ||||||
func (e *Engine) connectAsAdmin(ctx context.Context, sessionCtx *common.Session, useDefaultValues bool) (*pgx.Conn, error) { | ||||||
loginSchema := "" | ||||||
loginDatabase := sessionCtx.DatabaseName | ||||||
if useDefaultDatabase && sessionCtx.Database.GetAdminUser().DefaultDatabase != "" { | ||||||
loginDatabase = sessionCtx.Database.GetAdminUser().DefaultDatabase | ||||||
} else { | ||||||
e.Log.WithField("database", loginDatabase).Info("Connecting to session database") | ||||||
if useDefaultValues { | ||||||
if sessionCtx.Database.GetAdminUser().DefaultDatabase != "" { | ||||||
loginDatabase = sessionCtx.Database.GetAdminUser().DefaultDatabase | ||||||
} else { | ||||||
e.Log.WithField("database", loginDatabase).Info("Connecting to session database") | ||||||
} | ||||||
|
||||||
if sessionCtx.Database.GetAdminUser().DefaultSchema != "" { | ||||||
loginSchema = sessionCtx.Database.GetAdminUser().DefaultSchema | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we log this schema the above log |
||||||
} | ||||||
} | ||||||
conn, err := e.pgxConnect(ctx, sessionCtx.WithUserAndDatabase(sessionCtx.Database.GetAdminUser().Name, loginDatabase)) | ||||||
|
||||||
conn, err := e.pgxConnect(ctx, sessionCtx.WithUserAndDatabase(sessionCtx.Database.GetAdminUser().Name, loginDatabase), loginSchema) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
What do you think if we update the sessionCtx with runtime params needed for admin instead of changing |
||||||
return conn, trace.Wrap(err) | ||||||
} | ||||||
|
||||||
|
@@ -408,15 +416,25 @@ func (e *Engine) initAutoUsers(ctx context.Context, sessionCtx *common.Session, | |||||
|
||||||
// pgxConnect connects to the database using pgx driver which is higher-level | ||||||
// than pgconn and is easier to use for executing queries. | ||||||
func (e *Engine) pgxConnect(ctx context.Context, sessionCtx *common.Session) (*pgx.Conn, error) { | ||||||
func (e *Engine) pgxConnect(ctx context.Context, sessionCtx *common.Session, searchPath string) (*pgx.Conn, error) { | ||||||
config, err := e.getConnectConfig(ctx, sessionCtx) | ||||||
if err != nil { | ||||||
return nil, trace.Wrap(err) | ||||||
} | ||||||
|
||||||
// Always reset runtime params to avoid using user provided flags to admin | ||||||
// connections. | ||||||
config.RuntimeParams = map[string]string{} | ||||||
|
||||||
if searchPath != "" { | ||||||
config.RuntimeParams[searchPathParam] = searchPath | ||||||
} | ||||||
|
||||||
pgxConf, err := pgx.ParseConfig("") | ||||||
if err != nil { | ||||||
return nil, trace.Wrap(err) | ||||||
} | ||||||
|
||||||
pgxConf.Config = *config | ||||||
return pgx.ConnectConfig(ctx, pgxConf) | ||||||
} | ||||||
|
@@ -459,6 +477,13 @@ func pickProcedures(sessionCtx *common.Session) map[string]string { | |||||
return procs | ||||||
} | ||||||
|
||||||
const ( | ||||||
// searchPathParam is the name of the startup parameter used to set the | ||||||
// connection `search_path`. | ||||||
// https://www.postgresql.org/docs/16/ddl-schemas.html#DDL-SCHEMAS-PATH | ||||||
searchPathParam = "search_path" | ||||||
) | ||||||
|
||||||
const ( | ||||||
// activateProcName is the name of the stored procedure Teleport will use | ||||||
// to automatically provision/activate database users. | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.