chore(clustertool): Improve logging
This commit is contained in:
@@ -11,13 +11,19 @@ import (
|
||||
|
||||
// EncryptAllFiles encrypts all unencrypted files as specified in the .sops.yaml configuration.
|
||||
func EncryptAllFiles() error {
|
||||
log.Trace().Msg("Starting EncryptAllFiles function")
|
||||
|
||||
files, err := ExecuteCheck(false) // Get the list of files and their encryption status
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Failed to execute check for file statuses")
|
||||
return err
|
||||
}
|
||||
|
||||
log.Debug().Int("fileCount", len(files)).Msg("Found files to process for encryption")
|
||||
|
||||
for _, file := range files {
|
||||
if err := processFileEncryption(file); err != nil {
|
||||
log.Error().Err(err).Msgf("Error processing encryption for file: %s", file.Path)
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -37,6 +43,7 @@ func processFileEncryption(file EncrFileData) error {
|
||||
// Check if the file is partially staged
|
||||
fullyStaged, err := helper.IsFileFullyStaged(file.Path)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("Error checking staged status of file: %s", file.Path)
|
||||
return fmt.Errorf("error checking staged status of file %s: %v", file.Path, err)
|
||||
}
|
||||
|
||||
@@ -45,6 +52,7 @@ func processFileEncryption(file EncrFileData) error {
|
||||
log.Info().Msgf("File %s is partially staged, staging fully...\n", file.Path)
|
||||
err := helper.StageFile(file.Path)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("Error staging file: %s", file.Path)
|
||||
return fmt.Errorf("error staging file %s: %v", file.Path, err)
|
||||
}
|
||||
log.Info().Msgf("File %s fully staged.\n", file.Path)
|
||||
@@ -53,6 +61,7 @@ func processFileEncryption(file EncrFileData) error {
|
||||
// Encrypt the file
|
||||
err = encryptFile(file.Path)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("Error encrypting file: %s", file.Path)
|
||||
return fmt.Errorf("error encrypting file %s: %v", file.Path, err)
|
||||
}
|
||||
|
||||
@@ -60,18 +69,22 @@ func processFileEncryption(file EncrFileData) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// encryptFilePlaceholder encrypts the content of the specified file and replaces the file with the encrypted data.
|
||||
// encryptFile encrypts the content of the specified file and replaces the file with the encrypted data.
|
||||
func encryptFile(filePath string) error {
|
||||
log.Trace().Msgf("Starting encryption for file: %s", filePath)
|
||||
|
||||
// Read the content of the file
|
||||
content, err := os.ReadFile(filePath)
|
||||
log.Info().Msgf("Encrypting '%s'... \n", filePath)
|
||||
log.Debug().Msgf("Encrypting '%s'... \n", filePath)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Error reading file")
|
||||
return fmt.Errorf("error reading file: %v", err)
|
||||
}
|
||||
|
||||
// Ensure the regex covers the whole content
|
||||
sopsConfig, err := LoadSopsConfig()
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Error loading SOPS configuration")
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -80,18 +93,24 @@ func encryptFile(filePath string) error {
|
||||
// Encrypt the content
|
||||
encryptedData, err := EncryptWithAgeKey(content, encrRegex, GetFormat(filePath))
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Error encrypting data")
|
||||
return fmt.Errorf("error encrypting data: %v", err)
|
||||
}
|
||||
|
||||
// Write the encrypted data back to the file
|
||||
if err := os.WriteFile(filePath, encryptedData, 0644); err != nil {
|
||||
log.Error().Err(err).Msg("Error writing encrypted data to file")
|
||||
return fmt.Errorf("error writing encrypted data to file: %v", err)
|
||||
}
|
||||
|
||||
log.Info().Msgf("Successfully encrypted file: %s", filePath)
|
||||
return nil
|
||||
}
|
||||
|
||||
// mergeRegex merges regex from the SOPS configuration.
|
||||
func mergeRegex(filePath string, config SopsConfig) string {
|
||||
log.Trace().Msgf("Merging regex for file: %s", filePath)
|
||||
|
||||
// Initialize an empty string for merged regex
|
||||
mergedRegex := ""
|
||||
|
||||
@@ -100,7 +119,7 @@ func mergeRegex(filePath string, config SopsConfig) string {
|
||||
// Compile the regex pattern
|
||||
r, err := regexp.Compile(rule.PathRegex)
|
||||
if err != nil {
|
||||
log.Info().Msgf("Error compiling regex: %v", err)
|
||||
log.Warn().Err(err).Msg("Error compiling regex")
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -108,12 +127,14 @@ func mergeRegex(filePath string, config SopsConfig) string {
|
||||
if r.MatchString(filePath) {
|
||||
// Merge the encrypted regex into the mergedRegex string
|
||||
mergedRegex += rule.EncryptedRegex + "|"
|
||||
log.Debug().Msgf("File %s matched regex, adding encrypted regex: %s", filePath, rule.EncryptedRegex)
|
||||
}
|
||||
}
|
||||
|
||||
// Remove the trailing "|" if mergedRegex is not empty
|
||||
if mergedRegex != "" {
|
||||
mergedRegex = mergedRegex[:len(mergedRegex)-1]
|
||||
log.Debug().Msgf("Merged regex for file %s: %s", filePath, mergedRegex)
|
||||
}
|
||||
|
||||
return mergedRegex
|
||||
|
||||
Reference in New Issue
Block a user