Skip to content

loopdb: add Secret type for reading password from file#1131

Open
kaldun-tech wants to merge 1 commit intolightninglabs:masterfrom
kaldun-tech:add-secret-type-reading-database-password
Open

loopdb: add Secret type for reading password from file#1131
kaldun-tech wants to merge 1 commit intolightninglabs:masterfrom
kaldun-tech:add-secret-type-reading-database-password

Conversation

@kaldun-tech
Copy link
Copy Markdown

@kaldun-tech kaldun-tech commented Apr 28, 2026

Summary

  • Add Secret type that implements go-flags.Unmarshaler to support reading sensitive values from files using @/path/to/file syntax
  • Change PostgresConfig.Password field from string to Secret
  • This allows the postgres password to be stored in a file rather than passed directly on the command line, avoiding exposure in process listings and shell history

Usage

# Direct password (existing behavior)
loopd --postgres.password=mypassword

# Password from file (new behavior)
echo "mypassword" > /path/to/secret.txt
chmod 600 /path/to/secret.txt
loopd --postgres.password=@/path/to/secret.txt

Trailing whitespace (spaces, tabs, newlines) is automatically stripped from file contents.

Design notes

  • No fmt.Stringer redaction: PostgresConfig.DSN(hidePassword bool) already handles password hiding explicitly. Automatic redaction could mask debugging issues.
  • No MarshalFlag: The resolved secret should not be written back to config files. Users should store the @/path syntax, not the resolved value.

Test plan

  • Unit tests for Secret type covering direct values, file references, edge cases
  • go-flags integration test confirming parser compatibility
  • Manual test: loopd --postgres.password=@/path/to/secret.txt parses successfully

Fixes #1088

🤖 Generated with Claude Code

@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request improves the security posture of the application by allowing sensitive database credentials to be read from external files rather than being passed as plain-text command-line arguments. By implementing a custom unmarshaler, the configuration system now supports a file-reference syntax, ensuring that secrets remain protected from accidental exposure in system logs or process monitoring tools.

Highlights

  • New Secret Type: Introduced a new Secret type that implements go-flags.Unmarshaler to support reading sensitive configuration values from files.
  • Enhanced Security: Updated PostgresConfig.Password to use the Secret type, allowing passwords to be loaded via @/path/to/file to prevent exposure in process lists and shell history.
  • Robust Testing: Added comprehensive unit and integration tests to verify file reading, newline handling, and go-flags compatibility.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new Secret type that allows database passwords to be read from files when prefixed with '@', enhancing security by preventing secrets from appearing in process listings. The feedback suggests restoring a gosec linter suppression to avoid CI failures, implementing the fmt.Stringer interface to prevent accidental logging of sensitive data, and broadening the whitespace trimming logic to include spaces and tabs when reading from secret files.

Comment thread loopdb/postgres.go
Port int `long:"port" description:"Database server port."`
User string `long:"user" description:"Database user."`
Password string `long:"password" description:"Database user's password."` //nolint:gosec
Password Secret `long:"password" description:"Database user's password. Use @/path/to/file to read from a file."`
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The //nolint:gosec directive was removed in this change. Since the field name contains "Password", it is likely to be flagged by security linters (like gosec G101) as a potential hardcoded credential. It's recommended to keep the suppression to avoid CI failures.

Suggested change
Password Secret `long:"password" description:"Database user's password. Use @/path/to/file to read from a file."`
Password Secret `long:"password" description:"Database user's password. Use @/path/to/file to read from a file."` //nolint:gosec

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be ignored: The gosec G101 rule looks for hardcoded credentials in string literals. Since Secret is a type (not a hardcoded value), it shouldn't trigger.
Running the linter showed no warnings in this file via golangci-lint run -v

Comment thread loopdb/secret.go
// Secret is a string type that can unmarshal values from files when prefixed
// with '@'. This allows sensitive values like passwords to be stored in files
// rather than directly in configuration.
type Secret string
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

It is a best practice for sensitive types like Secret to implement the fmt.Stringer interface. This prevents the actual secret value from being accidentally exposed in logs if the configuration struct or the Secret value itself is printed using %v or %+v.

type Secret string

// String implements the fmt.Stringer interface to prevent the secret from
// being leaked in logs.
func (s Secret) String() string {
	if s == "" {
		return ""
	}
	return "xxxxxxxx"
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a deliberate design decision made because:

  • DSN() already handles password hiding explicitly via the hidePassword parameter
  • Automatic redaction could mask debugging issues
  • This keeps the Secret type simple and predictable

Comment thread loopdb/secret.go Outdated
Introduce a Secret type that implements go-flags Unmarshaler to support
reading sensitive values from files using @/path/to/file syntax. This
allows the postgres password to be stored in a file rather than passed
directly on the command line, avoiding exposure in process listings and
shell history.

Trailing whitespace (spaces, tabs, newlines) is automatically stripped
from file contents.

Fixes lightninglabs#1088

Signed-off-by: kaldun-tech <tsmereka@protonmail.com>
@kaldun-tech kaldun-tech force-pushed the add-secret-type-reading-database-password branch from d6beab0 to 7529656 Compare April 28, 2026 19:55
@kaldun-tech kaldun-tech marked this pull request as ready for review April 28, 2026 19:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Secret type for reading database password from file

1 participant