OmniSciDB  0fdbebe030
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Catalog_Namespace::SysCatalog Class Reference

#include <SysCatalog.h>

+ Inheritance diagram for Catalog_Namespace::SysCatalog:
+ Collaboration diagram for Catalog_Namespace::SysCatalog:

Public Member Functions

void init (const std::string &basePath, std::shared_ptr< Data_Namespace::DataMgr > dataMgr, const AuthMetadata &authMetadata, std::shared_ptr< Calcite > calcite, bool is_new_db, bool aggregator, const std::vector< LeafHostInfo > &string_dict_hosts)
 
std::shared_ptr< Cataloglogin (std::string &db, std::string &username, const std::string &password, UserMetadata &user_meta, bool check_password=true)
 
std::shared_ptr< CatalogswitchDatabase (std::string &dbname, const std::string &username)
 
void createUser (const std::string &name, const std::string &passwd, bool issuper, const std::string &dbname, bool can_login)
 
void dropUser (const std::string &name)
 
void alterUser (const int32_t userid, const std::string *passwd, bool *issuper, const std::string *dbname, bool *can_login)
 
void renameUser (std::string const &old_name, std::string const &new_name)
 
void createDatabase (const std::string &dbname, int owner)
 
void renameDatabase (std::string const &old_name, std::string const &new_name)
 
void dropDatabase (const DBMetadata &db)
 
bool getMetadataForUser (const std::string &name, UserMetadata &user)
 
bool getMetadataForUserById (const int32_t idIn, UserMetadata &user)
 
bool checkPasswordForUser (const std::string &passwd, std::string &name, UserMetadata &user)
 
void getMetadataWithDefaultDB (std::string &dbname, const std::string &username, Catalog_Namespace::DBMetadata &db_meta, UserMetadata &user_meta)
 
bool getMetadataForDB (const std::string &name, DBMetadata &db)
 
bool getMetadataForDBById (const int32_t idIn, DBMetadata &db)
 
Data_Namespace::DataMgrgetDataMgr () const
 
CalcitegetCalciteMgr () const
 
const std::string & getBasePath () const
 
SqliteConnectorgetSqliteConnector ()
 
std::list< DBMetadatagetAllDBMetadata ()
 
std::list< UserMetadatagetAllUserMetadata ()
 
std::list< UserMetadatagetAllUserMetadata (const int64_t dbId)
 
DBSummaryList getDatabaseListForUser (const UserMetadata &user)
 
void createDBObject (const UserMetadata &user, const std::string &objectName, DBObjectType type, const Catalog_Namespace::Catalog &catalog, int32_t objectId=-1)
 
void grantDBObjectPrivileges (const std::string &grantee, const DBObject &object, const Catalog_Namespace::Catalog &catalog)
 
void grantDBObjectPrivilegesBatch (const std::vector< std::string > &grantees, const std::vector< DBObject > &objects, const Catalog_Namespace::Catalog &catalog)
 
void revokeDBObjectPrivileges (const std::string &grantee, const DBObject &object, const Catalog_Namespace::Catalog &catalog)
 
void revokeDBObjectPrivilegesBatch (const std::vector< std::string > &grantees, const std::vector< DBObject > &objects, const Catalog_Namespace::Catalog &catalog)
 
void revokeDBObjectPrivilegesFromAll (DBObject object, Catalog *catalog)
 
void revokeDBObjectPrivilegesFromAll_unsafe (DBObject object, Catalog *catalog)
 
void getDBObjectPrivileges (const std::string &granteeName, DBObject &object, const Catalog_Namespace::Catalog &catalog) const
 
bool verifyDBObjectOwnership (const UserMetadata &user, DBObject object, const Catalog_Namespace::Catalog &catalog)
 
void createRole (const std::string &roleName, const bool &userPrivateRole=false)
 
void dropRole (const std::string &roleName)
 
void grantRoleBatch (const std::vector< std::string > &roles, const std::vector< std::string > &grantees)
 
void grantRole (const std::string &role, const std::string &grantee)
 
void revokeRoleBatch (const std::vector< std::string > &roles, const std::vector< std::string > &grantees)
 
void revokeRole (const std::string &role, const std::string &grantee)
 
bool hasAnyPrivileges (const UserMetadata &user, std::vector< DBObject > &privObjects)
 
bool checkPrivileges (const UserMetadata &user, const std::vector< DBObject > &privObjects) const
 
bool checkPrivileges (const std::string &userName, const std::vector< DBObject > &privObjects) const
 
GranteegetGrantee (const std::string &name) const
 
RolegetRoleGrantee (const std::string &name) const
 
UsergetUserGrantee (const std::string &name) const
 
std::vector
< ObjectRoleDescriptor * > 
getMetadataForObject (int32_t dbId, int32_t dbType, int32_t objectId) const
 
bool isRoleGrantedToGrantee (const std::string &granteeName, const std::string &roleName, bool only_direct) const
 
std::vector< std::string > getRoles (bool userPrivateRole, bool isSuper, const std::string &userName)
 
std::vector< std::string > getRoles (const std::string &userName, const int32_t dbId)
 
void revokeDashboardSystemRole (const std::string roleName, const std::vector< std::string > grantees)
 
bool isAggregator () const
 
void populateRoleDbObjects (const std::vector< DBObject > &objects)
 
std::string name () const
 
void renameObjectsInDescriptorMap (DBObject &object, const Catalog_Namespace::Catalog &cat)
 
void syncUserWithRemoteProvider (const std::string &user_name, std::vector< std::string > idp_roles, bool *issuper)
 
std::unordered_map
< std::string, std::vector
< std::string > > 
getGranteesOfSharedDashboards (const std::vector< std::string > &dashboard_ids)
 
void check_for_session_encryption (const std::string &pki_cert, std::string &session)
 

Static Public Member Functions

static SysCataloginstance ()
 

Public Attributes

std::mutex sqliteMutex_
 
mapd_shared_mutex sharedMutex_
 
std::atomic< std::thread::id > thread_holding_sqlite_lock
 
std::atomic< std::thread::id > thread_holding_write_lock
 

Static Public Attributes

static thread_local bool thread_holds_read_lock = false
 

Private Types

using GranteeMap = std::map< std::string, Grantee * >
 
using ObjectRoleDescriptorMap = std::multimap< std::string, ObjectRoleDescriptor * >
 

Private Member Functions

 SysCatalog ()
 
virtual ~SysCatalog ()
 
void initDB ()
 
void buildRoleMap ()
 
void buildUserRoleMap ()
 
void buildObjectDescriptorMap ()
 
void checkAndExecuteMigrations ()
 
void importDataFromOldMapdDB ()
 
void createUserRoles ()
 
void migratePrivileges ()
 
void migratePrivileged_old ()
 
void updateUserSchema ()
 
void updatePasswordsToHashes ()
 
void updateBlankPasswordsToRandom ()
 
void updateSupportUserDeactivation ()
 
void migrateDBAccessPrivileges ()
 
void loginImpl (std::string &username, const std::string &password, UserMetadata &user_meta)
 
bool checkPasswordForUserImpl (const std::string &passwd, std::string &name, UserMetadata &user)
 
void grantDefaultPrivilegesToRole_unsafe (const std::string &name, bool issuper)
 
void createRole_unsafe (const std::string &roleName, const bool &userPrivateRole=false)
 
void dropRole_unsafe (const std::string &roleName)
 
void grantRoleBatch_unsafe (const std::vector< std::string > &roles, const std::vector< std::string > &grantees)
 
void grantRole_unsafe (const std::string &roleName, const std::string &granteeName)
 
void revokeRoleBatch_unsafe (const std::vector< std::string > &roles, const std::vector< std::string > &grantees)
 
void revokeRole_unsafe (const std::string &roleName, const std::string &granteeName)
 
void updateObjectDescriptorMap (const std::string &roleName, DBObject &object, bool roleType, const Catalog_Namespace::Catalog &cat)
 
void deleteObjectDescriptorMap (const std::string &roleName)
 
void deleteObjectDescriptorMap (const std::string &roleName, DBObject &object, const Catalog_Namespace::Catalog &cat)
 
void grantDBObjectPrivilegesBatch_unsafe (const std::vector< std::string > &grantees, const std::vector< DBObject > &objects, const Catalog_Namespace::Catalog &catalog)
 
void grantDBObjectPrivileges_unsafe (const std::string &granteeName, const DBObject object, const Catalog_Namespace::Catalog &catalog)
 
void revokeDBObjectPrivilegesBatch_unsafe (const std::vector< std::string > &grantees, const std::vector< DBObject > &objects, const Catalog_Namespace::Catalog &catalog)
 
void revokeDBObjectPrivileges_unsafe (const std::string &granteeName, DBObject object, const Catalog_Namespace::Catalog &catalog)
 
void grantAllOnDatabase_unsafe (const std::string &roleName, DBObject &object, const Catalog_Namespace::Catalog &catalog)
 
void revokeAllOnDatabase_unsafe (const std::string &roleName, int32_t dbId, Grantee *grantee)
 
bool isDashboardSystemRole (const std::string &roleName)
 
void updateUserRoleName (const std::string &roleName, const std::string &newName)
 
template<typename F , typename... Args>
void execInTransaction (F &&f, Args &&...args)
 
auto yieldTransactionStreamer ()
 
- Private Member Functions inherited from Catalog_Namespace::CommonFileOperations
 CommonFileOperations (std::string const &base_path)
 
void removeCatalogByFullPath (std::string const &full_path)
 
void removeCatalogByName (std::string const &name)
 
auto duplicateAndRenameCatalog (std::string const &current_name, std::string const &new_name)
 
auto assembleCatalogName (std::string const &name)
 

Private Attributes

std::string basePath_
 
GranteeMap granteeMap_
 
ObjectRoleDescriptorMap objectDescriptorMap_
 
std::unique_ptr< SqliteConnectorsqliteConnector_
 
std::shared_ptr
< Data_Namespace::DataMgr
dataMgr_
 
std::unique_ptr< LdapServerldap_server_
 
std::unique_ptr< RestServerrest_server_
 
std::unique_ptr< PkiServerpki_server_
 
const AuthMetadataauthMetadata_
 
std::shared_ptr< CalcitecalciteMgr_
 
std::vector< LeafHostInfostring_dict_hosts_
 
bool aggregator_
 

Detailed Description

Definition at line 141 of file SysCatalog.h.

Member Typedef Documentation

using Catalog_Namespace::SysCatalog::GranteeMap = std::map<std::string, Grantee*>
private

Definition at line 273 of file SysCatalog.h.

Definition at line 274 of file SysCatalog.h.

Constructor & Destructor Documentation

Catalog_Namespace::SysCatalog::SysCatalog ( )
inlineprivate

Definition at line 276 of file SysCatalog.h.

278  , aggregator_(false)
279  , sqliteMutex_()
280  , sharedMutex_()
281  , thread_holding_sqlite_lock(std::thread::id())
282  , thread_holding_write_lock(std::thread::id()) {}
std::atomic< std::thread::id > thread_holding_sqlite_lock
Definition: SysCatalog.h:368
mapd_shared_mutex sharedMutex_
Definition: SysCatalog.h:367
CommonFileOperations(std::string const &base_path)
Definition: SysCatalog.h:124
std::atomic< std::thread::id > thread_holding_write_lock
Definition: SysCatalog.h:369
Catalog_Namespace::SysCatalog::~SysCatalog ( )
privatevirtual

Definition at line 150 of file SysCatalog.cpp.

150  {
151  sys_write_lock write_lock(this);
152  for (auto grantee = granteeMap_.begin(); grantee != granteeMap_.end(); ++grantee) {
153  delete grantee->second;
154  }
155  granteeMap_.clear();
156  for (ObjectRoleDescriptorMap::iterator objectIt = objectDescriptorMap_.begin();
157  objectIt != objectDescriptorMap_.end();) {
158  ObjectRoleDescriptorMap::iterator eraseIt = objectIt++;
159  delete eraseIt->second;
160  }
161  objectDescriptorMap_.clear();
162 }
ObjectRoleDescriptorMap objectDescriptorMap_
Definition: SysCatalog.h:352
write_lock< SysCatalog > sys_write_lock
Definition: SysCatalog.cpp:80

Member Function Documentation

void Catalog_Namespace::SysCatalog::alterUser ( const int32_t  userid,
const std::string *  passwd,
bool *  issuper,
const std::string *  dbname,
bool *  can_login 
)

Definition at line 861 of file SysCatalog.cpp.

References Catalog_Namespace::anonymous_namespace{SysCatalog.cpp}::append_with_commas, Catalog_Namespace::DBMetadata::dbId, anonymous_namespace{SysCatalog.cpp}::hash_with_bcrypt(), and to_string().

865  {
866  sys_sqlite_lock sqlite_lock(this);
867  sqliteConnector_->query("BEGIN TRANSACTION");
868  try {
869  string sql;
870  std::vector<std::string> values;
871  if (passwd != nullptr) {
872  append_with_commas(sql, "passwd_hash = ?");
873  values.push_back(hash_with_bcrypt(*passwd));
874  }
875  if (issuper != nullptr) {
876  append_with_commas(sql, "issuper = ?");
877  values.push_back(std::to_string(*issuper));
878  }
879  if (dbname != nullptr) {
880  if (!dbname->empty()) {
881  append_with_commas(sql, "default_db = ?");
882  DBMetadata db;
883  if (!SysCatalog::instance().getMetadataForDB(*dbname, db)) {
884  throw runtime_error(string("DEFAULT_DB ") + *dbname + " not found.");
885  }
886  values.push_back(std::to_string(db.dbId));
887  } else {
888  append_with_commas(sql, "default_db = NULL");
889  }
890  }
891  if (can_login != nullptr) {
892  append_with_commas(sql, "can_login = ?");
893  values.push_back(std::to_string(*can_login));
894  }
895 
896  sql = "UPDATE mapd_users SET " + sql + " WHERE userid = ?";
897  values.push_back(std::to_string(userid));
898 
899  sqliteConnector_->query_with_text_params(sql, values);
900  } catch (const std::exception& e) {
901  sqliteConnector_->query("ROLLBACK TRANSACTION");
902  throw;
903  }
904  sqliteConnector_->query("END TRANSACTION");
905 }
std::string to_string(char const *&&v)
sqlite_lock< SysCatalog > sys_sqlite_lock
Definition: SysCatalog.cpp:81
static SysCatalog & instance()
Definition: SysCatalog.h:256
std::string hash_with_bcrypt(const std::string &pwd)
Definition: SysCatalog.cpp:66
std::unique_ptr< SqliteConnector > sqliteConnector_
Definition: SysCatalog.h:353
bool getMetadataForDB(const std::string &name, DBMetadata &db)

+ Here is the call graph for this function:

void Catalog_Namespace::SysCatalog::buildObjectDescriptorMap ( )
private

Definition at line 2118 of file SysCatalog.cpp.

References ObjectRoleDescriptor::dbId, ObjectRoleDescriptor::objectId, ObjectRoleDescriptor::objectName, ObjectRoleDescriptor::objectOwnerId, ObjectRoleDescriptor::objectType, AccessPrivileges::privileges, ObjectRoleDescriptor::privs, ObjectRoleDescriptor::roleName, ObjectRoleDescriptor::roleType, and to_string().

2118  {
2119  sys_write_lock write_lock(this);
2120  sys_sqlite_lock sqlite_lock(this);
2121  string objectQuery(
2122  "SELECT roleName, roleType, objectPermissionsType, dbId, objectId, "
2123  "objectPermissions, objectOwnerId, objectName "
2124  "from mapd_object_permissions");
2125  sqliteConnector_->query(objectQuery);
2126  size_t numRows = sqliteConnector_->getNumRows();
2127  for (size_t r = 0; r < numRows; ++r) {
2129  od->roleName = sqliteConnector_->getData<string>(r, 0);
2130  od->roleType = sqliteConnector_->getData<bool>(r, 1);
2131  od->objectType = sqliteConnector_->getData<int>(r, 2);
2132  od->dbId = sqliteConnector_->getData<int>(r, 3);
2133  od->objectId = sqliteConnector_->getData<int>(r, 4);
2134  od->privs.privileges = sqliteConnector_->getData<int>(r, 5);
2135  od->objectOwnerId = sqliteConnector_->getData<int>(r, 6);
2136  od->objectName = sqliteConnector_->getData<string>(r, 7);
2137  objectDescriptorMap_.insert(ObjectRoleDescriptorMap::value_type(
2138  std::to_string(od->dbId) + ":" + std::to_string(od->objectType) + ":" +
2139  std::to_string(od->objectId),
2140  od));
2141  }
2142 }
specifies the object_roles content in-memory of a row in mapd_object_permissions table ...
ObjectRoleDescriptorMap objectDescriptorMap_
Definition: SysCatalog.h:352
std::string to_string(char const *&&v)
sqlite_lock< SysCatalog > sys_sqlite_lock
Definition: SysCatalog.cpp:81
write_lock< SysCatalog > sys_write_lock
Definition: SysCatalog.cpp:80
std::unique_ptr< SqliteConnector > sqliteConnector_
Definition: SysCatalog.h:353
int64_t privileges
Definition: DBObject.h:133

+ Here is the call graph for this function:

void Catalog_Namespace::SysCatalog::buildRoleMap ( )
private

Definition at line 2000 of file SysCatalog.cpp.

References DatabaseDBObjectType, DBObjectKey::fromString(), AccessPrivileges::privileges, and to_upper().

2000  {
2001  sys_write_lock write_lock(this);
2002  sys_sqlite_lock sqlite_lock(this);
2003  string roleQuery(
2004  "SELECT roleName, roleType, objectPermissionsType, dbId, objectId, "
2005  "objectPermissions, objectOwnerId, objectName "
2006  "from mapd_object_permissions");
2007  sqliteConnector_->query(roleQuery);
2008  size_t numRows = sqliteConnector_->getNumRows();
2009  std::vector<std::string> objectKeyStr(4);
2010  DBObjectKey objectKey;
2011  AccessPrivileges privs;
2012  bool userPrivateRole(false);
2013  for (size_t r = 0; r < numRows; ++r) {
2014  std::string roleName = sqliteConnector_->getData<string>(r, 0);
2015  userPrivateRole = sqliteConnector_->getData<bool>(r, 1);
2016  DBObjectType permissionType =
2017  static_cast<DBObjectType>(sqliteConnector_->getData<int>(r, 2));
2018  objectKeyStr[0] = sqliteConnector_->getData<string>(r, 2);
2019  objectKeyStr[1] = sqliteConnector_->getData<string>(r, 3);
2020  objectKeyStr[2] = sqliteConnector_->getData<string>(r, 4);
2021  objectKey = DBObjectKey::fromString(objectKeyStr, permissionType);
2022  privs.privileges = sqliteConnector_->getData<int>(r, 5);
2023  int32_t owner = sqliteConnector_->getData<int>(r, 6);
2024  std::string name = sqliteConnector_->getData<string>(r, 7);
2025 
2026  DBObject dbObject(objectKey, privs, owner);
2027  dbObject.setName(name);
2028  if (-1 == objectKey.objectId) {
2029  dbObject.setObjectType(DBObjectType::DatabaseDBObjectType);
2030  } else {
2031  dbObject.setObjectType(permissionType);
2032  }
2033 
2034  auto* rl = getGrantee(roleName);
2035  if (!rl) {
2036  if (userPrivateRole) {
2037  rl = new User(roleName);
2038  } else {
2039  rl = new Role(roleName);
2040  }
2041  granteeMap_[to_upper(roleName)] = rl;
2042  }
2043  rl->grantPrivileges(dbObject);
2044  }
2045 }
DBObjectType
Definition: DBObject.h:42
std::string name() const
Definition: SysCatalog.h:262
Definition: Grantee.h:70
Grantee * getGrantee(const std::string &name) const
Definition: Grantee.h:76
sqlite_lock< SysCatalog > sys_sqlite_lock
Definition: SysCatalog.cpp:81
static DBObjectKey fromString(const std::vector< std::string > &key, const DBObjectType &type)
Definition: DBObject.cpp:257
write_lock< SysCatalog > sys_write_lock
Definition: SysCatalog.cpp:80
std::unique_ptr< SqliteConnector > sqliteConnector_
Definition: SysCatalog.h:353
std::string to_upper(const std::string &str)
int64_t privileges
Definition: DBObject.h:133

+ Here is the call graph for this function:

void Catalog_Namespace::SysCatalog::buildUserRoleMap ( )
private

Definition at line 2070 of file SysCatalog.cpp.

References OMNISCI_ROOT_USER.

2070  {
2071  sys_write_lock write_lock(this);
2072  sys_sqlite_lock sqlite_lock(this);
2073  std::vector<std::pair<std::string, std::string>> granteeRooles;
2074  string userRoleQuery("SELECT roleName, userName from mapd_roles");
2075  sqliteConnector_->query(userRoleQuery);
2076  size_t numRows = sqliteConnector_->getNumRows();
2077  for (size_t r = 0; r < numRows; ++r) {
2078  std::string roleName = sqliteConnector_->getData<string>(r, 0);
2079  std::string userName = sqliteConnector_->getData<string>(r, 1);
2080  // required for declared nomenclature before v4.0.0
2081  if ((boost::equals(roleName, "mapd_default_suser_role") &&
2082  boost::equals(userName, OMNISCI_ROOT_USER)) ||
2083  (boost::equals(roleName, "mapd_default_user_role") &&
2084  !boost::equals(userName, "mapd_default_user_role"))) {
2085  // grouprole already exists with roleName==userName in mapd_roles table
2086  // ignore duplicate instances of userRole which exists before v4.0.0
2087  continue;
2088  }
2089  auto* rl = getGrantee(roleName);
2090  if (!rl) {
2091  throw runtime_error("Data inconsistency when building role map. Role " + roleName +
2092  " from db not found in the map.");
2093  }
2094  std::pair<std::string, std::string> roleVecElem(roleName, userName);
2095  granteeRooles.push_back(roleVecElem);
2096  }
2097 
2098  for (size_t i = 0; i < granteeRooles.size(); i++) {
2099  std::string roleName = granteeRooles[i].first;
2100  std::string granteeName = granteeRooles[i].second;
2101  auto* grantee = getGrantee(granteeName);
2102  if (!grantee) {
2103  throw runtime_error("Data inconsistency when building role map. Grantee " +
2104  granteeName + " not found in the map.");
2105  }
2106  if (granteeName == roleName) {
2107  continue;
2108  }
2109  Role* rl = dynamic_cast<Role*>(getGrantee(roleName));
2110  if (!rl) {
2111  throw runtime_error("Data inconsistency when building role map. Role " + roleName +
2112  " not found in the map.");
2113  }
2114  grantee->grantRole(rl);
2115  }
2116 }
Grantee * getGrantee(const std::string &name) const
Definition: Grantee.h:76
sqlite_lock< SysCatalog > sys_sqlite_lock
Definition: SysCatalog.cpp:81
write_lock< SysCatalog > sys_write_lock
Definition: SysCatalog.cpp:80
std::unique_ptr< SqliteConnector > sqliteConnector_
Definition: SysCatalog.h:353
const std::string OMNISCI_ROOT_USER
Definition: SysCatalog.h:59
void Catalog_Namespace::SysCatalog::check_for_session_encryption ( const std::string &  pki_cert,
std::string &  session 
)

Definition at line 764 of file SysCatalog.cpp.

765  {
766  if (!pki_server_->inUse()) {
767  return;
768  }
769  pki_server_->encrypt_session(pki_cert, session);
770 }
std::unique_ptr< PkiServer > pki_server_
Definition: SysCatalog.h:358
void Catalog_Namespace::SysCatalog::checkAndExecuteMigrations ( )
private

Definition at line 202 of file SysCatalog.cpp.

202  {
204  createUserRoles();
207  updateUserSchema(); // must come before updatePasswordsToHashes()
209  updateBlankPasswordsToRandom(); // must come after updatePasswordsToHashes()
211 }
bool Catalog_Namespace::SysCatalog::checkPasswordForUser ( const std::string &  passwd,
std::string &  name,
UserMetadata user 
)

Definition at line 1184 of file SysCatalog.cpp.

1186  {
1187  return checkPasswordForUserImpl(passwd, name, user);
1188 }
std::string name() const
Definition: SysCatalog.h:262
bool checkPasswordForUserImpl(const std::string &passwd, std::string &name, UserMetadata &user)
bool Catalog_Namespace::SysCatalog::checkPasswordForUserImpl ( const std::string &  passwd,
std::string &  name,
UserMetadata user 
)
private

Definition at line 1190 of file SysCatalog.cpp.

References CHECK(), LOG, Catalog_Namespace::UserMetadata::passwd_hash, and logger::WARNING.

1192  {
1193  if (!getMetadataForUser(name, user)) {
1194  // Check password against some fake hash just to waste time so that response times
1195  // for invalid password and invalid user are similar and a caller can't say the
1196  // difference
1197  char fake_hash[BCRYPT_HASHSIZE];
1198  CHECK(bcrypt_gensalt(-1, fake_hash) == 0);
1199  bcrypt_checkpw(passwd.c_str(), fake_hash);
1200  LOG(WARNING) << "Local login failed";
1201  return false;
1202  }
1203  int pwd_check_result = bcrypt_checkpw(passwd.c_str(), user.passwd_hash.c_str());
1204  // if the check fails there is a good chance that data on disc is broken
1205  CHECK(pwd_check_result >= 0);
1206  return pwd_check_result == 0;
1207 }
#define LOG(tag)
Definition: Logger.h:188
bool getMetadataForUser(const std::string &name, UserMetadata &user)
std::string name() const
Definition: SysCatalog.h:262
CHECK(cgen_state)

+ Here is the call graph for this function:

bool Catalog_Namespace::SysCatalog::checkPrivileges ( const UserMetadata user,
const std::vector< DBObject > &  privObjects 
) const

Definition at line 1861 of file SysCatalog.cpp.

References Catalog_Namespace::UserMetadata::isSuper, and Catalog_Namespace::UserMetadata::userName.

Referenced by Catalog_Namespace::SessionInfo::checkDBAccessPrivileges().

1862  {
1863  sys_read_lock read_lock(this);
1864  if (user.isSuper) {
1865  return true;
1866  }
1867 
1868  auto* user_rl = instance().getUserGrantee(user.userName);
1869  if (!user_rl) {
1870  throw runtime_error("User " + user.userName + " does not exist.");
1871  }
1872  for (auto& object : privObjects) {
1873  if (!user_rl->checkPrivileges(object)) {
1874  return false;
1875  }
1876  }
1877  return true;
1878 }
read_lock< SysCatalog > sys_read_lock
Definition: SysCatalog.cpp:79
static SysCatalog & instance()
Definition: SysCatalog.h:256
User * getUserGrantee(const std::string &name) const

+ Here is the caller graph for this function:

bool Catalog_Namespace::SysCatalog::checkPrivileges ( const std::string &  userName,
const std::vector< DBObject > &  privObjects 
) const

Definition at line 1880 of file SysCatalog.cpp.

1881  {
1882  UserMetadata user;
1883  if (!instance().getMetadataForUser(userName, user)) {
1884  throw runtime_error("Request to check privileges for user " + userName +
1885  " failed because user with this name does not exist.");
1886  }
1887  return (checkPrivileges(user, privObjects));
1888 }
bool getMetadataForUser(const std::string &name, UserMetadata &user)
bool checkPrivileges(const UserMetadata &user, const std::vector< DBObject > &privObjects) const
static SysCatalog & instance()
Definition: SysCatalog.h:256
void Catalog_Namespace::SysCatalog::createDatabase ( const std::string &  dbname,
int  owner 
)

Definition at line 1019 of file SysCatalog.cpp.

References CHECK(), DatabaseDBObjectType, g_enable_fsi, OMNISCI_ROOT_USER_ID, OMNISCI_SYSTEM_CATALOG, to_string(), to_upper(), and Catalog_Namespace::UserMetadata::userName.

1019  {
1020  sys_write_lock write_lock(this);
1021  sys_sqlite_lock sqlite_lock(this);
1022 
1023  DBMetadata db;
1024  if (getMetadataForDB(name, db)) {
1025  throw runtime_error("Database " + name + " already exists.");
1026  }
1028  throw runtime_error("Database name " + name + " is reserved.");
1029  }
1030 
1031  std::unique_ptr<SqliteConnector> dbConn(
1032  new SqliteConnector(name, basePath_ + "/mapd_catalogs/"));
1033  // NOTE(max): it's okay to run this in a separate transaction. If we fail later
1034  // we delete the database anyways.
1035  // If we run it in the same transaction as SysCatalog functions, then Catalog
1036  // constructor won't find the tables we have just created.
1037  dbConn->query("BEGIN TRANSACTION");
1038  try {
1039  dbConn->query(
1040  "CREATE TABLE mapd_tables (tableid integer primary key, name text unique, userid "
1041  "integer, ncolumns integer, "
1042  "isview boolean, "
1043  "fragments text, frag_type integer, max_frag_rows integer, max_chunk_size "
1044  "bigint, "
1045  "frag_page_size integer, "
1046  "max_rows bigint, partitions text, shard_column_id integer, shard integer, "
1047  "sort_column_id integer default 0, storage_type text default '',"
1048  "num_shards integer, key_metainfo TEXT, version_num "
1049  "BIGINT DEFAULT 1) ");
1050  dbConn->query(
1051  "CREATE TABLE mapd_columns (tableid integer references mapd_tables, columnid "
1052  "integer, name text, coltype "
1053  "integer, colsubtype integer, coldim integer, colscale integer, is_notnull "
1054  "boolean, compression integer, "
1055  "comp_param integer, size integer, chunks text, is_systemcol boolean, "
1056  "is_virtualcol boolean, virtual_expr "
1057  "text, is_deletedcol boolean, version_num BIGINT, "
1058  "primary key(tableid, columnid), unique(tableid, name))");
1059  dbConn->query(
1060  "CREATE TABLE mapd_views (tableid integer references mapd_tables, sql text)");
1061  dbConn->query(
1062  "CREATE TABLE mapd_dashboards (id integer primary key autoincrement, name text , "
1063  "userid integer references mapd_users, state text, image_hash text, update_time "
1064  "timestamp, "
1065  "metadata text, UNIQUE(userid, name) )");
1066  dbConn->query(
1067  "CREATE TABLE mapd_links (linkid integer primary key, userid integer references "
1068  "mapd_users, "
1069  "link text unique, view_state text, update_time timestamp, view_metadata text)");
1070  dbConn->query(
1071  "CREATE TABLE mapd_dictionaries (dictid integer primary key, name text unique, "
1072  "nbits int, is_shared boolean, "
1073  "refcount int, version_num BIGINT DEFAULT 1)");
1074  dbConn->query(
1075  "CREATE TABLE mapd_logical_to_physical(logical_table_id integer, "
1076  "physical_table_id "
1077  "integer)");
1078  dbConn->query("CREATE TABLE mapd_record_ownership_marker (dummy integer)");
1079  dbConn->query_with_text_params(
1080  "INSERT INTO mapd_record_ownership_marker (dummy) VALUES (?1)",
1081  std::vector<std::string>{std::to_string(owner)});
1082 
1083  if (g_enable_fsi) {
1084  dbConn->query(
1085  "CREATE TABLE omnisci_foreign_servers("
1086  "id integer primary key, "
1087  "name text unique, "
1088  "data_wrapper_type text, "
1089  "owner_user_id integer, "
1090  "options text)");
1091  dbConn->query(
1092  "CREATE TABLE omnisci_foreign_tables("
1093  "table_id integer unique, "
1094  "server_id integer, "
1095  "options text, "
1096  "FOREIGN KEY(table_id) REFERENCES mapd_tables(tableid), "
1097  "FOREIGN KEY(server_id) REFERENCES omnisci_foreign_servers(id))");
1098  }
1099  } catch (const std::exception&) {
1100  dbConn->query("ROLLBACK TRANSACTION");
1101  boost::filesystem::remove(basePath_ + "/mapd_catalogs/" + name);
1102  throw;
1103  }
1104  dbConn->query("END TRANSACTION");
1105 
1106  std::shared_ptr<Catalog> cat;
1107  // Now update SysCatalog with privileges and the new database
1108  sqliteConnector_->query("BEGIN TRANSACTION");
1109  try {
1110  sqliteConnector_->query_with_text_param(
1111  "INSERT INTO mapd_databases (name, owner) VALUES (?, " + std::to_string(owner) +
1112  ")",
1113  name);
1114  CHECK(getMetadataForDB(name, db));
1116  if (owner != OMNISCI_ROOT_USER_ID) {
1118  object.loadKey(*cat);
1119  UserMetadata user;
1120  CHECK(getMetadataForUserById(owner, user));
1121  grantAllOnDatabase_unsafe(user.userName, object, *cat);
1122  }
1123  } catch (const std::exception&) {
1124  sqliteConnector_->query("ROLLBACK TRANSACTION");
1125  boost::filesystem::remove(basePath_ + "/mapd_catalogs/" + name);
1126  throw;
1127  }
1128  sqliteConnector_->query("END TRANSACTION");
1129 
1130  if (g_enable_fsi) {
1131  try {
1132  cat->createDefaultServersIfNotExists();
1133  } catch (...) {
1134  boost::filesystem::remove(basePath_ + "/mapd_catalogs/" + name);
1135  throw;
1136  }
1137  }
1138 }
static std::shared_ptr< Catalog > get(const std::string &dbName)
Definition: Catalog.cpp:3418
std::string name() const
Definition: SysCatalog.h:262
const std::string OMNISCI_SYSTEM_CATALOG
Definition: SysCatalog.h:57
bool getMetadataForUserById(const int32_t idIn, UserMetadata &user)
std::string to_string(char const *&&v)
sqlite_lock< SysCatalog > sys_sqlite_lock
Definition: SysCatalog.cpp:81
CHECK(cgen_state)
void grantAllOnDatabase_unsafe(const std::string &roleName, DBObject &object, const Catalog_Namespace::Catalog &catalog)
std::shared_ptr< Data_Namespace::DataMgr > dataMgr_
Definition: SysCatalog.h:355
write_lock< SysCatalog > sys_write_lock
Definition: SysCatalog.cpp:80
std::unique_ptr< SqliteConnector > sqliteConnector_
Definition: SysCatalog.h:353
std::string to_upper(const std::string &str)
const int OMNISCI_ROOT_USER_ID
Definition: SysCatalog.h:60
std::vector< LeafHostInfo > string_dict_hosts_
Definition: SysCatalog.h:361
std::shared_ptr< Calcite > calciteMgr_
Definition: SysCatalog.h:360
bool g_enable_fsi
Definition: Catalog.cpp:89
bool getMetadataForDB(const std::string &name, DBMetadata &db)

+ Here is the call graph for this function:

void Catalog_Namespace::SysCatalog::createDBObject ( const UserMetadata user,
const std::string &  objectName,
DBObjectType  type,
const Catalog_Namespace::Catalog catalog,
int32_t  objectId = -1 
)

Definition at line 1390 of file SysCatalog.cpp.

References AccessPrivileges::ALL_DASHBOARD, AccessPrivileges::ALL_DATABASE, AccessPrivileges::ALL_SERVER, AccessPrivileges::ALL_TABLE, DashboardDBObjectType, Catalog_Namespace::UserMetadata::isSuper, ServerDBObjectType, TableDBObjectType, Catalog_Namespace::UserMetadata::userId, and Catalog_Namespace::UserMetadata::userName.

Referenced by CreateForeignServerCommand::execute(), and CreateForeignTableCommand::execute().

1394  {
1395  sys_write_lock write_lock(this);
1396  sys_sqlite_lock sqlite_lock(this);
1397 
1398  DBObject object =
1399  objectId == -1 ? DBObject(objectName, type) : DBObject(objectId, type);
1400  object.loadKey(catalog);
1401  switch (type) {
1402  case TableDBObjectType:
1403  object.setPrivileges(AccessPrivileges::ALL_TABLE);
1404  break;
1405  case DashboardDBObjectType:
1406  object.setPrivileges(AccessPrivileges::ALL_DASHBOARD);
1407  break;
1408  case ServerDBObjectType:
1409  object.setPrivileges(AccessPrivileges::ALL_SERVER);
1410  break;
1411  default:
1412  object.setPrivileges(AccessPrivileges::ALL_DATABASE);
1413  break;
1414  }
1415  object.setOwner(user.userId);
1416  sqliteConnector_->query("BEGIN TRANSACTION");
1417  try {
1418  if (!user.isSuper) { // no need to grant to suser, has all privs by default
1419  grantDBObjectPrivileges_unsafe(user.userName, object, catalog);
1420  auto* grantee = instance().getUserGrantee(user.userName);
1421  if (!grantee) {
1422  throw runtime_error("User " + user.userName + " does not exist.");
1423  }
1424  grantee->grantPrivileges(object);
1425  }
1426  } catch (std::exception& e) {
1427  sqliteConnector_->query("ROLLBACK TRANSACTION");
1428  throw;
1429  }
1430  sqliteConnector_->query("END TRANSACTION");
1431 }
static const AccessPrivileges ALL_DATABASE
Definition: DBObject.h:151
void grantDBObjectPrivileges_unsafe(const std::string &granteeName, const DBObject object, const Catalog_Namespace::Catalog &catalog)
sqlite_lock< SysCatalog > sys_sqlite_lock
Definition: SysCatalog.cpp:81
static SysCatalog & instance()
Definition: SysCatalog.h:256
write_lock< SysCatalog > sys_write_lock
Definition: SysCatalog.cpp:80
static const AccessPrivileges ALL_SERVER
Definition: DBObject.h:187
User * getUserGrantee(const std::string &name) const
std::unique_ptr< SqliteConnector > sqliteConnector_
Definition: SysCatalog.h:353
void loadKey()
Definition: DBObject.cpp:177
static const AccessPrivileges ALL_DASHBOARD
Definition: DBObject.h:169
static const AccessPrivileges ALL_TABLE
Definition: DBObject.h:157

+ Here is the caller graph for this function:

void Catalog_Namespace::SysCatalog::createRole ( const std::string &  roleName,
const bool &  userPrivateRole = false 
)

Definition at line 2158 of file SysCatalog.cpp.

Referenced by Catalog_Namespace::Catalog::createOrUpdateDashboardSystemRole().

2158  {
2159  execInTransaction(&SysCatalog::createRole_unsafe, roleName, userPrivateRole);
2160 }
void createRole_unsafe(const std::string &roleName, const bool &userPrivateRole=false)
void execInTransaction(F &&f, Args &&...args)

+ Here is the caller graph for this function:

void Catalog_Namespace::SysCatalog::createRole_unsafe ( const std::string &  roleName,
const bool &  userPrivateRole = false 
)
private

Definition at line 1633 of file SysCatalog.cpp.

References DatabaseDBObjectType, DBObjectKey::dbId, Catalog_Namespace::insertOrUpdateObjectPrivileges(), OMNISCI_DEFAULT_DB, DBObjectKey::permissionType, DBObject::setObjectKey(), and to_upper().

1634  {
1635  sys_write_lock write_lock(this);
1636 
1637  auto* grantee = getGrantee(roleName);
1638  if (grantee) {
1639  throw std::runtime_error("CREATE ROLE " + roleName +
1640  " failed because grantee with this name already exists.");
1641  }
1642  if (userPrivateRole) {
1643  grantee = new User(roleName);
1644  } else {
1645  grantee = new Role(roleName);
1646  }
1647  granteeMap_[to_upper(roleName)] = grantee;
1648 
1649  // NOTE (max): Why create an empty privileges record for a role?
1650  /* grant none privileges to this role and add it to sqlite DB */
1652  DBObjectKey objKey;
1653  // 0 is an id that does not exist
1654  objKey.dbId = 0;
1656  dbObject.setObjectKey(objKey);
1657  grantee->grantPrivileges(dbObject);
1658 
1659  sys_sqlite_lock sqlite_lock(this);
1660  insertOrUpdateObjectPrivileges(sqliteConnector_, roleName, userPrivateRole, dbObject);
1661 }
Definition: Grantee.h:70
Grantee * getGrantee(const std::string &name) const
Definition: Grantee.h:76
void insertOrUpdateObjectPrivileges(std::unique_ptr< SqliteConnector > &sqliteConnector, std::string roleName, bool userRole, DBObject &object)
Definition: SysCatalog.cpp:341
sqlite_lock< SysCatalog > sys_sqlite_lock
Definition: SysCatalog.cpp:81
const std::string OMNISCI_DEFAULT_DB
Definition: SysCatalog.h:58
write_lock< SysCatalog > sys_write_lock
Definition: SysCatalog.cpp:80
std::unique_ptr< SqliteConnector > sqliteConnector_
Definition: SysCatalog.h:353
std::string to_upper(const std::string &str)
int32_t dbId
Definition: DBObject.h:56
int32_t permissionType
Definition: DBObject.h:55

+ Here is the call graph for this function:

void Catalog_Namespace::SysCatalog::createUser ( const std::string &  name,
const std::string &  passwd,
bool  issuper,
const std::string &  dbname,
bool  can_login 
)

Definition at line 772 of file SysCatalog.cpp.

References Catalog_Namespace::DBMetadata::dbId, anonymous_namespace{SysCatalog.cpp}::hash_with_bcrypt(), and to_string().

776  {
777  sys_write_lock write_lock(this);
778  sys_sqlite_lock sqlite_lock(this);
779 
780  UserMetadata user;
781  if (getMetadataForUser(name, user)) {
782  throw runtime_error("User " + name + " already exists.");
783  }
784  if (getGrantee(name)) {
785  throw runtime_error(
786  "User name " + name +
787  " is same as one of existing grantees. User and role names should be unique.");
788  }
789  sqliteConnector_->query("BEGIN TRANSACTION");
790  try {
791  std::vector<std::string> vals;
792  if (!dbname.empty()) {
793  DBMetadata db;
794  if (!SysCatalog::instance().getMetadataForDB(dbname, db)) {
795  throw runtime_error("DEFAULT_DB " + dbname + " not found.");
796  }
797  vals = {name,
798  hash_with_bcrypt(passwd),
799  std::to_string(issuper),
800  std::to_string(db.dbId),
801  std::to_string(can_login)};
802  sqliteConnector_->query_with_text_params(
803  "INSERT INTO mapd_users (name, passwd_hash, issuper, default_db, can_login) "
804  "VALUES (?, ?, ?, ?, ?)",
805  vals);
806  } else {
807  vals = {name,
808  hash_with_bcrypt(passwd),
809  std::to_string(issuper),
810  std::to_string(can_login)};
811  sqliteConnector_->query_with_text_params(
812  "INSERT INTO mapd_users (name, passwd_hash, issuper, can_login) "
813  "VALUES (?, ?, ?, ?)",
814  vals);
815  }
816  createRole_unsafe(name, true);
817  } catch (const std::exception& e) {
818  sqliteConnector_->query("ROLLBACK TRANSACTION");
819  throw;
820  }
821  sqliteConnector_->query("END TRANSACTION");
822 }
bool getMetadataForUser(const std::string &name, UserMetadata &user)
std::string name() const
Definition: SysCatalog.h:262
void createRole_unsafe(const std::string &roleName, const bool &userPrivateRole=false)
Grantee * getGrantee(const std::string &name) const
std::string to_string(char const *&&v)
sqlite_lock< SysCatalog > sys_sqlite_lock
Definition: SysCatalog.cpp:81
static SysCatalog & instance()
Definition: SysCatalog.h:256
std::string hash_with_bcrypt(const std::string &pwd)
Definition: SysCatalog.cpp:66
write_lock< SysCatalog > sys_write_lock
Definition: SysCatalog.cpp:80
std::unique_ptr< SqliteConnector > sqliteConnector_
Definition: SysCatalog.h:353
bool getMetadataForDB(const std::string &name, DBMetadata &db)

+ Here is the call graph for this function:

void Catalog_Namespace::SysCatalog::createUserRoles ( )
private

Definition at line 286 of file SysCatalog.cpp.

References OMNISCI_ROOT_USER.

286  {
287  sys_sqlite_lock sqlite_lock(this);
288  sqliteConnector_->query("BEGIN TRANSACTION");
289  try {
290  sqliteConnector_->query(
291  "SELECT name FROM sqlite_master WHERE type='table' AND name='mapd_roles'");
292  if (sqliteConnector_->getNumRows() != 0) {
293  // already done
294  sqliteConnector_->query("END TRANSACTION");
295  return;
296  }
297  sqliteConnector_->query(
298  "CREATE TABLE mapd_roles(roleName text, userName text, UNIQUE(roleName, "
299  "userName))");
300  // need to account for old conversions where we are building and moving
301  // from pre version 4.0 and 'mapd' was default superuser
302  sqliteConnector_->query("SELECT name FROM mapd_users WHERE name NOT IN ( \'" +
303  OMNISCI_ROOT_USER + "\', 'mapd')");
304  size_t numRows = sqliteConnector_->getNumRows();
305  vector<string> user_names;
306  for (size_t i = 0; i < numRows; ++i) {
307  user_names.push_back(sqliteConnector_->getData<string>(i, 0));
308  }
309  for (const auto& user_name : user_names) {
310  // for each user, create a fake role with the same name
311  sqliteConnector_->query_with_text_params(
312  "INSERT INTO mapd_roles(roleName, userName) VALUES (?, ?)",
313  vector<string>{user_name, user_name});
314  }
315  } catch (const std::exception&) {
316  sqliteConnector_->query("ROLLBACK TRANSACTION");
317  throw;
318  }
319  sqliteConnector_->query("END TRANSACTION");
320 }
sqlite_lock< SysCatalog > sys_sqlite_lock
Definition: SysCatalog.cpp:81
std::unique_ptr< SqliteConnector > sqliteConnector_
Definition: SysCatalog.h:353
const std::string OMNISCI_ROOT_USER
Definition: SysCatalog.h:59
void Catalog_Namespace::SysCatalog::deleteObjectDescriptorMap ( const std::string &  roleName)
private

Definition at line 1808 of file SysCatalog.cpp.

1808  {
1809  sys_write_lock write_lock(this);
1810 
1811  for (auto d = objectDescriptorMap_.begin(); d != objectDescriptorMap_.end();) {
1812  if (d->second->roleName == roleName) {
1813  delete d->second;
1814  d = objectDescriptorMap_.erase(d);
1815  } else {
1816  d++;
1817  }
1818  }
1819 }
ObjectRoleDescriptorMap objectDescriptorMap_
Definition: SysCatalog.h:352
write_lock< SysCatalog > sys_write_lock
Definition: SysCatalog.cpp:80
void Catalog_Namespace::SysCatalog::deleteObjectDescriptorMap ( const std::string &  roleName,
DBObject object,
const Catalog_Namespace::Catalog cat 
)
private

Definition at line 1822 of file SysCatalog.cpp.

References Catalog_Namespace::DBMetadata::dbId, Catalog_Namespace::Catalog::getCurrentDB(), and to_string().

1824  {
1825  sys_write_lock write_lock(this);
1826  auto range = objectDescriptorMap_.equal_range(
1827  std::to_string(cat.getCurrentDB().dbId) + ":" +
1828  std::to_string(object.getObjectKey().permissionType) + ":" +
1829  std::to_string(object.getObjectKey().objectId));
1830  for (auto d = range.first; d != range.second;) {
1831  // remove the entry
1832  if (d->second->roleName == roleName) {
1833  delete d->second;
1834  d = objectDescriptorMap_.erase(d);
1835  } else {
1836  d++;
1837  }
1838  }
1839 }
ObjectRoleDescriptorMap objectDescriptorMap_
Definition: SysCatalog.h:352
std::string to_string(char const *&&v)
const DBMetadata & getCurrentDB() const
Definition: Catalog.h:182
write_lock< SysCatalog > sys_write_lock
Definition: SysCatalog.cpp:80

+ Here is the call graph for this function:

void Catalog_Namespace::SysCatalog::dropDatabase ( const DBMetadata db)

Definition at line 1140 of file SysCatalog.cpp.

References DashboardDBObjectType, Catalog_Namespace::DBMetadata::dbId, Catalog_Namespace::DBMetadata::dbName, TableDBObjectType, run_benchmark_import::tables, and to_string().

1140  {
1141  sys_write_lock write_lock(this);
1142  sys_sqlite_lock sqlite_lock(this);
1143  auto cat =
1145  sqliteConnector_->query("BEGIN TRANSACTION");
1146  try {
1147  // remove this database ID from any users that have it set as their default database
1148  sqliteConnector_->query_with_text_param(
1149  "UPDATE mapd_users SET default_db = NULL WHERE default_db = ?",
1150  std::to_string(db.dbId));
1151  /* revoke object privileges to all tables of the database being dropped */
1152  const auto tables = cat->getAllTableMetadata();
1153  for (const auto table : tables) {
1154  if (table->shard >= 0) {
1155  // skip shards, they're not standalone tables
1156  continue;
1157  }
1159  DBObject(table->tableName, TableDBObjectType), cat.get());
1160  }
1161  const auto dashboards = cat->getAllDashboardsMetadata();
1162  for (const auto dashboard : dashboards) {
1164  DBObject(dashboard->dashboardId, DashboardDBObjectType), cat.get());
1165  }
1166  /* revoke object privileges to the database being dropped */
1167  for (const auto& grantee : granteeMap_) {
1168  if (grantee.second->hasAnyPrivilegesOnDb(db.dbId, true)) {
1169  revokeAllOnDatabase_unsafe(grantee.second->getName(), db.dbId, grantee.second);
1170  }
1171  }
1172  sqliteConnector_->query_with_text_param("DELETE FROM mapd_databases WHERE dbid = ?",
1173  std::to_string(db.dbId));
1174  cat->eraseDBData();
1175  Catalog::remove(db.dbName);
1176  } catch (const std::exception&) {
1177  sqliteConnector_->query("ROLLBACK TRANSACTION");
1178  throw;
1179  }
1180  sqliteConnector_->query("END TRANSACTION");
1181 }
void revokeAllOnDatabase_unsafe(const std::string &roleName, int32_t dbId, Grantee *grantee)
static std::shared_ptr< Catalog > get(const std::string &dbName)
Definition: Catalog.cpp:3418
std::string to_string(char const *&&v)
sqlite_lock< SysCatalog > sys_sqlite_lock
Definition: SysCatalog.cpp:81
std::shared_ptr< Data_Namespace::DataMgr > dataMgr_
Definition: SysCatalog.h:355
write_lock< SysCatalog > sys_write_lock
Definition: SysCatalog.cpp:80
std::unique_ptr< SqliteConnector > sqliteConnector_
Definition: SysCatalog.h:353
static void remove(const std::string &dbName)
Definition: Catalog.cpp:3444
void revokeDBObjectPrivilegesFromAll_unsafe(DBObject object, Catalog *catalog)
std::vector< LeafHostInfo > string_dict_hosts_
Definition: SysCatalog.h:361
std::shared_ptr< Calcite > calciteMgr_
Definition: SysCatalog.h:360

+ Here is the call graph for this function:

void Catalog_Namespace::SysCatalog::dropRole ( const std::string &  roleName)

Definition at line 2162 of file SysCatalog.cpp.

2162  {
2164 }
void execInTransaction(F &&f, Args &&...args)
void dropRole_unsafe(const std::string &roleName)
void Catalog_Namespace::SysCatalog::dropRole_unsafe ( const std::string &  roleName)
private

Definition at line 1663 of file SysCatalog.cpp.

References to_upper().

1663  {
1664  sys_write_lock write_lock(this);
1665 
1666  // it may very well be a user "role", so keep it generic
1667  auto* rl = getGrantee(roleName);
1668  if (rl) { // admin super user may not exist in roles
1669  delete rl;
1670  }
1671  granteeMap_.erase(to_upper(roleName));
1672 
1673  sys_sqlite_lock sqlite_lock(this);
1674  sqliteConnector_->query_with_text_param("DELETE FROM mapd_roles WHERE roleName = ?",
1675  roleName);
1676  sqliteConnector_->query_with_text_param(
1677  "DELETE FROM mapd_object_permissions WHERE roleName = ?", roleName);
1678 }
Grantee * getGrantee(const std::string &name) const
sqlite_lock< SysCatalog > sys_sqlite_lock
Definition: SysCatalog.cpp:81
write_lock< SysCatalog > sys_write_lock
Definition: SysCatalog.cpp:80
std::unique_ptr< SqliteConnector > sqliteConnector_
Definition: SysCatalog.h:353
std::string to_upper(const std::string &str)

+ Here is the call graph for this function:

void Catalog_Namespace::SysCatalog::dropUser ( const std::string &  name)

Definition at line 824 of file SysCatalog.cpp.

References to_string(), and Catalog_Namespace::UserMetadata::userId.

824  {
825  sys_write_lock write_lock(this);
826  sys_sqlite_lock sqlite_lock(this);
827 
828  sqliteConnector_->query("BEGIN TRANSACTION");
829  try {
830  UserMetadata user;
831  if (!getMetadataForUser(name, user)) {
832  throw runtime_error("User " + name + " does not exist.");
833  }
836  const std::string& roleName(name);
837  sqliteConnector_->query_with_text_param("DELETE FROM mapd_roles WHERE userName = ?",
838  roleName);
839  sqliteConnector_->query("DELETE FROM mapd_users WHERE userid = " +
840  std::to_string(user.userId));
841  sqliteConnector_->query("DELETE FROM mapd_privileges WHERE userid = " +
842  std::to_string(user.userId));
843  } catch (const std::exception& e) {
844  sqliteConnector_->query("ROLLBACK TRANSACTION");
845  throw;
846  }
847  sqliteConnector_->query("END TRANSACTION");
848 }
bool getMetadataForUser(const std::string &name, UserMetadata &user)
std::string name() const
Definition: SysCatalog.h:262
std::string to_string(char const *&&v)
sqlite_lock< SysCatalog > sys_sqlite_lock
Definition: SysCatalog.cpp:81
write_lock< SysCatalog > sys_write_lock
Definition: SysCatalog.cpp:80
std::unique_ptr< SqliteConnector > sqliteConnector_
Definition: SysCatalog.h:353
void deleteObjectDescriptorMap(const std::string &roleName)
void dropRole_unsafe(const std::string &roleName)

+ Here is the call graph for this function:

template<typename F , typename... Args>
void Catalog_Namespace::SysCatalog::execInTransaction ( F &&  f,
Args &&...  args 
)
private

Definition at line 2145 of file SysCatalog.cpp.

References run_benchmark_import::args.

2145  {
2146  sys_write_lock write_lock(this);
2147  sys_sqlite_lock sqlite_lock(this);
2148  sqliteConnector_->query("BEGIN TRANSACTION");
2149  try {
2150  (this->*f)(std::forward<Args>(args)...);
2151  } catch (std::exception&) {
2152  sqliteConnector_->query("ROLLBACK TRANSACTION");
2153  throw;
2154  }
2155  sqliteConnector_->query("END TRANSACTION");
2156 }
sqlite_lock< SysCatalog > sys_sqlite_lock
Definition: SysCatalog.cpp:81
write_lock< SysCatalog > sys_write_lock
Definition: SysCatalog.cpp:80
std::unique_ptr< SqliteConnector > sqliteConnector_
Definition: SysCatalog.h:353
list< DBMetadata > Catalog_Namespace::SysCatalog::getAllDBMetadata ( )

Definition at line 1242 of file SysCatalog.cpp.

References Catalog_Namespace::DBMetadata::dbId, Catalog_Namespace::DBMetadata::dbName, and Catalog_Namespace::DBMetadata::dbOwner.

1242  {
1243  sys_sqlite_lock sqlite_lock(this);
1244  sqliteConnector_->query("SELECT dbid, name, owner FROM mapd_databases");
1245  int numRows = sqliteConnector_->getNumRows();
1246  list<DBMetadata> db_list;
1247  for (int r = 0; r < numRows; ++r) {
1248  DBMetadata db;
1249  db.dbId = sqliteConnector_->getData<int>(r, 0);
1250  db.dbName = sqliteConnector_->getData<string>(r, 1);
1251  db.dbOwner = sqliteConnector_->getData<int>(r, 2);
1252  db_list.push_back(db);
1253  }
1254  return db_list;
1255 }
sqlite_lock< SysCatalog > sys_sqlite_lock
Definition: SysCatalog.cpp:81
std::unique_ptr< SqliteConnector > sqliteConnector_
Definition: SysCatalog.h:353
list< UserMetadata > Catalog_Namespace::SysCatalog::getAllUserMetadata ( )

Definition at line 1298 of file SysCatalog.cpp.

References Catalog_Namespace::anonymous_namespace{SysCatalog.cpp}::get_users().

1298  {
1299  sys_sqlite_lock sqlite_lock(this);
1300  return get_users(sqliteConnector_);
1301 }
sqlite_lock< SysCatalog > sys_sqlite_lock
Definition: SysCatalog.cpp:81
std::unique_ptr< SqliteConnector > sqliteConnector_
Definition: SysCatalog.h:353
auto get_users(std::unique_ptr< SqliteConnector > &sqliteConnector, const int32_t dbId=-1)

+ Here is the call graph for this function:

list< UserMetadata > Catalog_Namespace::SysCatalog::getAllUserMetadata ( const int64_t  dbId)

return the users associated with the given DB

Definition at line 1291 of file SysCatalog.cpp.

References Catalog_Namespace::anonymous_namespace{SysCatalog.cpp}::get_users().

1291  {
1292  // this call is to return users that have some form of permissions to objects in the db
1293  // sadly mapd_object_permissions table is also misused to manage user roles.
1294  sys_sqlite_lock sqlite_lock(this);
1295  return get_users(sqliteConnector_, dbId);
1296 }
sqlite_lock< SysCatalog > sys_sqlite_lock
Definition: SysCatalog.cpp:81
std::unique_ptr< SqliteConnector > sqliteConnector_
Definition: SysCatalog.h:353
auto get_users(std::unique_ptr< SqliteConnector > &sqliteConnector, const int32_t dbId=-1)

+ Here is the call graph for this function:

const std::string& Catalog_Namespace::SysCatalog::getBasePath ( ) const
inline

Definition at line 191 of file SysCatalog.h.

References basePath_.

191 { return basePath_; }
Calcite& Catalog_Namespace::SysCatalog::getCalciteMgr ( ) const
inline

Definition at line 190 of file SysCatalog.h.

References calciteMgr_.

190 { return *calciteMgr_; }
std::shared_ptr< Calcite > calciteMgr_
Definition: SysCatalog.h:360
DBSummaryList Catalog_Namespace::SysCatalog::getDatabaseListForUser ( const UserMetadata user)

Definition at line 1366 of file SysCatalog.cpp.

References AccessPrivileges::ACCESS, DatabaseDBObjectType, Catalog_Namespace::DBSummary::dbName, DBObject::loadKey(), and DBObject::setPrivileges().

Referenced by ShowDatabasesCommand::execute().

1366  {
1367  DBSummaryList ret;
1368 
1369  std::list<Catalog_Namespace::DBMetadata> db_list = getAllDBMetadata();
1370  std::list<Catalog_Namespace::UserMetadata> user_list = getAllUserMetadata();
1371 
1372  for (auto d : db_list) {
1373  DBObject dbObject(d.dbName, DatabaseDBObjectType);
1374  dbObject.loadKey();
1375  dbObject.setPrivileges(AccessPrivileges::ACCESS);
1376  if (!checkPrivileges(user, std::vector<DBObject>{dbObject})) {
1377  continue;
1378  }
1379  for (auto u : user_list) {
1380  if (d.dbOwner == u.userId) {
1381  ret.emplace_back(DBSummary{d.dbName, u.userName});
1382  break;
1383  }
1384  }
1385  }
1386 
1387  return ret;
1388 }
bool checkPrivileges(const UserMetadata &user, const std::vector< DBObject > &privObjects) const
std::list< UserMetadata > getAllUserMetadata()
std::list< DBMetadata > getAllDBMetadata()
static const AccessPrivileges ACCESS
Definition: DBObject.h:153
std::list< DBSummary > DBSummaryList
Definition: SysCatalog.h:120

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Data_Namespace::DataMgr& Catalog_Namespace::SysCatalog::getDataMgr ( ) const
inline

Definition at line 189 of file SysCatalog.h.

References dataMgr_.

Referenced by Executor::clearMemory().

189 { return *dataMgr_; }
std::shared_ptr< Data_Namespace::DataMgr > dataMgr_
Definition: SysCatalog.h:355

+ Here is the caller graph for this function:

void Catalog_Namespace::SysCatalog::getDBObjectPrivileges ( const std::string &  granteeName,
DBObject object,
const Catalog_Namespace::Catalog catalog 
) const

Definition at line 1611 of file SysCatalog.cpp.

References Catalog_Namespace::UserMetadata::isSuper.

1613  {
1614  sys_read_lock read_lock(this);
1615  UserMetadata user_meta;
1616 
1617  if (instance().getMetadataForUser(granteeName, user_meta)) {
1618  if (user_meta.isSuper) {
1619  throw runtime_error(
1620  "Request to show privileges from " + granteeName +
1621  " failed because user is super user and has all privileges by default.");
1622  }
1623  }
1624  auto* grantee = instance().getGrantee(granteeName);
1625  if (!grantee) {
1626  throw runtime_error("Request to show privileges for " + granteeName +
1627  " failed because role or user with this name does not exist.");
1628  }
1629  object.loadKey(catalog);
1630  grantee->getPrivileges(object, true);
1631 }
read_lock< SysCatalog > sys_read_lock
Definition: SysCatalog.cpp:79
bool getMetadataForUser(const std::string &name, UserMetadata &user)
Grantee * getGrantee(const std::string &name) const
static SysCatalog & instance()
Definition: SysCatalog.h:256
Grantee * Catalog_Namespace::SysCatalog::getGrantee ( const std::string &  name) const

Definition at line 1890 of file SysCatalog.cpp.

References to_upper().

Referenced by Catalog_Namespace::Catalog::renameTable().

1890  {
1891  sys_read_lock read_lock(this);
1892  auto grantee = granteeMap_.find(to_upper(name));
1893  if (grantee == granteeMap_.end()) { // check to make sure role exists
1894  return nullptr;
1895  }
1896  return grantee->second; // returns pointer to role
1897 }
read_lock< SysCatalog > sys_read_lock
Definition: SysCatalog.cpp:79
std::string name() const
Definition: SysCatalog.h:262
std::string to_upper(const std::string &str)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::unordered_map< std::string, std::vector< std::string > > Catalog_Namespace::SysCatalog::getGranteesOfSharedDashboards ( const std::vector< std::string > &  dashboard_ids)

Definition at line 2277 of file SysCatalog.cpp.

References DashboardDBObjectType, num_rows, and to_string().

Referenced by Catalog_Namespace::Catalog::createDashboardSystemRoles().

2277  {
2278  sys_sqlite_lock sqlite_lock(this);
2279  std::unordered_map<std::string, std::vector<std::string>> active_grantees;
2280  sqliteConnector_->query("BEGIN TRANSACTION");
2281  try {
2282  for (auto dash : dashboard_ids) {
2283  std::vector<std::string> grantees = {};
2284  sqliteConnector_->query_with_text_params(
2285  "SELECT roleName FROM mapd_object_permissions WHERE objectPermissions NOT IN "
2286  "(0,1) AND objectPermissionsType = ? AND objectId = ?",
2287  std::vector<std::string>{
2288  std::to_string(static_cast<int32_t>(DashboardDBObjectType)), dash});
2289  int num_rows = sqliteConnector_->getNumRows();
2290  if (num_rows == 0) {
2291  // no grantees
2292  continue;
2293  } else {
2294  for (size_t i = 0; i < sqliteConnector_->getNumRows(); ++i) {
2295  grantees.push_back(sqliteConnector_->getData<string>(i, 0));
2296  }
2297  active_grantees[dash] = grantees;
2298  }
2299  }
2300  } catch (const std::exception& e) {
2301  sqliteConnector_->query("ROLLBACK TRANSACTION");
2302  throw;
2303  }
2304  sqliteConnector_->query("END TRANSACTION");
2305  return active_grantees;
2306 }
const int8_t const int64_t * num_rows
std::string to_string(char const *&&v)
sqlite_lock< SysCatalog > sys_sqlite_lock
Definition: SysCatalog.cpp:81
std::unique_ptr< SqliteConnector > sqliteConnector_
Definition: SysCatalog.h:353

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool Catalog_Namespace::SysCatalog::getMetadataForDB ( const std::string &  name,
DBMetadata db 
)

Definition at line 1337 of file SysCatalog.cpp.

References Catalog_Namespace::DBMetadata::dbId, Catalog_Namespace::DBMetadata::dbName, and Catalog_Namespace::DBMetadata::dbOwner.

1337  {
1338  sys_sqlite_lock sqlite_lock(this);
1339  sqliteConnector_->query_with_text_param(
1340  "SELECT dbid, name, owner FROM mapd_databases WHERE name = ?", name);
1341  int numRows = sqliteConnector_->getNumRows();
1342  if (numRows == 0) {
1343  return false;
1344  }
1345  db.dbId = sqliteConnector_->getData<int>(0, 0);
1346  db.dbName = sqliteConnector_->getData<string>(0, 1);
1347  db.dbOwner = sqliteConnector_->getData<int>(0, 2);
1348  return true;
1349 }
std::string name() const
Definition: SysCatalog.h:262
sqlite_lock< SysCatalog > sys_sqlite_lock
Definition: SysCatalog.cpp:81
std::unique_ptr< SqliteConnector > sqliteConnector_
Definition: SysCatalog.h:353
bool Catalog_Namespace::SysCatalog::getMetadataForDBById ( const int32_t  idIn,
DBMetadata db 
)

Definition at line 1351 of file SysCatalog.cpp.

References Catalog_Namespace::DBMetadata::dbId, Catalog_Namespace::DBMetadata::dbName, Catalog_Namespace::DBMetadata::dbOwner, and to_string().

1351  {
1352  sys_sqlite_lock sqlite_lock(this);
1353  sqliteConnector_->query_with_text_param(
1354  "SELECT dbid, name, owner FROM mapd_databases WHERE dbid = ?",
1355  std::to_string(idIn));
1356  int numRows = sqliteConnector_->getNumRows();
1357  if (numRows == 0) {
1358  return false;
1359  }
1360  db.dbId = sqliteConnector_->getData<int>(0, 0);
1361  db.dbName = sqliteConnector_->getData<string>(0, 1);
1362  db.dbOwner = sqliteConnector_->getData<int>(0, 2);
1363  return true;
1364 }
std::string to_string(char const *&&v)
sqlite_lock< SysCatalog > sys_sqlite_lock
Definition: SysCatalog.cpp:81
std::unique_ptr< SqliteConnector > sqliteConnector_
Definition: SysCatalog.h:353

+ Here is the call graph for this function:

std::vector< ObjectRoleDescriptor * > Catalog_Namespace::SysCatalog::getMetadataForObject ( int32_t  dbId,
int32_t  dbType,
int32_t  objectId 
) const

Definition at line 1908 of file SysCatalog.cpp.

References to_string().

Referenced by Catalog_Namespace::Catalog::renameTable().

1908  {
1909  sys_read_lock read_lock(this);
1910  std::vector<ObjectRoleDescriptor*> objectsList;
1911 
1912  auto range = objectDescriptorMap_.equal_range(std::to_string(dbId) + ":" +
1913  std::to_string(dbType) + ":" +
1914  std::to_string(objectId));
1915  for (auto d = range.first; d != range.second; ++d) {
1916  objectsList.push_back(d->second);
1917  }
1918  return objectsList; // return pointers to objects
1919 }
read_lock< SysCatalog > sys_read_lock
Definition: SysCatalog.cpp:79
ObjectRoleDescriptorMap objectDescriptorMap_
Definition: SysCatalog.h:352
std::string to_string(char const *&&v)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool Catalog_Namespace::SysCatalog::getMetadataForUser ( const std::string &  name,
UserMetadata user 
)

Definition at line 1224 of file SysCatalog.cpp.

References Catalog_Namespace::parseUserMetadataFromSQLite().

1224  {
1225  sys_sqlite_lock sqlite_lock(this);
1226  sqliteConnector_->query_with_text_param(
1227  "SELECT userid, name, passwd_hash, issuper, default_db, can_login FROM mapd_users "
1228  "WHERE name = ?",
1229  name);
1231 }
std::string name() const
Definition: SysCatalog.h:262
sqlite_lock< SysCatalog > sys_sqlite_lock
Definition: SysCatalog.cpp:81
static bool parseUserMetadataFromSQLite(const std::unique_ptr< SqliteConnector > &conn, UserMetadata &user)
std::unique_ptr< SqliteConnector > sqliteConnector_
Definition: SysCatalog.h:353

+ Here is the call graph for this function:

bool Catalog_Namespace::SysCatalog::getMetadataForUserById ( const int32_t  idIn,
UserMetadata user 
)

Definition at line 1233 of file SysCatalog.cpp.

References Catalog_Namespace::parseUserMetadataFromSQLite(), and to_string().

1233  {
1234  sys_sqlite_lock sqlite_lock(this);
1235  sqliteConnector_->query_with_text_param(
1236  "SELECT userid, name, passwd_hash, issuper, default_db, can_login FROM mapd_users "
1237  "WHERE userid = ?",
1238  std::to_string(idIn));
1240 }
std::string to_string(char const *&&v)
sqlite_lock< SysCatalog > sys_sqlite_lock
Definition: SysCatalog.cpp:81
static bool parseUserMetadataFromSQLite(const std::unique_ptr< SqliteConnector > &conn, UserMetadata &user)
std::unique_ptr< SqliteConnector > sqliteConnector_
Definition: SysCatalog.h:353

+ Here is the call graph for this function:

void Catalog_Namespace::SysCatalog::getMetadataWithDefaultDB ( std::string &  dbname,
const std::string &  username,
Catalog_Namespace::DBMetadata db_meta,
UserMetadata user_meta 
)

Definition at line 1303 of file SysCatalog.cpp.

References Catalog_Namespace::DBMetadata::dbName, Catalog_Namespace::UserMetadata::defaultDbId, OMNISCI_DEFAULT_DB, to_string(), Catalog_Namespace::UserMetadata::userId, and Catalog_Namespace::UserMetadata::userName.

1306  {
1307  if (!getMetadataForUser(username, user_meta)) {
1308  throw std::runtime_error("Invalid credentials.");
1309  }
1310 
1311  if (!dbname.empty()) {
1312  if (!getMetadataForDB(dbname, db_meta)) {
1313  throw std::runtime_error("Database name " + dbname + " does not exist.");
1314  }
1315  // loaded the requested database
1316  } else {
1317  if (user_meta.defaultDbId != -1) {
1318  if (!getMetadataForDBById(user_meta.defaultDbId, db_meta)) {
1319  throw std::runtime_error(
1320  "Server error: User #" + std::to_string(user_meta.userId) + " " +
1321  user_meta.userName + " has invalid default_db #" +
1322  std::to_string(user_meta.defaultDbId) + " which does not exist.");
1323  }
1324  dbname = db_meta.dbName;
1325  // loaded the user's default database
1326  } else {
1327  if (!getMetadataForDB(OMNISCI_DEFAULT_DB, db_meta)) {
1328  throw std::runtime_error(std::string("Database ") + OMNISCI_DEFAULT_DB +
1329  " does not exist.");
1330  }
1331  dbname = OMNISCI_DEFAULT_DB;
1332  // loaded the mapd database by default
1333  }
1334  }
1335 }
bool getMetadataForUser(const std::string &name, UserMetadata &user)
std::string to_string(char const *&&v)
const std::string OMNISCI_DEFAULT_DB
Definition: SysCatalog.h:58
bool getMetadataForDBById(const int32_t idIn, DBMetadata &db)
bool getMetadataForDB(const std::string &name, DBMetadata &db)

+ Here is the call graph for this function:

Role * Catalog_Namespace::SysCatalog::getRoleGrantee ( const std::string &  name) const

Definition at line 1899 of file SysCatalog.cpp.

Referenced by Catalog_Namespace::Catalog::createDashboardSystemRoles(), and Catalog_Namespace::Catalog::createOrUpdateDashboardSystemRole().

1899  {
1900  return dynamic_cast<Role*>(getGrantee(name));
1901 }
std::string name() const
Definition: SysCatalog.h:262
Grantee * getGrantee(const std::string &name) const
Definition: Grantee.h:76

+ Here is the caller graph for this function:

std::vector< std::string > Catalog_Namespace::SysCatalog::getRoles ( bool  userPrivateRole,
bool  isSuper,
const std::string &  userName 
)

Definition at line 1968 of file SysCatalog.cpp.

1970  {
1971  sys_read_lock read_lock(this);
1972  std::vector<std::string> roles;
1973  for (auto& grantee : granteeMap_) {
1974  if (!userPrivateRole && grantee.second->isUser()) {
1975  continue;
1976  }
1977  if (!isSuper && !isRoleGrantedToGrantee(userName, grantee.second->getName(), false)) {
1978  continue;
1979  }
1980  if (isDashboardSystemRole(grantee.second->getName())) {
1981  continue;
1982  }
1983  roles.push_back(grantee.second->getName());
1984  }
1985  return roles;
1986 }
read_lock< SysCatalog > sys_read_lock
Definition: SysCatalog.cpp:79
bool isDashboardSystemRole(const std::string &roleName)
bool isRoleGrantedToGrantee(const std::string &granteeName, const std::string &roleName, bool only_direct) const
std::vector< std::string > Catalog_Namespace::SysCatalog::getRoles ( const std::string &  userName,
const int32_t  dbId 
)

Definition at line 1947 of file SysCatalog.cpp.

References to_string().

1948  {
1949  sys_sqlite_lock sqlite_lock(this);
1950  std::string sql =
1951  "SELECT DISTINCT roleName FROM mapd_object_permissions WHERE "
1952  "objectPermissions<>0 "
1953  "AND roleType=0 AND dbId=" +
1954  std::to_string(dbId);
1955  sqliteConnector_->query(sql);
1956  int numRows = sqliteConnector_->getNumRows();
1957  std::vector<std::string> roles(0);
1958  for (int r = 0; r < numRows; ++r) {
1959  auto roleName = sqliteConnector_->getData<string>(r, 0);
1960  if (isRoleGrantedToGrantee(userName, roleName, false) &&
1961  !isDashboardSystemRole(roleName)) {
1962  roles.push_back(roleName);
1963  }
1964  }
1965  return roles;
1966 }
bool isDashboardSystemRole(const std::string &roleName)
std::string to_string(char const *&&v)
sqlite_lock< SysCatalog > sys_sqlite_lock
Definition: SysCatalog.cpp:81
std::unique_ptr< SqliteConnector > sqliteConnector_
Definition: SysCatalog.h:353
bool isRoleGrantedToGrantee(const std::string &granteeName, const std::string &roleName, bool only_direct) const

+ Here is the call graph for this function:

SqliteConnector* Catalog_Namespace::SysCatalog::getSqliteConnector ( )
inline

Definition at line 192 of file SysCatalog.h.

References sqliteConnector_.

192 { return sqliteConnector_.get(); }
std::unique_ptr< SqliteConnector > sqliteConnector_
Definition: SysCatalog.h:353
User * Catalog_Namespace::SysCatalog::getUserGrantee ( const std::string &  name) const

Definition at line 1903 of file SysCatalog.cpp.

1903  {
1904  return dynamic_cast<User*>(getGrantee(name));
1905 }
std::string name() const
Definition: SysCatalog.h:262
Definition: Grantee.h:70
Grantee * getGrantee(const std::string &name) const
void Catalog_Namespace::SysCatalog::grantAllOnDatabase_unsafe ( const std::string &  roleName,
DBObject object,
const Catalog_Namespace::Catalog catalog 
)
private

Definition at line 1482 of file SysCatalog.cpp.

References AccessPrivileges::ACCESS, AccessPrivileges::ALL_DASHBOARD, AccessPrivileges::ALL_SERVER, AccessPrivileges::ALL_TABLE, AccessPrivileges::ALL_VIEW, DashboardDBObjectType, DatabaseDBObjectType, ServerDBObjectType, DBObject::setPermissionType(), DBObject::setPrivileges(), TableDBObjectType, AccessPrivileges::VIEW_SQL_EDITOR, and ViewDBObjectType.

1484  {
1485  // It's a separate use case because it's easier for implementation to convert ALL ON
1486  // DATABASE into ALL ON DASHBOARDS, ALL ON VIEWS and ALL ON TABLES
1487  // Add DB Access privileges
1488  DBObject tmp_object = object;
1491  grantDBObjectPrivileges_unsafe(roleName, tmp_object, catalog);
1494  grantDBObjectPrivileges_unsafe(roleName, tmp_object, catalog);
1497  grantDBObjectPrivileges_unsafe(roleName, tmp_object, catalog);
1499  tmp_object.setPermissionType(ViewDBObjectType);
1500  grantDBObjectPrivileges_unsafe(roleName, tmp_object, catalog);
1503  grantDBObjectPrivileges_unsafe(roleName, tmp_object, catalog);
1506  grantDBObjectPrivileges_unsafe(roleName, tmp_object, catalog);
1507  return;
1508 }
static const AccessPrivileges VIEW_SQL_EDITOR
Definition: DBObject.h:152
void setPrivileges(const AccessPrivileges &privs)
Definition: DBObject.h:215
void grantDBObjectPrivileges_unsafe(const std::string &granteeName, const DBObject object, const Catalog_Namespace::Catalog &catalog)
static const AccessPrivileges ALL_VIEW
Definition: DBObject.h:177
void setPermissionType(const DBObjectType &permissionType)
Definition: DBObject.cpp:147
static const AccessPrivileges ALL_SERVER
Definition: DBObject.h:187
static const AccessPrivileges ALL_DASHBOARD
Definition: DBObject.h:169
static const AccessPrivileges ACCESS
Definition: DBObject.h:153
static const AccessPrivileges ALL_TABLE
Definition: DBObject.h:157

+ Here is the call graph for this function:

void Catalog_Namespace::SysCatalog::grantDBObjectPrivileges ( const std::string &  grantee,
const DBObject object,
const Catalog_Namespace::Catalog catalog 
)

Definition at line 2184 of file SysCatalog.cpp.

2186  {
2188  &SysCatalog::grantDBObjectPrivileges_unsafe, grantee, object, catalog);
2189 }
void grantDBObjectPrivileges_unsafe(const std::string &granteeName, const DBObject object, const Catalog_Namespace::Catalog &catalog)
void execInTransaction(F &&f, Args &&...args)
void Catalog_Namespace::SysCatalog::grantDBObjectPrivileges_unsafe ( const std::string &  granteeName,
const DBObject  object,
const Catalog_Namespace::Catalog catalog 
)
private

Definition at line 1445 of file SysCatalog.cpp.

References DatabasePrivileges::ALL, DatabaseDBObjectType, Catalog_Namespace::insertOrUpdateObjectPrivileges(), and Catalog_Namespace::UserMetadata::isSuper.

1448  {
1449  object.loadKey(catalog);
1450  if (object.getPrivileges().hasPermission(DatabasePrivileges::ALL) &&
1451  object.getObjectKey().permissionType == DatabaseDBObjectType) {
1452  return grantAllOnDatabase_unsafe(granteeName, object, catalog);
1453  }
1454 
1455  sys_write_lock write_lock(this);
1456 
1457  UserMetadata user_meta;
1458  if (instance().getMetadataForUser(granteeName, user_meta)) {
1459  if (user_meta.isSuper) {
1460  // super doesn't have explicit privileges so nothing to do
1461  return;
1462  }
1463  }
1464  auto* grantee = instance().getGrantee(granteeName);
1465  if (!grantee) {
1466  throw runtime_error("Request to grant privileges to " + granteeName +
1467  " failed because role or user with this name does not exist.");
1468  }
1469  grantee->grantPrivileges(object);
1470 
1471  /* apply grant privileges statement to sqlite DB */
1472  std::vector<std::string> objectKey = object.toString();
1473  object.resetPrivileges();
1474  grantee->getPrivileges(object, true);
1475 
1476  sys_sqlite_lock sqlite_lock(this);
1478  sqliteConnector_, granteeName, grantee->isUser(), object);
1479  updateObjectDescriptorMap(granteeName, object, grantee->isUser(), catalog);
1480 }
static const int32_t ALL
Definition: DBObject.h:79
bool getMetadataForUser(const std::string &name, UserMetadata &user)
Grantee * getGrantee(const std::string &name) const
void insertOrUpdateObjectPrivileges(std::unique_ptr< SqliteConnector > &sqliteConnector, std::string roleName, bool userRole, DBObject &object)
Definition: SysCatalog.cpp:341
sqlite_lock< SysCatalog > sys_sqlite_lock
Definition: SysCatalog.cpp:81
static SysCatalog & instance()
Definition: SysCatalog.h:256
void grantAllOnDatabase_unsafe(const std::string &roleName, DBObject &object, const Catalog_Namespace::Catalog &catalog)
write_lock< SysCatalog > sys_write_lock
Definition: SysCatalog.cpp:80
std::unique_ptr< SqliteConnector > sqliteConnector_
Definition: SysCatalog.h:353
void updateObjectDescriptorMap(const std::string &roleName, DBObject &object, bool roleType, const Catalog_Namespace::Catalog &cat)

+ Here is the call graph for this function:

void Catalog_Namespace::SysCatalog::grantDBObjectPrivilegesBatch ( const std::vector< std::string > &  grantees,
const std::vector< DBObject > &  objects,
const Catalog_Namespace::Catalog catalog 
)

Definition at line 2191 of file SysCatalog.cpp.

Referenced by Catalog_Namespace::Catalog::createOrUpdateDashboardSystemRole().

2193  {
2195  &SysCatalog::grantDBObjectPrivilegesBatch_unsafe, grantees, objects, catalog);
2196 }
void grantDBObjectPrivilegesBatch_unsafe(const std::vector< std::string > &grantees, const std::vector< DBObject > &objects, const Catalog_Namespace::Catalog &catalog)
void execInTransaction(F &&f, Args &&...args)

+ Here is the caller graph for this function:

void Catalog_Namespace::SysCatalog::grantDBObjectPrivilegesBatch_unsafe ( const std::vector< std::string > &  grantees,
const std::vector< DBObject > &  objects,
const Catalog_Namespace::Catalog catalog 
)
private

Definition at line 1433 of file SysCatalog.cpp.

1436  {
1437  for (const auto& grantee : grantees) {
1438  for (const auto& object : objects) {
1439  grantDBObjectPrivileges_unsafe(grantee, object, catalog);
1440  }
1441  }
1442 }
void grantDBObjectPrivileges_unsafe(const std::string &granteeName, const DBObject object, const Catalog_Namespace::Catalog &catalog)
void Catalog_Namespace::SysCatalog::grantDefaultPrivilegesToRole_unsafe ( const std::string &  name,
bool  issuper 
)
private
void Catalog_Namespace::SysCatalog::grantRole ( const std::string &  role,
const std::string &  grantee 
)

Definition at line 2171 of file SysCatalog.cpp.

2171  {
2173 }
void grantRole_unsafe(const std::string &roleName, const std::string &granteeName)
void execInTransaction(F &&f, Args &&...args)
void Catalog_Namespace::SysCatalog::grantRole_unsafe ( const std::string &  roleName,
const std::string &  granteeName 
)
private

Definition at line 1690 of file SysCatalog.cpp.

1691  {
1692  auto* rl = getRoleGrantee(roleName);
1693  if (!rl) {
1694  throw runtime_error("Request to grant role " + roleName +
1695  " failed because role with this name does not exist.");
1696  }
1697  auto* grantee = getGrantee(granteeName);
1698  if (!grantee) {
1699  throw runtime_error("Request to grant role " + roleName + " failed because grantee " +
1700  granteeName + " does not exist.");
1701  }
1702  sys_write_lock write_lock(this);
1703  if (!grantee->hasRole(rl, true)) {
1704  grantee->grantRole(rl);
1705  sys_sqlite_lock sqlite_lock(this);
1706  sqliteConnector_->query_with_text_params(
1707  "INSERT INTO mapd_roles(roleName, userName) VALUES (?, ?)",
1708  std::vector<std::string>{rl->getName(), grantee->getName()});
1709  }
1710 }
Grantee * getGrantee(const std::string &name) const
sqlite_lock< SysCatalog > sys_sqlite_lock
Definition: SysCatalog.cpp:81
write_lock< SysCatalog > sys_write_lock
Definition: SysCatalog.cpp:80
Role * getRoleGrantee(const std::string &name) const
std::unique_ptr< SqliteConnector > sqliteConnector_
Definition: SysCatalog.h:353
void Catalog_Namespace::SysCatalog::grantRoleBatch ( const std::vector< std::string > &  roles,
const std::vector< std::string > &  grantees 
)

Definition at line 2166 of file SysCatalog.cpp.

Referenced by Catalog_Namespace::Catalog::createDashboardSystemRoles().

2167  {
2169 }
void grantRoleBatch_unsafe(const std::vector< std::string > &roles, const std::vector< std::string > &grantees)
void execInTransaction(F &&f, Args &&...args)

+ Here is the caller graph for this function:

void Catalog_Namespace::SysCatalog::grantRoleBatch_unsafe ( const std::vector< std::string > &  roles,
const std::vector< std::string > &  grantees 
)
private

Definition at line 1680 of file SysCatalog.cpp.

1681  {
1682  for (const auto& role : roles) {
1683  for (const auto& grantee : grantees) {
1684  grantRole_unsafe(role, grantee);
1685  }
1686  }
1687 }
void grantRole_unsafe(const std::string &roleName, const std::string &granteeName)
bool Catalog_Namespace::SysCatalog::hasAnyPrivileges ( const UserMetadata user,
std::vector< DBObject > &  privObjects 
)

Definition at line 1841 of file SysCatalog.cpp.

References Catalog_Namespace::UserMetadata::isSuper, and Catalog_Namespace::UserMetadata::userName.

1842  {
1843  sys_read_lock read_lock(this);
1844  if (user.isSuper) {
1845  return true;
1846  }
1847  auto* user_rl = instance().getUserGrantee(user.userName);
1848  if (!user_rl) {
1849  throw runtime_error("User " + user.userName + " does not exist.");
1850  }
1851  for (std::vector<DBObject>::iterator objectIt = privObjects.begin();
1852  objectIt != privObjects.end();
1853  ++objectIt) {
1854  if (!user_rl->hasAnyPrivileges(*objectIt, false)) {
1855  return false;
1856  }
1857  }
1858  return true;
1859 }
read_lock< SysCatalog > sys_read_lock
Definition: SysCatalog.cpp:79
static SysCatalog & instance()
Definition: SysCatalog.h:256
User * getUserGrantee(const std::string &name) const
void Catalog_Namespace::SysCatalog::importDataFromOldMapdDB ( )
private

Definition at line 237 of file SysCatalog.cpp.

References logger::ERROR, logger::INFO, LOG, and OMNISCI_SYSTEM_CATALOG.

237  {
238  sys_sqlite_lock sqlite_lock(this);
239  std::string mapd_db_path = basePath_ + "/mapd_catalogs/mapd";
240  sqliteConnector_->query("ATTACH DATABASE `" + mapd_db_path + "` as old_cat");
241  sqliteConnector_->query("BEGIN TRANSACTION");
242  LOG(INFO) << "Moving global metadata into a separate catalog";
243  try {
244  auto moveTableIfExists = [conn = sqliteConnector_.get()](const std::string& tableName,
245  bool deleteOld = true) {
246  conn->query("SELECT sql FROM old_cat.sqlite_master WHERE type='table' AND name='" +
247  tableName + "'");
248  if (conn->getNumRows() != 0) {
249  conn->query(conn->getData<string>(0, 0));
250  conn->query("INSERT INTO " + tableName + " SELECT * FROM old_cat." + tableName);
251  if (deleteOld) {
252  conn->query("DROP TABLE old_cat." + tableName);
253  }
254  }
255  };
256  moveTableIfExists("mapd_users");
257  moveTableIfExists("mapd_databases");
258  moveTableIfExists("mapd_roles");
259  moveTableIfExists("mapd_object_permissions");
260  moveTableIfExists("mapd_privileges");
261  moveTableIfExists("mapd_version_history", false);
262  } catch (const std::exception& e) {
263  LOG(ERROR) << "Failed to move global metadata into a separate catalog: " << e.what();
264  sqliteConnector_->query("ROLLBACK TRANSACTION");
265  try {
266  sqliteConnector_->query("DETACH DATABASE old_cat");
267  } catch (const std::exception&) {
268  // nothing to do here
269  }
270  throw;
271  }
272  sqliteConnector_->query("END TRANSACTION");
273  const std::string sys_catalog_path =
274  basePath_ + "/mapd_catalogs/" + OMNISCI_SYSTEM_CATALOG;
275  LOG(INFO) << "Global metadata has been successfully moved into a separate catalog: "
276  << sys_catalog_path
277  << ". Using this database with an older version of omnisci_server "
278  "is now impossible.";
279  try {
280  sqliteConnector_->query("DETACH DATABASE old_cat");
281  } catch (const std::exception&) {
282  // nothing to do here
283  }
284 }
#define LOG(tag)
Definition: Logger.h:188
const std::string OMNISCI_SYSTEM_CATALOG
Definition: SysCatalog.h:57
sqlite_lock< SysCatalog > sys_sqlite_lock
Definition: SysCatalog.cpp:81
std::unique_ptr< SqliteConnector > sqliteConnector_
Definition: SysCatalog.h:353
void Catalog_Namespace::SysCatalog::init ( const std::string &  basePath,
std::shared_ptr< Data_Namespace::DataMgr dataMgr,
const AuthMetadata authMetadata,
std::shared_ptr< Calcite calcite,
bool  is_new_db,
bool  aggregator,
const std::vector< LeafHostInfo > &  string_dict_hosts 
)

Definition at line 112 of file SysCatalog.cpp.

References OMNISCI_SYSTEM_CATALOG.

118  {
119  {
120  sys_write_lock write_lock(this);
121  sys_sqlite_lock sqlite_lock(this);
122 
123  basePath_ = basePath;
124  dataMgr_ = dataMgr;
125  authMetadata_ = &authMetadata;
128  pki_server_.reset(new PkiServer(*authMetadata_));
129  calciteMgr_ = calcite;
130  string_dict_hosts_ = string_dict_hosts;
131  aggregator_ = aggregator;
132  bool db_exists =
133  boost::filesystem::exists(basePath_ + "/mapd_catalogs/" + OMNISCI_SYSTEM_CATALOG);
134  sqliteConnector_.reset(
135  new SqliteConnector(OMNISCI_SYSTEM_CATALOG, basePath_ + "/mapd_catalogs/"));
136  if (is_new_db) {
137  initDB();
138  } else {
139  if (!db_exists) {
141  }
143  }
144  buildRoleMap();
147  }
148 }
std::unique_ptr< LdapServer > ldap_server_
Definition: SysCatalog.h:356
std::unique_ptr< RestServer > rest_server_
Definition: SysCatalog.h:357
const std::string OMNISCI_SYSTEM_CATALOG
Definition: SysCatalog.h:57
std::unique_ptr< PkiServer > pki_server_
Definition: SysCatalog.h:358
const AuthMetadata * authMetadata_
Definition: SysCatalog.h:359
sqlite_lock< SysCatalog > sys_sqlite_lock
Definition: SysCatalog.cpp:81
std::shared_ptr< Data_Namespace::DataMgr > dataMgr_
Definition: SysCatalog.h:355
write_lock< SysCatalog > sys_write_lock
Definition: SysCatalog.cpp:80
std::unique_ptr< SqliteConnector > sqliteConnector_
Definition: SysCatalog.h:353
std::vector< LeafHostInfo > string_dict_hosts_
Definition: SysCatalog.h:361
std::shared_ptr< Calcite > calciteMgr_
Definition: SysCatalog.h:360
void Catalog_Namespace::SysCatalog::initDB ( )
private

Definition at line 164 of file SysCatalog.cpp.

References anonymous_namespace{SysCatalog.cpp}::hash_with_bcrypt(), OMNISCI_DEFAULT_DB, OMNISCI_ROOT_PASSWD_DEFAULT, OMNISCI_ROOT_USER, OMNISCI_ROOT_USER_ID, and OMNISCI_ROOT_USER_ID_STR.

164  {
165  sys_sqlite_lock sqlite_lock(this);
166  sqliteConnector_->query("BEGIN TRANSACTION");
167  try {
168  sqliteConnector_->query(
169  "CREATE TABLE mapd_users (userid integer primary key, name text unique, "
170  "passwd_hash text, issuper boolean, default_db integer references "
171  "mapd_databases, can_login boolean)");
172  sqliteConnector_->query_with_text_params(
173  "INSERT INTO mapd_users VALUES (?, ?, ?, 1, NULL, 1)",
174  std::vector<std::string>{OMNISCI_ROOT_USER_ID_STR,
177  sqliteConnector_->query(
178  "CREATE TABLE mapd_databases (dbid integer primary key, name text unique, owner "
179  "integer references mapd_users)");
180  sqliteConnector_->query(
181  "CREATE TABLE mapd_roles(roleName text, userName text, UNIQUE(roleName, "
182  "userName))");
183  sqliteConnector_->query(
184  "CREATE TABLE mapd_object_permissions ("
185  "roleName text, "
186  "roleType bool, "
187  "dbId integer references mapd_databases, "
188  "objectName text, "
189  "objectId integer, "
190  "objectPermissionsType integer, "
191  "objectPermissions integer, "
192  "objectOwnerId integer, UNIQUE(roleName, objectPermissionsType, dbId, "
193  "objectId))");
194  } catch (const std::exception&) {
195  sqliteConnector_->query("ROLLBACK TRANSACTION");
196  throw;
197  }
198  sqliteConnector_->query("END TRANSACTION");
200 }
sqlite_lock< SysCatalog > sys_sqlite_lock
Definition: SysCatalog.cpp:81
const std::string OMNISCI_DEFAULT_DB
Definition: SysCatalog.h:58
std::string hash_with_bcrypt(const std::string &pwd)
Definition: SysCatalog.cpp:66
std::unique_ptr< SqliteConnector > sqliteConnector_
Definition: SysCatalog.h:353
const std::string OMNISCI_ROOT_USER
Definition: SysCatalog.h:59
const std::string OMNISCI_ROOT_PASSWD_DEFAULT
Definition: SysCatalog.h:62
const int OMNISCI_ROOT_USER_ID
Definition: SysCatalog.h:60
const std::string OMNISCI_ROOT_USER_ID_STR
Definition: SysCatalog.h:61
void createDatabase(const std::string &dbname, int owner)

+ Here is the call graph for this function:

static SysCatalog& Catalog_Namespace::SysCatalog::instance ( )
inlinestatic
bool Catalog_Namespace::SysCatalog::isAggregator ( ) const
inline

Definition at line 255 of file SysCatalog.h.

References aggregator_.

255 { return aggregator_; }
bool Catalog_Namespace::SysCatalog::isDashboardSystemRole ( const std::string &  roleName)
private

Definition at line 1943 of file SysCatalog.cpp.

References SYSTEM_ROLE_TAG().

1943  {
1944  return boost::algorithm::ends_with(roleName, SYSTEM_ROLE_TAG);
1945 }
static const std::string SYSTEM_ROLE_TAG("#dash_system_role")
specifies the content in-memory of a row in the dashboard

+ Here is the call graph for this function:

bool Catalog_Namespace::SysCatalog::isRoleGrantedToGrantee ( const std::string &  granteeName,
const std::string &  roleName,
bool  only_direct 
) const

Definition at line 1921 of file SysCatalog.cpp.

References CHECK().

1923  {
1924  sys_read_lock read_lock(this);
1925  if (roleName == granteeName) {
1926  return true;
1927  }
1928  bool is_role_granted = false;
1929  auto* target_role = instance().getRoleGrantee(roleName);
1930  auto has_role = [&](auto grantee_rl) {
1931  is_role_granted = target_role && grantee_rl->hasRole(target_role, only_direct);
1932  };
1933  if (auto* user_role = instance().getUserGrantee(granteeName); user_role) {
1934  has_role(user_role);
1935  } else if (auto* role = instance().getRoleGrantee(granteeName); role) {
1936  has_role(role);
1937  } else {
1938  CHECK(false);
1939  }
1940  return is_role_granted;
1941 }
read_lock< SysCatalog > sys_read_lock
Definition: SysCatalog.cpp:79
static SysCatalog & instance()
Definition: SysCatalog.h:256
CHECK(cgen_state)
Role * getRoleGrantee(const std::string &name) const
User * getUserGrantee(const std::string &name) const
bool hasRole(Role *role, bool only_direct) const
Definition: Grantee.cpp:42

+ Here is the call graph for this function:

std::shared_ptr< Catalog > Catalog_Namespace::SysCatalog::login ( std::string &  db,
std::string &  username,
const std::string &  password,
UserMetadata user_meta,
bool  check_password = true 
)

logins (connects) a user against a database.

throws a std::exception in all error cases! (including wrong password)

Definition at line 705 of file SysCatalog.cpp.

References Catalog_Namespace::UserMetadata::can_login.

709  {
710  // NOTE(sy): The dbname isn't const because getMetadataWithDefaultDB()
711  // can reset it. The username isn't const because SamlServer's
712  // login()/authenticate_user() can reset it.
713 
714  sys_write_lock write_lock(this);
715  if (check_password) {
716  loginImpl(username, password, user_meta);
717  } else { // not checking for password so user must exist
718  if (!getMetadataForUser(username, user_meta)) {
719  throw std::runtime_error("Invalid credentials.");
720  }
721  }
722  // we should have a user and user_meta by now
723  if (!user_meta.can_login) {
724  throw std::runtime_error("Unauthorized Access: User " + username + " is deactivated");
725  }
727  getMetadataWithDefaultDB(dbname, username, db_meta, user_meta);
728  return Catalog::get(
729  basePath_, db_meta, dataMgr_, string_dict_hosts_, calciteMgr_, false);
730 }
static std::shared_ptr< Catalog > get(const std::string &dbName)
Definition: Catalog.cpp:3418
bool getMetadataForUser(const std::string &name, UserMetadata &user)
void loginImpl(std::string &username, const std::string &password, UserMetadata &user_meta)
Definition: SysCatalog.cpp:733
void getMetadataWithDefaultDB(std::string &dbname, const std::string &username, Catalog_Namespace::DBMetadata &db_meta, UserMetadata &user_meta)
std::shared_ptr< Data_Namespace::DataMgr > dataMgr_
Definition: SysCatalog.h:355
write_lock< SysCatalog > sys_write_lock
Definition: SysCatalog.cpp:80
std::vector< LeafHostInfo > string_dict_hosts_
Definition: SysCatalog.h:361
std::shared_ptr< Calcite > calciteMgr_
Definition: SysCatalog.h:360
void Catalog_Namespace::SysCatalog::loginImpl ( std::string &  username,
const std::string &  password,
UserMetadata user_meta 
)
private

Definition at line 733 of file SysCatalog.cpp.

735  {
736  if (!checkPasswordForUser(password, username, user_meta)) {
737  throw std::runtime_error("Authentication failure");
738  }
739 }
bool checkPasswordForUser(const std::string &passwd, std::string &name, UserMetadata &user)
void Catalog_Namespace::SysCatalog::migrateDBAccessPrivileges ( )
private

Definition at line 610 of file SysCatalog.cpp.

References AccessPrivileges::ACCESS, CHECK(), DatabaseDBObjectType, DBObjectKey::dbId, Catalog_Namespace::DBMetadata::dbId, Catalog_Namespace::DBMetadata::dbName, Catalog_Namespace::DBMetadata::dbOwner, logger::ERROR, logger::INFO, Catalog_Namespace::insertOrUpdateObjectPrivileges(), LOG, MAPD_VERSION, OMNISCI_ROOT_USER_ID, DBObjectKey::permissionType, DBObject::setName(), DBObject::setObjectType(), to_string(), DBObject::updatePrivileges(), and AccessPrivileges::VIEW_SQL_EDITOR.

610  {
611  sys_sqlite_lock sqlite_lock(this);
612  sqliteConnector_->query("BEGIN TRANSACTION");
613  try {
614  sqliteConnector_->query(
615  "select name from sqlite_master WHERE type='table' AND "
616  "name='mapd_version_history'");
617  if (sqliteConnector_->getNumRows() == 0) {
618  sqliteConnector_->query(
619  "CREATE TABLE mapd_version_history(version integer, migration_history text "
620  "unique)");
621  } else {
622  sqliteConnector_->query(
623  "select * from mapd_version_history where migration_history = "
624  "'db_access_privileges'");
625  if (sqliteConnector_->getNumRows() != 0) {
626  // both privileges migrated
627  // no need for further execution
628  sqliteConnector_->query("END TRANSACTION");
629  return;
630  }
631  }
632  // Insert check for migration
633  sqliteConnector_->query_with_text_params(
634  "INSERT INTO mapd_version_history(version, migration_history) values(?,?)",
635  std::vector<std::string>{std::to_string(MAPD_VERSION), "db_access_privileges"});
636 
637  sqliteConnector_->query("select dbid, name from mapd_databases");
638  std::unordered_map<int, string> databases;
639  for (size_t i = 0; i < sqliteConnector_->getNumRows(); ++i) {
640  databases[sqliteConnector_->getData<int>(i, 0)] =
641  sqliteConnector_->getData<string>(i, 1);
642  }
643 
644  sqliteConnector_->query("select userid, name from mapd_users");
645  std::unordered_map<int, string> users;
646  for (size_t i = 0; i < sqliteConnector_->getNumRows(); ++i) {
647  users[sqliteConnector_->getData<int>(i, 0)] =
648  sqliteConnector_->getData<string>(i, 1);
649  }
650 
651  // All existing users by default will be granted DB Access permissions
652  // and view sql editor privileges
653  DBMetadata dbmeta;
654  for (auto db_ : databases) {
655  CHECK(SysCatalog::instance().getMetadataForDB(db_.second, dbmeta));
656  for (auto user : users) {
657  if (user.first != OMNISCI_ROOT_USER_ID) {
658  {
659  DBObjectKey key;
661  key.dbId = dbmeta.dbId;
662 
663  // access permission;
664  DBObject object_access(key, AccessPrivileges::ACCESS, dbmeta.dbOwner);
665  object_access.setObjectType(DBObjectType::DatabaseDBObjectType);
666  object_access.setName(dbmeta.dbName);
667  // sql_editor permission
668  DBObject object_editor(
669  key, AccessPrivileges::VIEW_SQL_EDITOR, dbmeta.dbOwner);
670  object_editor.setObjectType(DBObjectType::DatabaseDBObjectType);
671  object_editor.setName(dbmeta.dbName);
672  object_editor.updatePrivileges(object_access);
674  sqliteConnector_, user.second, true, object_editor);
675  }
676  }
677  }
678  }
679  } catch (const std::exception& e) {
680  LOG(ERROR) << "Failed to migrate db access privileges: " << e.what();
681  sqliteConnector_->query("ROLLBACK TRANSACTION");
682  throw;
683  }
684  sqliteConnector_->query("END TRANSACTION");
685  LOG(INFO) << "Successfully migrated db access privileges";
686 }
static const AccessPrivileges VIEW_SQL_EDITOR
Definition: DBObject.h:152
#define LOG(tag)
Definition: Logger.h:188
void insertOrUpdateObjectPrivileges(std::unique_ptr< SqliteConnector > &sqliteConnector, std::string roleName, bool userRole, DBObject &object)
Definition: SysCatalog.cpp:341
std::string to_string(char const *&&v)
sqlite_lock< SysCatalog > sys_sqlite_lock
Definition: SysCatalog.cpp:81
static SysCatalog & instance()
Definition: SysCatalog.h:256
CHECK(cgen_state)
static const int32_t MAPD_VERSION
Definition: release.h:33
std::unique_ptr< SqliteConnector > sqliteConnector_
Definition: SysCatalog.h:353
const int OMNISCI_ROOT_USER_ID
Definition: SysCatalog.h:60
int32_t dbId
Definition: DBObject.h:56
static const AccessPrivileges ACCESS
Definition: DBObject.h:153
int32_t permissionType
Definition: DBObject.h:55
bool getMetadataForDB(const std::string &name, DBMetadata &db)

+ Here is the call graph for this function:

void Catalog_Namespace::SysCatalog::migratePrivileged_old ( )
private

Definition at line 688 of file SysCatalog.cpp.

688  {
689  sys_sqlite_lock sqlite_lock(this);
690 
691  sqliteConnector_->query("BEGIN TRANSACTION");
692  try {
693  sqliteConnector_->query(
694  "CREATE TABLE IF NOT EXISTS mapd_privileges (userid integer references "
695  "mapd_users, dbid integer references "
696  "mapd_databases, select_priv boolean, insert_priv boolean, UNIQUE(userid, "
697  "dbid))");
698  } catch (const std::exception& e) {
699  sqliteConnector_->query("ROLLBACK TRANSACTION");
700  throw;
701  }
702  sqliteConnector_->query("END TRANSACTION");
703 }
sqlite_lock< SysCatalog > sys_sqlite_lock
Definition: SysCatalog.cpp:81
std::unique_ptr< SqliteConnector > sqliteConnector_
Definition: SysCatalog.h:353
void Catalog_Namespace::SysCatalog::migratePrivileges ( )
private

Definition at line 371 of file SysCatalog.cpp.

References AccessPrivileges::ALL_DASHBOARD_MIGRATE, AccessPrivileges::ALL_TABLE_MIGRATE, AccessPrivileges::ALL_VIEW_MIGRATE, DashboardDBObjectType, DatabaseDBObjectType, DBObjectKey::dbId, Catalog_Namespace::insertOrUpdateObjectPrivileges(), AccessPrivileges::NONE, OMNISCI_ROOT_USER_ID, DBObjectKey::permissionType, TableDBObjectType, and ViewDBObjectType.

371  {
372  sys_sqlite_lock sqlite_lock(this);
373  sqliteConnector_->query("BEGIN TRANSACTION");
374  try {
375  sqliteConnector_->query(
376  "SELECT name FROM sqlite_master WHERE type='table' AND "
377  "name='mapd_object_permissions'");
378  if (sqliteConnector_->getNumRows() != 0) {
379  // already done
380  sqliteConnector_->query("END TRANSACTION");
381  return;
382  }
383 
384  sqliteConnector_->query(
385  "CREATE TABLE IF NOT EXISTS mapd_object_permissions ("
386  "roleName text, "
387  "roleType bool, "
388  "dbId integer references mapd_databases, "
389  "objectName text, "
390  "objectId integer, "
391  "objectPermissionsType integer, "
392  "objectPermissions integer, "
393  "objectOwnerId integer, UNIQUE(roleName, objectPermissionsType, dbId, "
394  "objectId))");
395 
396  // get the list of databases and their grantees
397  sqliteConnector_->query(
398  "SELECT userid, dbid FROM mapd_privileges WHERE select_priv = 1 and insert_priv "
399  "= 1");
400  size_t numRows = sqliteConnector_->getNumRows();
401  vector<pair<int, int>> db_grantees(numRows);
402  for (size_t i = 0; i < numRows; ++i) {
403  db_grantees[i].first = sqliteConnector_->getData<int>(i, 0);
404  db_grantees[i].second = sqliteConnector_->getData<int>(i, 1);
405  }
406  // map user names to user ids
407  sqliteConnector_->query("select userid, name from mapd_users");
408  numRows = sqliteConnector_->getNumRows();
409  std::unordered_map<int, string> users_by_id;
410  std::unordered_map<int, bool> user_has_privs;
411  for (size_t i = 0; i < numRows; ++i) {
412  users_by_id[sqliteConnector_->getData<int>(i, 0)] =
413  sqliteConnector_->getData<string>(i, 1);
414  user_has_privs[sqliteConnector_->getData<int>(i, 0)] = false;
415  }
416  // map db names to db ids
417  sqliteConnector_->query("select dbid, name from mapd_databases");
418  numRows = sqliteConnector_->getNumRows();
419  std::unordered_map<int, string> dbs_by_id;
420  for (size_t i = 0; i < numRows; ++i) {
421  dbs_by_id[sqliteConnector_->getData<int>(i, 0)] =
422  sqliteConnector_->getData<string>(i, 1);
423  }
424  // migrate old privileges to new privileges: if user had insert access to database, he
425  // was a grantee
426  for (const auto& grantee : db_grantees) {
427  user_has_privs[grantee.first] = true;
428  auto dbName = dbs_by_id[grantee.second];
429  {
430  // table level permissions
431  DBObjectKey key;
433  key.dbId = grantee.second;
435  object.setName(dbName);
437  sqliteConnector_, users_by_id[grantee.first], true, object);
438  }
439 
440  {
441  // dashboard level permissions
442  DBObjectKey key;
444  key.dbId = grantee.second;
445  DBObject object(
447  object.setName(dbName);
449  sqliteConnector_, users_by_id[grantee.first], true, object);
450  }
451 
452  {
453  // view level permissions
454  DBObjectKey key;
456  key.dbId = grantee.second;
458  object.setName(dbName);
460  sqliteConnector_, users_by_id[grantee.first], true, object);
461  }
462  }
463  for (auto user : user_has_privs) {
464  auto dbName = dbs_by_id[0];
465  if (user.second == false && user.first != OMNISCI_ROOT_USER_ID) {
466  {
467  DBObjectKey key;
469  key.dbId = 0;
471  object.setName(dbName);
473  sqliteConnector_, users_by_id[user.first], true, object);
474  }
475  }
476  }
477  } catch (const std::exception&) {
478  sqliteConnector_->query("ROLLBACK TRANSACTION");
479  throw;
480  }
481  sqliteConnector_->query("END TRANSACTION");
482 }
static const AccessPrivileges ALL_TABLE_MIGRATE
Definition: DBObject.h:156
void insertOrUpdateObjectPrivileges(std::unique_ptr< SqliteConnector > &sqliteConnector, std::string roleName, bool userRole, DBObject &object)
Definition: SysCatalog.cpp:341
sqlite_lock< SysCatalog > sys_sqlite_lock
Definition: SysCatalog.cpp:81
static const AccessPrivileges ALL_DASHBOARD_MIGRATE
Definition: DBObject.h:168
std::unique_ptr< SqliteConnector > sqliteConnector_
Definition: SysCatalog.h:353
static const AccessPrivileges NONE
Definition: DBObject.h:148
static const AccessPrivileges ALL_VIEW_MIGRATE
Definition: DBObject.h:176
const int OMNISCI_ROOT_USER_ID
Definition: SysCatalog.h:60
int32_t dbId
Definition: DBObject.h:56
int32_t permissionType
Definition: DBObject.h:55

+ Here is the call graph for this function:

std::string Catalog_Namespace::SysCatalog::name ( ) const
inline

Definition at line 262 of file SysCatalog.h.

References OMNISCI_DEFAULT_DB.

262 { return OMNISCI_DEFAULT_DB; }
const std::string OMNISCI_DEFAULT_DB
Definition: SysCatalog.h:58
void Catalog_Namespace::SysCatalog::populateRoleDbObjects ( const std::vector< DBObject > &  objects)

Definition at line 2047 of file SysCatalog.cpp.

References CHECK(), Catalog_Namespace::insertOrUpdateObjectPrivileges(), and Catalog_Namespace::UserMetadata::userName.

Referenced by Catalog_Namespace::Catalog::recordOwnershipOfObjectsInObjectPermissions().

2047  {
2048  sys_write_lock write_lock(this);
2049  sys_sqlite_lock sqlite_lock(this);
2050  sqliteConnector_->query("BEGIN TRANSACTION");
2051  try {
2052  for (auto dbobject : objects) {
2053  UserMetadata user;
2054  CHECK(getMetadataForUserById(dbobject.getOwner(), user));
2055  auto* grantee = getUserGrantee(user.userName);
2056  if (grantee) {
2058  sqliteConnector_, grantee->getName(), true, dbobject);
2059  grantee->grantPrivileges(dbobject);
2060  }
2061  }
2062 
2063  } catch (const std::exception& e) {
2064  sqliteConnector_->query("ROLLBACK TRANSACTION");
2065  throw;
2066  }
2067  sqliteConnector_->query("END TRANSACTION");
2068 }
bool getMetadataForUserById(const int32_t idIn, UserMetadata &user)
void insertOrUpdateObjectPrivileges(std::unique_ptr< SqliteConnector > &sqliteConnector, std::string roleName, bool userRole, DBObject &object)
Definition: SysCatalog.cpp:341
sqlite_lock< SysCatalog > sys_sqlite_lock
Definition: SysCatalog.cpp:81
CHECK(cgen_state)
write_lock< SysCatalog > sys_write_lock
Definition: SysCatalog.cpp:80
User * getUserGrantee(const std::string &name) const
std::unique_ptr< SqliteConnector > sqliteConnector_
Definition: SysCatalog.h:353

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void Catalog_Namespace::SysCatalog::renameDatabase ( std::string const &  old_name,
std::string const &  new_name 
)

Definition at line 975 of file SysCatalog.cpp.

References DatabaseDBObjectType, OMNISCI_SYSTEM_CATALOG, to_string(), and to_upper().

976  {
977  using namespace std::string_literals;
978  sys_write_lock write_lock(this);
979  sys_sqlite_lock sqlite_lock(this);
980 
981  DBMetadata new_db;
982  if (getMetadataForDB(new_name, new_db)) {
983  throw std::runtime_error("Database " + new_name + " already exists.");
984  }
985  if (to_upper(new_name) == to_upper(OMNISCI_SYSTEM_CATALOG)) {
986  throw std::runtime_error("Database name " + new_name + "is reserved.");
987  }
988 
989  DBMetadata old_db;
990  if (!getMetadataForDB(old_name, old_db)) {
991  throw std::runtime_error("Database " + old_name + " does not exists.");
992  }
993 
994  Catalog::remove(old_db.dbName);
995 
996  std::string old_catalog_path, new_catalog_path;
997  std::tie(old_catalog_path, new_catalog_path) =
998  duplicateAndRenameCatalog(old_name, new_name);
999 
1000  auto transaction_streamer = yieldTransactionStreamer();
1001  auto failure_handler = [this, new_catalog_path] {
1002  removeCatalogByFullPath(new_catalog_path);
1003  };
1004  auto success_handler = [this, old_catalog_path] {
1005  removeCatalogByFullPath(old_catalog_path);
1006  };
1007 
1008  auto q1 = {"UPDATE mapd_databases SET name=?1 WHERE name=?2;"s, new_name, old_name};
1009  auto q2 = {
1010  "UPDATE mapd_object_permissions SET objectName=?1 WHERE objectNAME=?2 and (objectPermissionsType=?3 or objectId = -1) and dbId=?4;"s,
1011  new_name,
1012  old_name,
1014  std::to_string(old_db.dbId)};
1015 
1016  transaction_streamer(sqliteConnector_, success_handler, failure_handler, q1, q2);
1017 }
auto duplicateAndRenameCatalog(std::string const &current_name, std::string const &new_name)
Definition: SysCatalog.cpp:95
const std::string OMNISCI_SYSTEM_CATALOG
Definition: SysCatalog.h:57
std::string to_string(char const *&&v)
sqlite_lock< SysCatalog > sys_sqlite_lock
Definition: SysCatalog.cpp:81
write_lock< SysCatalog > sys_write_lock
Definition: SysCatalog.cpp:80
std::unique_ptr< SqliteConnector > sqliteConnector_
Definition: SysCatalog.h:353
std::string to_upper(const std::string &str)
static void remove(const std::string &dbName)
Definition: Catalog.cpp:3444
void removeCatalogByFullPath(std::string const &full_path)
Definition: SysCatalog.cpp:87
bool getMetadataForDB(const std::string &name, DBMetadata &db)

+ Here is the call graph for this function:

void Catalog_Namespace::SysCatalog::renameObjectsInDescriptorMap ( DBObject object,
const Catalog_Namespace::Catalog cat 
)

Definition at line 1779 of file SysCatalog.cpp.

References Catalog_Namespace::DBMetadata::dbId, Catalog_Namespace::Catalog::getCurrentDB(), and to_string().

Referenced by Catalog_Namespace::Catalog::renameTable().

1780  {
1781  sys_write_lock write_lock(this);
1782  sys_sqlite_lock sqlite_lock(this);
1783  auto range = objectDescriptorMap_.equal_range(
1784  std::to_string(cat.getCurrentDB().dbId) + ":" +
1785  std::to_string(object.getObjectKey().permissionType) + ":" +
1786  std::to_string(object.getObjectKey().objectId));
1787  for (auto d = range.first; d != range.second; ++d) {
1788  // rename object
1789  d->second->objectName = object.getName();
1790  }
1791 
1792  sqliteConnector_->query("BEGIN TRANSACTION");
1793  try {
1794  sqliteConnector_->query_with_text_params(
1795  "UPDATE mapd_object_permissions SET objectName = ?1 WHERE "
1796  "dbId = ?2 AND objectId = ?3",
1797  std::vector<std::string>{object.getName(),
1799  std::to_string(object.getObjectKey().objectId)});
1800  } catch (const std::exception& e) {
1801  sqliteConnector_->query("ROLLBACK TRANSACTION");
1802  throw;
1803  }
1804  sqliteConnector_->query("END TRANSACTION");
1805 }
ObjectRoleDescriptorMap objectDescriptorMap_
Definition: SysCatalog.h:352
std::string to_string(char const *&&v)
sqlite_lock< SysCatalog > sys_sqlite_lock
Definition: SysCatalog.cpp:81
const DBMetadata & getCurrentDB() const
Definition: Catalog.h:182
write_lock< SysCatalog > sys_write_lock
Definition: SysCatalog.cpp:80
std::unique_ptr< SqliteConnector > sqliteConnector_
Definition: SysCatalog.h:353

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void Catalog_Namespace::SysCatalog::renameUser ( std::string const &  old_name,
std::string const &  new_name 
)

Definition at line 942 of file SysCatalog.cpp.

942  {
943  using namespace std::string_literals;
944  sys_write_lock write_lock(this);
945  sys_sqlite_lock sqlite_lock(this);
946 
947  UserMetadata old_user;
948  if (!getMetadataForUser(old_name, old_user)) {
949  throw std::runtime_error("User " + old_name + " doesn't exist.");
950  }
951 
952  UserMetadata new_user;
953  if (getMetadataForUser(new_name, new_user)) {
954  throw std::runtime_error("User " + new_name + " already exists.");
955  }
956 
957  if (getGrantee(new_name)) {
958  throw runtime_error(
959  "User name " + new_name +
960  " is same as one of existing grantees. User and role names should be unique.");
961  }
962 
963  auto transaction_streamer = yieldTransactionStreamer();
964  auto failure_handler = [] {};
965  auto success_handler = [this, &old_name, &new_name] {
966  updateUserRoleName(old_name, new_name);
967  };
968  auto q1 = {"UPDATE mapd_users SET name=?1 where name=?2;"s, new_name, old_name};
969  auto q2 = {"UPDATE mapd_object_permissions set roleName=?1 WHERE roleName=?2;"s,
970  new_name,
971  old_name};
972  transaction_streamer(sqliteConnector_, success_handler, failure_handler, q1, q2);
973 }
bool getMetadataForUser(const std::string &name, UserMetadata &user)
Grantee * getGrantee(const std::string &name) const
sqlite_lock< SysCatalog > sys_sqlite_lock
Definition: SysCatalog.cpp:81
write_lock< SysCatalog > sys_write_lock
Definition: SysCatalog.cpp:80
std::unique_ptr< SqliteConnector > sqliteConnector_
Definition: SysCatalog.h:353
void updateUserRoleName(const std::string &roleName, const std::string &newName)
Definition: SysCatalog.cpp:930
void Catalog_Namespace::SysCatalog::revokeAllOnDatabase_unsafe ( const std::string &  roleName,
int32_t  dbId,
Grantee grantee 
)
private

Definition at line 1560 of file SysCatalog.cpp.

References Grantee::revokeAllOnDatabase(), and to_string().

1562  {
1563  sys_sqlite_lock sqlite_lock(this);
1564  sqliteConnector_->query_with_text_params(
1565  "DELETE FROM mapd_object_permissions WHERE roleName = ?1 and dbId = ?2",
1566  std::vector<std::string>{roleName, std::to_string(dbId)});
1567  grantee->revokeAllOnDatabase(dbId);
1568  for (auto d = objectDescriptorMap_.begin(); d != objectDescriptorMap_.end();) {
1569  if (d->second->roleName == roleName && d->second->dbId == dbId) {
1570  delete d->second;
1571  d = objectDescriptorMap_.erase(d);
1572  } else {
1573  d++;
1574  }
1575  }
1576 }
ObjectRoleDescriptorMap objectDescriptorMap_
Definition: SysCatalog.h:352
std::string to_string(char const *&&v)
sqlite_lock< SysCatalog > sys_sqlite_lock
Definition: SysCatalog.cpp:81
virtual void revokeAllOnDatabase(int32_t dbId)
Definition: Grantee.cpp:279
std::unique_ptr< SqliteConnector > sqliteConnector_
Definition: SysCatalog.h:353

+ Here is the call graph for this function:

void Catalog_Namespace::SysCatalog::revokeDashboardSystemRole ( const std::string  roleName,
const std::vector< std::string >  grantees 
)

Definition at line 1988 of file SysCatalog.cpp.

1989  {
1990  auto* rl = getRoleGrantee(roleName);
1991  for (auto granteeName : grantees) {
1992  const auto* grantee = SysCatalog::instance().getGrantee(granteeName);
1993  if (rl && grantee->hasRole(rl, true)) {
1994  // Grantees existence have been already validated
1995  SysCatalog::instance().revokeRole(roleName, granteeName);
1996  }
1997  }
1998 }
Grantee * getGrantee(const std::string &name) const
static SysCatalog & instance()
Definition: SysCatalog.h:256
Role * getRoleGrantee(const std::string &name) const
void revokeRole(const std::string &role, const std::string &grantee)
void Catalog_Namespace::SysCatalog::revokeDBObjectPrivileges ( const std::string &  grantee,
const DBObject object,
const Catalog_Namespace::Catalog catalog 
)

Definition at line 2198 of file SysCatalog.cpp.

Referenced by Catalog_Namespace::Catalog::createOrUpdateDashboardSystemRole().

2200  {
2202  &SysCatalog::revokeDBObjectPrivileges_unsafe, grantee, object, catalog);
2203 }
void revokeDBObjectPrivileges_unsafe(const std::string &granteeName, DBObject object, const Catalog_Namespace::Catalog &catalog)
void execInTransaction(F &&f, Args &&...args)

+ Here is the caller graph for this function:

void Catalog_Namespace::SysCatalog::revokeDBObjectPrivileges_unsafe ( const std::string &  granteeName,
DBObject  object,
const Catalog_Namespace::Catalog catalog 
)
private

Definition at line 1522 of file SysCatalog.cpp.

References DatabasePrivileges::ALL, DatabaseDBObjectType, Catalog_Namespace::deleteObjectPrivileges(), Catalog_Namespace::insertOrUpdateObjectPrivileges(), and Catalog_Namespace::UserMetadata::isSuper.

1525  {
1526  sys_write_lock write_lock(this);
1527 
1528  UserMetadata user_meta;
1529  if (instance().getMetadataForUser(granteeName, user_meta)) {
1530  if (user_meta.isSuper) {
1531  // super doesn't have explicit privileges so nothing to do
1532  return;
1533  }
1534  }
1535  auto* grantee = getGrantee(granteeName);
1536  if (!grantee) {
1537  throw runtime_error("Request to revoke privileges from " + granteeName +
1538  " failed because role or user with this name does not exist.");
1539  }
1540  object.loadKey(catalog);
1541 
1542  if (object.getPrivileges().hasPermission(DatabasePrivileges::ALL) &&
1543  object.getObjectKey().permissionType == DatabaseDBObjectType) {
1544  return revokeAllOnDatabase_unsafe(granteeName, object.getObjectKey().dbId, grantee);
1545  }
1546 
1547  auto ret_object = grantee->revokePrivileges(object);
1548  if (ret_object) {
1549  sys_sqlite_lock sqlite_lock(this);
1551  sqliteConnector_, granteeName, grantee->isUser(), *ret_object);
1552  updateObjectDescriptorMap(granteeName, *ret_object, grantee->isUser(), catalog);
1553  } else {
1554  sys_sqlite_lock sqlite_lock(this);
1555  deleteObjectPrivileges(sqliteConnector_, granteeName, grantee->isUser(), object);
1556  deleteObjectDescriptorMap(granteeName, object, catalog);
1557  }
1558 }
void revokeAllOnDatabase_unsafe(const std::string &roleName, int32_t dbId, Grantee *grantee)
static const int32_t ALL
Definition: DBObject.h:79
bool getMetadataForUser(const std::string &name, UserMetadata &user)
Grantee * getGrantee(const std::string &name) const
void insertOrUpdateObjectPrivileges(std::unique_ptr< SqliteConnector > &sqliteConnector, std::string roleName, bool userRole, DBObject &object)
Definition: SysCatalog.cpp:341
sqlite_lock< SysCatalog > sys_sqlite_lock
Definition: SysCatalog.cpp:81
static SysCatalog & instance()
Definition: SysCatalog.h:256
write_lock< SysCatalog > sys_write_lock
Definition: SysCatalog.cpp:80
std::unique_ptr< SqliteConnector > sqliteConnector_
Definition: SysCatalog.h:353
void deleteObjectPrivileges(std::unique_ptr< SqliteConnector > &sqliteConnector, std::string roleName, bool userRole, DBObject &object)
Definition: SysCatalog.cpp:322
void deleteObjectDescriptorMap(const std::string &roleName)
void updateObjectDescriptorMap(const std::string &roleName, DBObject &object, bool roleType, const Catalog_Namespace::Catalog &cat)

+ Here is the call graph for this function:

void Catalog_Namespace::SysCatalog::revokeDBObjectPrivilegesBatch ( const std::vector< std::string > &  grantees,
const std::vector< DBObject > &  objects,
const Catalog_Namespace::Catalog catalog 
)

Definition at line 2205 of file SysCatalog.cpp.

2208  {
2210  &SysCatalog::revokeDBObjectPrivilegesBatch_unsafe, grantees, objects, catalog);
2211 }
void revokeDBObjectPrivilegesBatch_unsafe(const std::vector< std::string > &grantees, const std::vector< DBObject > &objects, const Catalog_Namespace::Catalog &catalog)
void execInTransaction(F &&f, Args &&...args)
void Catalog_Namespace::SysCatalog::revokeDBObjectPrivilegesBatch_unsafe ( const std::vector< std::string > &  grantees,
const std::vector< DBObject > &  objects,
const Catalog_Namespace::Catalog catalog 
)
private

Definition at line 1510 of file SysCatalog.cpp.

1513  {
1514  for (const auto& grantee : grantees) {
1515  for (const auto& object : objects) {
1516  revokeDBObjectPrivileges_unsafe(grantee, object, catalog);
1517  }
1518  }
1519 }
void revokeDBObjectPrivileges_unsafe(const std::string &granteeName, DBObject object, const Catalog_Namespace::Catalog &catalog)
void Catalog_Namespace::SysCatalog::revokeDBObjectPrivilegesFromAll ( DBObject  object,
Catalog catalog 
)

Definition at line 2213 of file SysCatalog.cpp.

Referenced by Catalog_Namespace::Catalog::deleteMetadataForDashboard(), Catalog_Namespace::Catalog::dropFsiSchemasAndTables(), and Catalog_Namespace::Catalog::dropTable().

2213  {
2215 }
void execInTransaction(F &&f, Args &&...args)
void revokeDBObjectPrivilegesFromAll_unsafe(DBObject object, Catalog *catalog)

+ Here is the caller graph for this function:

void Catalog_Namespace::SysCatalog::revokeDBObjectPrivilegesFromAll_unsafe ( DBObject  object,
Catalog catalog 
)

Definition at line 1578 of file SysCatalog.cpp.

References AccessPrivileges::ALL_DASHBOARD, AccessPrivileges::ALL_TABLE, DashboardDBObjectType, DBObject::getObjectKey(), DBObject::loadKey(), DBObjectKey::permissionType, DBObject::setPrivileges(), and TableDBObjectType.

1579  {
1580  sys_write_lock write_lock(this);
1581  dbObject.loadKey(*catalog);
1582  auto privs = (dbObject.getObjectKey().permissionType == TableDBObjectType)
1584  : (dbObject.getObjectKey().permissionType == DashboardDBObjectType)
1587  dbObject.setPrivileges(privs);
1588  for (const auto& grantee : granteeMap_) {
1589  if (grantee.second->findDbObject(dbObject.getObjectKey(), true)) {
1590  revokeDBObjectPrivileges_unsafe(grantee.second->getName(), dbObject, *catalog);
1591  }
1592  }
1593 }
void revokeDBObjectPrivileges_unsafe(const std::string &granteeName, DBObject object, const Catalog_Namespace::Catalog &catalog)
write_lock< SysCatalog > sys_write_lock
Definition: SysCatalog.cpp:80
static const AccessPrivileges ALL_DASHBOARD
Definition: DBObject.h:169
static const AccessPrivileges ALL_TABLE
Definition: DBObject.h:157

+ Here is the call graph for this function:

void Catalog_Namespace::SysCatalog::revokeRole ( const std::string &  role,
const std::string &  grantee 
)

Definition at line 2180 of file SysCatalog.cpp.

2180  {
2182 }
void execInTransaction(F &&f, Args &&...args)
void revokeRole_unsafe(const std::string &roleName, const std::string &granteeName)
void Catalog_Namespace::SysCatalog::revokeRole_unsafe ( const std::string &  roleName,
const std::string &  granteeName 
)
private

Definition at line 1722 of file SysCatalog.cpp.

1723  {
1724  auto* rl = getRoleGrantee(roleName);
1725  if (!rl) {
1726  throw runtime_error("Request to revoke role " + roleName +
1727  " failed because role with this name does not exist.");
1728  }
1729  auto* grantee = getGrantee(granteeName);
1730  if (!grantee) {
1731  throw runtime_error("Request to revoke role from " + granteeName +
1732  " failed because grantee with this name does not exist.");
1733  }
1734  sys_write_lock write_lock(this);
1735  grantee->revokeRole(rl);
1736  sys_sqlite_lock sqlite_lock(this);
1737  sqliteConnector_->query_with_text_params(
1738  "DELETE FROM mapd_roles WHERE roleName = ? AND userName = ?",
1739  std::vector<std::string>{rl->getName(), grantee->getName()});
1740 }
Grantee * getGrantee(const std::string &name) const
sqlite_lock< SysCatalog > sys_sqlite_lock
Definition: SysCatalog.cpp:81
write_lock< SysCatalog > sys_write_lock
Definition: SysCatalog.cpp:80
Role * getRoleGrantee(const std::string &name) const
std::unique_ptr< SqliteConnector > sqliteConnector_
Definition: SysCatalog.h:353
void Catalog_Namespace::SysCatalog::revokeRoleBatch ( const std::vector< std::string > &  roles,
const std::vector< std::string > &  grantees 
)

Definition at line 2175 of file SysCatalog.cpp.

2176  {
2178 }
void revokeRoleBatch_unsafe(const std::vector< std::string > &roles, const std::vector< std::string > &grantees)
void execInTransaction(F &&f, Args &&...args)
void Catalog_Namespace::SysCatalog::revokeRoleBatch_unsafe ( const std::vector< std::string > &  roles,
const std::vector< std::string > &  grantees 
)
private

Definition at line 1712 of file SysCatalog.cpp.

1713  {
1714  for (const auto& role : roles) {
1715  for (const auto& grantee : grantees) {
1716  revokeRole_unsafe(role, grantee);
1717  }
1718  }
1719 }
void revokeRole_unsafe(const std::string &roleName, const std::string &granteeName)
std::shared_ptr< Catalog > Catalog_Namespace::SysCatalog::switchDatabase ( std::string &  dbname,
const std::string &  username 
)

Definition at line 741 of file SysCatalog.cpp.

References AccessPrivileges::ACCESS, DatabaseDBObjectType, DBObject::loadKey(), and DBObject::setPrivileges().

742  {
743  DBMetadata db_meta;
744  UserMetadata user_meta;
745 
746  getMetadataWithDefaultDB(dbname, username, db_meta, user_meta);
747 
748  // NOTE(max): register database in Catalog that early to allow ldap
749  // and saml create default user and role privileges on databases
750  auto cat =
752 
753  DBObject dbObject(dbname, DatabaseDBObjectType);
754  dbObject.loadKey();
755  dbObject.setPrivileges(AccessPrivileges::ACCESS);
756  if (!checkPrivileges(user_meta, std::vector<DBObject>{dbObject})) {
757  throw std::runtime_error("Unauthorized Access: user " + username +
758  " is not allowed to access database " + dbname + ".");
759  }
760 
761  return cat;
762 }
static std::shared_ptr< Catalog > get(const std::string &dbName)
Definition: Catalog.cpp:3418
bool checkPrivileges(const UserMetadata &user, const std::vector< DBObject > &privObjects) const
void getMetadataWithDefaultDB(std::string &dbname, const std::string &username, Catalog_Namespace::DBMetadata &db_meta, UserMetadata &user_meta)
std::shared_ptr< Data_Namespace::DataMgr > dataMgr_
Definition: SysCatalog.h:355
static const AccessPrivileges ACCESS
Definition: DBObject.h:153
std::vector< LeafHostInfo > string_dict_hosts_
Definition: SysCatalog.h:361
std::shared_ptr< Calcite > calciteMgr_
Definition: SysCatalog.h:360

+ Here is the call graph for this function:

void Catalog_Namespace::SysCatalog::syncUserWithRemoteProvider ( const std::string &  user_name,
std::vector< std::string >  idp_roles,
bool *  issuper 
)

Definition at line 2217 of file SysCatalog.cpp.

References generate_random_string(), logger::INFO, Catalog_Namespace::UserMetadata::isSuper, join(), LOG, to_upper(), Catalog_Namespace::UserMetadata::userId, and logger::WARNING.

2219  {
2220  UserMetadata user_meta;
2221  bool is_super_user = is_super ? *is_super : false;
2222  if (!getMetadataForUser(user_name, user_meta)) {
2223  createUser(user_name, generate_random_string(72), is_super_user, "", true);
2224  LOG(INFO) << "User " << user_name << " has been created by remote identity provider"
2225  << " with IS_SUPER = " << (is_super_user ? "'TRUE'" : "'FALSE'");
2226  } else if (is_super && is_super_user != user_meta.isSuper) {
2227  alterUser(user_meta.userId, nullptr, is_super, nullptr, nullptr);
2228  LOG(INFO) << "IS_SUPER for user " << user_name << " has been changed to "
2229  << (is_super_user ? "TRUE" : "FALSE") << " by remote identity provider";
2230  }
2231  std::vector<std::string> current_roles = {};
2232  auto* user_rl = getUserGrantee(user_name);
2233  if (user_rl) {
2234  current_roles = user_rl->getRoles();
2235  }
2236  std::transform(
2237  current_roles.begin(), current_roles.end(), current_roles.begin(), to_upper);
2238  std::transform(idp_roles.begin(), idp_roles.end(), idp_roles.begin(), to_upper);
2239  std::list<std::string> roles_revoked, roles_granted;
2240  // first remove obsolete ones
2241  for (auto& current_role_name : current_roles) {
2242  if (std::find(idp_roles.begin(), idp_roles.end(), current_role_name) ==
2243  idp_roles.end()) {
2244  revokeRole(current_role_name, user_name);
2245  roles_revoked.push_back(current_role_name);
2246  }
2247  }
2248  for (auto& role_name : idp_roles) {
2249  if (std::find(current_roles.begin(), current_roles.end(), role_name) ==
2250  current_roles.end()) {
2251  auto* rl = getRoleGrantee(role_name);
2252  if (rl) {
2253  grantRole(role_name, user_name);
2254  roles_granted.push_back(role_name);
2255  } else {
2256  LOG(WARNING) << "Error synchronizing roles for user " << user_name << ": role "
2257  << role_name << " does not exist";
2258  }
2259  }
2260  }
2261  if (roles_granted.empty() && roles_revoked.empty()) {
2262  LOG(INFO) << "Roles for user " << user_name
2263  << " are up to date with remote identity provider";
2264  } else {
2265  if (!roles_revoked.empty()) {
2266  LOG(INFO) << "Roles revoked during synchronization with identity provider for user "
2267  << user_name << ": " << join(roles_revoked, " ");
2268  }
2269  if (!roles_granted.empty()) {
2270  LOG(INFO) << "Roles granted during synchronization with identity provider for user "
2271  << user_name << ": " << join(roles_granted, " ");
2272  }
2273  }
2274 }
#define LOG(tag)
Definition: Logger.h:188
bool getMetadataForUser(const std::string &name, UserMetadata &user)
std::string join(T const &container, std::string const &delim)
void createUser(const std::string &name, const std::string &passwd, bool issuper, const std::string &dbname, bool can_login)
Definition: SysCatalog.cpp:772
void alterUser(const int32_t userid, const std::string *passwd, bool *issuper, const std::string *dbname, bool *can_login)
Definition: SysCatalog.cpp:861
std::string generate_random_string(const size_t len)
Role * getRoleGrantee(const std::string &name) const
User * getUserGrantee(const std::string &name) const
std::string to_upper(const std::string &str)
void grantRole(const std::string &role, const std::string &grantee)
void revokeRole(const std::string &role, const std::string &grantee)

+ Here is the call graph for this function:

void Catalog_Namespace::SysCatalog::updateBlankPasswordsToRandom ( )
private

Definition at line 536 of file SysCatalog.cpp.

References CHECK(), logger::ERROR, generate_random_string(), anonymous_namespace{SysCatalog.cpp}::hash_with_bcrypt(), LOG, MAPD_VERSION, to_string(), and logger::WARNING.

536  {
537  const std::string UPDATE_BLANK_PASSWORDS_TO_RANDOM = "update_blank_passwords_to_random";
538  sqliteConnector_->query_with_text_params(
539  "SELECT migration_history FROM mapd_version_history WHERE migration_history = ?",
540  std::vector<std::string>{UPDATE_BLANK_PASSWORDS_TO_RANDOM});
541  if (sqliteConnector_->getNumRows()) {
542  return;
543  }
544 
545  sys_sqlite_lock sqlite_lock(this);
546  sqliteConnector_->query("BEGIN TRANSACTION");
547  try {
548  sqliteConnector_->query(
549  "SELECT userid, passwd_hash, name FROM mapd_users WHERE name <> 'mapd'");
550  auto numRows = sqliteConnector_->getNumRows();
551  vector<std::string> users, passwords, names;
552  for (size_t i = 0; i < numRows; i++) {
553  users.push_back(sqliteConnector_->getData<std::string>(i, 0));
554  passwords.push_back(sqliteConnector_->getData<std::string>(i, 1));
555  names.push_back(sqliteConnector_->getData<std::string>(i, 2));
556  }
557  for (size_t i = 0; i < users.size(); ++i) {
558  int pwd_check_result = bcrypt_checkpw("", passwords[i].c_str());
559  // if the check fails there is a good chance that data on disc is broken
560  CHECK(pwd_check_result >= 0);
561  if (pwd_check_result != 0) {
562  continue;
563  }
564  LOG(WARNING) << "resetting blank password for user " << names[i] << " (" << users[i]
565  << ") to a random password";
566  sqliteConnector_->query_with_text_params(
567  "UPDATE mapd_users SET passwd_hash = ? WHERE userid = ?",
568  std::vector<std::string>{hash_with_bcrypt(generate_random_string(72)),
569  users[i]});
570  }
571  sqliteConnector_->query_with_text_params(
572  "INSERT INTO mapd_version_history(version, migration_history) values(?,?)",
573  std::vector<std::string>{std::to_string(MAPD_VERSION),
574  UPDATE_BLANK_PASSWORDS_TO_RANDOM});
575  } catch (const std::exception& e) {
576  LOG(ERROR) << "Failed to fix blank passwords: " << e.what();
577  sqliteConnector_->query("ROLLBACK TRANSACTION");
578  throw;
579  }
580  sqliteConnector_->query("END TRANSACTION");
581 }
#define LOG(tag)
Definition: Logger.h:188
std::string to_string(char const *&&v)
sqlite_lock< SysCatalog > sys_sqlite_lock
Definition: SysCatalog.cpp:81
CHECK(cgen_state)
std::string generate_random_string(const size_t len)
std::string hash_with_bcrypt(const std::string &pwd)
Definition: SysCatalog.cpp:66
static const int32_t MAPD_VERSION
Definition: release.h:33
std::unique_ptr< SqliteConnector > sqliteConnector_
Definition: SysCatalog.h:353

+ Here is the call graph for this function:

void Catalog_Namespace::SysCatalog::updateObjectDescriptorMap ( const std::string &  roleName,
DBObject object,
bool  roleType,
const Catalog_Namespace::Catalog cat 
)
private

Definition at line 1743 of file SysCatalog.cpp.

References ObjectRoleDescriptor::dbId, Catalog_Namespace::DBMetadata::dbId, Catalog_Namespace::Catalog::getCurrentDB(), ObjectRoleDescriptor::objectId, ObjectRoleDescriptor::objectName, ObjectRoleDescriptor::objectOwnerId, ObjectRoleDescriptor::objectType, ObjectRoleDescriptor::privs, ObjectRoleDescriptor::roleName, ObjectRoleDescriptor::roleType, and to_string().

1746  {
1747  bool present = false;
1748  auto privs = object.getPrivileges();
1749  sys_write_lock write_lock(this);
1750  auto range = objectDescriptorMap_.equal_range(
1751  std::to_string(cat.getCurrentDB().dbId) + ":" +
1752  std::to_string(object.getObjectKey().permissionType) + ":" +
1753  std::to_string(object.getObjectKey().objectId));
1754  for (auto d = range.first; d != range.second; ++d) {
1755  if (d->second->roleName == roleName) {
1756  // overwrite permissions
1757  d->second->privs = privs;
1758  present = true;
1759  }
1760  }
1761  if (!present) {
1763  od->roleName = roleName;
1764  od->roleType = roleType;
1765  od->objectType = object.getObjectKey().permissionType;
1766  od->dbId = object.getObjectKey().dbId;
1767  od->objectId = object.getObjectKey().objectId;
1768  od->privs = object.getPrivileges();
1769  od->objectOwnerId = object.getOwner();
1770  od->objectName = object.getName();
1771  objectDescriptorMap_.insert(ObjectRoleDescriptorMap::value_type(
1772  std::to_string(od->dbId) + ":" + std::to_string(od->objectType) + ":" +
1773  std::to_string(od->objectId),
1774  od));
1775  }
1776 }
specifies the object_roles content in-memory of a row in mapd_object_permissions table ...
ObjectRoleDescriptorMap objectDescriptorMap_
Definition: SysCatalog.h:352
std::string to_string(char const *&&v)
const DBMetadata & getCurrentDB() const
Definition: Catalog.h:182
write_lock< SysCatalog > sys_write_lock
Definition: SysCatalog.cpp:80

+ Here is the call graph for this function:

void Catalog_Namespace::SysCatalog::updatePasswordsToHashes ( )
private

Definition at line 484 of file SysCatalog.cpp.

References logger::ERROR, anonymous_namespace{SysCatalog.cpp}::hash_with_bcrypt(), logger::INFO, and LOG.

484  {
485  sys_sqlite_lock sqlite_lock(this);
486  sqliteConnector_->query("BEGIN TRANSACTION");
487  try {
488  sqliteConnector_->query(
489  "SELECT name FROM sqlite_master WHERE type='table' AND name='mapd_users'");
490  if (sqliteConnector_->getNumRows() == 0) {
491  // Nothing to update
492  sqliteConnector_->query("END TRANSACTION");
493  return;
494  }
495  sqliteConnector_->query("PRAGMA TABLE_INFO(mapd_users)");
496  for (size_t i = 0; i < sqliteConnector_->getNumRows(); i++) {
497  const auto& col_name = sqliteConnector_->getData<std::string>(i, 1);
498  if (col_name == "passwd_hash") {
499  sqliteConnector_->query("END TRANSACTION");
500  return;
501  }
502  }
503  // Alas, SQLite can't drop columns so we have to recreate the table
504  // (or, optionally, add the new column and reset the old one to a bunch of nulls)
505  sqliteConnector_->query("SELECT userid, passwd FROM mapd_users");
506  auto numRows = sqliteConnector_->getNumRows();
507  vector<std::string> users, passwords;
508  for (size_t i = 0; i < numRows; i++) {
509  users.push_back(sqliteConnector_->getData<std::string>(i, 0));
510  passwords.push_back(sqliteConnector_->getData<std::string>(i, 1));
511  }
512  sqliteConnector_->query(
513  "CREATE TABLE mapd_users_tmp (userid integer primary key, name text unique, "
514  "passwd_hash text, issuper boolean, default_db integer references "
515  "mapd_databases)");
516  sqliteConnector_->query(
517  "INSERT INTO mapd_users_tmp(userid, name, passwd_hash, issuper, default_db) "
518  "SELECT userid, name, null, issuper, default_db FROM mapd_users");
519  for (size_t i = 0; i < users.size(); ++i) {
520  sqliteConnector_->query_with_text_params(
521  "UPDATE mapd_users_tmp SET passwd_hash = ? WHERE userid = ?",
522  std::vector<std::string>{hash_with_bcrypt(passwords[i]), users[i]});
523  }
524  sqliteConnector_->query("DROP TABLE mapd_users");
525  sqliteConnector_->query("ALTER TABLE mapd_users_tmp RENAME TO mapd_users");
526  } catch (const std::exception& e) {
527  LOG(ERROR) << "Failed to hash passwords: " << e.what();
528  sqliteConnector_->query("ROLLBACK TRANSACTION");
529  throw;
530  }
531  sqliteConnector_->query("END TRANSACTION");
532  sqliteConnector_->query("VACUUM"); // physically delete plain text passwords
533  LOG(INFO) << "Passwords were successfully hashed";
534 }
#define LOG(tag)
Definition: Logger.h:188
sqlite_lock< SysCatalog > sys_sqlite_lock
Definition: SysCatalog.cpp:81
std::string hash_with_bcrypt(const std::string &pwd)
Definition: SysCatalog.cpp:66
std::unique_ptr< SqliteConnector > sqliteConnector_
Definition: SysCatalog.h:353

+ Here is the call graph for this function:

void Catalog_Namespace::SysCatalog::updateSupportUserDeactivation ( )
private

Definition at line 583 of file SysCatalog.cpp.

References logger::ERROR, LOG, MAPD_VERSION, and to_string().

583  {
584  const std::string UPDATE_SUPPORT_USER_DEACTIVATION = "update_support_user_deactivation";
585  sys_sqlite_lock sqlite_lock(this);
586  // check to see if the new column already exists
587  sqliteConnector_->query("PRAGMA TABLE_INFO(mapd_users)");
588  for (size_t i = 0; i < sqliteConnector_->getNumRows(); i++) {
589  const auto& col_name = sqliteConnector_->getData<std::string>(i, 1);
590  if (col_name == "can_login") {
591  return; // new column already exists
592  }
593  }
594  sqliteConnector_->query("BEGIN TRANSACTION");
595  try {
596  sqliteConnector_->query("ALTER TABLE mapd_users ADD COLUMN can_login BOOLEAN");
597  sqliteConnector_->query("UPDATE mapd_users SET can_login = true");
598  sqliteConnector_->query_with_text_params(
599  "INSERT INTO mapd_version_history(version, migration_history) values(?,?)",
600  std::vector<std::string>{std::to_string(MAPD_VERSION),
601  UPDATE_SUPPORT_USER_DEACTIVATION});
602  } catch (const std::exception& e) {
603  LOG(ERROR) << "Failed to add support for user deactivation: " << e.what();
604  sqliteConnector_->query("ROLLBACK TRANSACTION");
605  throw;
606  }
607  sqliteConnector_->query("END TRANSACTION");
608 }
#define LOG(tag)
Definition: Logger.h:188
std::string to_string(char const *&&v)
sqlite_lock< SysCatalog > sys_sqlite_lock
Definition: SysCatalog.cpp:81
static const int32_t MAPD_VERSION
Definition: release.h:33
std::unique_ptr< SqliteConnector > sqliteConnector_
Definition: SysCatalog.h:353

+ Here is the call graph for this function:

void Catalog_Namespace::SysCatalog::updateUserRoleName ( const std::string &  roleName,
const std::string &  newName 
)
private

Definition at line 930 of file SysCatalog.cpp.

References to_upper().

931  {
932  sys_write_lock write_lock(this);
933 
934  auto it = granteeMap_.find(to_upper(roleName));
935  if (it != granteeMap_.end()) {
936  it->second->setName(newName);
937  std::swap(granteeMap_[to_upper(newName)], it->second);
938  granteeMap_.erase(it);
939  }
940 }
write_lock< SysCatalog > sys_write_lock
Definition: SysCatalog.cpp:80
std::string to_upper(const std::string &str)

+ Here is the call graph for this function:

void Catalog_Namespace::SysCatalog::updateUserSchema ( )
private

Definition at line 213 of file SysCatalog.cpp.

213  {
214  sys_sqlite_lock sqlite_lock(this);
215 
216  // check to see if the new column already exists
217  sqliteConnector_->query("PRAGMA TABLE_INFO(mapd_users)");
218  for (size_t i = 0; i < sqliteConnector_->getNumRows(); i++) {
219  const auto& col_name = sqliteConnector_->getData<std::string>(i, 1);
220  if (col_name == "default_db") {
221  return; // new column already exists
222  }
223  }
224 
225  // create the new column
226  sqliteConnector_->query("BEGIN TRANSACTION");
227  try {
228  sqliteConnector_->query(
229  "ALTER TABLE mapd_users ADD COLUMN default_db INTEGER REFERENCES mapd_databases");
230  } catch (const std::exception& e) {
231  sqliteConnector_->query("ROLLBACK TRANSACTION");
232  throw;
233  }
234  sqliteConnector_->query("END TRANSACTION");
235 }
sqlite_lock< SysCatalog > sys_sqlite_lock
Definition: SysCatalog.cpp:81
std::unique_ptr< SqliteConnector > sqliteConnector_
Definition: SysCatalog.h:353
bool Catalog_Namespace::SysCatalog::verifyDBObjectOwnership ( const UserMetadata user,
DBObject  object,
const Catalog_Namespace::Catalog catalog 
)

Definition at line 1595 of file SysCatalog.cpp.

References Catalog_Namespace::UserMetadata::userId, and Catalog_Namespace::UserMetadata::userName.

1597  {
1598  sys_read_lock read_lock(this);
1599 
1600  auto* grantee = instance().getUserGrantee(user.userName);
1601  if (grantee) {
1602  object.loadKey(catalog);
1603  auto* found_object = grantee->findDbObject(object.getObjectKey(), false);
1604  if (found_object && found_object->getOwner() == user.userId) {
1605  return true;
1606  }
1607  }
1608  return false;
1609 }
read_lock< SysCatalog > sys_read_lock
Definition: SysCatalog.cpp:79
DBObject * findDbObject(const DBObjectKey &objectKey, bool only_direct) const
Definition: Grantee.cpp:72
static SysCatalog & instance()
Definition: SysCatalog.h:256
User * getUserGrantee(const std::string &name) const
auto Catalog_Namespace::SysCatalog::yieldTransactionStreamer ( )
private

Definition at line 907 of file SysCatalog.cpp.

907  {
908  return
909  [](auto& db_connector, auto on_success, auto on_failure, auto&&... query_requests) {
910  auto query_runner = [&db_connector](auto&&... query_reqs) {
911  [[gnu::unused]] int throw_away[] = {
912  (db_connector->query_with_text_params(
913  std::forward<decltype(query_reqs)>(query_reqs)),
914  0)...};
915  };
916 
917  db_connector->query("BEGIN TRANSACTION");
918  try {
919  query_runner(std::forward<decltype(query_requests)>(query_requests)...);
920  on_success();
921  } catch (std::exception&) {
922  db_connector->query("ROLLBACK TRANSACTION");
923  on_failure();
924  throw;
925  }
926  db_connector->query("END TRANSACTION");
927  };
928 }

Member Data Documentation

bool Catalog_Namespace::SysCatalog::aggregator_
private

Definition at line 362 of file SysCatalog.h.

Referenced by isAggregator().

const AuthMetadata* Catalog_Namespace::SysCatalog::authMetadata_
private

Definition at line 359 of file SysCatalog.h.

std::string Catalog_Namespace::SysCatalog::basePath_
private

Definition at line 350 of file SysCatalog.h.

Referenced by getBasePath().

std::shared_ptr<Calcite> Catalog_Namespace::SysCatalog::calciteMgr_
private

Definition at line 360 of file SysCatalog.h.

Referenced by getCalciteMgr().

std::shared_ptr<Data_Namespace::DataMgr> Catalog_Namespace::SysCatalog::dataMgr_
private

Definition at line 355 of file SysCatalog.h.

Referenced by getDataMgr().

GranteeMap Catalog_Namespace::SysCatalog::granteeMap_
private

Definition at line 351 of file SysCatalog.h.

std::unique_ptr<LdapServer> Catalog_Namespace::SysCatalog::ldap_server_
private

Definition at line 356 of file SysCatalog.h.

ObjectRoleDescriptorMap Catalog_Namespace::SysCatalog::objectDescriptorMap_
private

Definition at line 352 of file SysCatalog.h.

std::unique_ptr<PkiServer> Catalog_Namespace::SysCatalog::pki_server_
private

Definition at line 358 of file SysCatalog.h.

std::unique_ptr<RestServer> Catalog_Namespace::SysCatalog::rest_server_
private

Definition at line 357 of file SysCatalog.h.

mapd_shared_mutex Catalog_Namespace::SysCatalog::sharedMutex_
mutable

Definition at line 367 of file SysCatalog.h.

std::unique_ptr<SqliteConnector> Catalog_Namespace::SysCatalog::sqliteConnector_
private

Definition at line 353 of file SysCatalog.h.

Referenced by getSqliteConnector().

std::mutex Catalog_Namespace::SysCatalog::sqliteMutex_
mutable

Definition at line 366 of file SysCatalog.h.

std::vector<LeafHostInfo> Catalog_Namespace::SysCatalog::string_dict_hosts_
private

Definition at line 361 of file SysCatalog.h.

std::atomic<std::thread::id> Catalog_Namespace::SysCatalog::thread_holding_sqlite_lock
mutable

Definition at line 368 of file SysCatalog.h.

std::atomic<std::thread::id> Catalog_Namespace::SysCatalog::thread_holding_write_lock
mutable

Definition at line 369 of file SysCatalog.h.

thread_local bool Catalog_Namespace::SysCatalog::thread_holds_read_lock = false
static

Definition at line 370 of file SysCatalog.h.


The documentation for this class was generated from the following files: