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

Allow age decryption directly from environment variable #946

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 23 additions & 14 deletions age/keysource.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,27 +96,36 @@ func (key *MasterKey) SetEncryptedDataKey(enc []byte) {

// Decrypt decrypts the EncryptedKey field with the age identity and returns the result.
func (key *MasterKey) Decrypt() ([]byte, error) {
ageKeyFilePath, ok := os.LookupEnv("SOPS_AGE_KEY_FILE")
var ageKeyData io.Reader
ageKeyText, ok := os.LookupEnv("SOPS_AGE_KEY")
ageKeyFilePath := "SOPS_AGE_KEY"
if ok {
ageKeyData = strings.NewReader(ageKeyText)
} else {
var ok bool
ageKeyFilePath, ok = os.LookupEnv("SOPS_AGE_KEY_FILE")

if !ok {
userConfigDir, err := os.UserConfigDir()

if err != nil {
return nil, fmt.Errorf("user config directory could not be determined: %w", err)
}

ageKeyFilePath = filepath.Join(userConfigDir, "sops", "age", "keys.txt")
}

if !ok {
userConfigDir, err := os.UserConfigDir()
ageKeyFile, err := os.Open(ageKeyFilePath)

if err != nil {
return nil, fmt.Errorf("user config directory could not be determined: %w", err)
return nil, fmt.Errorf("failed to open file: %w", err)
}

ageKeyFilePath = filepath.Join(userConfigDir, "sops", "age", "keys.txt")
}

ageKeyFile, err := os.Open(ageKeyFilePath)

if err != nil {
return nil, fmt.Errorf("failed to open file: %w", err)
defer ageKeyFile.Close()
ageKeyData = ageKeyFile
}

defer ageKeyFile.Close()

identities, err := age.ParseIdentities(ageKeyFile)
identities, err := age.ParseIdentities(ageKeyData)

if err != nil {
return nil, err
Expand Down