aicam commented on code in PR #4130: URL: https://github.com/apache/texera/pull/4130#discussion_r2620402454
########## common/auth/src/main/scala/org/apache/texera/auth/UploadTokenParser.scala: ########## @@ -0,0 +1,136 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.texera.auth + +import org.apache.texera.auth.util.CryptoService +import org.apache.texera.config.AuthConfig + +import java.nio.charset.StandardCharsets +import java.util.Base64 + +/** + * Upload-token codec. + * + * Stateless, self-contained encrypted token that encodes: + * - uploadId + * - did (dataset id) + * - uid (user id) + * - filePath + * - physicalAddress (e.g. s3://bucket/key) + * + * Wire format (before AES-GCM encryption): + * v1|uploadId|did|uid|filePathB64|physicalB64 + */ +object UploadTokenParser { + + final case class UploadTokenPayload( + uploadId: String, + did: Int, + uid: Int, + filePath: String, + physicalAddress: String + ) + + private val Version = "v1" + private val Encoder = Base64.getUrlEncoder.withoutPadding() + private val Decoder = Base64.getUrlDecoder + + private val crypto: CryptoService = + CryptoService(AuthConfig.uploadTokenSecretKey) + + /** + * Build a payload (no expiration). + */ + def buildPayload( + did: Int, + uid: Int, + filePath: String, + uploadId: String, + physicalAddress: String + ): UploadTokenPayload = + UploadTokenPayload( + uploadId = uploadId, + did = did, + uid = uid, + filePath = filePath, + physicalAddress = physicalAddress + ) + + /** + * Encode a Payload into an encrypted, URL-safe token string. + */ + def encode(payload: UploadTokenPayload): String = { + val filePathB64 = Encoder.encodeToString( + payload.filePath.getBytes(StandardCharsets.UTF_8) + ) + val physicalB64 = Encoder.encodeToString( + payload.physicalAddress.getBytes(StandardCharsets.UTF_8) + ) + + val raw = + s"$Version|${payload.uploadId}|${payload.did}|${payload.uid}|$filePathB64|$physicalB64" + + crypto.encrypt(raw) + } + + /** + * Decode and decrypt a token string into a Payload. + * + * Throws IllegalArgumentException on: + * - invalid ciphertext + * - malformed structure + * - unsupported version + */ + def decode(token: String): UploadTokenPayload = { Review Comment: Same here, decode function has too much manual work which I believe library should already provide high level functions ########## common/auth/src/main/scala/org/apache/texera/auth/util/CryptoService.scala: ########## @@ -0,0 +1,97 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.texera.auth.util + +import java.nio.charset.StandardCharsets +import java.security.{MessageDigest, SecureRandom} +import java.util.Base64 +import javax.crypto.{Cipher, SecretKey} +import javax.crypto.spec.{GCMParameterSpec, SecretKeySpec} + +/** + * Generic AES-GCM crypto utilities. + * + * Usage: + * val crypto = CryptoService("secret") + * val token = crypto.encrypt("hello") + * val plain = crypto.decrypt(token) + */ +final class CryptoService private (private val key: SecretKey) { + + def encrypt(plain: String): String = + CryptoService.encrypt(plain, key) + + def decrypt(token: String): String = + CryptoService.decrypt(token, key) +} + +object CryptoService { + private val Algorithm = "AES/GCM/NoPadding" + private val IvLength = 12 + private val TagLength = 128 + + private val random = new SecureRandom() + + /** Build an instance from a String secret. */ + def apply(secret: String): CryptoService = + new CryptoService(deriveKeyFromSecret(secret)) + + /** Derive a 256-bit AES key from a String. */ + def deriveKeyFromSecret(secret: String): SecretKey = { + val digest = MessageDigest.getInstance("SHA-256") + val keyBytes = digest.digest(secret.getBytes(StandardCharsets.UTF_8)) + new SecretKeySpec(keyBytes, "AES") + } + + /** Low-level encrypt with explicit key. */ + def encrypt(plain: String, key: SecretKey): String = { + val iv = new Array[Byte](IvLength) + random.nextBytes(iv) + + val cipher = Cipher.getInstance(Algorithm) + cipher.init(Cipher.ENCRYPT_MODE, key, new GCMParameterSpec(TagLength, iv)) + + val cipherText = cipher.doFinal(plain.getBytes(StandardCharsets.UTF_8)) + + val combined = new Array[Byte](iv.length + cipherText.length) Review Comment: Add comments to explain which part of algorithm is done by each line and why we need it ########## common/auth/src/main/scala/org/apache/texera/auth/UploadTokenParser.scala: ########## @@ -0,0 +1,136 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.texera.auth + +import org.apache.texera.auth.util.CryptoService +import org.apache.texera.config.AuthConfig + +import java.nio.charset.StandardCharsets +import java.util.Base64 + +/** + * Upload-token codec. + * + * Stateless, self-contained encrypted token that encodes: + * - uploadId + * - did (dataset id) + * - uid (user id) + * - filePath + * - physicalAddress (e.g. s3://bucket/key) + * + * Wire format (before AES-GCM encryption): + * v1|uploadId|did|uid|filePathB64|physicalB64 + */ +object UploadTokenParser { + + final case class UploadTokenPayload( + uploadId: String, + did: Int, + uid: Int, + filePath: String, + physicalAddress: String + ) + + private val Version = "v1" + private val Encoder = Base64.getUrlEncoder.withoutPadding() + private val Decoder = Base64.getUrlDecoder + + private val crypto: CryptoService = + CryptoService(AuthConfig.uploadTokenSecretKey) + + /** + * Build a payload (no expiration). + */ + def buildPayload( + did: Int, + uid: Int, + filePath: String, + uploadId: String, + physicalAddress: String + ): UploadTokenPayload = + UploadTokenPayload( + uploadId = uploadId, + did = did, + uid = uid, + filePath = filePath, + physicalAddress = physicalAddress + ) + + /** + * Encode a Payload into an encrypted, URL-safe token string. + */ + def encode(payload: UploadTokenPayload): String = { + val filePathB64 = Encoder.encodeToString( + payload.filePath.getBytes(StandardCharsets.UTF_8) + ) + val physicalB64 = Encoder.encodeToString( + payload.physicalAddress.getBytes(StandardCharsets.UTF_8) + ) + + val raw = + s"$Version|${payload.uploadId}|${payload.did}|${payload.uid}|$filePathB64|$physicalB64" Review Comment: I think there are structured ways to use encryption instead of manually concatenating by `|` ########## common/workflow-core/src/main/scala/org/apache/texera/service/util/S3StorageClient.scala: ########## @@ -39,6 +39,12 @@ object S3StorageClient { val MINIMUM_NUM_OF_MULTIPART_S3_PART: Long = 5L * 1024 * 1024 // 5 MiB val MAXIMUM_NUM_OF_MULTIPART_S3_PARTS = 10_000 + /** Minimal info about an active multipart upload. */ + final case class MultipartUploadInfo(key: String, uploadId: String) + + /** Minimal info about a completed part in an upload. */ + final case class PartInfo(partNumber: Int, eTag: String) Review Comment: Are these case classes used as type definition? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
