-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add rbac rules for non-resource urls (#279)
* refactor: move rbac into its own package Signed-off-by: Dustin Scott <[email protected]> * fix: Fixes #274, added capability to generate rules for nonresource urls Signed-off-by: Dustin Scott <[email protected]> * refactor: move utils file into proper utils package Signed-off-by: Dustin Scott <[email protected]> * test: added test cases for rbac after refactor Signed-off-by: Dustin Scott <[email protected]> * chore: fix linting Signed-off-by: Dustin Scott <[email protected]> * test: fix test case for roles with non-resource urls Signed-off-by: Dustin Scott <[email protected]>
- Loading branch information
Showing
22 changed files
with
2,161 additions
and
529 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Copyright 2021 VMware, Inc. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package utils | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
var ( | ||
ErrConvertArrayInterface = errors.New("unable to convert to []interface{}") | ||
ErrConvertArrayString = errors.New("unable to convert to []string") | ||
ErrConvertMapStringInterface = errors.New("unable to convert to map[string]interface{}") | ||
ErrConvertString = errors.New("unable to convert to string") | ||
) | ||
|
||
// ToArrayInterface attempts a conversion from an interface to an underlying array of | ||
// interface type. Returns an error if the conversion is not possible. | ||
func ToArrayInterface(in interface{}) ([]interface{}, error) { | ||
out, ok := in.([]interface{}) | ||
if !ok { | ||
return nil, ErrConvertArrayInterface | ||
} | ||
|
||
return out, nil | ||
} | ||
|
||
// ToArrayString attempts a conversion from an interface to an underlying array of | ||
// string type. Returns an error if the conversion is not possible. | ||
func ToArrayString(in interface{}) ([]string, error) { | ||
// attempt direct conversion | ||
out, ok := in.([]string) | ||
if !ok { | ||
// attempt conversion for each item | ||
outInterfaces, err := ToArrayInterface(in) | ||
if err != nil { | ||
return nil, fmt.Errorf("%w; %s", err, ErrConvertArrayString) | ||
} | ||
|
||
outStrings := make([]string, len(outInterfaces)) | ||
|
||
for i := range outInterfaces { | ||
outString, err := ToString(outInterfaces[i]) | ||
if err != nil { | ||
return nil, fmt.Errorf("%w; %s", err, ErrConvertArrayString) | ||
} | ||
|
||
outStrings[i] = outString | ||
} | ||
|
||
return outStrings, nil | ||
} | ||
|
||
return out, nil | ||
} | ||
|
||
// ToMapStringInterface attempts a conversion from an interface to an underlying map | ||
// string interface type. Returns an error if the conversion is not possible. | ||
func ToMapStringInterface(in interface{}) (map[string]interface{}, error) { | ||
out, ok := in.(map[string]interface{}) | ||
if !ok { | ||
return nil, ErrConvertMapStringInterface | ||
} | ||
|
||
return out, nil | ||
} | ||
|
||
// ToArrayInterface attempts a conversion from an interface to an underlying | ||
// string type. Returns an error if the conversion is not possible. | ||
func ToString(in interface{}) (string, error) { | ||
out, ok := in.(string) | ||
if !ok { | ||
return "", ErrConvertString | ||
} | ||
|
||
return out, 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
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
Oops, something went wrong.