| 1 | // Copyright 2024 Lance Developers. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | /* eslint-disable @typescript-eslint/naming-convention */ |
| 16 | |
| 17 | import { |
| 18 | CreateTableCommand, |
| 19 | DeleteTableCommand, |
| 20 | DynamoDBClient, |
| 21 | } from "@aws-sdk/client-dynamodb"; |
| 22 | import { |
| 23 | CreateKeyCommand, |
| 24 | KMSClient, |
| 25 | ScheduleKeyDeletionCommand, |
| 26 | } from "@aws-sdk/client-kms"; |
| 27 | import { |
| 28 | CreateBucketCommand, |
| 29 | DeleteBucketCommand, |
| 30 | DeleteObjectCommand, |
| 31 | HeadObjectCommand, |
| 32 | ListObjectsV2Command, |
| 33 | S3Client, |
| 34 | } from "@aws-sdk/client-s3"; |
| 35 | import { connect } from "../lancedb"; |
| 36 | |
| 37 | // Skip these tests unless the S3_TEST environment variable is set |
| 38 | const maybeDescribe = process.env.S3_TEST ? describe : describe.skip; |
| 39 | |
| 40 | // These are all keys that are accepted by storage_options |
| 41 | const CONFIG = { |
| 42 | allowHttp: "true", |
| 43 | awsAccessKeyId: "ACCESSKEY", |
| 44 | awsSecretAccessKey: "SECRETKEY", |
| 45 | awsEndpoint: "http://127.0.0.1:4566", |
| 46 | dynamodbEndpoint: "http://127.0.0.1:4566", |
| 47 | awsRegion: "us-east-1", |
| 48 | }; |
| 49 | |
| 50 | class S3Bucket { |
| 51 | name: string; |
| 52 | constructor(name: string) { |
| 53 | this.name = name; |
| 54 | } |
| 55 | |
| 56 | static s3Client() { |
| 57 | return new S3Client({ |
| 58 | region: CONFIG.awsRegion, |
| 59 | credentials: { |
| 60 | accessKeyId: CONFIG.awsAccessKeyId, |
| 61 | secretAccessKey: CONFIG.awsSecretAccessKey, |
| 62 | }, |
| 63 | endpoint: CONFIG.awsEndpoint, |
| 64 | }); |
| 65 | } |
| 66 | |
| 67 | public static async create(name: string): Promise<S3Bucket> { |
| 68 | const client = this.s3Client(); |
| 69 | // Delete the bucket if it already exists |
| 70 | try { |
| 71 | await this.deleteBucket(client, name); |
| 72 | } catch { |
| 73 | // It's fine if the bucket doesn't exist |
| 74 | } |
| 75 | await client.send(new CreateBucketCommand({ Bucket: name })); |
| 76 | return new S3Bucket(name); |
| 77 | } |
| 78 | |
| 79 | public async delete() { |
| 80 | const client = S3Bucket.s3Client(); |
| 81 | await S3Bucket.deleteBucket(client, this.name); |
| 82 | } |
| 83 | |
| 84 | static async deleteBucket(client: S3Client, name: string) { |
| 85 | // Must delete all objects before we can delete the bucket |
| 86 | const objects = await client.send( |
| 87 | new ListObjectsV2Command({ Bucket: name }), |
| 88 | ); |
| 89 | if (objects.Contents) { |
| 90 | for (const object of objects.Contents) { |
| 91 | await client.send( |
| 92 | new DeleteObjectCommand({ Bucket: name, Key: object.Key }), |
| 93 | ); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | await client.send(new DeleteBucketCommand({ Bucket: name })); |
| 98 | } |
| 99 | |
| 100 | public async assertAllEncrypted(path: string, keyId: string) { |
| 101 | const client = S3Bucket.s3Client(); |
| 102 | const objects = await client.send( |
| 103 | new ListObjectsV2Command({ Bucket: this.name, Prefix: path }), |
| 104 | ); |
| 105 | if (objects.Contents) { |
| 106 | for (const object of objects.Contents) { |
| 107 | const metadata = await client.send( |
| 108 | new HeadObjectCommand({ Bucket: this.name, Key: object.Key }), |
| 109 | ); |
| 110 | expect(metadata.ServerSideEncryption).toBe("aws:kms"); |
| 111 | expect(metadata.SSEKMSKeyId).toContain(keyId); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | class KmsKey { |
| 118 | keyId: string; |
| 119 | constructor(keyId: string) { |
| 120 | this.keyId = keyId; |
| 121 | } |
| 122 | |
| 123 | static kmsClient() { |
| 124 | return new KMSClient({ |
| 125 | region: CONFIG.awsRegion, |
| 126 | credentials: { |
| 127 | accessKeyId: CONFIG.awsAccessKeyId, |
| 128 | secretAccessKey: CONFIG.awsSecretAccessKey, |
| 129 | }, |
| 130 | endpoint: CONFIG.awsEndpoint, |
| 131 | }); |
| 132 | } |
| 133 | |
| 134 | public static async create(): Promise<KmsKey> { |
| 135 | const client = this.kmsClient(); |
| 136 | const key = await client.send(new CreateKeyCommand({})); |
| 137 | const keyId = key?.KeyMetadata?.KeyId; |
| 138 | if (!keyId) { |
| 139 | throw new Error("Failed to create KMS key"); |
| 140 | } |
| 141 | return new KmsKey(keyId); |
| 142 | } |
| 143 | |
| 144 | public async delete() { |
| 145 | const client = KmsKey.kmsClient(); |
| 146 | await client.send(new ScheduleKeyDeletionCommand({ KeyId: this.keyId })); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | maybeDescribe("storage_options", () => { |
| 151 | let bucket: S3Bucket; |
| 152 | let kmsKey: KmsKey; |
| 153 | beforeAll(async () => { |
| 154 | bucket = await S3Bucket.create("lancedb"); |
| 155 | kmsKey = await KmsKey.create(); |
| 156 | }); |
| 157 | afterAll(async () => { |
| 158 | await kmsKey.delete(); |
| 159 | await bucket.delete(); |
| 160 | }); |
| 161 | |
| 162 | it("can be used to configure auth and endpoints", async () => { |
| 163 | const uri = `s3://${bucket.name}/test`; |
| 164 | const db = await connect(uri, { storageOptions: CONFIG }); |
| 165 | |
| 166 | let table = await db.createTable("test", [{ a: 1, b: 2 }]); |
| 167 | |
| 168 | let rowCount = await table.countRows(); |
| 169 | expect(rowCount).toBe(1); |
| 170 | |
| 171 | let tableNames = await db.tableNames(); |
| 172 | expect(tableNames).toEqual(["test"]); |
| 173 | |
| 174 | table = await db.openTable("test"); |
| 175 | rowCount = await table.countRows(); |
| 176 | expect(rowCount).toBe(1); |
| 177 | |
| 178 | await table.add([ |
| 179 | { a: 2, b: 3 }, |
| 180 | { a: 3, b: 4 }, |
| 181 | ]); |
| 182 | rowCount = await table.countRows(); |
| 183 | expect(rowCount).toBe(3); |
| 184 | |
| 185 | await db.dropTable("test"); |
| 186 | |
| 187 | tableNames = await db.tableNames(); |
| 188 | expect(tableNames).toEqual([]); |
| 189 | }); |
| 190 | |
| 191 | it("can configure encryption at connection and table level", async () => { |
| 192 | const uri = `s3://${bucket.name}/test`; |
| 193 | let db = await connect(uri, { storageOptions: CONFIG }); |
| 194 | |
| 195 | let table = await db.createTable("table1", [{ a: 1, b: 2 }], { |
| 196 | storageOptions: { |
| 197 | awsServerSideEncryption: "aws:kms", |
| 198 | awsSseKmsKeyId: kmsKey.keyId, |
| 199 | }, |
| 200 | }); |
| 201 | |
| 202 | let rowCount = await table.countRows(); |
| 203 | expect(rowCount).toBe(1); |
| 204 | |
| 205 | await table.add([{ a: 2, b: 3 }]); |
| 206 | |
| 207 | await bucket.assertAllEncrypted("test/table1.lance", kmsKey.keyId); |
| 208 | |
| 209 | // Now with encryption settings at connection level |
| 210 | db = await connect(uri, { |
| 211 | storageOptions: { |
| 212 | ...CONFIG, |
| 213 | awsServerSideEncryption: "aws:kms", |
| 214 | awsSseKmsKeyId: kmsKey.keyId, |
| 215 | }, |
| 216 | }); |
| 217 | table = await db.createTable("table2", [{ a: 1, b: 2 }]); |
| 218 | rowCount = await table.countRows(); |
| 219 | expect(rowCount).toBe(1); |
| 220 | |
| 221 | await table.add([{ a: 2, b: 3 }]); |
| 222 | |
| 223 | await bucket.assertAllEncrypted("test/table2.lance", kmsKey.keyId); |
| 224 | }); |
| 225 | }); |
| 226 | |
| 227 | class DynamoDBCommitTable { |
| 228 | name: string; |
| 229 | constructor(name: string) { |
| 230 | this.name = name; |
| 231 | } |
| 232 | |
| 233 | static dynamoClient() { |
| 234 | return new DynamoDBClient({ |
| 235 | region: CONFIG.awsRegion, |
| 236 | credentials: { |
| 237 | accessKeyId: CONFIG.awsAccessKeyId, |
| 238 | secretAccessKey: CONFIG.awsSecretAccessKey, |
| 239 | }, |
| 240 | endpoint: CONFIG.awsEndpoint, |
| 241 | }); |
| 242 | } |
| 243 | |
| 244 | public static async create(name: string): Promise<DynamoDBCommitTable> { |
| 245 | const client = DynamoDBCommitTable.dynamoClient(); |
| 246 | const command = new CreateTableCommand({ |
| 247 | TableName: name, |
| 248 | AttributeDefinitions: [ |
| 249 | { |
| 250 | AttributeName: "base_uri", |
| 251 | AttributeType: "S", |
| 252 | }, |
| 253 | { |
| 254 | AttributeName: "version", |
| 255 | AttributeType: "N", |
| 256 | }, |
| 257 | ], |
| 258 | KeySchema: [ |
| 259 | { AttributeName: "base_uri", KeyType: "HASH" }, |
| 260 | { AttributeName: "version", KeyType: "RANGE" }, |
| 261 | ], |
| 262 | ProvisionedThroughput: { |
| 263 | ReadCapacityUnits: 1, |
| 264 | WriteCapacityUnits: 1, |
| 265 | }, |
| 266 | }); |
| 267 | await client.send(command); |
| 268 | return new DynamoDBCommitTable(name); |
| 269 | } |
| 270 | |
| 271 | public async delete() { |
| 272 | const client = DynamoDBCommitTable.dynamoClient(); |
| 273 | await client.send(new DeleteTableCommand({ TableName: this.name })); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | maybeDescribe("DynamoDB Lock", () => { |
| 278 | let bucket: S3Bucket; |
| 279 | let commitTable: DynamoDBCommitTable; |
| 280 | |
| 281 | beforeAll(async () => { |
| 282 | bucket = await S3Bucket.create("lancedb2"); |
| 283 | commitTable = await DynamoDBCommitTable.create("commitTable"); |
| 284 | }); |
| 285 | |
| 286 | afterAll(async () => { |
| 287 | await commitTable.delete(); |
| 288 | await bucket.delete(); |
| 289 | }); |
| 290 | |
| 291 | it("can be used to configure a DynamoDB table for commit log", async () => { |
| 292 | const uri = `s3+ddb://${bucket.name}/test?ddbTableName=${commitTable.name}`; |
| 293 | const db = await connect(uri, { |
| 294 | storageOptions: CONFIG, |
| 295 | readConsistencyInterval: 0, |
| 296 | }); |
| 297 | |
| 298 | const table = await db.createTable("test", [{ a: 1, b: 2 }]); |
| 299 | |
| 300 | // 5 concurrent appends |
| 301 | const futs = Array.from({ length: 5 }, async () => { |
| 302 | // Open a table so each append has a separate table reference. Otherwise |
| 303 | // they will share the same table reference and the internal ReadWriteLock |
| 304 | // will prevent any real concurrency. |
| 305 | const table = await db.openTable("test"); |
| 306 | await table.add([{ a: 2, b: 3 }]); |
| 307 | }); |
| 308 | await Promise.all(futs); |
| 309 | |
| 310 | const rowCount = await table.countRows(); |
| 311 | expect(rowCount).toBe(6); |
| 312 | }); |
| 313 | }); |