Mercurial > hg > orthanc-databases
annotate Framework/MySQL/MySQLParameters.cpp @ 371:c1fe28de1bf6
todo
author | Alain Mazy <am@osimis.io> |
---|---|
date | Tue, 14 Mar 2023 09:08:45 +0100 |
parents | 16aac0287485 |
children | 3d6886f3e5b3 |
rev | line source |
---|---|
0 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
359
16aac0287485
copyright upgraded to 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
5 * Copyright (C) 2017-2022 Osimis S.A., Belgium |
16aac0287485
copyright upgraded to 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
6 * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
0 | 7 * |
8 * This program is free software: you can redistribute it and/or | |
9 * modify it under the terms of the GNU Affero General Public License | |
10 * as published by the Free Software Foundation, either version 3 of | |
11 * the License, or (at your option) any later version. | |
12 * | |
13 * This program is distributed in the hope that it will be useful, but | |
14 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 * Affero General Public License for more details. | |
17 * | |
18 * You should have received a copy of the GNU Affero General Public License | |
19 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 **/ | |
21 | |
22 | |
23 #include "MySQLParameters.h" | |
24 | |
61 | 25 #include "MySQLDatabase.h" |
26 | |
152 | 27 #include <Logging.h> |
28 #include <OrthancException.h> | |
0 | 29 |
30 namespace OrthancDatabases | |
31 { | |
32 void MySQLParameters::Reset() | |
33 { | |
34 host_ = "localhost"; | |
35 username_.clear(); | |
36 password_.clear(); | |
37 database_.clear(); | |
38 port_ = 3306; | |
46
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
39 |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
40 #if defined(_WIN32) |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
41 unixSocket_.clear(); |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
42 #else |
0 | 43 unixSocket_ = "/var/run/mysqld/mysqld.sock"; |
46
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
44 #endif |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
45 |
0 | 46 lock_ = true; |
47 } | |
48 | |
49 | |
186 | 50 MySQLParameters::MySQLParameters() : |
51 ssl_(false), | |
52 verifySslServerCertificates_(true), | |
53 maxConnectionRetries_(10), | |
54 connectionRetryInterval_(5) | |
0 | 55 { |
56 Reset(); | |
57 } | |
58 | |
59 | |
186 | 60 MySQLParameters::MySQLParameters(const OrthancPlugins::OrthancConfiguration& pluginConfiguration, |
61 const OrthancPlugins::OrthancConfiguration& orthancConfiguration) | |
0 | 62 { |
63 Reset(); | |
64 | |
65 std::string s; | |
163
4d32c9c8d6c0
Added support for TLS connections
Alain Mazy <alain@mazy.be>
parents:
152
diff
changeset
|
66 if (pluginConfiguration.LookupStringValue(s, "Host")) |
0 | 67 { |
68 SetHost(s); | |
69 } | |
70 | |
163
4d32c9c8d6c0
Added support for TLS connections
Alain Mazy <alain@mazy.be>
parents:
152
diff
changeset
|
71 if (pluginConfiguration.LookupStringValue(s, "Username")) |
0 | 72 { |
73 SetUsername(s); | |
74 } | |
75 | |
163
4d32c9c8d6c0
Added support for TLS connections
Alain Mazy <alain@mazy.be>
parents:
152
diff
changeset
|
76 if (pluginConfiguration.LookupStringValue(s, "Password")) |
0 | 77 { |
78 SetPassword(s); | |
79 } | |
80 | |
163
4d32c9c8d6c0
Added support for TLS connections
Alain Mazy <alain@mazy.be>
parents:
152
diff
changeset
|
81 if (pluginConfiguration.LookupStringValue(s, "Database")) |
0 | 82 { |
83 SetDatabase(s); | |
84 } | |
85 | |
86 unsigned int port; | |
163
4d32c9c8d6c0
Added support for TLS connections
Alain Mazy <alain@mazy.be>
parents:
152
diff
changeset
|
87 if (pluginConfiguration.LookupUnsignedIntegerValue(port, "Port")) |
0 | 88 { |
89 SetPort(port); | |
90 } | |
91 | |
163
4d32c9c8d6c0
Added support for TLS connections
Alain Mazy <alain@mazy.be>
parents:
152
diff
changeset
|
92 if (pluginConfiguration.LookupStringValue(s, "UnixSocket")) |
0 | 93 { |
94 SetUnixSocket(s); | |
95 } | |
96 | |
163
4d32c9c8d6c0
Added support for TLS connections
Alain Mazy <alain@mazy.be>
parents:
152
diff
changeset
|
97 lock_ = pluginConfiguration.GetBooleanValue("Lock", true); // Use locking by default |
4d32c9c8d6c0
Added support for TLS connections
Alain Mazy <alain@mazy.be>
parents:
152
diff
changeset
|
98 |
4d32c9c8d6c0
Added support for TLS connections
Alain Mazy <alain@mazy.be>
parents:
152
diff
changeset
|
99 ssl_ = pluginConfiguration.GetBooleanValue("EnableSsl", false); |
4d32c9c8d6c0
Added support for TLS connections
Alain Mazy <alain@mazy.be>
parents:
152
diff
changeset
|
100 verifySslServerCertificates_ = pluginConfiguration.GetBooleanValue("SslVerifyServerCertificates", true); |
4d32c9c8d6c0
Added support for TLS connections
Alain Mazy <alain@mazy.be>
parents:
152
diff
changeset
|
101 |
4d32c9c8d6c0
Added support for TLS connections
Alain Mazy <alain@mazy.be>
parents:
152
diff
changeset
|
102 const std::string defaultCaCertificates = orthancConfiguration.GetStringValue("HttpsCACertificates", ""); |
4d32c9c8d6c0
Added support for TLS connections
Alain Mazy <alain@mazy.be>
parents:
152
diff
changeset
|
103 sslCaCertificates_ = pluginConfiguration.GetStringValue("SslCACertificates", defaultCaCertificates); |
141
0b3e9ee53c46
Added 'MaximumConnectionRetries' & 'ConnectionRetryInterval' to configure the retries when connecting to the DB at startup
Alain Mazy <alain@mazy.be>
parents:
140
diff
changeset
|
104 |
163
4d32c9c8d6c0
Added support for TLS connections
Alain Mazy <alain@mazy.be>
parents:
152
diff
changeset
|
105 if (ssl_ && verifySslServerCertificates_ && sslCaCertificates_.empty()) |
4d32c9c8d6c0
Added support for TLS connections
Alain Mazy <alain@mazy.be>
parents:
152
diff
changeset
|
106 { |
4d32c9c8d6c0
Added support for TLS connections
Alain Mazy <alain@mazy.be>
parents:
152
diff
changeset
|
107 LOG(ERROR) << "MySQL: No SslCACertificates defined, unable to check SSL Server certificates"; |
4d32c9c8d6c0
Added support for TLS connections
Alain Mazy <alain@mazy.be>
parents:
152
diff
changeset
|
108 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); |
4d32c9c8d6c0
Added support for TLS connections
Alain Mazy <alain@mazy.be>
parents:
152
diff
changeset
|
109 } |
4d32c9c8d6c0
Added support for TLS connections
Alain Mazy <alain@mazy.be>
parents:
152
diff
changeset
|
110 |
4d32c9c8d6c0
Added support for TLS connections
Alain Mazy <alain@mazy.be>
parents:
152
diff
changeset
|
111 maxConnectionRetries_ = pluginConfiguration.GetUnsignedIntegerValue("MaximumConnectionRetries", 10); |
4d32c9c8d6c0
Added support for TLS connections
Alain Mazy <alain@mazy.be>
parents:
152
diff
changeset
|
112 connectionRetryInterval_ = pluginConfiguration.GetUnsignedIntegerValue("ConnectionRetryInterval", 5); |
0 | 113 } |
114 | |
115 | |
116 void MySQLParameters::SetHost(const std::string& host) | |
117 { | |
118 host_ = host; | |
119 } | |
120 | |
121 | |
122 void MySQLParameters::SetUsername(const std::string& username) | |
123 { | |
124 username_ = username; | |
125 } | |
126 | |
127 | |
128 void MySQLParameters::SetPassword(const std::string& password) | |
129 { | |
130 password_ = password; | |
131 } | |
132 | |
133 | |
134 void MySQLParameters::SetDatabase(const std::string& database) | |
135 { | |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
136 if (database.empty()) |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
137 { |
46
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
138 LOG(ERROR) << "MySQL: Empty database name"; |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
139 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
140 } |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
141 |
61 | 142 if (!MySQLDatabase::IsValidDatabaseIdentifier(database)) |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
143 { |
61 | 144 LOG(ERROR) << "MySQL: Only alphanumeric characters are allowed in a " |
145 << "database name: \"" << database << "\""; | |
146 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
147 } |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
148 |
0 | 149 database_ = database; |
150 } | |
151 | |
152 | |
153 void MySQLParameters::SetPort(unsigned int port) | |
154 { | |
155 if (port >= 65535) | |
156 { | |
157 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
158 } | |
159 else | |
160 { | |
161 port_ = port; | |
162 } | |
163 } | |
164 | |
165 | |
166 void MySQLParameters::SetUnixSocket(const std::string& socket) | |
167 { | |
46
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
168 #if defined(_WIN32) |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
169 if (!socket.empty()) |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
170 { |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
171 LOG(WARNING) << "MySQL: Setting an UNIX socket on Windows has no effect"; |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
172 } |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
173 #endif |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
174 |
0 | 175 unixSocket_ = socket; |
176 } | |
22
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
177 |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
178 |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
179 void MySQLParameters::Format(Json::Value& target) const |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
180 { |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
181 target = Json::objectValue; |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
182 target["Host"] = host_; |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
183 target["Username"] = username_; |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
184 target["Password"] = password_; |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
185 target["Database"] = database_; |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
186 target["Port"] = port_; |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
187 target["UnixSocket"] = unixSocket_; |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
188 target["Lock"] = lock_; |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
189 } |
0 | 190 } |