I have a database structure that looks like this:
Firestore-root
|
--- users (collection)
| |
| --- UidOne (document)
| |
| --- userName: "UserOne"
|
--- items (collection)
|
--- ItemIdOne (document)
| |
| --- itemName: "ItemOne"
|
--- ItemIdTwo
|
--- itemName: "ItemTwo"
What I want to achieve is to restrict every user from reading item names from each document within items collection using security rules. This is how I do it:
service cloud.firestore {
match /databases/{database}/documents {
match /items/{item} {
allow read, write: if false;
}
}
}
To display the item names I use the following query:
Query query = itemsRef.orderBy("itemName", Query.Direction.ASCENDING);
When I try to compile my app I get the following error:
com.google.firebase.firestore.FirebaseFirestoreException: PERMISSION_DENIED: Missing or insufficient permissions.
But the item names are still displayed in my RecyclerView. How can I stop this from happening?
Maybe check to see if your items are still coming from the local cache.
From this page add this to your OnEvent
String source = querySnapshot.getMetadata().isFromCache() ?
"local cache" : "server";
Log.d(TAG, "Data fetched from " + source);
If it is reading from the local cache you can set PersistenceEnabled(false) like this (also mentioned on that page):
FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder()
.setPersistenceEnabled(false)
.build();
db.setFirestoreSettings(settings);
Even if you are online it will read from the local snapshot, and only updates the snapshot if the data changes. It's your rules that changed not your data. I found when testing with it set to true I got some unexpected results. I find I prefer it to be false when testing and changing code/rules.
Related
I have two collections: an org and a user. A user can be a regular user of org A but can also be an admin of org B. So the user collection would look something like this:
{
email: "john#example.com",
name: "John Doe",
access: [
{
org: "orgA",
role: "user"
},
{
org: "orgB",
role: "admin"
}
]}
The problem with keeping everything in the same collection is that I do not like admins of org A to update the access array and impact org B. If I move the access array in a sub-collection under the /user collection when showing the list of users for each collection, I'd have to make a call for each user to get the access info. Should I save the user IDs in an array in a sub-collection under the /org collection?
I guess my goal is to find a best practice solution for this problem.
The simplest database structure I can think of would be:
Firestore-root
|
--- users (collection)
| |
| --- $uid (document)
| |
| --- email: "john#example.com"
| |
| --- name: "John Doe"
| |
| --- userOf (map)
| | |
| | --- orgA: true
| |
| --- adminOf (map)
| |
| --- orgB: true
|
--- organizations (collection)
|
--- $orgA (document)
| |
| --- users: ["uidOne", "uidTwo"] (array)
|
--- $orgB (document)
|
--- admins: ["uidThree", "uidFour"] (array)
In this way, you can simply query the "users" collection to get regular users of some organization, as well as admin, or even both.
You have two approaches, one is insert a key of access in user, and the same in access. Otherwise you can use a Junction table, with the id of both. There is no exact answer, with the right safety rules and for performance is practically the same. Then decide based on the approach you feel is most appropriate for you design.
I am developing a service that requires access to a DynamoDB table which must be managed by authorizing user access to the table. Account management is handled by Cognito. I am currently investigating direct access to the DynamoDB table with read/write access limited based on User Groups with associated IAM policies.
Multiple organisations exist within the table, and multiple users are tied to an organisation. An example of the model is below. I also store sector and department information in a many-to-one relationship.
The Cognito Sub for a user is stored as their user id within the database under USR#.
+-------+-------+-----------------+------------+--------+
| PK | SK | Name | GSI1PK | GSI2PK |
+-------+-------+-----------------+------------+--------+
| ORG#1 | ORG#1 | Acme Inc | | |
| ORG#1 | USR#1 | John Doe | | |
| ORG#2 | ORG#2 | Globetex | | |
| ORG#2 | USR#2 | Jane Doe | | |
| ORG#1 | SEC#1 | Sector A1 | ORG#1SEC#1 | SEC#1 |
| DEP#1 | DEP#1 | Human Resources | ORG#1SEC#1 | DEP#1 |
+-------+-------+-----------------+------------+--------+
So far I can limit access in a hardcoded manner to each organisation in a specific IAM policy. However, this is not scalable. If a hundred organisations were to exist, a hundred user groups must also exist with a separate policy. An example of this policy is below.
Is there any way to create an IAM policy that utilises a custom Cognito variable, such as 'organization' that would allow me to create a single policy that limits access to only rows leading with that organization? I am unable to get this working with the below code.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"dynamodb:GetItem",
"dynamodb:PutItem",
"dynamodb:Query"
],
"Resource": [
"arn:aws:dynamodb:region:id:table/TableName"
],
"Condition": {
"ForAllValues:StringEquals": {
"dynamodb:LeadingKeys": [
"${cognito-identity.amazonaws.com:org}"
]
}
}
}
]
}
Edit: For clarity, my query is to insert a custom Cognito variable dynamically into the IAM policy at validation.
For instance, User A has custom:org = Acme as a Cognito attribute and User B has custom:org = Globex as their custom Cognito attribute.
A single policy as detailed in the code above can insert this attribute directly into the policy, so one policy may be used for multiple users in separate orgs.
After further research I am unsure this is possible at all, but if anyone has any experience with trying something like this I'd love to hear it.
I think you're close, according to this article it should be StringLike not StringEquals
"Condition": {
"ForAllValues:StringLike": {
"dynamodb:LeadingKeys": [
"{TENANTID}-*"
]
}
May also want to read the Multi-tenant SaaS Storage Strategies whitepaper
Edit
I don't beleive it's possible to have a static policy do what you want.
However the code in the linked article does provide the ability to "manage access from users from any tenant".
The key points are the use of the role/AccessDynamoWithTenantContext
tenantPolicy = getPolicy(event['tenantID'])
assumed_role = sts_client.assume_role(
RoleArn="arn:aws:iam::<account-id>:role/AccessDynamoWithTenantContext",
RoleSessionName="tenant-aware-product",
Policy=tenantPolicy,
)
And the dynamic injection of the tenentId in getPolicy()
policy = json.dumps(policyTemplate).replace("{TENANTID}", tenantID)
return policy
I want to secure my Firestore database with the integrated code editor in the firebase website. My aim is to check before reading document called "moyenne", if:
numr == request.auth.token.phone_number || numr in get(/databases/$(database)/documents/Profil/$(request.auth.token.phone_number).resource.data.numbers
I tried this:
//first try
match /Note/{numr}/moyenne/moyenne {
allow read:
if (numr == request.auth.token.phone_number || numr in get(/databases/$(database)/documents/Profil/$(request.auth.token.phone_number)).resource.data.numbers);
}
//second try
match /Note/{numr}/moyenne/moyenne {
allow read:
if (numr == request.auth.token.phone_number ||
numr in get(/databases/$(database)/documents/Profil/$(path(request.auth.token.phone_number))).resource.data.numbers);
}
But unfortunately, it doesn't work.
Error for the first try (the test number is +91909090909):
Call the [get] function with a path to a non-existing resource: / databases /% 28default% 29 / documents / Profile /% 2B33651177261
Error for the second try:
Error: simulator.rules line [14], column [17]. Property resource is undefined on object.
PS: structure of the profil document:
I have a database node called (people) that looks like this:
people
|
|
-------UserID1 //which is a random id
| |
| |
| ----UserId2 //which is a random id
| |
| |
| name:"some_name"
| id:"UserId2"
| image:"image_url"
|
|
|
-------UserId2
|
|
----UserId3
|
|
name:"some_name"
id:"UserId3"
image:"image_url"
If we look at the (people / UserID1 / UserId2) node :
Since UserId1 and UserId2 are 2 random ids, then if we want to write a rule to UserId2 we will notice that it is 2 random id level deep.
What I want is to write a rule at this specified path that says these:
1) people / UserId1 : can be written by (UserID1) and (UserId2).
2) people / UserId1 : can be read by (UserID1) and (UserId2).
3) people / UserId1 / UserId2 : must end up with a newData that has (name, id, image).
How do I do this?
Thanks.
Due to the way Firebase Realtime Database rules cascade into deeper keys, allowing people/UserId1 to be writable by UserId2 is not advised, as this would allow UserId2 write access to the data of other users stored under people/UserId1 like people/UserId1/UserId3.
But using this trait, we can "add" users that are allowed read & write permissions as we go deeper into the data structure.
So the new conditions are:
people/UserId1 - UserId1 has read & write access
people/UserId1/UserId2 - UserId2 has read & write access
people/UserId1/UserId2 - must always contain 'name', 'id' and 'image' keys
people/UserId1/UserId3 - cannot be read/written by UserId2
{
"rules": {
"people": {
"$userId1": {
"$userId2": {
".read": "auth.uid == $userId2", // add $userId2 to those granted read permission, cascades into deeper keys
".write": "auth.uid == $userId2", // add $userId2 to those granted write permission, cascades into deeper keys
".validate": "newData.hasChildren(['name', 'id', 'image'])" // any new data must have 'name', 'id' and 'image' fields.
},
".read": "auth.uid == $userId1", // add $userId1 to those granted read permission, cascades into deeper keys
".write": "auth.uid == $userId1" // add $userId1 to those granted write permission, cascades into deeper keys
}
}
}
Lastly, if it is also required that people/UserId1/UserId2/id is equal to UserId2, you can change the ".validate" rule to enforce this:
".validate": "newData.hasChildren(['name', 'id', 'image']) && newData.child('id').val() == $userId2" // any new data must have 'name', 'id' and 'image' fields and 'id' must have a value of $userId2
I have the following event which I would like to query using Analytics in Application Insights
{
"Description": "Error",
"EventData": {
"AccountId": "123",
"Exception Message": "Data at the root level is invalid.",
"Error Type": "ExceptionThrown"
}
}
I am able to query the property which does not contain spaces.
Events | where Timestamp > ago(30min)
| project Data.EventData.AccountId
How do I query a property which has spaces. The following query does not work
Events | where Timestamp > ago(30min)
| project Data.EventData.[Exception Message]
The array format accepts quotes.
Events | where Timestamp > ago(30min) | project Data.EventData.["Exception Message"]