Skip to content

Commit

Permalink
Support unix_socket option of database (#623)
Browse files Browse the repository at this point in the history
* Support unix_socket option of database

* Set default auth plugin when use unix_socket

---------

Co-authored-by: 杨赫然 <[email protected]>
  • Loading branch information
feiniks and 杨赫然 authored Jun 27, 2023
1 parent a978b24 commit 864cef1
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 35 deletions.
8 changes: 8 additions & 0 deletions common/seaf-db.c
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,14 @@ mysql_db_get_connection (SeafDB *vdb)
if (db->charset)
mysql_options(db_conn, MYSQL_SET_CHARSET_NAME, db->charset);

if (db->unix_socket) {
int pro_type = MYSQL_PROTOCOL_SOCKET;
mysql_options (db_conn, MYSQL_OPT_PROTOCOL, &pro_type);
if (!db->user) {
mysql_options (db_conn, MYSQL_DEFAULT_AUTH, "unix_socket");
}
}

mysql_options(db_conn, MYSQL_OPT_CONNECT_TIMEOUT, (const char*)&conn_timeout);
mysql_options(db_conn, MYSQL_OPT_READ_TIMEOUT, (const char*)&read_write_timeout);
mysql_options(db_conn, MYSQL_OPT_WRITE_TIMEOUT, (const char*)&read_write_timeout);
Expand Down
22 changes: 11 additions & 11 deletions common/seaf-utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,11 @@ mysql_db_start (SeafileSession *session)
int max_connections = 0;
GError *error = NULL;

unix_socket = seaf_key_file_get_string (session->config,
"database", "unix_socket", NULL);

host = seaf_key_file_get_string (session->config, "database", "host", &error);
if (!host) {
if (!host && !unix_socket) {
seaf_warning ("DB host not set in config.\n");
return -1;
}
Expand All @@ -79,13 +82,13 @@ mysql_db_start (SeafileSession *session)
}

user = seaf_key_file_get_string (session->config, "database", "user", &error);
if (!user) {
if (!user && !unix_socket) {
seaf_warning ("DB user not set in config.\n");
return -1;
}

passwd = seaf_key_file_get_string (session->config, "database", "password", &error);
if (!passwd) {
if (!passwd && !unix_socket) {
seaf_warning ("DB passwd not set in config.\n");
return -1;
}
Expand All @@ -96,9 +99,6 @@ mysql_db_start (SeafileSession *session)
return -1;
}

unix_socket = seaf_key_file_get_string (session->config,
"database", "unix_socket", NULL);

use_ssl = g_key_file_get_boolean (session->config,
"database", "use_ssl", NULL);

Expand Down Expand Up @@ -274,16 +274,18 @@ ccnet_init_mysql_database (SeafileSession *session)
user = ccnet_key_file_get_string (session->ccnet_config, "Database", "USER");
passwd = ccnet_key_file_get_string (session->ccnet_config, "Database", "PASSWD");
db = ccnet_key_file_get_string (session->ccnet_config, "Database", "DB");
unix_socket = ccnet_key_file_get_string (session->ccnet_config,
"Database", "UNIX_SOCKET");

if (!host) {
if (!host && !unix_socket) {
seaf_warning ("DB host not set in config.\n");
return -1;
}
if (!user) {
if (!user && !unix_socket) {
seaf_warning ("DB user not set in config.\n");
return -1;
}
if (!passwd) {
if (!passwd && !unix_socket) {
seaf_warning ("DB passwd not set in config.\n");
return -1;
}
Expand All @@ -299,8 +301,6 @@ ccnet_init_mysql_database (SeafileSession *session)
port = MYSQL_DEFAULT_PORT;
}

unix_socket = ccnet_key_file_get_string (session->ccnet_config,
"Database", "UNIX_SOCKET");
use_ssl = g_key_file_get_boolean (session->ccnet_config, "Database", "USE_SSL", NULL);
skip_verify = g_key_file_get_boolean (session->ccnet_config, "Database", "SKIP_VERIFY", NULL);
if (use_ssl && !skip_verify) {
Expand Down
43 changes: 27 additions & 16 deletions fileserver/fileserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,27 @@ func loadCcnetDB() {
}

if strings.EqualFold(dbEngine, "mysql") {
if key, err = section.GetKey("HOST"); err != nil {
unixSocket := ""
if key, err = section.GetKey("UNIX_SOCKET"); err == nil {
unixSocket = key.String()
}
host := ""
if key, err = section.GetKey("HOST"); err == nil {
host = key.String()
} else if unixSocket == "" {
log.Fatal("No database host in ccnet.conf.")
}
host := key.String()
// user is required.
if key, err = section.GetKey("USER"); err != nil {
log.Fatal("No database user in ccnet.conf.")
}
user := key.String()
if key, err = section.GetKey("PASSWD"); err != nil {
password := ""
if key, err = section.GetKey("PASSWD"); err == nil {
password = key.String()
} else if unixSocket == "" {
log.Fatal("No database password in ccnet.conf.")
}
password := key.String()
if key, err = section.GetKey("DB"); err != nil {
log.Fatal("No database db_name in ccnet.conf.")
}
Expand All @@ -111,10 +120,6 @@ func loadCcnetDB() {
if key, err = section.GetKey("PORT"); err == nil {
port, _ = key.Int()
}
unixSocket := ""
if key, err = section.GetKey("UNIX_SOCKET"); err == nil {
unixSocket = key.String()
}
useTLS := false
if key, err = section.GetKey("USE_SSL"); err == nil {
useTLS, _ = key.Bool()
Expand Down Expand Up @@ -191,18 +196,28 @@ func loadSeafileDB() {
dbEngine = key.String()
}
if strings.EqualFold(dbEngine, "mysql") {
if key, err = section.GetKey("host"); err != nil {
unixSocket := ""
if key, err = section.GetKey("unix_socket"); err == nil {
unixSocket = key.String()
}
host := ""
if key, err = section.GetKey("host"); err == nil {
host = key.String()
} else if unixSocket == "" {
log.Fatal("No database host in seafile.conf.")
}
host := key.String()
// user is required.
if key, err = section.GetKey("user"); err != nil {
log.Fatal("No database user in seafile.conf.")
}
user := key.String()
if key, err = section.GetKey("password"); err != nil {

password := ""
if key, err = section.GetKey("password"); err == nil {
password = key.String()
} else if unixSocket == "" {
log.Fatal("No database password in seafile.conf.")
}
password := key.String()
if key, err = section.GetKey("db_name"); err != nil {
log.Fatal("No database db_name in seafile.conf.")
}
Expand All @@ -211,10 +226,6 @@ func loadSeafileDB() {
if key, err = section.GetKey("port"); err == nil {
port, _ = key.Int()
}
unixSocket := ""
if key, err = section.GetKey("unix_socket"); err == nil {
unixSocket = key.String()
}
useTLS := false
if key, err = section.GetKey("use_ssl"); err == nil {
useTLS, _ = key.Bool()
Expand Down
22 changes: 14 additions & 8 deletions notification-server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,28 @@ func loadCcnetDB() {
log.Fatalf("Unsupported database %s.", dbEngine)
}

if key, err = section.GetKey("HOST"); err != nil {
unixSocket := ""
if key, err = section.GetKey("UNIX_SOCKET"); err == nil {
unixSocket = key.String()
}

host := ""
if key, err = section.GetKey("HOST"); err == nil {
host = key.String()
} else if unixSocket == "" {
log.Fatal("No database host in ccnet.conf.")
}
host := key.String()
// user is required.
if key, err = section.GetKey("USER"); err != nil {
log.Fatal("No database user in ccnet.conf.")
}
user := key.String()
if key, err = section.GetKey("PASSWD"); err != nil {
password := ""
if key, err = section.GetKey("PASSWD"); err == nil {
password = key.String()
} else if unixSocket == "" {
log.Fatal("No database password in ccnet.conf.")
}
password := key.String()
if key, err = section.GetKey("DB"); err != nil {
log.Fatal("No database db_name in ccnet.conf.")
}
Expand All @@ -124,10 +134,6 @@ func loadCcnetDB() {
if key, err = section.GetKey("PORT"); err == nil {
port, _ = key.Int()
}
unixSocket := ""
if key, err = section.GetKey("UNIX_SOCKET"); err == nil {
unixSocket = key.String()
}
useTLS := false
if key, err = section.GetKey("USE_SSL"); err == nil {
useTLS, _ = key.Bool()
Expand Down

0 comments on commit 864cef1

Please sign in to comment.