-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(authz): added a helper function to the policy engine to simplify (#…
…3419) auth method verification Signed-off-by: Roman Dmytrenko <[email protected]>
- Loading branch information
Showing
6 changed files
with
133 additions
and
24 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,53 @@ | ||
package ext | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/open-policy-agent/opa/ast" | ||
"github.com/open-policy-agent/opa/rego" | ||
"github.com/open-policy-agent/opa/types" | ||
authrpc "go.flipt.io/flipt/rpc/flipt/auth" | ||
) | ||
|
||
func init() { | ||
rego.RegisterBuiltin2(®o.Function{ | ||
Name: "flipt.is_auth_method", | ||
Decl: types.NewFunction(types.Args(types.A, types.S), types.B), | ||
}, isAuthMethod) | ||
} | ||
|
||
var labelMethodTable = map[string]*ast.Term{ | ||
"token": ast.IntNumberTerm(int(authrpc.Method_METHOD_TOKEN.Number())), | ||
"oidc": ast.IntNumberTerm(int(authrpc.Method_METHOD_OIDC.Number())), | ||
"kubernetes": ast.IntNumberTerm(int(authrpc.Method_METHOD_KUBERNETES.Number())), | ||
"k8s": ast.IntNumberTerm(int(authrpc.Method_METHOD_KUBERNETES.Number())), | ||
"github": ast.IntNumberTerm(int(authrpc.Method_METHOD_GITHUB.Number())), | ||
"jwt": ast.IntNumberTerm(int(authrpc.Method_METHOD_JWT.Number())), | ||
"cloud": ast.IntNumberTerm(int(authrpc.Method_METHOD_CLOUD.Number())), | ||
} | ||
|
||
var ( | ||
errNoAuthenticationFound = errors.New("no authentication found") | ||
authTerm = ast.StringTerm("authentication") | ||
methodTerm = ast.StringTerm("method") | ||
) | ||
|
||
func isAuthMethod(_ rego.BuiltinContext, input, key *ast.Term) (*ast.Term, error) { | ||
var authMethod string | ||
if err := ast.As(key.Value, &authMethod); err != nil { | ||
return nil, err | ||
} | ||
|
||
methodCode, ok := labelMethodTable[authMethod] | ||
if !ok { | ||
return nil, fmt.Errorf("unsupported auth method %s", authMethod) | ||
} | ||
|
||
auth := input.Get(authTerm) | ||
if auth == nil { | ||
return nil, errNoAuthenticationFound | ||
} | ||
|
||
return ast.BooleanTerm(methodCode.Equal(auth.Get(methodTerm))), 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