-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #573 from openziti/permission_model
Permission Model: Phase 1 (#432)
- Loading branch information
Showing
27 changed files
with
739 additions
and
22 deletions.
There are no files selected for viewing
This file contains 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 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 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,75 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
httptransport "github.com/go-openapi/runtime/client" | ||
"github.com/openziti/zrok/environment" | ||
"github.com/openziti/zrok/rest_client_zrok/share" | ||
"github.com/openziti/zrok/rest_model_zrok" | ||
"github.com/openziti/zrok/tui" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func init() { | ||
modifyCmd.AddCommand(newModifyShareCommand().cmd) | ||
} | ||
|
||
type modifyShareCommand struct { | ||
addAccessGrants []string | ||
removeAccessGrants []string | ||
cmd *cobra.Command | ||
} | ||
|
||
func newModifyShareCommand() *modifyShareCommand { | ||
cmd := &cobra.Command{ | ||
Use: "share <shareToken>", | ||
Args: cobra.ExactArgs(1), | ||
Short: "Modify a share", | ||
} | ||
command := &modifyShareCommand{cmd: cmd} | ||
cmd.Flags().StringArrayVar(&command.addAccessGrants, "add-access-grant", []string{}, "Add an access grant (email address)") | ||
cmd.Flags().StringArrayVar(&command.removeAccessGrants, "remove-access-grant", []string{}, "Remove an access grant (email address)") | ||
cmd.Run = command.run | ||
return command | ||
} | ||
|
||
func (cmd *modifyShareCommand) run(_ *cobra.Command, args []string) { | ||
shrToken := args[0] | ||
|
||
root, err := environment.LoadRoot() | ||
if err != nil { | ||
if !panicInstead { | ||
tui.Error("error loading environment", err) | ||
} | ||
panic(err) | ||
} | ||
|
||
if !root.IsEnabled() { | ||
tui.Error("unable to load environment; did you 'zrok enable'?", nil) | ||
} | ||
|
||
zrok, err := root.Client() | ||
if err != nil { | ||
if !panicInstead { | ||
tui.Error("unable to create zrok client", err) | ||
} | ||
panic(err) | ||
} | ||
auth := httptransport.APIKeyAuth("X-TOKEN", "header", root.Environment().Token) | ||
|
||
if len(cmd.addAccessGrants) > 0 || len(cmd.removeAccessGrants) > 0 { | ||
req := share.NewUpdateShareParams() | ||
req.Body = &rest_model_zrok.UpdateShareRequest{ | ||
ShrToken: shrToken, | ||
AddAccessGrants: cmd.addAccessGrants, | ||
RemoveAccessGrants: cmd.removeAccessGrants, | ||
} | ||
if _, err := zrok.Share.UpdateShare(req, auth); err != nil { | ||
if !panicInstead { | ||
tui.Error("unable to update share", err) | ||
} | ||
panic(err) | ||
} | ||
fmt.Println("updated") | ||
} | ||
} |
This file contains 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 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 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 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 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 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,57 @@ | ||
package store | ||
|
||
import ( | ||
"github.com/jmoiron/sqlx" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
type AccessGrant struct { | ||
Model | ||
ShareId int | ||
AccountId int | ||
} | ||
|
||
func (str *Store) CreateAccessGrant(shareId, accountId int, tx *sqlx.Tx) (int, error) { | ||
stmt, err := tx.Prepare("insert into access_grants (share_id, account_id) values ($1, $2) returning id") | ||
if err != nil { | ||
return 0, errors.Wrap(err, "error preparing access_grant insert statement") | ||
} | ||
var id int | ||
if err := stmt.QueryRow(shareId, accountId).Scan(&id); err != nil { | ||
return 0, errors.Wrap(err, "error executing access_grant insert statement") | ||
} | ||
return id, nil | ||
} | ||
|
||
func (str *Store) CheckAccessGrantForShareAndAccount(shrId, acctId int, tx *sqlx.Tx) (int, error) { | ||
count := 0 | ||
err := tx.QueryRowx("select count(0) from access_grants where share_id = $1 and account_id = $2 and not deleted", shrId, acctId).Scan(&count) | ||
if err != nil { | ||
return 0, errors.Wrap(err, "error selecting access_grants by share_id and account_id") | ||
} | ||
return count, nil | ||
} | ||
|
||
func (str *Store) DeleteAccessGrantsForShare(shrId int, tx *sqlx.Tx) error { | ||
stmt, err := tx.Prepare("update access_grants set updated_at = current_timestamp, deleted = true where share_id = $1") | ||
if err != nil { | ||
return errors.Wrap(err, "error preparing access_grants delete for shares statement") | ||
} | ||
_, err = stmt.Exec(shrId) | ||
if err != nil { | ||
return errors.Wrap(err, "error executing access_grants delete for shares statement") | ||
} | ||
return nil | ||
} | ||
|
||
func (str *Store) DeleteAccessGrantsForShareAndAccount(shrId, acctId int, tx *sqlx.Tx) error { | ||
stmt, err := tx.Prepare("update access_grants set updated_at = current_timestamp, deleted = true where share_id = $1 and account_id = $2") | ||
if err != nil { | ||
return errors.Wrap(err, "error preparing access_grants delete for share and account statement") | ||
} | ||
_, err = stmt.Exec(shrId, acctId) | ||
if err != nil { | ||
return errors.Wrap(err, "error executing access_grants delete for share and account statement") | ||
} | ||
return nil | ||
} |
This file contains 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
Oops, something went wrong.