Commit 1c0b3dcd authored by Abdullah Danish's avatar Abdullah Danish

AFP-001: implements azure vault

parent b7ba21fe
...@@ -28,6 +28,18 @@ ...@@ -28,6 +28,18 @@
<artifactId>azure-storage</artifactId> <artifactId>azure-storage</artifactId>
<version>8.6.4</version> <version>8.6.4</version>
</dependency> </dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-security-keyvault-secrets</artifactId>
<version>4.2.3</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.2.0</version>
</dependency>
<dependency> <dependency>
<groupId>org.bouncycastle</groupId> <groupId>org.bouncycastle</groupId>
<artifactId>bcpg-jdk16</artifactId> <artifactId>bcpg-jdk16</artifactId>
......
...@@ -2,6 +2,7 @@ package com.nisum.demo.blobStorage; ...@@ -2,6 +2,7 @@ package com.nisum.demo.blobStorage;
import com.microsoft.azure.functions.annotation.*; import com.microsoft.azure.functions.annotation.*;
import com.microsoft.azure.functions.*; import com.microsoft.azure.functions.*;
import com.nisum.demo.blobStorage.pgp.PGPService;
import com.nisum.demo.blobStorage.pgp.PGPUtils; import com.nisum.demo.blobStorage.pgp.PGPUtils;
import java.io.IOException; import java.io.IOException;
...@@ -21,11 +22,13 @@ public class BlobTriggerFunction { ...@@ -21,11 +22,13 @@ public class BlobTriggerFunction {
*/ */
@FunctionName("BlobTriggerFunc") @FunctionName("BlobTriggerFunc")
public void run( public void run(
@BlobTrigger(name = "file", path = "test/{name}", dataType = "binary", connection = "AzureWebJobsStorage") byte[] content, @BlobTrigger(name = "file", path = "test/{name}", dataType = "binary", connection = "AzureWebJobsStorage")
byte[] content,
@BindingName("name") String name, @BindingName("name") String name,
final ExecutionContext context final ExecutionContext context
) throws IOException { ) throws IOException {
ResourceBundle resourceBundle = new PropertyResourceBundle(new FileInputStream("/Users/eali/Projects/azure/myazurefunctionsdemo/src/main/resources/application.properties")); ResourceBundle resourceBundle = new PropertyResourceBundle(new FileInputStream(
"/Users/eali/Projects/azure/myazurefunctionsdemo/src/main/resources/application.properties"));
if (Pattern.matches("[a-z|A-Z]*.csv", name)) { if (Pattern.matches("[a-z|A-Z]*.csv", name)) {
String directoryPath = resourceBundle.getString("files.directory.path") + name; String directoryPath = resourceBundle.getString("files.directory.path") + name;
if (!Files.exists(Paths.get(directoryPath))) { if (!Files.exists(Paths.get(directoryPath))) {
...@@ -36,38 +39,12 @@ public class BlobTriggerFunction { ...@@ -36,38 +39,12 @@ public class BlobTriggerFunction {
fileOutputStream.write(content); fileOutputStream.write(content);
} }
InputStream inputStreamSecretKey = new FileInputStream(resourceBundle.getString("gpg.keychain.secret.key")); PGPService pgpService = new PGPService();
InputStream inputStreamPublicKey = new FileInputStream(resourceBundle.getString("gpg.keychain.public.key")); pgpService.encryptFile(name);
char[] pass = {'n', 'i', 's', 'u', 'm', '1', '2', '3', '4'};
// Writes data to the output stream context.getLogger()
OutputStream outbound = new FileOutputStream(resourceBundle.getString("encrypted.files.directory.path") + name.replaceFirst(".csv",".asc")); .info("Java Blob trigger function processed a blob. Name: " + name + "\n Size: " + content.length +
String inbound= resourceBundle.getString("files.directory.path") + name; " Bytes");
try {
// use it when using only public key
// PGPPublicKey key = PGPUtils.readPublicKey(inputStream);
// use it to get secret key when using public + private key
// PGPSecretKey pgpSecretKey = PGPUtils.readSecretKey(inputStream);
// for encryption
PGPUtils.encryptAndSignFile(outbound, inbound, inputStreamPublicKey, inputStreamSecretKey, true, true, pass);
context.getLogger().info("---File Encrypted---");
// for decryption
// InputStream fileToBeDecrypt = new FileInputStream(resourceBundle.getString("encrypted.files.directory.path") + name.replaceFirst(".csv", ".asc"));
// OutputStream inboundN = new FileOutputStream(resourceBundle.getString("decrypted.files.directory.path"));
// PGPUtils.decryptFile(fileToBeDecrypt, inboundN, inputStreamSecretKey, pass, inputStreamPublicKey);
} catch (Exception e) {
e.printStackTrace();
} }
// Closes the output stream
}
context.getLogger().info("Java Blob trigger function processed a blob. Name: " + name + "\n Size: " + content.length + " Bytes");
} }
} }
package com.nisum.demo.blobStorage.pgp; package com.nisum.demo.blobStorage.pgp;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class PGPService { public class PGPService {
Logger logger = LoggerFactory.getLogger(PGPService.class);
public void encryptFile(String name) throws IOException {
ResourceBundle resourceBundle = new PropertyResourceBundle(new FileInputStream("/Users/eali/Projects/azure/myazurefunctionsdemo/src/main/resources/application.properties"));
InputStream inputStreamSecretKey = new FileInputStream(resourceBundle.getString("gpg.keychain.secret.key"));
InputStream inputStreamPublicKey = new FileInputStream(resourceBundle.getString("gpg.keychain.public.key"));
char[] pass = {'n', 'i', 's', 'u', 'm', '1', '2', '3', '4'};
// Writes data to the output stream
OutputStream
outbound = new FileOutputStream(resourceBundle.getString("encrypted.files.directory.path") + name.replaceFirst(".csv",".asc"));
String inbound= resourceBundle.getString("files.directory.path") + name;
try {
// use it when using only public key
// PGPPublicKey key = PGPUtils.readPublicKey(inputStream);
// use it to get secret key when using public + private key
// PGPSecretKey pgpSecretKey = PGPUtils.readSecretKey(inputStream);
// for encryption
PGPUtils.encryptAndSignFile(outbound, inbound, inputStreamPublicKey, inputStreamSecretKey, true, true, pass);
logger.info("---File Encrypted---");
} catch (Exception e) {
e.printStackTrace();
}
}
public void decryptFile(String name) throws IOException {
ResourceBundle resourceBundle = new PropertyResourceBundle(new FileInputStream("/Users/eali/Projects/azure/myazurefunctionsdemo/src/main/resources/application.properties"));
InputStream inputStreamSecretKey = new FileInputStream(resourceBundle.getString("gpg.keychain.secret.key"));
InputStream inputStreamPublicKey = new FileInputStream(resourceBundle.getString("gpg.keychain.public.key"));
char[] pass = {'n', 'i', 's', 'u', 'm', '1', '2', '3', '4'};
// Writes data to the output stream
OutputStream
outbound = new FileOutputStream(resourceBundle.getString("encrypted.files.directory.path") + name.replaceFirst(".csv",".asc"));
String inbound= resourceBundle.getString("files.directory.path") + name;
try {
// use it when using only public key
// PGPPublicKey key = PGPUtils.readPublicKey(inputStream);
// use it to get secret key when using public + private key
// PGPSecretKey pgpSecretKey = PGPUtils.readSecretKey(inputStream);
// for decryption
InputStream fileToBeDecrypt = new FileInputStream(resourceBundle.getString
("encrypted.files.directory.path") + name.replaceFirst(".csv", ".asc"));
OutputStream inboundN = new FileOutputStream(resourceBundle.getString("decrypted.files.directory.path"));
PGPUtils.decryptFile(fileToBeDecrypt, inboundN, inputStreamSecretKey, pass, inputStreamPublicKey);
logger.info("---File Decrypted---");
} catch (Exception e) {
e.printStackTrace();
}
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment