2024-01-12 18:28:21 +01:00
|
|
|
// These functions have been copied from the age project
|
|
|
|
|
// https://github.com/FiloSottile/age/blob/v1.0.0/cmd/age/encrypted_keys.go
|
2025-01-02 16:09:09 +08:00
|
|
|
//
|
2024-01-12 18:28:21 +01:00
|
|
|
// Copyright 2021 The age Authors. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by a BSD-style
|
2025-01-02 14:56:44 +08:00
|
|
|
// license that can be found in age's LICENSE file at
|
|
|
|
|
// https://github.com/FiloSottile/age/blob/v1.0.0/LICENSE
|
|
|
|
|
//
|
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2024-01-12 18:28:21 +01:00
|
|
|
|
|
|
|
|
package age
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"os"
|
|
|
|
|
"runtime"
|
|
|
|
|
|
|
|
|
|
"golang.org/x/term"
|
|
|
|
|
)
|
|
|
|
|
|
2025-02-22 13:55:12 +01:00
|
|
|
const (
|
|
|
|
|
SopsAgePasswordEnv = "SOPS_AGE_PASSWORD"
|
|
|
|
|
)
|
|
|
|
|
|
2024-01-12 18:28:21 +01:00
|
|
|
// readPassphrase reads a passphrase from the terminal. It does not read from a
|
|
|
|
|
// non-terminal stdin, so it does not check stdinInUse.
|
|
|
|
|
func readPassphrase(prompt string) ([]byte, error) {
|
2025-02-22 13:55:12 +01:00
|
|
|
password := os.Getenv(SopsAgePasswordEnv)
|
|
|
|
|
if password != "" {
|
|
|
|
|
return []byte(password), nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-12 18:28:21 +01:00
|
|
|
var in, out *os.File
|
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
|
var err error
|
|
|
|
|
in, err = os.OpenFile("CONIN$", os.O_RDWR, 0)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
defer in.Close()
|
|
|
|
|
out, err = os.OpenFile("CONOUT$", os.O_WRONLY, 0)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
defer out.Close()
|
|
|
|
|
} else if _, err := os.Stat("/dev/tty"); err == nil {
|
|
|
|
|
tty, err := os.OpenFile("/dev/tty", os.O_RDWR, 0)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
defer tty.Close()
|
|
|
|
|
in, out = tty, tty
|
|
|
|
|
} else {
|
|
|
|
|
if !term.IsTerminal(int(os.Stdin.Fd())) {
|
|
|
|
|
return nil, fmt.Errorf("standard input is not a terminal, and /dev/tty is not available: %v", err)
|
|
|
|
|
}
|
|
|
|
|
in, out = os.Stdin, os.Stderr
|
|
|
|
|
}
|
|
|
|
|
fmt.Fprintf(out, "%s ", prompt)
|
|
|
|
|
// Use CRLF to work around an apparent bug in WSL2's handling of CONOUT$.
|
|
|
|
|
// Only when running a Windows binary from WSL2, the cursor would not go
|
|
|
|
|
// back to the start of the line with a simple LF. Honestly, it's impressive
|
|
|
|
|
// CONIN$ and CONOUT$ even work at all inside WSL2.
|
|
|
|
|
defer fmt.Fprintf(out, "\r\n")
|
|
|
|
|
return term.ReadPassword(int(in.Fd()))
|
|
|
|
|
}
|