Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring and naming conventions #15

Merged
merged 3 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmd/crypto-vault-cli/internal/commands/aes-commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func EncryptAESCmd(cmd *cobra.Command, args []string) {
log.Fatalf("Error: input, output, and keyDir flags are required\n")
}

aes := &cryptography.AESImpl{}
aes := &cryptography.AESCrypto{}

// Generate AES Key
key, err := aes.GenerateKey(keySize)
Expand Down Expand Up @@ -85,7 +85,7 @@ func DecryptAESCmd(cmd *cobra.Command, args []string) {
log.Fatalf("Error reading encrypted file: %v\n", err)
}

aes := &cryptography.AESImpl{}
aes := &cryptography.AESCrypto{}

decryptedData, err := aes.Decrypt(encryptedData, key)
if err != nil {
Expand Down
18 changes: 9 additions & 9 deletions cmd/crypto-vault-cli/internal/commands/ecdsa-commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ func SignECCCmd(cmd *cobra.Command, args []string) {
}

// ECC implementation
ecdsaImpl := &cryptography.ECDSAImpl{}
ECCrypto := &cryptography.ECCrypto{}
var privateKey *ecdsa.PrivateKey
var publicKey *ecdsa.PublicKey
var err error

// Generate new ECC keys if no private key is provided
privateKey, publicKey, err = ecdsaImpl.GenerateKeys(elliptic.P256())
privateKey, publicKey, err = ECCrypto.GenerateKeys(elliptic.P256())
if err != nil {
log.Fatalf("Error generating ECC keys: %v\n", err)
}
Expand All @@ -44,7 +44,7 @@ func SignECCCmd(cmd *cobra.Command, args []string) {
}

// Sign the file content (hash the content before signing)
signature, err := ecdsaImpl.Sign(fileContent, privateKey)
signature, err := ECCrypto.Sign(fileContent, privateKey)
if err != nil {
log.Fatalf("Error signing file content: %v\n", err)
}
Expand All @@ -57,7 +57,7 @@ func SignECCCmd(cmd *cobra.Command, args []string) {
if privateKey != nil && keyDir != "" {
privateKeyFilePath := fmt.Sprintf("%s/%s-private_key.pem", keyDir, uniqueID.String())

err = ecdsaImpl.SavePrivateKeyToFile(privateKey, privateKeyFilePath)
err = ECCrypto.SavePrivateKeyToFile(privateKey, privateKeyFilePath)
if err != nil {
log.Fatalf("Error saving private key: %v\n", err)
}
Expand All @@ -66,7 +66,7 @@ func SignECCCmd(cmd *cobra.Command, args []string) {

if publicKey != nil && keyDir != "" {
publicKeyFilePath := fmt.Sprintf("%s/%s-public_key.pem", keyDir, uniqueID.String())
err = ecdsaImpl.SavePublicKeyToFile(publicKey, publicKeyFilePath)
err = ECCrypto.SavePublicKeyToFile(publicKey, publicKeyFilePath)
if err != nil {
log.Fatalf("Error saving public key: %v\n", err)
}
Expand All @@ -76,7 +76,7 @@ func SignECCCmd(cmd *cobra.Command, args []string) {
// Save the signature to a file in the data folder (optional, based on the input file)
if keyDir != "" {
signatureFilePath := fmt.Sprintf("%s/%s-signature.sig", keyDir, uniqueID.String())
err = ecdsaImpl.SaveSignatureToFile(signatureFilePath, signature)
err = ECCrypto.SaveSignatureToFile(signatureFilePath, signature)
if err != nil {
log.Fatalf("Error saving signature: %v\n", err)
}
Expand All @@ -91,15 +91,15 @@ func VerifyECCCmd(cmd *cobra.Command, args []string) {
signatureFile, _ := cmd.Flags().GetString("signature") // Path to signature file

// ECC implementation
ecdsaImpl := &cryptography.ECDSAImpl{}
ECCrypto := &cryptography.ECCrypto{}
var publicKey *ecdsa.PublicKey
var err error

// Read the public key
if publicKeyPath == "" {
log.Fatalf("Public key is required for ECC signature verification.\n")
} else {
publicKey, err = ecdsaImpl.ReadPublicKey(publicKeyPath, elliptic.P256())
publicKey, err = ECCrypto.ReadPublicKey(publicKeyPath, elliptic.P256())
if err != nil {
log.Fatalf("Error reading public key: %v\n", err)
}
Expand All @@ -124,7 +124,7 @@ func VerifyECCCmd(cmd *cobra.Command, args []string) {
}

// Verify the signature
valid, err := ecdsaImpl.Verify(fileContent, signature, publicKey)
valid, err := ECCrypto.Verify(fileContent, signature, publicKey)
if err != nil {
log.Fatalf("Error verifying signature: %v\n", err)
}
Expand Down
18 changes: 9 additions & 9 deletions cmd/crypto-vault-cli/internal/commands/pkcs11-commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func IsTokenSetCmd(cmd *cobra.Command, args []string) {
modulePath, _ := cmd.Flags().GetString("module")
Label, _ := cmd.Flags().GetString("token-label")

token := &cryptography.PKCS11TokenImpl{
token := &cryptography.PKCS11TokenHandler{
ModulePath: modulePath,
Label: Label,
}
Expand All @@ -30,7 +30,7 @@ func IsObjectSetCmd(cmd *cobra.Command, args []string) {
objectLabel, _ := cmd.Flags().GetString("object-label")
userPin, _ := cmd.Flags().GetString("user-pin")

token := &cryptography.PKCS11TokenImpl{
token := &cryptography.PKCS11TokenHandler{
ModulePath: modulePath,
Label: Label,
ObjectLabel: objectLabel,
Expand All @@ -51,7 +51,7 @@ func InitializeTokenCmd(cmd *cobra.Command, args []string) {
soPin, _ := cmd.Flags().GetString("so-pin")
userPin, _ := cmd.Flags().GetString("user-pin")

token := &cryptography.PKCS11TokenImpl{
token := &cryptography.PKCS11TokenHandler{
ModulePath: modulePath,
Label: Label,
SOPin: soPin,
Expand All @@ -72,7 +72,7 @@ func AddKeyCmd(cmd *cobra.Command, args []string) {
keySize, _ := cmd.Flags().GetInt("key-size")
userPin, _ := cmd.Flags().GetString("user-pin")

token := &cryptography.PKCS11TokenImpl{
token := &cryptography.PKCS11TokenHandler{
ModulePath: modulePath,
Label: Label,
ObjectLabel: objectLabel,
Expand All @@ -94,7 +94,7 @@ func DeleteObjectCmd(cmd *cobra.Command, args []string) {
objectType, _ := cmd.Flags().GetString("object-type")
userPin, _ := cmd.Flags().GetString("user-pin")

token := &cryptography.PKCS11TokenImpl{
token := &cryptography.PKCS11TokenHandler{
ModulePath: modulePath,
Label: Label,
ObjectLabel: objectLabel,
Expand All @@ -116,7 +116,7 @@ func EncryptCmd(cmd *cobra.Command, args []string) {
inputFilePath, _ := cmd.Flags().GetString("input-file")
outputFilePath, _ := cmd.Flags().GetString("output-file")

token := &cryptography.PKCS11TokenImpl{
token := &cryptography.PKCS11TokenHandler{
ModulePath: modulePath,
Label: Label,
ObjectLabel: objectLabel,
Expand All @@ -139,7 +139,7 @@ func DecryptCmd(cmd *cobra.Command, args []string) {
inputFilePath, _ := cmd.Flags().GetString("input-file")
outputFilePath, _ := cmd.Flags().GetString("output-file")

token := &cryptography.PKCS11TokenImpl{
token := &cryptography.PKCS11TokenHandler{
ModulePath: modulePath,
Label: Label,
ObjectLabel: objectLabel,
Expand All @@ -162,7 +162,7 @@ func SignCmd(cmd *cobra.Command, args []string) {
inputFilePath, _ := cmd.Flags().GetString("input-file")
outputFilePath, _ := cmd.Flags().GetString("output-file")

token := &cryptography.PKCS11TokenImpl{
token := &cryptography.PKCS11TokenHandler{
ModulePath: modulePath,
Label: Label,
ObjectLabel: objectLabel,
Expand All @@ -185,7 +185,7 @@ func VerifyCmd(cmd *cobra.Command, args []string) {
dataFilePath, _ := cmd.Flags().GetString("data-file")
signatureFilePath, _ := cmd.Flags().GetString("signature-file")

token := &cryptography.PKCS11TokenImpl{
token := &cryptography.PKCS11TokenHandler{
ModulePath: modulePath,
Label: Label,
ObjectLabel: objectLabel,
Expand Down
4 changes: 2 additions & 2 deletions cmd/crypto-vault-cli/internal/commands/rsa-commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func EncryptRSACmd(cmd *cobra.Command, args []string) {
// Generate RSA keys if no public key is provided
var publicKey *rsa.PublicKey
var err error
rsa := &cryptography.RSAImpl{}
rsa := &cryptography.RSACrypto{}

uniqueID := uuid.New()
// Generate RSA keys
Expand Down Expand Up @@ -78,7 +78,7 @@ func DecryptRSACmd(cmd *cobra.Command, args []string) {
// Generate RSA keys if no private key is provided
var privateKey *rsa.PrivateKey
var err error
rsa := &cryptography.RSAImpl{}
rsa := &cryptography.RSACrypto{}
if privateKeyPath == "" {
// Generate RSA keys
privKey, _, genErr := rsa.GenerateKeys(2048)
Expand Down
1 change: 0 additions & 1 deletion internal/infrastructure/connector/az_vault.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,25 @@ import (
"github.com/google/uuid"
)

// AzureBlobConnector is an interface for interacting with Azure Blob storage
type AzureBlobConnector interface {
// Upload uploads multiple files to Azure Blob Storage and returns their metadata.
// BlobConnector is an interface for interacting with Blob storage
type BlobConnector interface {
// Upload uploads multiple files to Blob Storage and returns their metadata.
Upload(filePaths []string) ([]*blobs.BlobMeta, error)
// Download retrieves a blob's content by its ID and name, and returns the data as a stream.
Download(blobId, blobName string) (*bytes.Buffer, error)
// Delete deletes a blob from Azure Blob Storage by its ID and Name, and returns any error encountered.
// Delete deletes a blob from Blob Storage by its ID and Name, and returns any error encountered.
Delete(blobId, blobName string) error
}

// AzureBlobConnectorImpl is a struct that holds the Azure Blob storage client.
type AzureBlobConnectorImpl struct {
// AzureBlobConnector is a struct that holds the Azure Blob storage client and implements the BlobConnector interfaces.
type AzureBlobConnector struct {
Client *azblob.Client
ContainerName string
}

// NewAzureBlobConnector creates a new AzureBlobConnectorImpl instance using a connection string.
// NewAzureBlobConnector creates a new AzureBlobConnector instance using a connection string.
// It returns the connector and any error encountered during the initialization.
func NewAzureBlobConnector(connectionString string, containerName string) (*AzureBlobConnectorImpl, error) {
func NewAzureBlobConnector(connectionString string, containerName string) (*AzureBlobConnector, error) {
client, err := azblob.NewClientFromConnectionString(connectionString, nil)
if err != nil {
return nil, fmt.Errorf("failed to create Azure Blob client: %w", err)
Expand All @@ -43,14 +43,14 @@ func NewAzureBlobConnector(connectionString string, containerName string) (*Azur
fmt.Printf("Failed to create Azure container: %v\n", err) // The container may already exist, so we should not return an error in this case.
}

return &AzureBlobConnectorImpl{
return &AzureBlobConnector{
Client: client,
ContainerName: containerName,
}, nil
}

// Upload uploads multiple files to Azure Blob Storage and returns their metadata.
func (abc *AzureBlobConnectorImpl) Upload(filePaths []string) ([]*blobs.BlobMeta, error) {
func (abc *AzureBlobConnector) Upload(filePaths []string) ([]*blobs.BlobMeta, error) {
var uploadedBlobs []*blobs.BlobMeta
blobID := uuid.New().String()

Expand Down Expand Up @@ -118,7 +118,7 @@ func (abc *AzureBlobConnectorImpl) Upload(filePaths []string) ([]*blobs.BlobMeta
}

// rollbackUploadedBlobs deletes the blobs that were uploaded successfully before the error occurred
func (abc *AzureBlobConnectorImpl) rollbackUploadedBlobs(blobs []*blobs.BlobMeta) {
func (abc *AzureBlobConnector) rollbackUploadedBlobs(blobs []*blobs.BlobMeta) {
for _, blob := range blobs {
err := abc.Delete(blob.ID, blob.Name)
if err != nil {
Expand All @@ -130,7 +130,7 @@ func (abc *AzureBlobConnectorImpl) rollbackUploadedBlobs(blobs []*blobs.BlobMeta
}

// Download retrieves a blob's content by its ID and name, and returns the data as a stream.
func (abc *AzureBlobConnectorImpl) Download(blobId, blobName string) (*bytes.Buffer, error) {
func (abc *AzureBlobConnector) Download(blobId, blobName string) (*bytes.Buffer, error) {
ctx := context.Background()

// Construct the full blob path by combining blob ID and name
Expand Down Expand Up @@ -163,7 +163,7 @@ func (abc *AzureBlobConnectorImpl) Download(blobId, blobName string) (*bytes.Buf
}

// Delete deletes a blob from Azure Blob Storage by its ID and Name, and returns any error encountered.
func (abc *AzureBlobConnectorImpl) Delete(blobId, blobName string) error {
func (abc *AzureBlobConnector) Delete(blobId, blobName string) error {
ctx := context.Background()

// Construct the full blob path by combining blob ID and name
Expand Down
21 changes: 21 additions & 0 deletions internal/infrastructure/connector/key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package connector

import (
"bytes"
"crypto_vault_service/internal/domain/keys"
)

// VaultConnector is an interface for interacting with key storages
type VaultConnector interface {
// Upload uploads multiple files to Vault Storage and returns their metadata.
Upload(filePaths []string) ([]*keys.CryptoKeyMeta, error)

// Download retrieves a blob's content by its ID and name, and returns the data as a stream.
Download(blobId, blobName string) (*bytes.Buffer, error)

//
// Rotate()

// Delete deletes a blob from Vault Storage by its ID and Name, and returns any error encountered.
Delete(blobId, blobName string) error
}
10 changes: 5 additions & 5 deletions internal/infrastructure/cryptography/aes.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ type AES interface {
GenerateKey(keySize int) ([]byte, error)
}

// AESImpl struct that implements the AES interface
type AESImpl struct{}
// AESCrypto struct that implements the AES interface
type AESCrypto struct{}

// Pad data to make it a multiple of AES block size
func pkcs7Pad(data []byte, blockSize int) []byte {
Expand All @@ -37,7 +37,7 @@ func pkcs7Unpad(data []byte, blockSize int) ([]byte, error) {
}

// GenerateRandomAESKey generates a random AES key of the specified size
func (a *AESImpl) GenerateKey(keySize int) ([]byte, error) {
func (a *AESCrypto) GenerateKey(keySize int) ([]byte, error) {
key := make([]byte, keySize)
_, err := rand.Read(key)
if err != nil {
Expand All @@ -47,7 +47,7 @@ func (a *AESImpl) GenerateKey(keySize int) ([]byte, error) {
}

// Encrypt data using AES in CBC mode
func (a *AESImpl) Encrypt(plainText, key []byte) ([]byte, error) {
func (a *AESCrypto) Encrypt(plainText, key []byte) ([]byte, error) {
if key == nil {
return nil, fmt.Errorf("key key cannot be nil")
}
Expand All @@ -72,7 +72,7 @@ func (a *AESImpl) Encrypt(plainText, key []byte) ([]byte, error) {
}

// Decrypt data using AES in CBC mode
func (a *AESImpl) Decrypt(ciphertext, key []byte) ([]byte, error) {
func (a *AESCrypto) Decrypt(ciphertext, key []byte) ([]byte, error) {
if key == nil {
return nil, fmt.Errorf("key key cannot be nil")
}
Expand Down
Loading
Loading