Skip to content

Commit 34887ab

Browse files
committed
Create ldap users on demand
1 parent c4d343d commit 34887ab

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

examples/connect.cpp

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ int main(int argc, const char **argv)
6666
const char* mysql_user = getenv("MYSQL_USER");
6767
const char* mysql_password = getenv("MYSQL_PASSWORD");
6868
const char* ldap_user = getenv("LDAP_USER");
69+
const char* ldap_user_dn = getenv("LDAP_USER_DN");
6970
const char* ldap_simple_pwd = getenv("LDAP_SIMPLE_PWD");
7071
const char* ldap_scram_pwd = getenv("LDAP_SCRAM_PWD");
7172
const char* plugin_dir = getenv("PLUGIN_DIR");
@@ -225,6 +226,37 @@ int main(int argc, const char **argv)
225226
cout << "#" << endl;
226227
cout << "#\t Demo of connection URL syntax" << endl;
227228

229+
//Lets first create ldap and sasl users
230+
if(ldap_user || ldap_user_dn)
231+
{
232+
if(ldap_user_dn)
233+
{
234+
stringstream user_create;
235+
stmt.reset(con->createStatement());
236+
user_create << "CREATE USER ldap_simple IDENTIFIED WITH "
237+
"authentication_ldap_simple AS \""
238+
<< ldap_user_dn
239+
<< "\"";
240+
try{
241+
stmt->execute(user_create.str());
242+
}catch(...)
243+
{}
244+
}
245+
246+
if(ldap_user)
247+
{
248+
stringstream user_create;
249+
stmt.reset(con->createStatement());
250+
user_create << "CREATE USER "
251+
<< ldap_user
252+
<< "@'%' IDENTIFIED WITH authentication_ldap_sasl";
253+
try{
254+
stmt->execute(user_create.str());
255+
}catch(...)
256+
{}
257+
}
258+
}
259+
228260
if(ldap_simple_pwd)
229261
{
230262
//AUTH USING SASL client side plugin
@@ -234,9 +266,10 @@ int main(int argc, const char **argv)
234266
plugin
235267
*/
236268

269+
270+
237271
sql::ConnectOptionsMap opts;
238272
opts[OPT_HOSTNAME] = url;
239-
// We expect ldap_simple is the mysql user
240273
opts[OPT_USERNAME] = "ldap_simple";
241274
opts[OPT_PASSWORD] = ldap_simple_pwd;
242275

@@ -294,7 +327,7 @@ int main(int argc, const char **argv)
294327
con->close();
295328

296329
} catch (sql::SQLException &e) {
297-
cout << "#\t\t " << url << " caused expected exception when connecting using "<< user << endl;
330+
cout << "#\t\t " << url << " caused expected exception when connecting using "<< ldap_user << endl;
298331
cout << "#\t\t " << e.what() << " (MySQL error code: " << e.getErrorCode();
299332
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
300333
throw;

0 commit comments

Comments
 (0)