Skip to content

Commit

Permalink
[FIX] fixes env line processing; (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
atlet99 authored Oct 28, 2024
1 parent 8a1a36b commit b99dd99
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ RUN apk --no-cache add build-base git

WORKDIR /app

COPY go.mod go.sum ./
COPY go.mod ./
RUN go mod download

COPY . .
Expand Down
28 changes: 17 additions & 11 deletions encryption/encryption.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,31 @@ import (
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"errors"
"fmt"
"io"
)

// Encrypt encrypts a plaintext string using AES encryption
// Encrypt encrypts a plaintext string using AES encryption and returns a base64-encoded ciphertext.
func Encrypt(password, plaintext string) (string, error) {
if plaintext == "" {
return "", nil
return "", errors.New("plaintext is empty")
}

key := make([]byte, 32)
copy(key, []byte(password))

block, err := aes.NewCipher(key)
if err != nil {
return "", err
return "", fmt.Errorf("failed to create AES cipher: %v", err)
}

content := pad([]byte(plaintext), block.BlockSize())
ciphertext := make([]byte, aes.BlockSize+len(content))

iv := ciphertext[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
return "", err
return "", fmt.Errorf("failed to generate IV: %v", err)
}

mode := cipher.NewCBCEncrypter(block, iv)
Expand All @@ -34,22 +39,23 @@ func Encrypt(password, plaintext string) (string, error) {
return base64.StdEncoding.EncodeToString(ciphertext), nil
}

// Decrypt decrypts an AES-encrypted string
// Decrypt decrypts a base64-encoded ciphertext string using AES and returns the plaintext.
func Decrypt(password, crypt64 string) (string, error) {
if crypt64 == "" {
return "", nil
return "", errors.New("encrypted text is empty")
}
key := make([]byte, 32)
copy(key, []byte(password))

crypt, err := base64.StdEncoding.DecodeString(crypt64)
if err != nil {
return "", err
return "", fmt.Errorf("failed to decode base64 string: %v", err)
}

key := make([]byte, 32)
copy(key, []byte(password))

block, err := aes.NewCipher(key)
if err != nil {
return "", err
return "", fmt.Errorf("failed to create AES cipher: %v", err)
}

iv := crypt[:aes.BlockSize]
Expand All @@ -68,7 +74,7 @@ func pad(data []byte, blockSize int) []byte {
return append(data, padtext...)
}

// unpad removes padding from data
// unpad removes padding from data, reversing the padding added by pad
func unpad(data []byte) []byte {
length := len(data)
unpadding := int(data[length-1])
Expand Down
47 changes: 39 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package main

import (
"atlet99/yaml-encrypter-decrypter/encryption"
"bufio"
"flag"
"fmt"
"log"
"os"
"strings"
"yaml-encrypter-decrypter/encryption"
)

const AES = "AES256:"
Expand Down Expand Up @@ -49,9 +49,17 @@ func main() {
// handleValue processes a single value for encryption or decryption based on the flag provided
func handleValue(flagKey, flagOperation, flagValue *string) {
if strings.HasPrefix(*flagValue, AES) {
fmt.Println(encryption.Decrypt(*flagKey, strings.TrimPrefix(*flagValue, AES)))
decryptedValue, err := encryption.Decrypt(*flagKey, strings.TrimPrefix(*flagValue, AES))
if err != nil {
log.Fatalf("Error decrypting value: %v", err)
}
fmt.Println(decryptedValue)
} else {
fmt.Println(AES + encryption.Encrypt(*flagKey, *flagValue))
encryptedValue, err := encryption.Encrypt(*flagKey, *flagValue)
if err != nil {
log.Fatalf("Error encrypting value: %v", err)
}
fmt.Println(AES + encryptedValue)
}
os.Exit(0)
}
Expand Down Expand Up @@ -90,14 +98,37 @@ func isEnvBlock(line string, envs []string) bool {
return false
}

// processYamlLine processes each line of the YAML file, either appending it to output or printing it (if dry-run)
// processYamlLine processes each line of the YAML file, either encrypting or decrypting it based on the operation
func processYamlLine(line string, envs []string, key, operation string, dryRun bool) {
if isEnvBlock(strings.TrimSpace(line), envs) {
// Processing the line within the env block
fmt.Println("Processing env block:", line)
// Add your encryption/decryption logic here
var processedLine string
if operation == "encrypt" {
encryptedValue, err := encryption.Encrypt(key, line)
if err != nil {
log.Fatalf("Error encrypting line: %v", err)
}
processedLine = AES + encryptedValue
} else if operation == "decrypt" {
decryptedValue, err := encryption.Decrypt(key, strings.TrimPrefix(line, AES))
if err != nil {
log.Fatalf("Error decrypting line: %v", err)
}
processedLine = decryptedValue
} else {
log.Fatalf("Invalid operation: %v", operation)
}

if dryRun {
fmt.Println(processedLine)
} else {
fmt.Println("Processed:", processedLine)
}
} else {
// Default handling for lines outside of env blocks
fmt.Println(line)
if dryRun {
fmt.Println(line)
} else {
fmt.Println("Unprocessed:", line)
}
}
}

0 comments on commit b99dd99

Please sign in to comment.