This repository was archived by the owner on Nov 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 77
Add postgresql_grant_role resource #189
Open
dvdliao
wants to merge
32
commits into
hashicorp:master
Choose a base branch
from
dvdliao:add-grant-role-resource
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 9 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
b030d17
feat: [resource_postgresql_grant_role] add grant role resource
Vince-Chenal ed22ce1
feat: [resource_postgresql_grant_role] add a test file
Vince-Chenal ae00dbf
feat: [resource_postgresql_grant_role] add doc
Vince-Chenal 63ee22a
Merge branch 'master' into add-grant-role-resource
4993411
minor
bbe8b0e
update website, fix tests
2d3e085
use previous query
330c3a0
add to changelog
5e5fb65
check version in test
a0f93d8
Update postgresql/resource_postgresql_grant_role.go
dvdliao 93dae89
Update postgresql/resource_postgresql_grant_role.go
dvdliao 8b1cd3e
Update postgresql/resource_postgresql_grant_role.go
dvdliao 36539f2
Update website/docs/r/postgresql_grant_role.html.markdown
dvdliao a73177d
protect against panic
406d3e2
add more notes to docs
949f81c
correctly save quoted role searchpath
lovromazgon 2392096
Merge branch 'master' into add-grant-role-resource
6c1fd97
Set up Github Workflows
thenonameguy acaaf19
travis: Remove make website command
cyrilgdn 3e0ab6d
Merge pull request #3 from thenonameguy/master
cyrilgdn 46715a5
Prepare CHANGELOG for v1.8.0
cyrilgdn 7da0aeb
Release v1.8.0
cyrilgdn d88b13b
Revert "Merge pull request #199 from estahn/lazy-connections"
cyrilgdn 4465ed5
Bugfix release: 1.8.1
cyrilgdn a3e8c91
README: Update documentation link.
cyrilgdn 3765f18
Merge pull request #1 from lovromazgon/searchpath-with-hyphen
cyrilgdn 6383ee2
merge
906dfb8
code review
f1ea383
code review
4c9f394
minor
021f001
minor
b9b2d65
cr
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,222 @@ | ||
package postgresql | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
"log" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/hashicorp/errwrap" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
|
||
"github.com/lib/pq" | ||
) | ||
|
||
const ( | ||
// This returns the role membership for role, grant_role | ||
getGrantRoleQuery = ` | ||
SELECT pg_get_userbyid(member) as role, pg_get_userbyid(roleid) as grant_role, admin_option | ||
FROM pg_auth_members | ||
WHERE pg_get_userbyid(member) = $1 | ||
AND pg_get_userbyid(roleid) = $2; | ||
` | ||
) | ||
|
||
func resourcePostgreSQLGrantRole() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourcePostgreSQLGrantRoleCreate, | ||
Read: resourcePostgreSQLGrantRoleRead, | ||
Delete: resourcePostgreSQLGrantRoleDelete, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"role": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
Description: "The name of the role to grant grant_role", | ||
}, | ||
"grant_role": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
Description: "The name of the role to is granted to role", | ||
dvdliao marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
}, | ||
"with_admin_option": { | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
ForceNew: true, | ||
Default: false, | ||
Description: "Permit the grant recipient to grant it to others", | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourcePostgreSQLGrantRoleRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*Client) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there not a worry of a panic if the type assertion fails? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've typically seen providers implemented without a worry here |
||
|
||
if !client.featureSupported(featurePrivileges) { | ||
return fmt.Errorf( | ||
"postgresql_grant_role resource is not supported for this Postgres version (%s)", | ||
client.version, | ||
) | ||
} | ||
|
||
client.catalogLock.RLock() | ||
defer client.catalogLock.RUnlock() | ||
|
||
txn, err := startTransaction(client, "") | ||
if err != nil { | ||
return err | ||
} | ||
defer deferredRollback(txn) | ||
|
||
return readGrantRole(txn, d) | ||
} | ||
|
||
func resourcePostgreSQLGrantRoleCreate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*Client) | ||
|
||
if !client.featureSupported(featurePrivileges) { | ||
return fmt.Errorf( | ||
"postgresql_grant_role resource is not supported for this Postgres version (%s)", | ||
client.version, | ||
) | ||
} | ||
|
||
client.catalogLock.Lock() | ||
defer client.catalogLock.Unlock() | ||
|
||
txn, err := startTransaction(client, "") | ||
if err != nil { | ||
return err | ||
} | ||
defer deferredRollback(txn) | ||
|
||
// Revoke the granted roles before granting them again. | ||
if err = revokeRole(txn, d); err != nil { | ||
return err | ||
} | ||
|
||
if err = grantRole(txn, d); err != nil { | ||
return err | ||
} | ||
|
||
if err = txn.Commit(); err != nil { | ||
return errwrap.Wrapf("could not commit transaction: {{err}}", err) | ||
} | ||
|
||
d.SetId(generateGrantRoleID(d)) | ||
|
||
txn, err = startTransaction(client, "") | ||
if err != nil { | ||
return err | ||
} | ||
defer deferredRollback(txn) | ||
|
||
return readGrantRole(txn, d) | ||
} | ||
|
||
func resourcePostgreSQLGrantRoleDelete(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*Client) | ||
|
||
if !client.featureSupported(featurePrivileges) { | ||
return fmt.Errorf( | ||
"postgresql_grant_role resource is not supported for this Postgres version (%s)", | ||
client.version, | ||
) | ||
} | ||
|
||
client.catalogLock.Lock() | ||
defer client.catalogLock.Unlock() | ||
|
||
txn, err := startTransaction(client, "") | ||
if err != nil { | ||
return err | ||
} | ||
defer deferredRollback(txn) | ||
|
||
if err = revokeRole(txn, d); err != nil { | ||
return err | ||
} | ||
|
||
if err = txn.Commit(); err != nil { | ||
return errwrap.Wrapf("could not commit transaction: {{err}}", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func readGrantRole(txn *sql.Tx, d *schema.ResourceData) error { | ||
var roleName, grantRoleName string | ||
var withAdminOption bool | ||
|
||
grantRoleId := d.Id() | ||
|
||
values := []interface{}{ | ||
&roleName, | ||
&grantRoleName, | ||
&withAdminOption, | ||
} | ||
|
||
err := txn.QueryRow(getGrantRoleQuery, d.Get("role"), d.Get("grant_role")).Scan(values...) | ||
switch { | ||
case err == sql.ErrNoRows: | ||
log.Printf("[WARN] PostgreSQL grant role (%q) not found", grantRoleId) | ||
d.SetId("") | ||
return nil | ||
case err != nil: | ||
return errwrap.Wrapf("Error reading grant role: {{err}}", err) | ||
} | ||
|
||
d.Set("role", roleName) | ||
d.Set("grant_role", grantRoleName) | ||
d.Set("with_admin_option", withAdminOption) | ||
|
||
d.SetId(generateGrantRoleID(d)) | ||
|
||
return nil | ||
} | ||
|
||
func createGrantRoleQuery(d *schema.ResourceData) string { | ||
var query string | ||
query = fmt.Sprintf( | ||
"GRANT %s TO %s", | ||
pq.QuoteIdentifier(d.Get("grant_role").(string)), | ||
pq.QuoteIdentifier(d.Get("role").(string)), | ||
dvdliao marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
) | ||
if d.Get("with_admin_option").(bool) == true { | ||
dvdliao marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
query = query + " WITH ADMIN OPTION" | ||
} | ||
|
||
return query | ||
} | ||
|
||
func createRevokeRoleQuery(d *schema.ResourceData) string { | ||
return fmt.Sprintf( | ||
"REVOKE %s FROM %s", | ||
pq.QuoteIdentifier(d.Get("grant_role").(string)), | ||
pq.QuoteIdentifier(d.Get("role").(string)), | ||
) | ||
} | ||
|
||
func grantRole(txn *sql.Tx, d *schema.ResourceData) error { | ||
query := createGrantRoleQuery(d) | ||
if _, err := txn.Exec(query); err != nil { | ||
return errwrap.Wrapf("could not execute grant query: {{err}}", err) | ||
} | ||
return nil | ||
} | ||
|
||
func revokeRole(txn *sql.Tx, d *schema.ResourceData) error { | ||
query := createRevokeRoleQuery(d) | ||
if _, err := txn.Exec(query); err != nil { | ||
return errwrap.Wrapf("could not execute revoke query: {{err}}", err) | ||
} | ||
return nil | ||
} | ||
|
||
func generateGrantRoleID(d *schema.ResourceData) string { | ||
return strings.Join([]string{d.Get("role").(string), d.Get("grant_role").(string), strconv.FormatBool(d.Get("with_admin_option").(bool))}, "_") | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.