Myasnikov Daniil e41d10df6f fix(postgres): keep users.app's password across pod restarts (#4984)
## What this PR does

A Postgres app that declares a user named `app` loses that user's
password every time its pod restarts. Clients keep the password
advertised in `<release>-credentials` and start failing with `password
authentication failed for user "app"`, with nothing changed on their
side.

`app` is the owner role CNPG bootstraps, with a generated password in
`<release>-app`, and a declared `users.app` adopts that role (the
init-script says so). The init-job sets the role's password from
`<release>-credentials`, but CNPG's instance manager re-applies the
owner's password from the bootstrap secret whenever it meets a secret
version it has not applied yet (`refreshCredentialsFromSecret` →
`reconcileUser` in
`internal/management/controller/instance_controller.go`). Its cache is
in memory, so every pod start re-applies `<release>-app` and silently
undoes the init-job. Only releases whose two secrets happen to differ
break, which is every release where `users.app` was declared at
creation.

This PR renders a `<release>-app-owner` basic-auth Secret from the same
password `<release>-credentials` holds, and sets
`spec.bootstrap.<method>.secret.name` to it, so both writers agree on
one value. CNPG resolves the owner secret through that field before
falling back to `<release>-app`, never rewrites the data of a secret it
does not own, and its webhook accepts a bootstrap change on an existing
Cluster. The secret goes on the method the live Cluster was bootstrapped
with, read through `lookup`, because CNPG rejects two methods and a
restored Cluster keeps `bootstrap.recovery` in its live spec. A release
with no `users.app` renders exactly what it rendered before, and a
restore in progress (`bootstrap.enabled`) is left alone, since the chart
flags its credentials as pending until the init-job runs.

We hit this in production: the cluster-wide pod restart of a platform
upgrade broke two of the four apps that declare `users.app`; the two
others survived only because their two secrets held the same value.

`tests/app_owner_secret_test.yaml` covers the shared password (preserved
and freshly generated), the initdb and recovery methods, a release
without `users.app`, and an in-progress restore. Without the template
change, 5 of its 8 tests fail. The full chart suite passes (217 tests).

### Screenshots

Not a UI change.

### Downstream repositories

Walked against the diff: two templates and one test file under
`packages/apps/postgres`. No package is added or renamed, `values.yaml`,
`values.schema.json` and `README.md` are untouched, and no output Secret
or Service is renamed (a new Secret is added; the Terraform provider
reads only `-credentials`, `-rw` and `-ro`).

- [x] No downstream repository is affected by this change
- [ ] [cozystack/website](https://github.com/cozystack/website) -
follow-up:
- [ ]
[cozystack/terraform-provider-cozystack](https://github.com/cozystack/terraform-provider-cozystack)
- follow-up:
- [ ]
[cozystack/ansible-cozystack](https://github.com/cozystack/ansible-cozystack)
- follow-up:
- [ ] [cozystack/ccp](https://github.com/cozystack/ccp) - follow-up:
- [ ] [cozystack/talm](https://github.com/cozystack/talm) - follow-up:
- [ ] [cozystack/cozyhr](https://github.com/cozystack/cozyhr) -
follow-up:
- [ ] [cozystack/cozy-proxy](https://github.com/cozystack/cozy-proxy) -
follow-up:
- [ ]
[cozystack/cozystack-telemetry-server](https://github.com/cozystack/cozystack-telemetry-server)
- follow-up:
- [ ]
[cozystack/external-apps-example](https://github.com/cozystack/external-apps-example)
- follow-up:
- [ ] [cozystack/examples](https://github.com/cozystack/examples) -
follow-up:
- [ ] [cozystack/community](https://github.com/cozystack/community) -
follow-up:

### Release note

```release-note
fix(postgres): a user named `app` no longer loses its password when the Postgres pod restarts. CNPG re-applied its own generated password to the `app` owner role on every pod start; the chart now hands CNPG the password stored in `<release>-credentials`.
```


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
<!-- coderabbit-ai-generated:v1 -->

## Summary by CodeRabbit

- **New Features**
  - Added owner credentials when the `app` user is configured.
- PostgreSQL clusters now use those credentials during initialization or
recovery, while preserving compatible recovery settings for restored and
actively recovering clusters.

- **Bug Fixes**
- Ensured generated or existing `app` passwords are reused for owner
credentials.

- **Tests**
- Added coverage for credential reuse, password generation, omitted-user
behavior, and cluster recovery scenarios.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

Backported-from: 39f3f73fbc
2026-11-25 10:49:43 +00:00
2025-10-08 09:43:34 +05:00
2024-02-08 12:04:32 +01:00

Cozystack Cozystack

GitHub Release Apache-2.0 License Commit activity OpenSSF Best Practices OpenSSF Scorecard CLOMonitor Support

Cozystack

Cozystack is a free and open-source platform and framework for building clouds.

Cozystack is a CNCF Sandbox project that was originally built and sponsored by Ænix.

With Cozystack, you can transform a bunch of servers into an intelligent system with a simple REST API for spawning Kubernetes clusters, Database-as-a-Service, virtual machines, load balancers, HTTP caching services, and other services with ease.

Use Cozystack to build your own cloud or provide a cost-effective development environment.

Cozystack user interface

Use-Cases

Documentation

The documentation is located on the cozystack.io website.

Read the Getting Started section for a quick start.

If you encounter any difficulties, start with the troubleshooting guide and work your way through the process that we've outlined.

Versioning

Versioning adheres to the Semantic Versioning principles.
A full list of the available releases is available in the GitHub repository's Release section.

Contributions

Contributions are highly appreciated and very welcomed!

In case of bugs, please check if the issue has already been opened by checking the GitHub Issues section. If it isn't, you can open a new one. A detailed report will help us replicate it, assess it, and work on a fix.

You are welcome to take on the fix yourself. Commits are used to generate the changelog, and their author will be referenced in it.

If you have a concrete feature request, open an issue. For a cross-cutting or architectural change, open a design proposal in the community repo. For questions or early ideas, use GitHub Discussions. See CONTRIBUTING.md for the full routing.

Community

You are welcome to join us in #cozystack, on either CNCF Slack (get an invite) or Kubernetes Slack (get an invite), in our Telegram group, and at our community meetings. Add them to your Google Calendar or iCal for convenience.

Project documents

Document What it covers
Governance How decisions are made and who makes them
Contributor Ladder How to grow from contributor to maintainer
Maintainers Who maintains the project
Code of Conduct What we expect of each other
Security Policy Reporting a vulnerability, and which releases get fixes
Adopters Who runs Cozystack in production
Roadmap Where the project is going

License

Cozystack is licensed under Apache 2.0.
The code is provided as-is with no warranties.

Commercial Support

A list of companies providing commercial support for this project can be found on official site.

Languages
Go 53.1%
Shell 33%
TypeScript 6.8%
Go Template 4.6%
Makefile 1.3%
Other 1.1%