Set up spring app with menu driven HDFS
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
package com.skycrate.backend.skycrateBackend.services;
|
||||
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.KeyGenerator;
|
||||
import javax.crypto.SecretKey;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.security.*;
|
||||
|
||||
public class EncryptionUtil {
|
||||
private static final String RSA_ALGORITHM = "RSA";
|
||||
private static final String AES_ALGORITHM = "AES";
|
||||
private static final int RSA_KEY_SIZE = 2048;
|
||||
private static final int AES_KEY_SIZE = 256;
|
||||
|
||||
public static KeyPair generateKeyPair() throws NoSuchAlgorithmException {
|
||||
KeyPairGenerator keyGen = KeyPairGenerator.getInstance(RSA_ALGORITHM);
|
||||
keyGen.initialize(RSA_KEY_SIZE);
|
||||
return keyGen.generateKeyPair();
|
||||
}
|
||||
|
||||
public static byte[] encrypt(byte[] data, PublicKey publicKey) throws Exception {
|
||||
// Generate a random AES key
|
||||
KeyGenerator keyGen = KeyGenerator.getInstance(AES_ALGORITHM);
|
||||
keyGen.init(AES_KEY_SIZE);
|
||||
SecretKey aesKey = keyGen.generateKey();
|
||||
|
||||
// Encrypt the data with the AES key
|
||||
Cipher aesCipher = Cipher.getInstance(AES_ALGORITHM);
|
||||
aesCipher.init(Cipher.ENCRYPT_MODE, aesKey);
|
||||
byte[] encryptedData = aesCipher.doFinal(data);
|
||||
|
||||
// Encrypt the AES key with the RSA public key
|
||||
Cipher rsaCipher = Cipher.getInstance(RSA_ALGORITHM);
|
||||
rsaCipher.init(Cipher.ENCRYPT_MODE, publicKey);
|
||||
byte[] encryptedAesKey = rsaCipher.doFinal(aesKey.getEncoded());
|
||||
|
||||
// Combine the encrypted AES key and the encrypted data
|
||||
byte[] combined = new byte[encryptedAesKey.length + encryptedData.length + 4];
|
||||
System.arraycopy(encryptedAesKey, 0, combined, 0, encryptedAesKey.length);
|
||||
System.arraycopy(encryptedData, 0, combined, encryptedAesKey.length, encryptedData.length);
|
||||
// Store the length of the encrypted AES key at the beginning
|
||||
combined[encryptedAesKey.length + encryptedData.length] = (byte) (encryptedAesKey.length >> 24);
|
||||
combined[encryptedAesKey.length + encryptedData.length + 1] = (byte) (encryptedAesKey.length >> 16);
|
||||
combined[encryptedAesKey.length + encryptedData.length + 2] = (byte) (encryptedAesKey.length >> 8);
|
||||
combined[encryptedAesKey.length + encryptedData.length + 3] = (byte) (encryptedAesKey.length);
|
||||
|
||||
return combined;
|
||||
}
|
||||
|
||||
public static byte[] decrypt(byte[] encryptedData, PrivateKey privateKey) throws Exception {
|
||||
// Read the length of the encrypted AES key
|
||||
int aesKeyLength = ((encryptedData[encryptedData.length - 4] & 0xFF) << 24) |
|
||||
((encryptedData[encryptedData.length - 3] & 0xFF) << 16) |
|
||||
((encryptedData[encryptedData.length - 2] & 0xFF) << 8) |
|
||||
(encryptedData[encryptedData.length - 1] & 0xFF);
|
||||
|
||||
// Extract the encrypted AES key and the encrypted data
|
||||
byte[] encryptedAesKey = new byte[aesKeyLength];
|
||||
byte[] encryptedDataBytes = new byte[encryptedData.length - aesKeyLength - 4];
|
||||
System.arraycopy(encryptedData, 0, encryptedAesKey, 0, aesKeyLength);
|
||||
System.arraycopy(encryptedData, aesKeyLength, encryptedDataBytes, 0, encryptedDataBytes.length);
|
||||
|
||||
// Decrypt the AES key with the RSA private key
|
||||
Cipher rsaCipher = Cipher.getInstance(RSA_ALGORITHM);
|
||||
rsaCipher.init(Cipher.DECRYPT_MODE, privateKey);
|
||||
byte[] aesKeyBytes = rsaCipher.doFinal(encryptedAesKey);
|
||||
SecretKey aesKey = new SecretKeySpec(aesKeyBytes, AES_ALGORITHM);
|
||||
|
||||
// Decrypt the data with the AES key
|
||||
Cipher aesCipher = Cipher.getInstance(AES_ALGORITHM);
|
||||
aesCipher.init(Cipher.DECRYPT_MODE, aesKey);
|
||||
return aesCipher.doFinal(encryptedDataBytes);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
package com.skycrate.backend.skycrateBackend.services;
|
||||
|
||||
|
||||
import com.skycrate.backend.skycrateBackend.config.HDFSConfig;
|
||||
import com.skycrate.backend.skycrateBackend.dto.User;
|
||||
import org.apache.hadoop.fs.*;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
@Service
|
||||
public class HDFSOperations {
|
||||
public static void uploadFile(String localPath, String hdfsPath, String uploadedFileName, User user) throws Exception {
|
||||
FileSystem fs = HDFSConfig.getHDFS();
|
||||
byte[] data = Files.readAllBytes(Paths.get(localPath)); // Read file as bytes
|
||||
byte[] encryptedData = EncryptionUtil.encrypt(data, user.getKeyPair().getPublic());
|
||||
|
||||
// Save encryptedData to a temporary file and upload it
|
||||
String tempFilePath = localPath + ".enc";
|
||||
Files.write(Paths.get(tempFilePath), encryptedData); // Write bytes to temp file
|
||||
|
||||
// Construct the final HDFS path using the provided uploaded file name
|
||||
String finalHdfsPath = hdfsPath.endsWith("/") ? hdfsPath + uploadedFileName : hdfsPath + "/" + uploadedFileName;
|
||||
|
||||
fs.copyFromLocalFile(new Path(tempFilePath), new Path(finalHdfsPath));
|
||||
System.out.println("✅ File uploaded: " + finalHdfsPath);
|
||||
|
||||
// Clean up temporary file
|
||||
Files.delete(Paths.get(tempFilePath));
|
||||
}
|
||||
|
||||
public static void downloadFile(String hdfsPath, String localPath, User user) throws Exception {
|
||||
FileSystem fs = HDFSConfig.getHDFS();
|
||||
String tempFilePath = localPath + ".enc";
|
||||
|
||||
fs.copyToLocalFile(new Path(hdfsPath), new Path(tempFilePath));
|
||||
|
||||
// Read the encrypted file as bytes
|
||||
byte[] encryptedData = Files.readAllBytes(Paths.get(tempFilePath));
|
||||
byte[] decryptedData = EncryptionUtil.decrypt(encryptedData, user.getKeyPair().getPrivate());
|
||||
|
||||
Files.write(Paths.get(localPath), decryptedData); // Write decrypted bytes to local file
|
||||
System.out.println("✅ File downloaded: " + localPath);
|
||||
|
||||
// Clean up temporary file
|
||||
Files.delete(Paths.get(tempFilePath));
|
||||
}
|
||||
|
||||
public static void createFolder(String hdfsPath) throws Exception {
|
||||
FileSystem fs = HDFSConfig.getHDFS();
|
||||
Path path = new Path(hdfsPath);
|
||||
if (!fs.exists(path)) {
|
||||
fs.mkdirs(path);
|
||||
System.out.println("✅ Folder created: " + hdfsPath);
|
||||
} else {
|
||||
System.out.println("⚠️ Folder already exists: " + hdfsPath);
|
||||
}
|
||||
}
|
||||
|
||||
public static void deleteFile(String hdfsFilePath) throws Exception {
|
||||
FileSystem fs = HDFSConfig.getHDFS();
|
||||
Path path = new Path(hdfsFilePath);
|
||||
if (fs.exists(path)) {
|
||||
fs.delete(path, false); // false means do not recursively delete
|
||||
System.out.println("✅ File deleted: " + hdfsFilePath);
|
||||
} else {
|
||||
System.out.println("⚠️ File does not exist: " + hdfsFilePath);
|
||||
}
|
||||
}
|
||||
|
||||
public static void deleteFolder(String hdfsFolderPath) throws Exception {
|
||||
FileSystem fs = HDFSConfig.getHDFS();
|
||||
Path path = new Path(hdfsFolderPath);
|
||||
if (fs.exists(path)) {
|
||||
fs.delete(path, true); // true means recursively delete
|
||||
System.out.println("✅ Folder deleted: " + hdfsFolderPath);
|
||||
} else {
|
||||
System.out.println("⚠️ Folder does not exist: " + hdfsFolderPath);
|
||||
}
|
||||
}
|
||||
|
||||
public static void listFilesAndFolders(String hdfsPath) throws Exception {
|
||||
FileSystem fs = HDFSConfig.getHDFS();
|
||||
Path path = new Path(hdfsPath);
|
||||
|
||||
if (!fs.exists(path)) {
|
||||
System.out.println("⚠️ Path does not exist: " + hdfsPath);
|
||||
return;
|
||||
}
|
||||
|
||||
System.out.println("Listing files and folders in: " + hdfsPath);
|
||||
listFilesAndFoldersRecursively(fs, path, "");
|
||||
}
|
||||
|
||||
private static void listFilesAndFoldersRecursively(FileSystem fs, Path path, String indent) throws IOException {
|
||||
FileStatus[] fileStatuses = fs.listStatus(path);
|
||||
for (FileStatus fileStatus : fileStatuses) {
|
||||
System.out.println(indent + (fileStatus.isDirectory() ? "📁 " : "📄 ") + fileStatus.getPath().getName());
|
||||
if (fileStatus.isDirectory()) {
|
||||
listFilesAndFoldersRecursively(fs, fileStatus.getPath(), indent + " "); // Indent for subdirectories
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user