Skip to content

Commit

Permalink
feat(profiles): improve UTC consistency, update file handling, and re…
Browse files Browse the repository at this point in the history
…fine encryption

Set timestamps to UTC: Updated CreatedAt and UpdatedAt timestamps in profileConfig.go to use UTC for consistent time storage.
Default store type change: Changed the default store type in _index.md from keyring to file.
File naming improvements: Modified hashNamespaceAndKey in storeFile.go to use a colon (:) separator for namespace and key, improving readability and consistency.
File metadata handling: Enhanced handling of metadata file paths, using TrimSuffix to replace file extensions dynamically, ensuring metadata files use .nfo extension consistently across functions.
Improved encryption process: Refactored encryptData to separate the nonce from the destination buffer, and prepend the nonce to ciphertext for better compatibility.
  • Loading branch information
jgilpin committed Nov 11, 2024
1 parent 20c7ea2 commit 0a6f173
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 12 deletions.
2 changes: 1 addition & 1 deletion docs/man/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ command:
- keyring
- in-memory
- file
default: keyring
default: file
---
Binary file added otdfctl-source.zip
Binary file not shown.
6 changes: 3 additions & 3 deletions pkg/profiles/profileConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ func NewProfileStore(newStore NewStoreInterface, profileName string, endpoint st
Endpoint: u.String(),
TlsNoVerify: tlsNoVerify,
Attributes: make(map[string]interface{}), // Empty map for flexible attributes
CreatedAt: time.Now(), // Set creation time
UpdatedAt: time.Now(), // Set initial update time
CreatedAt: time.Now().UTC(), // Set creation time
UpdatedAt: time.Now().UTC(), // Set initial update time
Version: URNNamespaceTemplate, // Set profile version to URN-based namespace template
},
}
Expand All @@ -74,7 +74,7 @@ func (p *ProfileStore) Get() error {

// Save saves the current profile configuration to storage and updates UpdatedAt timestamp
func (p *ProfileStore) Save() error {
p.config.UpdatedAt = time.Now()
p.config.UpdatedAt = time.Now().UTC()
return p.store.Set(p.config)
}

Expand Down
24 changes: 16 additions & 8 deletions pkg/profiles/storeFile.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
"time"

"github.com/zalando/go-keyring"
Expand All @@ -32,8 +33,8 @@ type FileMetadata struct {
}

// Generates a safe, hashed filename from namespace and key
func hashNamespaceAndKey(namespace, key string) string {
hash := sha256.Sum256([]byte(namespace + "_" + key))
func hashNamespaceAndKey(namespace string, key string) string {
hash := sha256.Sum256([]byte(namespace + ":" + key))
return hex.EncodeToString(hash[:])
}

Expand Down Expand Up @@ -67,8 +68,9 @@ var NewFileStore NewStoreInterface = func(namespace string, key string) StoreInt
panic(fmt.Sprintf("unable to delete temp file in profiles directory %s: please ensure delete permissions are granted", baseDir))
}

// Generate the filename from namespace and key, hashed for uniqueness
fileName := hashNamespaceAndKey(namespace, key)
// Generate the filename hashed for uniqueness
// Note: other stores use the config.AppName, but want to rely on something more resilient like the namespace
fileName := hashNamespaceAndKey(URNNamespaceTemplate, key)
filePath := filepath.Join(baseDir, fileName+".enc")

return &FileStore{
Expand Down Expand Up @@ -137,7 +139,8 @@ func (f *FileStore) Delete() error {
return err
}

metadataFilePath := f.filePath + ".nfo"
// Remove the extension from filePath and add .nfo for the metadata file
metadataFilePath := strings.TrimSuffix(f.filePath, filepath.Ext(f.filePath)) + ".nfo"
return os.Remove(metadataFilePath)
}

Expand Down Expand Up @@ -184,7 +187,12 @@ func encryptData(key, data []byte) ([]byte, error) {
return nil, err
}

return aesGCM.Seal(nonce, nonce, data, nil), nil
// Encrypt the data with a separate destination buffer
ciphertext := aesGCM.Seal(nil, nonce, data, nil)

// Prepend the nonce to the ciphertext
result := append(nonce, ciphertext...)

Check failure on line 194 in pkg/profiles/storeFile.go

View workflow job for this annotation

GitHub Actions / lint

appendAssign: append result not assigned to the same slice (gocritic)
return result, nil
}

// decryptData decrypts data using AES-GCM
Expand Down Expand Up @@ -222,13 +230,13 @@ func (f *FileStore) SaveMetadata(profileName string) error {
return err
}

metadataFilePath := f.filePath + ".nfo"
metadataFilePath := strings.TrimSuffix(f.filePath, filepath.Ext(f.filePath)) + ".nfo"
return os.WriteFile(metadataFilePath, data, 0600)

Check failure on line 234 in pkg/profiles/storeFile.go

View workflow job for this annotation

GitHub Actions / lint

Magic number: 0600, in <argument> detected (mnd)
}

// LoadMetadata loads and parses metadata from a .nfo file
func (f *FileStore) LoadMetadata() (*FileMetadata, error) {
metadataFilePath := f.filePath + ".nfo"
metadataFilePath := strings.TrimSuffix(f.filePath, filepath.Ext(f.filePath)) + ".nfo"
data, err := os.ReadFile(metadataFilePath)
if err != nil {
return nil, err
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
6��R6�Jh�ԴfNG�V+�䉄��Ong��X/��||��K�#���x�<�.����ɵ��n������C�έ-�}�P�rVX
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"profile_name": "global",
"created_at": "2024-11-10T13:43:20-05:00",
"encryption_alg": "AES-256-GCM",
"version": "urn:opentdf:otdfctl:profile:v1"
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"profile_name": "profile-test",
"created_at": "2024-11-10T13:44:18-05:00",
"encryption_alg": "AES-256-GCM",
"version": "urn:opentdf:otdfctl:profile:v1"
}

0 comments on commit 0a6f173

Please sign in to comment.