@@ -73,7 +73,7 @@ func (spec dbUserSpec) String() string {
7373}
7474
7575func getDbUser (dbRole dbrole.DbRole ) dbUserSpec {
76- for _ , spec := range getAllDbUsers ("ANY" ) {
76+ for _ , spec := range getAllDbUsers () {
7777 if spec .username == dbRole {
7878 return spec
7979 }
@@ -86,35 +86,40 @@ Generates specifications of 2 DB users:
8686- user with read-only access (to specific org/instance)
8787- user with read-write access (to specific org/instance)
8888All the users have additional conditions to restrict access to records
89- belonging to specific instance, if withInstanceIdCheck is set.
89+ belonging to specific instance, if instancerRole is set.
9090*/
91- func getDbUsers (tableName string , withTenantIdCheck , withInstanceIdCheck bool ) []dbUserSpec {
91+ func getDbUsers (tableName string , tenantRole , instancerRole bool , isTableTenanted , isTableInstanced bool ) []dbUserSpec {
9292 readerCommands := []string {"SELECT" }
9393 writerCommands := []string {"SELECT" , "INSERT" , "UPDATE" , "DELETE" }
9494
9595 tenantCond := COLUMN_ORGID + " = current_setting('" + DbConfigOrgId + "')"
9696 instanceCond := COLUMN_INSTANCEID + " = current_setting('" + DbConfigInstanceId + "')"
9797 tenantInstanceCond := tenantCond + " AND " + instanceCond
9898
99- cond := "true"
10099 rwUser := dbrole .WRITER
101100 rUser := dbrole .READER
102-
103101 switch {
104- case withInstanceIdCheck && withTenantIdCheck :
105- cond = tenantInstanceCond
102+ case tenantRole && instancerRole :
106103 rwUser = dbrole .TENANT_INSTANCE_WRITER
107104 rUser = dbrole .TENANT_INSTANCE_READER
108- case withTenantIdCheck :
109- cond = tenantCond
105+ case tenantRole :
110106 rwUser = dbrole .TENANT_WRITER
111107 rUser = dbrole .TENANT_READER
112- case withInstanceIdCheck :
113- cond = instanceCond
108+ case instancerRole :
114109 rwUser = dbrole .INSTANCE_WRITER
115110 rUser = dbrole .INSTANCE_READER
116111 }
117112
113+ cond := "true"
114+ switch {
115+ case isTableInstanced && isTableTenanted :
116+ cond = tenantInstanceCond
117+ case isTableTenanted :
118+ cond = tenantCond
119+ case isTableInstanced :
120+ cond = instanceCond
121+ }
122+
118123 writer := dbUserSpec {
119124 username : rwUser ,
120125 commands : writerCommands ,
@@ -134,8 +139,8 @@ func getDbUsers(tableName string, withTenantIdCheck, withInstanceIdCheck bool) [
134139 dbUsers [i ].password = getPassword (string (dbUsers [i ].username ))
135140 dbUsers [i ].policyName = getRlsPolicyName (string (dbUsers [i ].username ), tableName )
136141 }
137- TRACE ("Returning DB user specs for table %q:\n \t withTenantIdCheck - %t \n \t withInstanceIdCheck - %t \n \t dbUsers - %+v\n " ,
138- tableName , withTenantIdCheck , withInstanceIdCheck , dbUsers )
142+ TRACE ("Returning DB user specs for table %q:\n \t [roleT=%s, roleI=%s, tableT=%s, tableI=%s] \n \t dbUsers - %+v\n " ,
143+ tableName , tenantRole , instancerRole , isTableTenanted , isTableInstanced , dbUsers )
139144 return dbUsers
140145}
141146
@@ -147,11 +152,12 @@ func getPassword(username string) string {
147152 return strconv .FormatInt (int64 (h .Sum32 ()), 16 )
148153}
149154
150- func getAllDbUsers (tableName string ) []dbUserSpec {
155+ func getAllDbUsers () []dbUserSpec {
156+ tableName := "ANY" // The returned user specs are for creating users only not policies
151157 allDbUsers := make ([]dbUserSpec , 0 )
152- allDbUsers = append (allDbUsers , getDbUsers (tableName , false , false )... )
153- allDbUsers = append (allDbUsers , getDbUsers (tableName , false , true )... )
154- allDbUsers = append (allDbUsers , getDbUsers (tableName , true , false )... )
155- allDbUsers = append (allDbUsers , getDbUsers (tableName , true , true )... )
158+ allDbUsers = append (allDbUsers , getDbUsers (tableName , false , false , true , true )... )
159+ allDbUsers = append (allDbUsers , getDbUsers (tableName , false , true , false , true )... )
160+ allDbUsers = append (allDbUsers , getDbUsers (tableName , true , false , true , false )... )
161+ allDbUsers = append (allDbUsers , getDbUsers (tableName , true , true , true , true )... )
156162 return allDbUsers
157163}
0 commit comments