-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0c5ad41
commit 391a8fb
Showing
29 changed files
with
684 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 39 additions & 2 deletions
41
src/main/java/code/shubham/TemplateServiceJavaSpringBootApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,51 @@ | ||
package code.shubham; | ||
|
||
import code.shubham.commons.annotations.SpringBootApp; | ||
import code.shubham.commons.aws.CloudFrontUtils; | ||
import code.shubham.commons.keystore.dao.entities.KeyStore; | ||
import code.shubham.commons.keystore.dao.repositories.KeyRepository; | ||
import code.shubham.encryption.keys.asymmetric.RSAUtil; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.bouncycastle.jce.provider.BouncyCastleProvider; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.CommandLineRunner; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.data.jpa.repository.config.EnableJpaAuditing; | ||
|
||
import java.io.DataInputStream; | ||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.security.Key; | ||
import java.security.KeyFactory; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.security.PrivateKey; | ||
import java.security.Security; | ||
import java.security.spec.InvalidKeySpecException; | ||
import java.security.spec.PKCS8EncodedKeySpec; | ||
|
||
@Slf4j | ||
@SpringBootApp | ||
public class TemplateServiceJavaSpringBootApplication { | ||
public class TemplateServiceJavaSpringBootApplication implements CommandLineRunner { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(TemplateServiceJavaSpringBootApplication.class, args); | ||
} | ||
|
||
@Autowired | ||
private KeyRepository keyRepository; | ||
|
||
@Override | ||
public void run(String... args) throws Exception { | ||
// final PrivateKey privateKey = CloudFrontUtils.readPrivateKeyFromFile(args[0]); | ||
// final KeyStore entity = | ||
// this.keyRepository.findByPurpose("DOCUMENTS-CDN-CLOUD_FRONT-PRIVATE_KEY"); | ||
// if (entity == null) | ||
// this.keyRepository.save( | ||
// KeyStore.builder() | ||
// .key(privateKey.getEncoded()) | ||
// .purpose("DOCUMENTS-CDN-CLOUD_FRONT-PRIVATE_KEY") | ||
// .build()); | ||
// System.out.println ("Key algorithm: " + privateKey.getAlgorithm()); | ||
} | ||
|
||
} |
83 changes: 83 additions & 0 deletions
83
src/main/java/code/shubham/commons/aws/CloudFrontUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package code.shubham.commons.aws; | ||
|
||
import com.amazonaws.services.cloudfront.CloudFrontUrlSigner; | ||
import com.amazonaws.services.cloudfront.model.KeyPairIds; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.bouncycastle.jce.provider.BouncyCastleProvider; | ||
|
||
import java.io.DataInputStream; | ||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.security.KeyFactory; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.security.PrivateKey; | ||
import java.security.Security; | ||
import java.security.spec.InvalidKeySpecException; | ||
import java.security.spec.PKCS8EncodedKeySpec; | ||
import java.util.Date; | ||
|
||
@Slf4j | ||
public class CloudFrontUtils { | ||
|
||
public static String signedURL(final String distributionDomain, final String s3ObjectKey, final String keyPairId, | ||
final PrivateKey privateKey, final Date dateLessThan) { | ||
return CloudFrontUrlSigner.getSignedURLWithCannedPolicy("https://" + distributionDomain + "/" + s3ObjectKey, | ||
keyPairId, privateKey, dateLessThan); | ||
} | ||
|
||
public static String keyPair(final String distributionDomain, final String s3ObjectKey, final String keyPairId, | ||
final PrivateKey privateKey, final Date dateLessThan) { | ||
return CloudFrontUrlSigner.getSignedURLWithCannedPolicy("https://" + distributionDomain + "/" + s3ObjectKey, | ||
keyPairId, privateKey, dateLessThan); | ||
} | ||
|
||
public static PrivateKey readPrivateKeyFromFile(final String filePath) | ||
throws IOException, NoSuchAlgorithmException, InvalidKeySpecException { | ||
Security.addProvider(new BouncyCastleProvider()); | ||
File file = new File(filePath); | ||
FileInputStream fileStream = new FileInputStream(file); | ||
|
||
PrivateKey var10; | ||
try { | ||
DataInputStream dataStream = new DataInputStream(fileStream); | ||
|
||
try { | ||
byte[] keyBytes = new byte[(int) file.length()]; | ||
dataStream.readFully(keyBytes); | ||
String temp = new String(keyBytes, "UTF-8"); | ||
String header = temp.replace("-----BEGIN PRIVATE KEY-----", ""); | ||
header = header.replace("-----END PRIVATE KEY-----", ""); | ||
header.replace("\n", ""); | ||
byte[] decoded = java.util.Base64.getMimeDecoder().decode(header); | ||
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(decoded); | ||
KeyFactory kf = KeyFactory.getInstance("RSA"); | ||
var10 = kf.generatePrivate(spec); | ||
} | ||
catch (Throwable var13) { | ||
try { | ||
dataStream.close(); | ||
} | ||
catch (Throwable var12) { | ||
var13.addSuppressed(var12); | ||
} | ||
log.error("", var13); | ||
throw var13; | ||
} | ||
|
||
dataStream.close(); | ||
} | ||
catch (Throwable var14) { | ||
try { | ||
fileStream.close(); | ||
} | ||
catch (Throwable var11) { | ||
var14.addSuppressed(var11); | ||
} | ||
log.error("", var14); | ||
throw var14; | ||
} | ||
return var10; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/main/java/code/shubham/commons/enums/DownloadURLSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package code.shubham.commons.enums; | ||
|
||
public enum DownloadURLSource { | ||
|
||
S3, CLOUD_FRONT | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.