Skip to content

feat(backend): add postgres initialization #9798

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

Merged
merged 24 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions backend/src/apiserver/client/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,22 @@
package client

import (
"bytes"
"fmt"

"github.com/go-sql-driver/mysql"
)

func CreateMySQLConfig(user, password string, mysqlServiceHost string,
mysqlServicePort string, dbName string, mysqlGroupConcatMaxLen string, mysqlExtraParams map[string]string,
const (
MYSQL_TEXT_FORMAT string = "longtext not null"
MYSQL_EXIST_ERROR string = "database exists"

PGX_TEXT_FORMAT string = "text"
PGX_EXIST_ERROR string = "already exists"
)

func CreateMySQLConfig(user, password, mysqlServiceHost, mysqlServicePort,
dbName, mysqlGroupConcatMaxLen string, mysqlExtraParams map[string]string,
) *mysql.Config {
params := map[string]string{
"charset": "utf8",
Expand All @@ -44,3 +53,26 @@ func CreateMySQLConfig(user, password string, mysqlServiceHost string,
AllowNativePasswords: true,
}
}

func CreatePostgreSQLConfig(user, password, postgresHost, dbName string, postgresPort uint16,
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you plan to introduce sslmode for security support in the coming PRs? It is important to make sure we are using secure channel when reading/writing to DB.

Reference: https://github.com/google/ml-metadata/blob/master/ml_metadata/proto/metadata_store.proto#L714-L743

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it would be great to improve security, but I wonder if it is necessary, since the DB and the backend are hosted in the same cluster.

Copy link
Contributor

Choose a reason for hiding this comment

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

This allows scenario like connecting to a managed database service. We can discuss about this in future implementation.

) string {
var b bytes.Buffer
if dbName != "" {
fmt.Fprintf(&b, "database=%s ", dbName)
}
if user != "" {
fmt.Fprintf(&b, "user=%s ", user)
}
if password != "" {
fmt.Fprintf(&b, "password=%s ", password)
}
Comment on lines +66 to +68
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

passfile sounds like a great way to enhance security. Could you tell me more about it? My guess is this will allow users to pass a file containing the password as part of the deployment config. Maybe this can be added together with other configs users can set.

Copy link
Contributor

Choose a reason for hiding this comment

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

That is right! So the passfile identify the location path to a password, so you don't need to expose the secret via deployment. https://www.postgresql.org/docs/current/libpq-pgpass.html When passing this parameter to postgresql, the password field can be omitted.

We can add this configuration in the coming PR, if this makes sense to you.

if postgresHost != "" {
fmt.Fprintf(&b, "host=%s ", postgresHost)
}
Comment on lines +69 to +71
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wonder if this is necessary, since the host address here is simply for communications inside the cluster. Could you tell me more why this is beneficial? Thank you!

Copy link
Contributor

@zijianjoy zijianjoy Aug 8, 2023

Choose a reason for hiding this comment

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

Sure thing, hostaddr is helpful when we are connecting to external database, for example: CloudSQL. Yes we can use host which is handy for Unix-domain communication, but hostaddr allows postgresql client to make TCP/IP communication instead.

Again, we can implement this feature in future PR.

if postgresPort != 0 {
fmt.Fprintf(&b, "port=%d ", postgresPort)
}
fmt.Fprint(&b, "sslmode=disable")

return b.String()
}
Loading