hydra123
hydra123

Reputation: 347

Sanitize password string in cpp

I have a string ,

userStmt:

SELECT * FROM RUN_ON_HIVE(SERVER('10.23.40.23'),USERNAME('user'),PASSWORD('pass'),DBNAME('default'),QUERY('ANALYZE TABLE default.test01 COMPUTE STATISTICS'));

I am trying to convert the password clause to "****".

String sanitizeduserstmt=userStmt;
  boost::regex pw_re("PASSWORD\\('[^']*'\\)");
  boost::regex_replace(sanitizeduserstmt, pw_re, "PASSWORD('****')");

But this is not working . Can someone tell me where am I going wrong? TIA

Upvotes: 1

Views: 190

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627327

The boost::regex_replace method does not modify the input string inplace, you need to assign the value back to the variable.

In your case, the fix will look like

#include <boost/regex.hpp>
#include <iostream>
using namespace std;

int main() {
    string sanitizeduserstmt="SELECT * FROM RUN_ON_HIVE(SERVER('10.23.40.23'),USERNAME('user'),PASSWORD('pass'),DBNAME('default'),QUERY('ANALYZE TABLE default.test01 COMPUTE STATISTICS'));";
    boost::regex pw_re("PASSWORD\\('[^']*'\\)");
    sanitizeduserstmt=boost::regex_replace(sanitizeduserstmt, pw_re, "PASSWORD('****')");
    std::cout << "Result: " << sanitizeduserstmt << "\n";
    // => Result: SELECT * FROM RUN_ON_HIVE(SERVER('10.23.40.23'),USERNAME('user'),PASSWORD('****'),DBNAME('default'),QUERY('ANALYZE TABLE default.test01 COMPUTE STATISTICS'));
}

See the Coliru demo.

Upvotes: 1

Related Questions