|
5 | 5 | # the root directory of this source tree. |
6 | 6 |
|
7 | 7 | from io import BytesIO |
| 8 | +from unittest.mock import patch |
8 | 9 |
|
9 | 10 | import pytest |
10 | 11 | from openai import OpenAI |
11 | 12 |
|
| 13 | +from llama_stack.distribution.datatypes import User |
12 | 14 | from llama_stack.distribution.library_client import LlamaStackAsLibraryClient |
13 | 15 |
|
14 | 16 |
|
@@ -61,3 +63,218 @@ def test_openai_client_basic_operations(compat_client, client_with_models): |
61 | 63 | except Exception: |
62 | 64 | pass |
63 | 65 | raise e |
| 66 | + |
| 67 | + |
| 68 | +@patch("llama_stack.providers.utils.sqlstore.authorized_sqlstore.get_authenticated_user") |
| 69 | +def test_files_authentication_isolation(mock_get_authenticated_user, compat_client, client_with_models): |
| 70 | + """Test that users can only access their own files.""" |
| 71 | + if isinstance(client_with_models, LlamaStackAsLibraryClient) and isinstance(compat_client, OpenAI): |
| 72 | + pytest.skip("OpenAI files are not supported when testing with LlamaStackAsLibraryClient") |
| 73 | + if not isinstance(client_with_models, LlamaStackAsLibraryClient): |
| 74 | + pytest.skip("Authentication tests require LlamaStackAsLibraryClient (library mode)") |
| 75 | + |
| 76 | + client = compat_client |
| 77 | + |
| 78 | + # Create two test users |
| 79 | + user1 = User("user1", {"roles": ["user"], "teams": ["team-a"]}) |
| 80 | + user2 = User("user2", {"roles": ["user"], "teams": ["team-b"]}) |
| 81 | + |
| 82 | + # User 1 uploads a file |
| 83 | + mock_get_authenticated_user.return_value = user1 |
| 84 | + test_content_1 = b"User 1's private file content" |
| 85 | + |
| 86 | + with BytesIO(test_content_1) as file_buffer: |
| 87 | + file_buffer.name = "user1_file.txt" |
| 88 | + user1_file = client.files.create(file=file_buffer, purpose="assistants") |
| 89 | + |
| 90 | + # User 2 uploads a file |
| 91 | + mock_get_authenticated_user.return_value = user2 |
| 92 | + test_content_2 = b"User 2's private file content" |
| 93 | + |
| 94 | + with BytesIO(test_content_2) as file_buffer: |
| 95 | + file_buffer.name = "user2_file.txt" |
| 96 | + user2_file = client.files.create(file=file_buffer, purpose="assistants") |
| 97 | + |
| 98 | + try: |
| 99 | + # User 1 can see their own file |
| 100 | + mock_get_authenticated_user.return_value = user1 |
| 101 | + user1_files = client.files.list() |
| 102 | + user1_file_ids = [f.id for f in user1_files.data] |
| 103 | + assert user1_file.id in user1_file_ids |
| 104 | + assert user2_file.id not in user1_file_ids # Cannot see user2's file |
| 105 | + |
| 106 | + # User 2 can see their own file |
| 107 | + mock_get_authenticated_user.return_value = user2 |
| 108 | + user2_files = client.files.list() |
| 109 | + user2_file_ids = [f.id for f in user2_files.data] |
| 110 | + assert user2_file.id in user2_file_ids |
| 111 | + assert user1_file.id not in user2_file_ids # Cannot see user1's file |
| 112 | + |
| 113 | + # User 1 can retrieve their own file |
| 114 | + mock_get_authenticated_user.return_value = user1 |
| 115 | + retrieved_file = client.files.retrieve(user1_file.id) |
| 116 | + assert retrieved_file.id == user1_file.id |
| 117 | + |
| 118 | + # User 1 cannot retrieve user2's file |
| 119 | + mock_get_authenticated_user.return_value = user1 |
| 120 | + with pytest.raises(ValueError, match="not found"): |
| 121 | + client.files.retrieve(user2_file.id) |
| 122 | + |
| 123 | + # User 1 can access their file content |
| 124 | + mock_get_authenticated_user.return_value = user1 |
| 125 | + content_response = client.files.content(user1_file.id) |
| 126 | + if isinstance(content_response, str): |
| 127 | + content = bytes(content_response, "utf-8") |
| 128 | + else: |
| 129 | + content = content_response.content |
| 130 | + assert content == test_content_1 |
| 131 | + |
| 132 | + # User 1 cannot access user2's file content |
| 133 | + mock_get_authenticated_user.return_value = user1 |
| 134 | + with pytest.raises(ValueError, match="not found"): |
| 135 | + client.files.content(user2_file.id) |
| 136 | + |
| 137 | + # User 1 can delete their own file |
| 138 | + mock_get_authenticated_user.return_value = user1 |
| 139 | + delete_response = client.files.delete(user1_file.id) |
| 140 | + assert delete_response.deleted is True |
| 141 | + |
| 142 | + # User 1 cannot delete user2's file |
| 143 | + mock_get_authenticated_user.return_value = user1 |
| 144 | + with pytest.raises(ValueError, match="not found"): |
| 145 | + client.files.delete(user2_file.id) |
| 146 | + |
| 147 | + # User 2 can still access their file after user1's file is deleted |
| 148 | + mock_get_authenticated_user.return_value = user2 |
| 149 | + retrieved_file = client.files.retrieve(user2_file.id) |
| 150 | + assert retrieved_file.id == user2_file.id |
| 151 | + |
| 152 | + # Cleanup user2's file |
| 153 | + mock_get_authenticated_user.return_value = user2 |
| 154 | + client.files.delete(user2_file.id) |
| 155 | + |
| 156 | + except Exception as e: |
| 157 | + # Cleanup in case of failure |
| 158 | + try: |
| 159 | + mock_get_authenticated_user.return_value = user1 |
| 160 | + client.files.delete(user1_file.id) |
| 161 | + except Exception: |
| 162 | + pass |
| 163 | + try: |
| 164 | + mock_get_authenticated_user.return_value = user2 |
| 165 | + client.files.delete(user2_file.id) |
| 166 | + except Exception: |
| 167 | + pass |
| 168 | + raise e |
| 169 | + |
| 170 | + |
| 171 | +@patch("llama_stack.providers.utils.sqlstore.authorized_sqlstore.get_authenticated_user") |
| 172 | +def test_files_authentication_shared_attributes(mock_get_authenticated_user, compat_client, client_with_models): |
| 173 | + """Test access control with users having identical attributes.""" |
| 174 | + if isinstance(client_with_models, LlamaStackAsLibraryClient) and isinstance(compat_client, OpenAI): |
| 175 | + pytest.skip("OpenAI files are not supported when testing with LlamaStackAsLibraryClient") |
| 176 | + if not isinstance(client_with_models, LlamaStackAsLibraryClient): |
| 177 | + pytest.skip("Authentication tests require LlamaStackAsLibraryClient (library mode)") |
| 178 | + |
| 179 | + client = compat_client |
| 180 | + |
| 181 | + # Create users with identical attributes (required for default policy) |
| 182 | + user_a = User("user-a", {"roles": ["user"], "teams": ["shared-team"]}) |
| 183 | + user_b = User("user-b", {"roles": ["user"], "teams": ["shared-team"]}) |
| 184 | + |
| 185 | + # User A uploads a file |
| 186 | + mock_get_authenticated_user.return_value = user_a |
| 187 | + test_content = b"Shared attributes file content" |
| 188 | + |
| 189 | + with BytesIO(test_content) as file_buffer: |
| 190 | + file_buffer.name = "shared_attributes_file.txt" |
| 191 | + shared_file = client.files.create(file=file_buffer, purpose="assistants") |
| 192 | + |
| 193 | + try: |
| 194 | + # User B with identical attributes can access the file |
| 195 | + mock_get_authenticated_user.return_value = user_b |
| 196 | + files_list = client.files.list() |
| 197 | + file_ids = [f.id for f in files_list.data] |
| 198 | + |
| 199 | + # User B should be able to see the file due to identical attributes |
| 200 | + assert shared_file.id in file_ids |
| 201 | + |
| 202 | + # User B can retrieve file info |
| 203 | + retrieved_file = client.files.retrieve(shared_file.id) |
| 204 | + assert retrieved_file.id == shared_file.id |
| 205 | + |
| 206 | + # User B can access file content |
| 207 | + content_response = client.files.content(shared_file.id) |
| 208 | + if isinstance(content_response, str): |
| 209 | + content = bytes(content_response, "utf-8") |
| 210 | + else: |
| 211 | + content = content_response.content |
| 212 | + assert content == test_content |
| 213 | + |
| 214 | + # Cleanup |
| 215 | + mock_get_authenticated_user.return_value = user_a |
| 216 | + client.files.delete(shared_file.id) |
| 217 | + |
| 218 | + except Exception as e: |
| 219 | + # Cleanup in case of failure |
| 220 | + try: |
| 221 | + mock_get_authenticated_user.return_value = user_a |
| 222 | + client.files.delete(shared_file.id) |
| 223 | + except Exception: |
| 224 | + pass |
| 225 | + try: |
| 226 | + mock_get_authenticated_user.return_value = user_b |
| 227 | + client.files.delete(shared_file.id) |
| 228 | + except Exception: |
| 229 | + pass |
| 230 | + raise e |
| 231 | + |
| 232 | + |
| 233 | +@patch("llama_stack.providers.utils.sqlstore.authorized_sqlstore.get_authenticated_user") |
| 234 | +def test_files_authentication_anonymous_access(mock_get_authenticated_user, compat_client, client_with_models): |
| 235 | + """Test anonymous user behavior when no authentication is present.""" |
| 236 | + if isinstance(client_with_models, LlamaStackAsLibraryClient) and isinstance(compat_client, OpenAI): |
| 237 | + pytest.skip("OpenAI files are not supported when testing with LlamaStackAsLibraryClient") |
| 238 | + if not isinstance(client_with_models, LlamaStackAsLibraryClient): |
| 239 | + pytest.skip("Authentication tests require LlamaStackAsLibraryClient (library mode)") |
| 240 | + |
| 241 | + client = compat_client |
| 242 | + |
| 243 | + # Simulate anonymous user (no authentication) |
| 244 | + mock_get_authenticated_user.return_value = None |
| 245 | + |
| 246 | + test_content = b"Anonymous file content" |
| 247 | + |
| 248 | + with BytesIO(test_content) as file_buffer: |
| 249 | + file_buffer.name = "anonymous_file.txt" |
| 250 | + anonymous_file = client.files.create(file=file_buffer, purpose="assistants") |
| 251 | + |
| 252 | + try: |
| 253 | + # Anonymous user should be able to access their own uploaded file |
| 254 | + files_list = client.files.list() |
| 255 | + file_ids = [f.id for f in files_list.data] |
| 256 | + assert anonymous_file.id in file_ids |
| 257 | + |
| 258 | + # Can retrieve file info |
| 259 | + retrieved_file = client.files.retrieve(anonymous_file.id) |
| 260 | + assert retrieved_file.id == anonymous_file.id |
| 261 | + |
| 262 | + # Can access file content |
| 263 | + content_response = client.files.content(anonymous_file.id) |
| 264 | + if isinstance(content_response, str): |
| 265 | + content = bytes(content_response, "utf-8") |
| 266 | + else: |
| 267 | + content = content_response.content |
| 268 | + assert content == test_content |
| 269 | + |
| 270 | + # Can delete the file |
| 271 | + delete_response = client.files.delete(anonymous_file.id) |
| 272 | + assert delete_response.deleted is True |
| 273 | + |
| 274 | + except Exception as e: |
| 275 | + # Cleanup in case of failure |
| 276 | + try: |
| 277 | + client.files.delete(anonymous_file.id) |
| 278 | + except Exception: |
| 279 | + pass |
| 280 | + raise e |
0 commit comments