-
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.
Merge pull request #16 from MGTheTrain/opt/general
Increase code coverage and refactoring
- Loading branch information
Showing
25 changed files
with
973 additions
and
385 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,132 @@ | ||
package services | ||
|
||
import ( | ||
"crypto_vault_service/internal/domain/blobs" | ||
"crypto_vault_service/internal/infrastructure/connector" | ||
"crypto_vault_service/internal/persistence/repository" | ||
"fmt" | ||
) | ||
|
||
// BlobUploadService implements the BlobUploadService interface for handling blob uploads | ||
type BlobUploadService struct { | ||
BlobConnector connector.BlobConnector | ||
BlobRepository repository.BlobRepository | ||
} | ||
|
||
// NewBlobUploadService creates a new instance of BlobUploadService | ||
func NewBlobUploadService(blobConnector connector.BlobConnector, blobRepository repository.BlobRepository) *BlobUploadService { | ||
return &BlobUploadService{ | ||
BlobConnector: blobConnector, | ||
BlobRepository: blobRepository, | ||
} | ||
} | ||
|
||
// Upload handles the upload of blobs and stores their metadata in the database. | ||
func (s *BlobUploadService) Upload(filePaths []string) ([]*blobs.BlobMeta, error) { | ||
|
||
// Use the BlobConnector to upload the files to Azure Blob Storage | ||
blobMeta, err := s.BlobConnector.Upload(filePaths) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to upload blobs: %w", err) | ||
} | ||
|
||
// If no blobs are uploaded, return early | ||
if len(blobMeta) == 0 { | ||
return nil, fmt.Errorf("no blobs uploaded") | ||
} | ||
|
||
// Store the metadata in the database using the BlobRepository | ||
for _, blob := range blobMeta { | ||
err := s.BlobRepository.Create(blob) | ||
if err != nil { | ||
// Rollback any previously uploaded blobs if the metadata fails to store | ||
// (you can call delete method to handle this as needed) | ||
return nil, fmt.Errorf("failed to store metadata for blob '%s': %w", blob.Name, err) | ||
} | ||
} | ||
|
||
// Return the metadata of uploaded blobs | ||
return blobMeta, nil | ||
} | ||
|
||
// BlobMetadataService implements the BlobMetadataService interface for retrieving and deleting blob metadata | ||
type BlobMetadataService struct { | ||
BlobConnector connector.BlobConnector | ||
BlobRepository repository.BlobRepository | ||
} | ||
|
||
// NewBlobMetadataService creates a new instance of BlobMetadataService | ||
func NewBlobMetadataService(blobRepository repository.BlobRepository, blobConnector connector.BlobConnector) *BlobMetadataService { | ||
return &BlobMetadataService{ | ||
BlobConnector: blobConnector, | ||
BlobRepository: blobRepository, | ||
} | ||
} | ||
|
||
// List retrieves all blobs' metadata considering a query filter | ||
func (s *BlobMetadataService) List(query *blobs.BlobMetaQuery) ([]*blobs.BlobMeta, error) { | ||
// Assuming BlobRepository has a method to query metadata, you can adapt to GORM queries. | ||
var blobsList []*blobs.BlobMeta | ||
|
||
// TBD | ||
|
||
return blobsList, nil | ||
} | ||
|
||
// GetByID retrieves a blob's metadata by its unique ID | ||
func (s *BlobMetadataService) GetByID(blobID string) (*blobs.BlobMeta, error) { | ||
// Retrieve the blob metadata using the BlobRepository | ||
blob, err := s.BlobRepository.GetById(blobID) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to retrieve blob metadata by ID '%s': %w", blobID, err) | ||
} | ||
return blob, nil | ||
} | ||
|
||
// DeleteByID deletes a blob and its associated metadata by ID | ||
func (s *BlobMetadataService) DeleteByID(blobID string) error { | ||
// Retrieve the blob metadata to ensure it exists | ||
blob, err := s.BlobRepository.GetById(blobID) | ||
if err != nil { | ||
return fmt.Errorf("failed to retrieve blob metadata by ID '%s' for deletion: %w", blobID, err) | ||
} | ||
|
||
// Delete the blob from Blob Storage using the BlobConnector | ||
err = s.BlobRepository.DeleteById(blobID) | ||
if err != nil { | ||
return fmt.Errorf("failed to delete blob metadata by ID '%s': %w", blobID, err) | ||
} | ||
|
||
// Now, delete the actual blob from the Blob Storage | ||
err = s.BlobConnector.Delete(blob.ID, blob.Name) | ||
if err != nil { | ||
return fmt.Errorf("failed to delete blob '%s' from Blob Storage: %w", blob.Name, err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// BlobDownloadService implements the BlobDownloadService interface for downloading blobs | ||
type BlobDownloadService struct { | ||
BlobConnector connector.BlobConnector | ||
} | ||
|
||
// NewBlobDownloadService creates a new instance of BlobDownloadService | ||
func NewBlobDownloadService(blobConnector connector.BlobConnector) *BlobDownloadService { | ||
return &BlobDownloadService{ | ||
BlobConnector: blobConnector, | ||
} | ||
} | ||
|
||
// Download retrieves a blob's content by its ID and name | ||
func (s *BlobDownloadService) Download(blobID, blobName string) ([]byte, error) { | ||
// Retrieve the blob metadata from the BlobRepository to ensure it exists | ||
// Here you might want to consider validating the blob's existence. | ||
blob, err := s.BlobConnector.Download(blobID, blobName) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to download blob '%s': %w", blobName, err) | ||
} | ||
|
||
// Return the metadata and content of the downloaded blob | ||
return blob, nil | ||
} |
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,5 @@ | ||
package services | ||
|
||
// CryptoKeyOperationService implements the ICryptoKeyOperationService interface for local cryptographic key management, encryption, signing, and PKCS#11 operations. | ||
type CryptoKeyOperationService struct { | ||
} |
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
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
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,69 @@ | ||
package crypto | ||
|
||
// ICryptoKeyOperationService defines methods for local cryptographic key management, encryption, signing, and PKCS#11 operations. | ||
type ICryptoKeyOperationService interface { | ||
|
||
// --- Key Generation --- | ||
|
||
// GenerateKey generates a cryptographic key of the specified type and size (e.g., AES, RSA, ECDSA). | ||
// It returns the generated key as a byte slice and any error encountered during the key generation. | ||
GenerateKey(keyType string, keySize int) ([]byte, error) | ||
|
||
// --- Key Storage and Retrieval --- | ||
|
||
// SaveKey saves a cryptographic key to a specified file. | ||
// It returns any error encountered during the saving process. | ||
SaveKey(key []byte, filename string) error | ||
|
||
// LoadKey loads a cryptographic key from a specified file. | ||
// It returns the loaded key as a byte slice and any error encountered during the loading process. | ||
LoadKey(filename string) ([]byte, error) | ||
|
||
// --- Encryption and Decryption (Symmetric algorithms like AES) --- | ||
|
||
// EncryptWithSymmetricKey encrypts data using a symmetric key (e.g., AES). | ||
// It returns the encrypted data as a byte slice and any error encountered during encryption. | ||
EncryptWithSymmetricKey(plainText []byte, key []byte) ([]byte, error) | ||
|
||
// DecryptWithSymmetricKey decrypts data using a symmetric key (e.g., AES). | ||
// It returns the decrypted data as a byte slice and any error encountered during decryption. | ||
DecryptWithSymmetricKey(cipherText []byte, key []byte) ([]byte, error) | ||
|
||
// --- Asymmetric Encryption (RSA, ECDSA, PKCS#11) --- | ||
|
||
// EncryptWithPublicKey encrypts data with a public key using asymmetric encryption algorithms (e.g., RSA, ECDSA). | ||
// It optionally supports PKCS#11 hardware tokens for key storage. | ||
// It returns the encrypted data as a byte slice and any error encountered during encryption. | ||
EncryptWithPublicKey(plainText []byte, publicKey interface{}) ([]byte, error) | ||
|
||
// DecryptWithPrivateKey decrypts data with a private key using asymmetric encryption algorithms (e.g., RSA, ECDSA). | ||
// It optionally supports PKCS#11 hardware tokens for key storage. | ||
// It returns the decrypted data as a byte slice and any error encountered during decryption. | ||
DecryptWithPrivateKey(cipherText []byte, privateKey interface{}) ([]byte, error) | ||
|
||
// --- Signing and Verification (For RSA, ECDSA) --- | ||
|
||
// SignWithPrivateKey signs a message using a private key with asymmetric algorithms (e.g., RSA, ECDSA). | ||
// It optionally supports PKCS#11 hardware tokens for key storage. | ||
// It returns the signature and any error encountered during the signing process. | ||
SignWithPrivateKey(message []byte, privateKey interface{}) ([]byte, error) | ||
|
||
// VerifyWithPublicKey verifies a signature using a public key with asymmetric algorithms (e.g., RSA, ECDSA). | ||
// It optionally supports PKCS#11 hardware tokens for key storage. | ||
// It returns true if the signature is valid, false otherwise, and any error encountered during the verification process. | ||
VerifyWithPublicKey(message []byte, signature []byte, publicKey interface{}) (bool, error) | ||
|
||
// --- PKCS#11 Operations --- | ||
|
||
// InitializeToken initializes a PKCS#11 token in the specified hardware slot. | ||
// It returns any error encountered during the initialization. | ||
InitializeToken(slot string) error | ||
|
||
// AddKeyToToken adds a cryptographic key to a PKCS#11 token. | ||
// It returns any error encountered during the addition of the key. | ||
AddKeyToToken() error | ||
|
||
// DeleteKeyFromToken deletes a cryptographic key from a PKCS#11 token by its type and label. | ||
// It returns any error encountered during the deletion of the key. | ||
DeleteKeyFromToken(objectType, objectLabel string) error | ||
} |
Oops, something went wrong.