Skip to content

Conversation

@pcallewaert
Copy link
Contributor

@pcallewaert pcallewaert commented Dec 17, 2025

I noticed with the updated GCP logic that actually the logic for all cloud providers are very simular, and can be even applied on self-hosted postgres instances.

If this works properly, this means we can recommend to create a separate role specific for the operator with a select number of permissions.

Will close #110

WIP

This comment was marked as outdated.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated 9 comments.

Comments suppressed due to low confidence (1)

pkg/postgres/database.go:49

  • The CreateDB function is missing logic to grant the role to the operator user before setting it as the database owner. This will fail on cloud providers (AWS RDS, Azure, GCP) where the operator user needs to be a member of the role to transfer ownership. The old cloud-specific implementations (aws.go, azure.go, gcp.go) all included this step before calling ALTER DATABASE OWNER. Without this, the ALTER DATABASE OWNER statement at line 40 will fail with a permission denied error.
func (c *pg) CreateDB(dbname, role string) error {
	// Create database
	err := c.execute(fmt.Sprintf(CREATE_DB, dbname))
	if err != nil {
		// eat DUPLICATE DATABASE ERROR
		if !isPgError(err, "42P04") {
			return err
		}
	}

	err = c.execute(fmt.Sprintf(ALTER_DB_OWNER, dbname, role))
	if err != nil {
		return err
	}

	err = c.execute(fmt.Sprintf(GRANT_CREATE_SCHEMA, dbname, role))
	if err != nil {
		return err
	}
	return nil

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +36 to +39
err = c.GrantRole(role, c.user)
if err != nil && !isPgError(err, "0LP01") {
return err
}
Copy link

Copilot AI Dec 17, 2025

Choose a reason for hiding this comment

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

The newly added logic to automatically grant the created role to the operator user may cause issues in some scenarios. This change modifies the behavior of CreateGroupRole even when cloud providers don't require it. If the operator role doesn't have CREATEROLE permission, this will fail. Additionally, silently ignoring the "already a member" error (0LP01) without logging could make debugging difficult when something goes wrong.

Copilot uses AI. Check for mistakes.
Comment on lines 62 to 65
err = c.GrantRole(role, c.user)
if err != nil {
return "", err
}
Copy link

Copilot AI Dec 17, 2025

Choose a reason for hiding this comment

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

The CreateUserRole function unconditionally grants the user role to the operator, but doesn't handle the "already a member" error (0LP01) like other similar code does. This will fail if the role membership already exists for some reason. The error handling should be consistent with CreateGroupRole at line 37 and DropRole at line 88.

Copilot uses AI. Check for mistakes.
@pcallewaert pcallewaert force-pushed the chore/remove-duplicate-code branch 6 times, most recently from eca26d5 to 57cc6d3 Compare December 18, 2025 09:34
Replice terminate_backend with WITH (FORCE)
@pcallewaert pcallewaert force-pushed the chore/remove-duplicate-code branch from 57cc6d3 to 326426b Compare December 18, 2025 10:36
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 operator user to created roles by default

2 participants