Files
threshold/internal/daemon/controller/auth/auth.go
T
1f17313a39 Normalise grants and roles tables to improve grants query performance (#5846)
* Create function to define valid set of scopes for each resource (#5558)

* Create function to define valid set of scopes for each resource

* chore(iam): Update validScopeTypes() to use scope.AllowedIn()

* chore(scope): Initialize with iota

* feat(scope): Return an error instead scope.Unknown

* feat(iam): Replace interface method `validScopeTypes` with `getResourceType`

This allows us to call scope.AllowedIn() in one place vs in each implementation of `validScopeTypes`

* chore(resource): Refactor other package functions into methods on resource.Type

* fix(scope): Add defensive checks around invalid resource types

* docs(resource): Add AllowedIn() to the areas to update when adding a new resource type

* docs(resource): Improve error message when an invalid type is provided

* test(grants): GrantsForUsers tests for Group resource (#5443)

* test(grants): WIP: First stab at group associations

* test(grants): Add GrantsForUser test for groups

* chore(grants): Consolidate repetitive setup logic into functions

* test(grants): Add GrantsForUser test for managed groups

* test(grants): Add another user with different grants

Ensure that non-applicable grants should not be returned because they are not applicable to the user

* chore(grants): cleanup

* chore(grants): Move common setup steps into a helper function

* feat: Define new grants tables (#5486)

Create new tables for grants:

1. `iam_role_global`:
  Roles that are placed in the global scope will be persisted in the `iam_role_global` table. A global role has a `grant_scope` which must be one of:
  * descendants
  * children
  * individual

  This enforces that a global role's grants either apply to:
  *  All orgs and projects.
  *  All orgs.
  *  An individual set of orgs and/or projects.

  When the `grant_scope` is set to `individual`, entries for the specific set of orgs and/or projects can be added to the 
  `iam_role_global_individual_grant_scope` table.

  Separately, a global role can be set to also apply its grant to the global scope by setting `grant_this_role_scope` to true.

2. `iam_role_org`:
  Roles that are placed in an org scope will be persisted in the `iam_role_org` table. An org role has a `grant_scope` which must be one of:
  * children
  * individual

  This enforces that an org role's grants either apply to:
  * All projects in the org.
  * An individual set of projects in the org.

  When the `grant_scope` is set to `individual`, entries for the specific set of projects can be added to the 
  `iam_role_org_individual_grant_scope` table.
  **NOTE**: The projects must belong to the org's scope

  Separately, an org role can be set to also apply its grant to the org by setting `grant_this_role_scope` to true.

3. `resource_enm`: 
  Contains all boundary resources. This is used by `iam_grant` to set the resource from a canonical_grant.

4. `iam_grant`
Stores the canonical grant string and the resource for filtering on specific grants.

Co-authored-by: Sorawis Nilparuk <sorawis.nilparuk@hashicorp.com>
Co-authored-by: David Kanney <david.kanney@hashicorp.com>

* add name scope id unique constraints to all iam_role tables (#5623)

* Bosorawis domain iam role subtypes (#5626)

* add subtype storage definitions

* make gen

* add all subtype definitions

* add const for grant scope individual

* remove unnecessary baseRole subtype

* add new proto files to make target protobuild

* make gen to get protoc-go-inject-tag

* add clone, setTableName, and GetScope tests

* add ResourceType and Actions test

* add create and delete tests for globalROle

* finish create and delete tests

* add trigger for deleting base role

* add trigger to sync update_time back to base iam_role table

* add update tests

* fix missing err checks

* fix iam_role delete subtype trigger function name and use new.update_time instead of now()

* add struct documentation to role subtypes

* add version update check

* add todo comment:

* bosorawis sql split global grants scope table (#5638)

* split iam_role_global_individual_grant_scope to have separate tables for org and project

* small comment change

* small comment change

* WIP: add tests

* remove grant_scope as immutable column

* add trigger to delete individual grant scope when grant_scope changes

* add a test that covers changing grant_scope

* rename function and trigger in iam_role_global

* improve assertion in sqltest for iam_role_global

* update iam_role_org to delete redundant grants scope

* minor comment fix

* no longer handle individual grant scope deletion with triggers and rename some functions

* add trigger test for grant_scope

* rename delete_base_iam_role to delete_iam_role_subtype

* SQL formatting use now() instead of interval

* bosorawis domain iam implement getRoleScopeType (#5629)

* implement getRoleScopeId

* move query to query.go

* improve notfound err message

* improve other err messages

* use named parameter and move getRoleScopeId implementation

* moved getRoleScopeId test

* rename getRoleScopeId to getRoleScopeType

* fix public_id ambiguous error

* undo unintended change to getUserWithAccount

* fix the correct query

* rename test

* change error code to RecordNotFound

* Update internal/iam/repository_role.go

Co-authored-by: David Kanney <david.kanney@hashicorp.com>

* switch to slice instead of counter

---------

Co-authored-by: David Kanney <david.kanney@hashicorp.com>

* domain: iam: upgrade repository code to use new tables (#5643)

* implement getRoleScopeId

* move query to query.go

* improve notfound err message

* improve other err messages

* use named parameter and move getRoleScopeId implementation

* moved getRoleScopeId test

* rename getRoleScopeId to getRoleScopeType

* fix public_id ambiguous error

* undo unintended change to getUserWithAccount

* fix the correct query

* split iam_role_global_individual_grant_scope to have separate tables for org and project

* small comment change

* small comment change

* WIP: add tests

* remove grant_scope as immutable column

* add trigger to delete individual grant scope when grant_scope changes

* add a test that covers changing grant_scope

* rename function and trigger in iam_role_global

* improve assertion in sqltest for iam_role_global

* update iam_role_org to delete redundant grants scope

* minor comment fix

* no longer handle individual grant scope deletion with triggers and rename some functions

* rename test

* add all subtype definitions

* remove unnecessary baseRole subtype

* add clone, setTableName, and GetScope tests

* add ResourceType and Actions test

* add create and delete tests for globalROle

* finish create and delete tests

* add trigger for deleting base role

* add trigger to sync update_time back to base iam_role table

* add update tests

* fix missing err checks

* fix iam_role delete subtype trigger function name and use new.update_time instead of now()

* add struct documentation to role subtypes

* add version update check

* implement getRoleScopeId

* implement getRoleScopeId

* save

* remove struct embedding from iam.Role

* fix tests to use new iam.Role definition

* repository_role_test.go move to new iam.Role model

* repository_principal_role_test.go use new iam.Role model

* repository_role_grant_test.go use new iam.Role model in test

* add oplog info to sql schema

* internal/iam/testing.go use new role schema in TestRole

* add toRole helper function to all role subtype

* remove tests that are no longer relevant

* internal/iam/repository_scope.go use new iam model

* internal/iam/repository_role_grant.go use new iam model

* internal/iam/repository_principal_role.go use new iam model

* internal/iam/repository_role_test.go add test case for global scoped role

* internal/iam/repository_grant_scope.go use new iam model

* fix query

* make create and lookup role work and add tests

* add role id to getRoleScopeId error message

* make DeleteRole work with new model and add tests

* fix update

* ensure oplog.ReplayableMessage is implemented on all role subtypes

* internal/iam/repository_role_grant.go fix slugging version properly

* internal/iam/repository_role.go minor correction to error message saying org instead of scope

* internal/iam/repository_role_test.go add more update tests

* add immutable_fields tests

* fix rebase

* change error code to RecordNotFound

* refactor to use getScopeType

* fix delete test

* add getRoleScope utility function

* repository_principal_role.go: refactor to remove multiple switch statements

* repository_role_grant.go: refactor to reduce LOC

* repository_role.go small refactor to use alloc func

* repository_grant_scope.go refactor

* review comments

* implement getRoleScopeId

* move query to query.go

* improve notfound err message

* improve other err messages

* use named parameter and move getRoleScopeId implementation

* moved getRoleScopeId test

* rename getRoleScopeId to getRoleScopeType

* fix public_id ambiguous error

* undo unintended change to getUserWithAccount

* fix the correct query

* rename test

* change error code to RecordNotFound

* Update internal/iam/repository_role.go

Co-authored-by: David Kanney <david.kanney@hashicorp.com>

* switch to slice instead of counter

* fix merge mistakes

* handling special scopes in test function

* fix TestRoleWithGrants

* fix minor typo

* make gen

* fix comment typos

* Bosorawis domain iam role use new model list role (#5676)

* add and use new list roles query

* run make gen

* tweaked returned error

* replace tabs with spaces in query string

* missed one tab

* remove leading spaces

---------

Co-authored-by: David Kanney <david.kanney@hashicorp.com>

* Domain: iam: Repository: update list-grant-scope and test setup to use new model (#5679)

* implement getRoleScopeId

* move query to query.go

* improve notfound err message

* improve other err messages

* use named parameter and move getRoleScopeId implementation

* moved getRoleScopeId test

* rename getRoleScopeId to getRoleScopeType

* fix public_id ambiguous error

* undo unintended change to getUserWithAccount

* fix the correct query

* split iam_role_global_individual_grant_scope to have separate tables for org and project

* small comment change

* small comment change

* WIP: add tests

* remove grant_scope as immutable column

* add trigger to delete individual grant scope when grant_scope changes

* add a test that covers changing grant_scope

* rename function and trigger in iam_role_global

* improve assertion in sqltest for iam_role_global

* update iam_role_org to delete redundant grants scope

* minor comment fix

* no longer handle individual grant scope deletion with triggers and rename some functions

* rename test

* add all subtype definitions

* remove unnecessary baseRole subtype

* add clone, setTableName, and GetScope tests

* add ResourceType and Actions test

* add create and delete tests for globalROle

* finish create and delete tests

* add trigger for deleting base role

* add trigger to sync update_time back to base iam_role table

* add update tests

* fix missing err checks

* fix iam_role delete subtype trigger function name and use new.update_time instead of now()

* add struct documentation to role subtypes

* add version update check

* implement getRoleScopeId

* implement getRoleScopeId

* save

* remove struct embedding from iam.Role

* fix tests to use new iam.Role definition

* repository_role_test.go move to new iam.Role model

* repository_principal_role_test.go use new iam.Role model

* repository_role_grant_test.go use new iam.Role model in test

* add oplog info to sql schema

* internal/iam/testing.go use new role schema in TestRole

* add toRole helper function to all role subtype

* remove tests that are no longer relevant

* internal/iam/repository_scope.go use new iam model

* internal/iam/repository_role_grant.go use new iam model

* internal/iam/repository_principal_role.go use new iam model

* internal/iam/repository_role_test.go add test case for global scoped role

* internal/iam/repository_grant_scope.go use new iam model

* fix query

* make create and lookup role work and add tests

* add role id to getRoleScopeId error message

* make DeleteRole work with new model and add tests

* fix update

* ensure oplog.ReplayableMessage is implemented on all role subtypes

* internal/iam/repository_role_grant.go fix slugging version properly

* internal/iam/repository_role.go minor correction to error message saying org instead of scope

* internal/iam/repository_role_test.go add more update tests

* add immutable_fields tests

* fix rebase

* change error code to RecordNotFound

* refactor to use getScopeType

* fix delete test

* add getRoleScope utility function

* repository_principal_role.go: refactor to remove multiple switch statements

* repository_role_grant.go: refactor to reduce LOC

* repository_role.go small refactor to use alloc func

* repository_grant_scope.go refactor

* review comments

* implement getRoleScopeId

* move query to query.go

* improve notfound err message

* improve other err messages

* use named parameter and move getRoleScopeId implementation

* moved getRoleScopeId test

* rename getRoleScopeId to getRoleScopeType

* fix public_id ambiguous error

* undo unintended change to getUserWithAccount

* fix the correct query

* rename test

* change error code to RecordNotFound

* Update internal/iam/repository_role.go

Co-authored-by: David Kanney <david.kanney@hashicorp.com>

* switch to slice instead of counter

* fix merge mistakes

* handling special scopes in test function

* fix TestRoleWithGrants

* fix minor typo

* make gen

* fix comment typos

* Bosorawis domain iam role use new model list role (#5676)

* add and use new list roles query

* run make gen

* tweaked returned error

* replace tabs with spaces in query string

* missed one tab

* remove leading spaces

* move ListRoleGrantScopes to repository_grant_scope.go

* rename repository_grant_scope to repository_role_grant_scope

* add proto definition for global role individual grant scope tables

* fix test from removing embeded struct from RoleGrantScope

* add grant_scope to proto definition

* implement GlobalRoleIndividualOrgGrantScope and GlobalRoleIndividualProjectGrantScope

* update comment

* run make gen to update comment

* implement OrgRoleIndividualGrantScope and add tests

* implement part of ListRoleGrantScopes

* Add more test

* add more test cases and remove add-grants test

* unexport listRoleGrantScopes

* use reader from function parameter instead of struct method

* rename test to match actual function

* run make gen

* unexport individual grants structs

* unexport individual grants structs - missed one file

* change TestRole and TestRoleGrantScope function to support new model

* add validation for special scopes

* add role_org_individual_grant_scope.pb.go to protobuild make target

* remove dead code from listRoleGrantScopes

* fix testRoleGrantScopeSpecial not handling org role special scope properly

* add back query removed by rebase

---------

Co-authored-by: David Kanney <david.kanney@hashicorp.com>

* feat: grantsForUser for Global Resources (#5612)

* feat: grantsForUser for Global Resources

add query to fetch grants for a user for resources that are only globally scoped

* Update query based on change to bifurcate individual table

* Create subtests for different resources

* Return grant.grant_scope instead of the request scope

* Remove 'individual' subquery & unused reqScope parameter

* Use sql.Named for better readability

* Fix op function name

* Remove individual grant scope logic from global resource repo function

No need to handle individual grant scopes since global resources can only be queried via 'this' grant scope at the global scope.

* Fix row scan order

* Remove data gen function

* Adjust query formatting

Remove canonical_grant filter from query. `iam_grant.canonical_grant` is a primary key, so it can't be null anyway -- no need to filter out null canonical grants

* Use the consts for u_auth and u_anon

* Specify "empty" instead of "NULL" in struct field comment

* Build query args with `pq.Array` instead of `fmt.Sprintf`

* Fix TestGrantsForUserGlobalResources

No longer using a hard-coded value for roleVersion

* Refactor grantsForUserGlobalResources tests into testcases

* go mod tidy

* Update query comment for correctness

---------

Co-authored-by: dkanney <dkanney@terpmail.umd.edu>
Co-authored-by: dkanney <david.kanney@hashicorp.com>

* Bosorawis domain iam implement role grant scopes all (#5701)

* implement getRoleScopeId

* move query to query.go

* improve notfound err message

* improve other err messages

* use named parameter and move getRoleScopeId implementation

* moved getRoleScopeId test

* rename getRoleScopeId to getRoleScopeType

* fix public_id ambiguous error

* undo unintended change to getUserWithAccount

* fix the correct query

* split iam_role_global_individual_grant_scope to have separate tables for org and project

* small comment change

* small comment change

* WIP: add tests

* remove grant_scope as immutable column

* add trigger to delete individual grant scope when grant_scope changes

* add a test that covers changing grant_scope

* rename function and trigger in iam_role_global

* improve assertion in sqltest for iam_role_global

* update iam_role_org to delete redundant grants scope

* minor comment fix

* no longer handle individual grant scope deletion with triggers and rename some functions

* rename test

* add all subtype definitions

* remove unnecessary baseRole subtype

* add clone, setTableName, and GetScope tests

* add ResourceType and Actions test

* add create and delete tests for globalROle

* finish create and delete tests

* add trigger for deleting base role

* add trigger to sync update_time back to base iam_role table

* add update tests

* fix missing err checks

* fix iam_role delete subtype trigger function name and use new.update_time instead of now()

* add struct documentation to role subtypes

* add version update check

* implement getRoleScopeId

* implement getRoleScopeId

* save

* remove struct embedding from iam.Role

* fix tests to use new iam.Role definition

* repository_role_test.go move to new iam.Role model

* repository_principal_role_test.go use new iam.Role model

* repository_role_grant_test.go use new iam.Role model in test

* add oplog info to sql schema

* internal/iam/testing.go use new role schema in TestRole

* add toRole helper function to all role subtype

* remove tests that are no longer relevant

* internal/iam/repository_scope.go use new iam model

* internal/iam/repository_role_grant.go use new iam model

* internal/iam/repository_principal_role.go use new iam model

* internal/iam/repository_role_test.go add test case for global scoped role

* internal/iam/repository_grant_scope.go use new iam model

* fix query

* make create and lookup role work and add tests

* add role id to getRoleScopeId error message

* make DeleteRole work with new model and add tests

* fix update

* ensure oplog.ReplayableMessage is implemented on all role subtypes

* internal/iam/repository_role_grant.go fix slugging version properly

* internal/iam/repository_role.go minor correction to error message saying org instead of scope

* internal/iam/repository_role_test.go add more update tests

* add immutable_fields tests

* fix rebase

* change error code to RecordNotFound

* refactor to use getScopeType

* fix delete test

* add getRoleScope utility function

* repository_principal_role.go: refactor to remove multiple switch statements

* repository_role_grant.go: refactor to reduce LOC

* repository_role.go small refactor to use alloc func

* repository_grant_scope.go refactor

* review comments

* implement getRoleScopeId

* move query to query.go

* improve notfound err message

* improve other err messages

* use named parameter and move getRoleScopeId implementation

* moved getRoleScopeId test

* rename getRoleScopeId to getRoleScopeType

* fix public_id ambiguous error

* undo unintended change to getUserWithAccount

* fix the correct query

* rename test

* change error code to RecordNotFound

* Update internal/iam/repository_role.go

Co-authored-by: David Kanney <david.kanney@hashicorp.com>

* switch to slice instead of counter

* fix merge mistakes

* handling special scopes in test function

* fix TestRoleWithGrants

* fix minor typo

* make gen

* fix comment typos

* Bosorawis domain iam role use new model list role (#5676)

* add and use new list roles query

* run make gen

* tweaked returned error

* move ListRoleGrantScopes to repository_grant_scope.go

* rename repository_grant_scope to repository_role_grant_scope

* add proto definition for global role individual grant scope tables

* fix test from removing embeded struct from RoleGrantScope

* add grant_scope to proto definition

* implement GlobalRoleIndividualOrgGrantScope and GlobalRoleIndividualProjectGrantScope

* update comment

* run make gen to update comment

* implement OrgRoleIndividualGrantScope and add tests

* implement part of ListRoleGrantScopes

* Add more test

* add more test cases and remove add-grants test

* unexport listRoleGrantScopes

* use reader from function parameter instead of struct method

* rename test to match actual function

* run make gen

* unexport individual grants structs

* unexport individual grants structs - missed one file

* change TestRole and TestRoleGrantScope function to support new model

* add validation for special scopes

* add role_org_individual_grant_scope.pb.go to protobuild make target

* remove dead code from listRoleGrantScopes

* fix testRoleGrantScopeSpecial not handling org role special scope properly

* change proto grant_this default to true

* make TestRole readback the role to get updated version

* implement toRoleGrantScope function on the subtypes

* implement conversion function

* add tests and AddRoleGrantScope before refactor

* working delete grant scope before refactor

* remove unused functions

* refactor repository_role_grant_scope.go

* add tests for SetRoleGrantScopes

* all tests passing

* refactor repository_role_grant_scope.go again

* run make gen

* no longer embed Resource in roleScopeGranter interface and make interface all internal functions

* add additional test case

* fix minor typo

* add a constraint check for iam_role_org.grant_scope

* refactor and comment repository_role_grant_scope.go and add test cases

* remove unused code

* rename roleScopeGranter to roleGrantScopeUpdater

* interface and function rename

* address PR comments

* remove redundant constraint on iam_role_org

* address pr comments

* grant this scope to role by default when creating a role

* tweak comments and variable names

---------

Co-authored-by: David Kanney <david.kanney@hashicorp.com>

* feat: add grant_this_role_scope column to iam_role_project (#5738)

* add grant_this_role_scope and sqltes for iam_project_role

* update role_project.proto to add grant_this_role_scope field

* fix missing fields in role_project.proto

* support GrantThisRoleScope field in iam/testing.go

* update repository_scope to set GrantThisRoleScope to true

* add GrantThisRoleScope to projec role tests to role_test.go

* add grant_this_role_scope check to query.go

* update roleGrantScopeUpdater to split setGrantScope to setHierarchicalGrantScope and removeHierarchicalGrantScope

* update create_role to set default vaule for iam_role_project.grant_this_role_scope

* update set, add, delete, list role grant scopes to support grant_this_role_scope in project_role

* rename methods

* run make gen

* make TestRole not bump version when creating roles with default 'this' grants

* address pr comments

* fix variable names

* feat: grantsForUser for Org resources (#5663)

* Create grantsForUser query for Org resources

* Finish & format query

* Change name of field in query

From 'role_type' to 'role_parent_scope_id'

* Finish grantsForUserOrgResources test

- Refactor to match the usual testcase pattern
- Use test functions to create test resources

* Remove unnecessary check against GrantScopeThis

"this" now has its own field and no longer lives alongside the other grant scopes, so we should not check for it against grantScope

* Ignore children & descendant grant scopes when querying org resources

The only grant_scope that mattters when querying org resources is 'this'

* Refactor query to simplify repo function

Create separate CTEs for each case: global special (children or descendant), global individual, and org (this). This allows us to query directly into perms.GrantTuple; no additional logic required.

* feat: grantsForUser for Project resources (#5669)

* Reuse testInput

* Create grantsForUserProjectResources query

* Create grantsForUserProjectResources repo func

* Create tests for grantsForUserProjectResources

* Split up CTEs by grant scope and simplify repo function

* Add test cases for missing reqScope id

* Remove unnecessary join to iam_scope_org table

* Reuse 'roles_with_grants' CTE across the grantsForUser queriers

* Change reqScope parameter type from `Scope` to `string` (#5748)

* Change 'reqScope' parameter type to: string

* Simplify grantsForUserGlobalQuery & repo func

* Test: GrantsForUser for Accounts for various user to role relationships (#5631)

* test(iam): Refactor GrantsForUser tests into a single test

* test(iam): Extend TestGrantsForUser to include Account resource

Note: These tests will fail until GrantsForUser is refactored to return only grants whose scopes are applicable to this resource

* test(iam): test(iam): Add Target resource to TestGrantsForUser

* feat: grantsForUser for recursive Global or Org resources (#5747)

* Create query: grantsForUserGlobalAndOrgResourcesQuery

* Create repo function: grantsForUserGlobalAndOrgResourcesRecursive

* Create tests for grantsForUserGlobalAndOrgResources

* Restrict recursive list for Global/Org resources to global scope only

Return error for non-recursive scopes

* Remove unused option parameter

* Add additional scope and test cases

- Test for grants against a resource with no permission granted for it
- Test for grants against a specific resource id without an explicit type set in the grant string
- Add a project scope to ensure its grants aren't returned

* Return error when passed 'Unknown' or 'All' resource type

* Use constants for 'unknown' & '*' resources

* feat: grantsForUser for recursive Project resources (#5783)

* Create query: grantsForUserProjectResourcesGlobalScopeQuery

* Create query: grantsForUserProjectResourcesOrgScopeQuery

* Create repo function: grantsForUserProjectResourcesRecursiveScopes

* Add recursive testcases to existing grantsForUserProjectResource test function

* Address PR feedback

- Add testcases for Unknown & All resource types
- Change a grant string to a pinned resource. Its resource type changed from Target type to Unknown type
- Call through to grantsForUserProjectResources when reqScopeId is a project scope (i.e. a non-recursve scope)

* Add 'Recursive' to query var naming

* feat: migration hook grants refactor (#5733)

* scaffolding

* comments

* rename migration number

* implement FindIllegalAssociations for 97006 hook

* refactor findIllegalAssociation query

* implement RepairIllegalAssociations and refactor queries

* make gen

* fix missing err check

* pr feedback fix hook fix message wording

* sql formatting

* update comment

* sql formatting

* Apply suggestions from code review

Co-authored-by: David Kanney <david.kanney@hashicorp.com>

* change 'illegal' to 'invalid'

* pr comment feedback

* small comment update

---------

Co-authored-by: David Kanney <david.kanney@hashicorp.com>

* feat: grantsForUser for Global/Org/Project resources (#5807)

* Change naming to be consistent with other grantsForUser functions

* Create grantsForUser query for global-scoped Global/Org/Proj resources

* Create grantsForUser repo function for Global/Org/Proj resources

* Create tests for grantsForUserGlobalOrOrgOrProjectResources

* Create grantsForUser query for org-scoped Globa/Org/Proj resources

* Add tests for grantsForUser Global/Org/Proj resource query's org & project request_scopes

* Add clarifying comments and shorter naming convention

* Add 'children' grant_scoped org roles to result set

* feat: grants: add resource type parameters to GrantsForUser (#5750)

* add resource.Type to auth.Verify callchain

* remove WithType option

* update internal/auth/additional_verification_test.go to no longer use WithType

* handle explicit resource type in aclAndGrantHashForUser

* update interceptor_test.go to use resource.Scope for testing auth.Verify

* use resource.Scope to test Audit events

* add godoc comment on GrantsForUser

* add recursive options to iam

* add recursive options to auth

* internal/auth/ldap/managed_group_role_grants_test.go fix GrantsForUser signature

* internal/daemon/controller/handlers/accounts/account_service_test.go fix GrantsForUser signature

* update GrantsForUser godoc

* replace GrantsForUser tests with a new version with scoped down grants

* add mix type assignment to TestGrantsForUser

* move GrantsForUser test to internal/iam/repository_role_grant_ext_test.go

* remove unused options

* rewrite GrantsForUser tests to match the new expectation

* handle recursive call in s.authResult

* fix missing import from rebase

* resolve ListResolvableAlias destination permissions properly

* use global scope for ListResolvableAlias aclAndGrantHashForUser

* make auth.WithRecursive take boolean input

* make iam.WithRecursive option takes a boolean input

* auth/options_test.go add assertion for default option values

* make gen

* add back ACL tests to grants for user

* Support previous grants use-cases  (#5823)

* rename parameter to TestManagedGroupMember

* make replace old GrantsForUser with new queries

* fix tests and bugs where global grants don't get resolved

* remove unnecessary grants from grantsForUserProjectResourcesOrgScopeRecursiveQuery

* return full grants for any recursive call

* handle this and role scope ID in test

* order list result by create time instead of update time

* add missing err check

* handle converting role scope ID to this in grant scope API

* fix role service tests

* correct default scope role name

* add special handling in grantsForUser for scope resource

* fix tests to match role creation that does not bump version

* creating alias in invalid scope now returns permissions denied instead of internal

* remove test which looks for account in an authmethod in project scope which is an invalid config

* fix TestLdapManagedGroupRoleGrants to use scope appropriate resource for the test

* fix TestFetchActionSetForId and split org/proj test cases

* Validate reqScopeId for grantsForUserRecursive()

* Update tests based on recursive grantsForUser changes

* Remove last recursive function in favor of grantsForUserRecursive()

* move Scope resource to be AllowedIn all scope types

* allow multiple resouce types when calling auth.Verify down to GrantsForUsers

* use append instead of ranging over slice

* minor fix to tests

* Skip default role creation to avoid returning default role in test

* Update wantErrMsg to accommodate multiple resource types

* Add new entries to TestGrantsForUser test results based on recursive query changes

* Update unneccessaryRoles in recursive global/org test case based on changes to recursive query

---------

Co-authored-by: dkanney <david.kanney@hashicorp.com>

* Migrate roles and grants to new table structure (#5814)

* Refactor migration files to create tables, functions, triggers, and complete the actual data migration in their own files.

update migrations and tests

clean up removed files

rebase onto bosorawis-sql-grants-migration-hook

* Add comments to migration files, clean up some formatting, and add 30k project roles to test migration efficiency

update migrations go test file

Delete duplicate test file

* Fix formatting in migration files

* Rename hook number to reflect new number of migration files
split migrations into hook files, large test file, and more specific test functions

* Include missed oss file

* Apply suggestions from code review

Co-authored-by: Sorawis Nilparuk (Bo) <sorawis.nilparuk@hashicorp.com>

---------

Co-authored-by: Sorawis Nilparuk (Bo) <sorawis.nilparuk@hashicorp.com>

* Refactor GrantsForUser subqueries to use aggregate functions (#5822)

* Use shorter var name

* Refactor grantsForUserGlobalResourcesQuery: Use aggregate function array_agg to reduce rows returned from DB

Adding mutliple grants to a role exposes the need to aggregate certain fields (canonical_grant).
- Before this change, a row is returned for each distinct canonical grant for each role.
- After this change, only one row is returned per role. Each row contains an array of all canonical_grants that apply to the role.

* Refactor grantsForUserOrgResourcesQuery: Use aggregate function array_agg to reduce rows returned from DB

Adding mutliple grants to a role exposes the need to aggregate certain fields (individual_grant_scopes, canonical_grant).
- Before this change, a row is returned for each distinct canonical grant for each role.
- After this change, only one row is returned per role. Each row contains an array of all canonical_grants (and individual_grant_scopes) that apply to the role.

* Refactor grantsForUserProjectResourcesQuery: Use aggregate function array_agg to reduce rows returned from DB

Adding mutliple grants to a role exposes the need to aggregate certain fields (individual_grant_scopes, canonical_grant).
- Before this change, a row is returned for each distinct canonical grant for each role.
- After this change, only one row is returned per role. Each row contains an array of all canonical_grants (and individual_grant_scopes) that apply to the role.

* Address PR feedback

* Finish PR feedback

* Remove unnecessary if-check

* Match test name to function name

* Refactor grantsForUserRecursiveQuery: Use aggregate function array_agg to reduce rows returned from DB

* fix broken test (#5842)

* update tests to not check a table that will be dropped in the migration (#5847)

* Fix global scope's parent scope id (#5844)

* Use variable over hard-coded string

* 'global' scope should have no parent scope

* Clean up migrated tables (#5837)

* Refactor migration files to create tables, functions, triggers, and complete the actual data migration in their own files.

update migrations and tests

clean up removed files

rebase onto bosorawis-sql-grants-migration-hook

* Add comments to migration files, clean up some formatting, and add 30k project roles to test migration efficiency

update migrations go test file

Delete duplicate test file

* Fix formatting in migration files

* Rename hook number to reflect new number of migration files
split migrations into hook files, large test file, and more specific test functions

* Drop unneeded iam_role_grant_scope table and unneeded name, description, and version columns from iam_role

* fix whitespace

* fix broken sql from removing iam role grant scope (#5860)

* Ensure table drop also cascades changes to cross-table dependencies

* fix broken pgtap from removing iam-role-grant-scope table

* drop trigger which deletes from iam-role-grant-scope

---------

Co-authored-by: Michael Milton <michael.milton@hashicorp.com>

* test(groups): add grants tests for groups API (#5403)

* first test with all the required setup

* v1 of test

* add primitive func and more test

* refactor read tests into a single top level

* move token generation to a function

* add test for creates

* add delete tests

* add update test

* only check for version and update_time

* move setup resource into testcase to support grants with specific ID

* add member tests

* add group-member test example with multiple actions

* remove duplicate group membership tests

* ran make gen

* fix missing parentID bug

* fix typo

* fix test names and add test cases

* switch from google/uuid to hashicorp/go-uuid

* add comment to groupmember tests

* small comment change

* pull shared test utility code from PR #5418

* refactor role grants out of authtoken package

* unexport utility function

* Remove dead code

* lint and make gen

* fix role cration logic

* fix password TestAccountFunc implementation

* implement TestAccountFunc for LDAP

* implement TestAccountFunc for OIDC

* implement TestUserFunc for managed groups

* use managed groups in grants test

* undo removal of authtoken.TestAuthTokenWithRoles for future refactor

* switch from list to map based test case for create tests

* undo merge mistakes

* fix merge mistakes

* lint

* add setup examples

* add output fields tests for getgroup

* reimplement with reflect

* add test for CreateGroup

* add all single resource action tests

* add list test

* rename function argument

* move AssertOutputFields to handlers package

* fix lint

* make gen

* use proto.Message instead of custom interface

* switch to hashicorp/go-uuid

* fix typo

* fix error message

* id= to ids=

* make generating test accounts more randomized

* Trigger CI checks

* refactor auth/iam grants test setup

* move a test to _test package

* lint

* minor comment fix

* use Id instead of ID

* make user/account setup in iam returns account instead of just account ID

* missed one change

* reorganize tests

* make gen

* feat: add grants tests for billing resource (#5559)

* first test with all the required setup

* v1 of test

* add primitive func and more test

* refactor read tests into a single top level

* move token generation to a function

* add test for creates

* add delete tests

* add update test

* only check for version and update_time

* move setup resource into testcase to support grants with specific ID

* add member tests

* add group-member test example with multiple actions

* remove duplicate group membership tests

* ran make gen

* fix missing parentID bug

* fix typo

* fix test names and add test cases

* switch from google/uuid to hashicorp/go-uuid

* add comment to groupmember tests

* small comment change

* pull shared test utility code from PR #5418

* refactor role grants out of authtoken package

* unexport utility function

* Remove dead code

* lint and make gen

* fix role cration logic

* fix password TestAccountFunc implementation

* implement TestAccountFunc for LDAP

* implement TestAccountFunc for OIDC

* implement TestUserFunc for managed groups

* use managed groups in grants test

* undo removal of authtoken.TestAuthTokenWithRoles for future refactor

* switch from list to map based test case for create tests

* undo merge mistakes

* fix merge mistakes

* lint

* add setup examples

* add output fields tests for getgroup

* reimplement with reflect

* add test for CreateGroup

* add all single resource action tests

* add list test

* rename function argument

* move AssertOutputFields to handlers package

* fix lint

* make gen

* use proto.Message instead of custom interface

* switch to hashicorp/go-uuid

* fix typo

* fix error message

* id= to ids=

* make generating test accounts more randomized

* Trigger CI checks

* refactor auth/iam grants test setup

* lint

* minor comment fix

* use Id instead of ID

* make user/account setup in iam returns account instead of just account ID

* missed one change

* reorganize tests

* make gen

* feat: add grants tests for billing resource

add test coverage for testing grants with billing resource. This tests `monthly-active-users` action with billing.

billing does not support output_fields so there are no tests for that

* add negative test coverage for descendant scope and org scope

* move negative tests to the billing resource

* revert alias changes

* revert alias test name change

* resolve rebase conflict

---------

Co-authored-by: Sorawis Nilparuk <sorawis.nilparuk@hashicorp.com>

* Add grants tests for accounts (#5566)

* first test with all the required setup

* v1 of test

* add primitive func and more test

* small comment change

* refactor role grants out of authtoken package

* undo removal of authtoken.TestAuthTokenWithRoles for future refactor

* undo merge mistakes

* fix merge mistakes

* Trigger CI checks

* refactor auth/iam grants test setup

* Trigger CI checks

* add CRUDL tests

* add change-password and set-password tests

* add UpdateAccount, ChangePassword, SetPassword tests

* add ListAccount output_fields test

* make gen

* fix error message

* rebased against llb

* make gen

* fix post rebase

* fix typo

* make some tests use type-specific grants

* fix rebase issue

* add negative test to read

* test(credentials): Add grants tests (#5608)

* test(credentials): List tests

* test(credentials): Get tests

* test(credentials): Add "attributes" output_field & one its subtypes to the Read tests

* test(credentials): Create tests

* test(credentials): Update tests

* test(credentials): Delete tests

* test(credentials): Add additional test cases for pinned cred store id

* feat: add grants tests for alias (#5550)

* first test with all the required setup

* v1 of test

* add primitive func and more test

* refactor read tests into a single top level

* move token generation to a function

* add test for creates

* add delete tests

* add update test

* only check for version and update_time

* move setup resource into testcase to support grants with specific ID

* add member tests

* add group-member test example with multiple actions

* remove duplicate group membership tests

* ran make gen

* fix missing parentID bug

* fix typo

* fix test names and add test cases

* switch from google/uuid to hashicorp/go-uuid

* add comment to groupmember tests

* small comment change

* pull shared test utility code from PR #5418

* refactor role grants out of authtoken package

* unexport utility function

* Remove dead code

* lint and make gen

* fix role cration logic

* fix password TestAccountFunc implementation

* implement TestAccountFunc for LDAP

* implement TestAccountFunc for OIDC

* implement TestUserFunc for managed groups

* use managed groups in grants test

* undo removal of authtoken.TestAuthTokenWithRoles for future refactor

* switch from list to map based test case for create tests

* undo merge mistakes

* fix merge mistakes

* lint

* add setup examples

* add output fields tests for getgroup

* reimplement with reflect

* add test for CreateGroup

* add all single resource action tests

* add list test

* rename function argument

* move AssertOutputFields to handlers package

* fix lint

* make gen

* use proto.Message instead of custom interface

* switch to hashicorp/go-uuid

* fix typo

* fix error message

* id= to ids=

* make generating test accounts more randomized

* Trigger CI checks

* refactor auth/iam grants test setup

* lint

* minor comment fix

* use Id instead of ID

* make user/account setup in iam returns account instead of just account ID

* missed one change

* reorganize tests

* make gen

* feat: add grants tests for alias

add test coverage for testing grants with alias resource. This tests all actions with aliases and different grant scopes

* add tests for output fields

* add more test cases for actions, id

* update output assert to use shared assert function

* rebase

* use hashicorp/go-uuid instead of google/uuid

---------

Co-authored-by: Sorawis Nilparuk <sorawis.nilparuk@hashicorp.com>
Co-authored-by: Elim Tsiagbey <elim.tsiagbey@hashicorp.com>

* test: add grants tests for host_catalogs (#5573)

* first test with all the required setup

* v1 of test

* add primitive func and more test

* refactor read tests into a single top level

* move token generation to a function

* add test for creates

* add delete tests

* add update test

* only check for version and update_time

* move setup resource into testcase to support grants with specific ID

* add member tests

* add group-member test example with multiple actions

* remove duplicate group membership tests

* ran make gen

* fix missing parentID bug

* fix typo

* fix test names and add test cases

* switch from google/uuid to hashicorp/go-uuid

* add comment to groupmember tests

* small comment change

* pull shared test utility code from PR #5418

* refactor role grants out of authtoken package

* unexport utility function

* Remove dead code

* lint and make gen

* fix role cration logic

* fix password TestAccountFunc implementation

* implement TestAccountFunc for LDAP

* implement TestAccountFunc for OIDC

* implement TestUserFunc for managed groups

* use managed groups in grants test

* undo removal of authtoken.TestAuthTokenWithRoles for future refactor

* switch from list to map based test case for create tests

* undo merge mistakes

* fix merge mistakes

* lint

* add setup examples

* add output fields tests for getgroup

* reimplement with reflect

* add test for CreateGroup

* add all single resource action tests

* add list test

* rename function argument

* move AssertOutputFields to handlers package

* fix lint

* make gen

* use proto.Message instead of custom interface

* switch to hashicorp/go-uuid

* fix typo

* fix error message

* id= to ids=

* make generating test accounts more randomized

* Trigger CI checks

* refactor auth/iam grants test setup

* move a test to _test package

* lint

* minor comment fix

* use Id instead of ID

* make user/account setup in iam returns account instead of just account ID

* missed one change

* reorganize tests

* make gen

* Trigger CI checks

* test: add grants tests for host_catalogs

* add test coverage for output fields and write actions

* add missing error check assertions

* fixup! address PR comments

* fixup! fix lint errors

* chore(host_catalogs_test): Remove duplicate import

* test(hostcatalogs): Add tests for actions w/o grants

* test(hostcatalogs): Use ldap managed group for all test cases

* feat: add grants tests for billing resource (#5559)

* first test with all the required setup

* v1 of test

* add primitive func and more test

* refactor read tests into a single top level

* move token generation to a function

* add test for creates

* add delete tests

* add update test

* only check for version and update_time

* move setup resource into testcase to support grants with specific ID

* add member tests

* add group-member test example with multiple actions

* remove duplicate group membership tests

* ran make gen

* fix missing parentID bug

* fix typo

* fix test names and add test cases

* switch from google/uuid to hashicorp/go-uuid

* add comment to groupmember tests

* small comment change

* pull shared test utility code from PR #5418

* refactor role grants out of authtoken package

* unexport utility function

* Remove dead code

* lint and make gen

* fix role cration logic

* fix password TestAccountFunc implementation

* implement TestAccountFunc for LDAP

* implement TestAccountFunc for OIDC

* implement TestUserFunc for managed groups

* use managed groups in grants test

* undo removal of authtoken.TestAuthTokenWithRoles for future refactor

* switch from list to map based test case for create tests

* undo merge mistakes

* fix merge mistakes

* lint

* add setup examples

* add output fields tests for getgroup

* reimplement with reflect

* add test for CreateGroup

* add all single resource action tests

* add list test

* rename function argument

* move AssertOutputFields to handlers package

* fix lint

* make gen

* use proto.Message instead of custom interface

* switch to hashicorp/go-uuid

* fix typo

* fix error message

* id= to ids=

* make generating test accounts more randomized

* Trigger CI checks

* refactor auth/iam grants test setup

* lint

* minor comment fix

* use Id instead of ID

* make user/account setup in iam returns account instead of just account ID

* missed one change

* reorganize tests

* make gen

* feat: add grants tests for billing resource

add test coverage for testing grants with billing resource. This tests `monthly-active-users` action with billing.

billing does not support output_fields so there are no tests for that

* add negative test coverage for descendant scope and org scope

* move negative tests to the billing resource

* revert alias changes

* revert alias test name change

* resolve rebase conflict

---------

Co-authored-by: Sorawis Nilparuk <sorawis.nilparuk@hashicorp.com>

* Add grants tests for accounts (#5566)

* first test with all the required setup

* v1 of test

* add primitive func and more test

* small comment change

* refactor role grants out of authtoken package

* undo removal of authtoken.TestAuthTokenWithRoles for future refactor

* undo merge mistakes

* fix merge mistakes

* Trigger CI checks

* refactor auth/iam grants test setup

* Trigger CI checks

* add CRUDL tests

* add change-password and set-password tests

* add UpdateAccount, ChangePassword, SetPassword tests

* add ListAccount output_fields test

* make gen

* fix error message

* rebased against llb

* make gen

* fix post rebase

* fix typo

* make some tests use type-specific grants

* fix rebase issue

* add negative test to read

* test(credentials): Add grants tests (#5608)

* test(credentials): List tests

* test(credentials): Get tests

* test(credentials): Add "attributes" output_field & one its subtypes to the Read tests

* test(credentials): Create tests

* test(credentials): Update tests

* test(credentials): Delete tests

* test(credentials): Add additional test cases for pinned cred store id

* feat: add grants tests for alias (#5550)

* first test with all the required setup

* v1 of test

* add primitive func and more test

* refactor read tests into a single top level

* move token generation to a function

* add test for creates

* add delete tests

* add update test

* only check for version and update_time

* move setup resource into testcase to support grants with specific ID

* add member tests

* add group-member test example with multiple actions

* remove duplicate group membership tests

* ran make gen

* fix missing parentID bug

* fix typo

* fix test names and add test cases

* switch from google/uuid to hashicorp/go-uuid

* add comment to groupmember tests

* small comment change

* pull shared test utility code from PR #5418

* refactor role grants out of authtoken package

* unexport utility function

* Remove dead code

* lint and make gen

* fix role cration logic

* fix password TestAccountFunc implementation

* implement TestAccountFunc for LDAP

* implement TestAccountFunc for OIDC

* implement TestUserFunc for managed groups

* use managed groups in grants test

* undo removal of authtoken.TestAuthTokenWithRoles for future refactor

* switch from list to map based test case for create tests

* undo merge mistakes

* fix merge mistakes

* lint

* add setup examples

* add output fields tests for getgroup

* reimplement with reflect

* add test for CreateGroup

* add all single resource action tests

* add list test

* rename function argument

* move AssertOutputFields to handlers package

* fix lint

* make gen

* use proto.Message instead of custom interface

* switch to hashicorp/go-uuid

* fix typo

* fix error message

* id= to ids=

* make generating test accounts more randomized

* Trigger CI checks

* refactor auth/iam grants test setup

* lint

* minor comment fix

* use Id instead of ID

* make user/account setup in iam returns account instead of just account ID

* missed one change

* reorganize tests

* make gen

* feat: add grants tests for alias

add test coverage for testing grants with alias resource. This tests all actions with aliases and different grant scopes

* add tests for output fields

* add more test cases for actions, id

* update output assert to use shared assert function

* rebase

* use hashicorp/go-uuid instead of google/uuid

---------

Co-authored-by: Sorawis Nilparuk <sorawis.nilparuk@hashicorp.com>
Co-authored-by: Elim Tsiagbey <elim.tsiagbey@hashicorp.com>

* test(hostcatalogs): Add an additional project to "create" test cases

---------

Co-authored-by: Sorawis Nilparuk <sorawis.nilparuk@hashicorp.com>
Co-authored-by: dkanney <dkanney@terpmail.umd.edu>
Co-authored-by: David Kanney <david.kanney@hashicorp.com>

* Add grants tests for authmethods (#5569)

* test(authmethods): Simplify read actions test cases

* test(authmethods): Create grants tests for write actions

* test(authmethods): Create test cases for action: authenticate

* test(authmethods): Update tests to use userFunc & other changes from upstream branch

* test(authmethods): Add output_fields to ReadActions tests

* test(authmethods): Add output_fields to WriteActions tests

* chore(authmethods_test): Consolidate common output fields into a single struct

* test(authmethods): Add test cases for project roles

* test(authmethods): Fix authenticate testcases

* test(authmethods): Add tests granting permission to specific ids using multiple roles

* test(hostsets): add grants tests (#5591)

* test(hostsets): List tests

* test(hostsets): Get tests

* test(hostsets): Create tests

* test(hostsets): Update tests

* test(hostsets): Delete tests

* test(hostsets}: AddHostSetHosts tests

* test(hostsets): RemoveHostSetHosts tests

* test(hostsets): SetHostSetHosts tests

* test(hostsets): Use unique host-catalog names to avoid duplicate key DB errors

* test(hostsets): Add second project to enforce exclusivity when Listing host-sets

* test: credentials store grants (#5592)

* first test with all the required setup

* v1 of test

* add primitive func and more test

* refactor read tests into a single top level

* move token generation to a function

* add test for creates

* add delete tests

* add update test

* only check for version and update_time

* move setup resource into testcase to support grants with specific ID

* add member tests

* add group-member test example with multiple actions

* remove duplicate group membership tests

* ran make gen

* fix missing parentID bug

* fix typo

* fix test names and add test cases

* switch from google/uuid to hashicorp/go-uuid

* add comment to groupmember tests

* small comment change

* pull shared test utility code from PR #5418

* refactor role grants out of authtoken package

* unexport utility function

* Remove dead code

* lint and make gen

* fix role cration logic

* fix password TestAccountFunc implementation

* implement TestAccountFunc for LDAP

* implement TestAccountFunc for OIDC

* implement TestUserFunc for managed groups

* use managed groups in grants test

* undo removal of authtoken.TestAuthTokenWithRoles for future refactor

* switch from list to map based test case for create tests

* undo merge mistakes

* fix merge mistakes

* lint

* add setup examples

* add output fields tests for getgroup

* reimplement with reflect

* add test for CreateGroup

* add all single resource action tests

* add list test

* rename function argument

* move AssertOutputFields to handlers package

* fix lint

* make gen

* use proto.Message instead of custom interface

* switch to hashicorp/go-uuid

* fix typo

* fix error message

* id= to ids=

* make generating test accounts more randomized

* Trigger CI checks

* refactor auth/iam grants test setup

* lint

* minor comment fix

* use Id instead of ID

* make user/account setup in iam returns account instead of just account ID

* missed one change

* save

* add list tests

* add get test

* add create and delete test

* add delete and update tests

* more tests

* fix collection_authorized_actions grants not resolving

* complete output_fields tests

* fix import groups

* make gen

* fixed broken tests

* fix rebase

* switch all tests to TestUserGroupGrantsFunc

* remove duplicate test

* test: add grants tests for managed groups resource (#5642)

* test: add grants tests for managed groups resource

* PR reviews

* address PR comments

* test: add grants tests for auth tokens resource (#5644)

* test: add grants tests for auth tokens resource

* add authorized actions tests for resources with sub-resources (#5835)

* add authorized actions tests for resources with sub-resources

* make gen

* bosorawis remove TestRoleGrantsForToken (#5840)

* refactor auth method grants tests

* refactor credential libraries grants tests

* refactor hosts grants tests

* refactor roles grants tests

* refactor scopes grants tests

* refactor tcp targets grants tests

* refactor users grants tests

* refactor worker grants tests

* fix authmethod test broken during the refactor

* remove authtoken.TestRoleGrantsForToken

* add pinned ID test

* correct event name

* fix make gen and lint

* Remove old test Role creation code

Removing leftover logic from old grants data model because it caused Group-association tests to fail

* update go mod

* run make gen and move new migrations to new folder (#5862)

* run make gen and move new migrations to new folder

* make tools and make gen

* update hook number

* update prior migration

* move 97005 to 97001 for consistency

* test: add grants tests for session resource (#5855)

* add grants tests for sessions

* fix missing parentScopeId

* make gen

* Resolve GrantsForUser queries via resolveQuery() (#5836)

* Add validation for nil resource type

* Remove redundant recursive test

* Refactor GrantsForUser() to perform query resolution & data mapping

- Remove grantsForUser sub-functions
- Resolve grantsForUser queries via resolveQuery()
- resolveQuery() and map data to GrantTuples in GrantsForUser()

* remove dead code

---------

Co-authored-by: Sorawis Nilparuk <sorawis.nilparuk@hashicorp.com>

* add grants tests for scope resource (#5845) (#5865)

* 'List' tests for scopes

* 'Get' tests for scopes

* 'Create' tests for scopes

* 'List Key Version Destruction Jobs' tests for scopes

* Add output_fields testing to 'Get' test

* Add additional test case for list-key-version-destruction-jobs

* test: add grants tests for roles resource (#5864)

* add grants tests for roles

* make gen

* test: add grants tests for worker resource (#5841)

* add read createControllerLed createWorkerLed tests

# Conflicts:
#	internal/daemon/controller/handlers/workers/grants_test.go

* minor refactor

* add output fields tests for list

* add tests for addworkertags readcertificateauthority reinitializecertificateauthority

* finish all worker api tests

* fix make gen and lint

* fix unchecked error lint

* add missing output fields assertion

* Bosorawis fix set role grant scope not handle children grant already exist (#5868)

* add new test case where role already has children and attempt to set children and individual project

* handle case where role already has children grant scope attempt to set children and project

* test: add grants tests for target resource (#5861)

* additional tests to target resource

* add test coverage for SetTargetCredentialSources

* make gen

* review suggestions

* test: add grants test for user resource and fix ACL bug (#5869)

* add some list-resolvable-aliases tests

* fix missing fields

* add more test and found edge case with children grants

* more list-resolvable test cases

* fix edge case where resource ids not overlapping between parent and child are not considered

* resolve conflict

* remove name, version, description from expect output_fields

* remove name and description from toProto and add godoc comment

---------

Co-authored-by: David Kanney <david.kanney@hashicorp.com>
Co-authored-by: Elim Tsiagbey <elim.tsiagbey@hashicorp.com>
Co-authored-by: Sorawis Nilparuk <sorawis.nilparuk@hashicorp.com>
Co-authored-by: Elim Tsiagbey <elimty02@gmail.com>
Co-authored-by: dkanney <dkanney@terpmail.umd.edu>
2025-06-20 15:31:57 -04:00

968 lines
34 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package auth
import (
"context"
stderrors "errors"
"fmt"
"net/http"
"strings"
"time"
"github.com/hashicorp/boundary/api/recovery"
"github.com/hashicorp/boundary/globals"
"github.com/hashicorp/boundary/internal/auth"
"github.com/hashicorp/boundary/internal/auth/ldap"
"github.com/hashicorp/boundary/internal/auth/oidc"
"github.com/hashicorp/boundary/internal/auth/password"
"github.com/hashicorp/boundary/internal/daemon/controller/common"
"github.com/hashicorp/boundary/internal/daemon/controller/handlers"
"github.com/hashicorp/boundary/internal/errors"
"github.com/hashicorp/boundary/internal/event"
authpb "github.com/hashicorp/boundary/internal/gen/controller/auth"
"github.com/hashicorp/boundary/internal/gen/controller/tokens"
"github.com/hashicorp/boundary/internal/iam"
"github.com/hashicorp/boundary/internal/kms"
"github.com/hashicorp/boundary/internal/perms"
"github.com/hashicorp/boundary/internal/requests"
"github.com/hashicorp/boundary/internal/server"
"github.com/hashicorp/boundary/internal/types/action"
"github.com/hashicorp/boundary/internal/types/resource"
"github.com/hashicorp/boundary/internal/types/scope"
"github.com/hashicorp/boundary/internal/util"
"github.com/hashicorp/boundary/internal/util/template"
"github.com/hashicorp/boundary/sdk/pbs/controller/api/resources/scopes"
wrapping "github.com/hashicorp/go-kms-wrapping/v2"
"github.com/mr-tron/base58"
"google.golang.org/protobuf/proto"
)
type TokenFormat uint32
const (
// We weren't given one or couldn't parse it
AuthTokenTypeUnknown TokenFormat = iota
// Came in via the Authentication: Bearer header
AuthTokenTypeBearer
// Came in via split cookies
AuthTokenTypeSplitCookie
// It's of recovery type
AuthTokenTypeRecoveryKms
)
// CallbackAction represents the action type for
// callback operations in a request's URL path.
// This is currently only used during auth method
// authentication.
const CallbackAction = "callback"
type key int
var verifierKey key
type VerifyResults struct {
UserData template.Data
// This is copied out from UserData above but used to avoid nil checks in
// lots of places that embed this value
UserId string
AuthTokenId string
Error error
Scope *scopes.ScopeInfo
// AuthenticatedFinished means that the request has passed through the
// authentication system successfully. This does _not_ indicate whether a
// token was provided on the request. Requests for `u_anon` will still have
// this set true! This is because if a request has a token that is invalid,
// we fall back to `u_anon` because the request may still be allowed for any
// anonymous user; it simply fails to validate for and look up grants for an
// actual known user.
//
// A good example is when running dev mode twice. The first time you can
// authenticate and get a token which is saved by the token helper. The
// second time, you run a command and it reads the token from the helper.
// That token is no longer valid, but if the action is granted to `u_anon`
// the action should still succeed. What happens internally is that the
// token fails to look up a non-anonymous user, so we fallback to the
// anonymous user, which is the default.
//
// If you want to know if the request had a valid token provided, use a
// switch on UserId. Anything that isn't `u_anon` will have to have had a
// valid token provided. And a valid token will never fall back to `u_anon`.
AuthenticationFinished bool
// RoundTripValue can be set to allow the function performing authentication
// (often accompanied by lookup(s)) to return a result of that lookup to the
// calling function. It is opaque to this package.
RoundTripValue any
// Used for additional verification
v *verifier
// Used to generate a hash of all grants
grants perms.GrantTuples
}
type verifier struct {
iamRepoFn common.IamRepoFactory
authTokenRepoFn common.AuthTokenRepoFactory
serversRepoFn common.ServersRepoFactory
passwordAuthRepoFn common.PasswordAuthRepoFactory
oidcAuthRepoFn common.OidcAuthRepoFactory
ldapAuthRepoFn common.LdapAuthRepoFactory
kms *kms.Kms
requestInfo *authpb.RequestInfo
res *perms.Resource
act action.Type
ctx context.Context
acl perms.ACL
}
// TODO (jefferai 10/2022): NewVerifierContextWithAccounts performs the function
// of NewVerifierContext (see the docs for that function) but with extra
// parameters that can be used to look up account information. This is not
// intended to be a long-lived function; see
// https://hashicorp.atlassian.net/browse/ICU-6571 and
// https://hashicorp.atlassian.net/browse/ICU-6572
//
// This is being added for a quick turnaround purpose and to avoid making large
// numbers of changes to tests when we may do a much bigger refactor; when those
// items are addressed this can be removed.
func NewVerifierContextWithAccounts(ctx context.Context,
iamRepoFn common.IamRepoFactory,
authTokenRepoFn common.AuthTokenRepoFactory,
serversRepoFn common.ServersRepoFactory,
passwordAuthRepoFn common.PasswordAuthRepoFactory,
oidcAuthRepoFn common.OidcAuthRepoFactory,
ldapAuthRepoFn common.LdapAuthRepoFactory,
kms *kms.Kms,
requestInfo *authpb.RequestInfo,
) context.Context {
return context.WithValue(ctx, verifierKey, &verifier{
iamRepoFn: iamRepoFn,
authTokenRepoFn: authTokenRepoFn,
serversRepoFn: serversRepoFn,
passwordAuthRepoFn: passwordAuthRepoFn,
oidcAuthRepoFn: oidcAuthRepoFn,
ldapAuthRepoFn: ldapAuthRepoFn,
kms: kms,
requestInfo: requestInfo,
})
}
// NewVerifierContext creates a context that carries a verifier object from the
// HTTP handlers to the gRPC service handlers. It should only be created in the
// HTTP handler and should exist for every request that reaches the service
// handlers.
func NewVerifierContext(ctx context.Context,
iamRepoFn common.IamRepoFactory,
authTokenRepoFn common.AuthTokenRepoFactory,
serversRepoFn common.ServersRepoFactory,
kms *kms.Kms,
requestInfo *authpb.RequestInfo,
) context.Context {
return NewVerifierContextWithAccounts(ctx, iamRepoFn, authTokenRepoFn, serversRepoFn, nil, nil, nil, kms, requestInfo)
}
// Verify takes in a context that has expected parameters as values and runs an
// authn/authz check. It returns a user ID, the scope ID for the request (which
// may come from the URL and may come from the token) and whether or not to
// proceed, e.g. whether the authn/authz check resulted in failure. If an error
// occurs it's logged to the system log.
func Verify(ctx context.Context, resourceType resource.Type, opt ...Option) (ret VerifyResults) {
const op = "auth.Verify"
ret.Error = handlers.ForbiddenError()
v, ok := ctx.Value(verifierKey).(*verifier)
if !ok {
// We don't have a logger yet and this should never happen in any
// context we won't catch in tests
panic("no verifier information found in context")
}
ret.v = v
v.ctx = ctx
ea := &event.Auth{}
defer func() {
if err := event.WriteAudit(ctx, op, event.WithAuth(ea)); err != nil {
event.WriteError(ctx, op, err)
}
}()
opts := getOpts(opt...)
ret.Scope = new(scopes.ScopeInfo)
// Note: we don't call RequestContextFromCtx here because that performs a
// SelfOrDefault. That's useful in the handlers, but here we don't want to
// do anything if it's nil. That's mainly useful in tests where
// DisableAuthEntirely is set. Later, if we don't have the value set at all,
// we have some safeguards to ensure we fail safe (e.g. no output fields at
// all). So it provides a good indication as well whether we have set this
// where and when needed.
var reqInfo *requests.RequestContext
reqInfoRaw := ctx.Value(requests.ContextRequestInformationKey)
if reqInfoRaw != nil {
reqInfo = reqInfoRaw.(*requests.RequestContext)
}
// In tests we often simply disable auth so we can test the service handlers
// without fuss
if v.requestInfo.DisableAuthEntirely {
yes := true
ea.DisabledAuthEntirely = &yes
const op = "auth.(disabled).lookupScope"
ret.Scope.Id = v.requestInfo.ScopeIdOverride
if ret.Scope.Id == "" {
ret.Scope.Id = opts.withScopeId
}
// Look up scope details to return. We can skip a lookup when using the
// global scope
switch ret.Scope.Id {
case scope.Global.String():
ret.Scope = &scopes.ScopeInfo{
Id: scope.Global.String(),
Type: scope.Global.String(),
Name: scope.Global.String(),
Description: "Global Scope",
ParentScopeId: "",
}
default:
iamRepo, err := v.iamRepoFn()
if err != nil {
ret.Error = errors.Wrap(ctx, err, op, errors.WithMsg("failed to get iam repo"))
return
}
scp, err := iamRepo.LookupScope(v.ctx, ret.Scope.Id)
if err != nil {
ret.Error = errors.Wrap(ctx, err, op)
return
}
if scp == nil {
ret.Error = errors.New(ctx, errors.InvalidParameter, op, fmt.Sprintf("non-existent scope %q", ret.Scope.Id))
return
}
ret.Scope = &scopes.ScopeInfo{
Id: scp.GetPublicId(),
Type: scp.GetType(),
Name: scp.GetName(),
Description: scp.GetDescription(),
ParentScopeId: scp.GetParentId(),
}
}
ret.UserId = v.requestInfo.UserIdOverride
ret.UserData.User.Id = util.Pointer(ret.UserId)
if reqInfo != nil {
reqInfo.UserId = ret.UserId
}
ea.UserInfo = &event.UserInfo{UserId: ret.UserId}
ret.Error = nil
return
}
v.act = opts.withAction
v.res = &perms.Resource{
ScopeId: opts.withScopeId,
Id: opts.withId,
Pin: opts.withPin,
Type: resourceType,
// Parent Scope ID will be filled in via performAuthCheck
}
// Global scope has no parent ID; account for this
if opts.withId == scope.Global.String() && resourceType == resource.Scope {
v.res.ScopeId = scope.Global.String()
}
if v.requestInfo.EncryptedToken != "" {
v.decryptToken(ctx)
}
resourcesToFetchGrants := append([]resource.Type{resourceType}, opts.withFetchAdditionalResourceGrants...)
var authResults perms.ACLResults
var userData template.Data
var err error
authResults, ret.UserData, ret.Scope, v.acl, ret.grants, err = v.performAuthCheck(ctx, resourcesToFetchGrants, opts.withRecursive)
if err != nil {
event.WriteError(ctx, op, err, event.WithInfoMsg("error performing authn/authz check"))
return
}
if ret.UserData.User.Id != nil {
ret.UserId = *ret.UserData.User.Id
}
ret.AuthTokenId = v.requestInfo.PublicId
ret.AuthenticationFinished = authResults.AuthenticationFinished
if !authResults.Authorized {
if v.requestInfo.DisableAuthzFailures {
ret.Error = nil
// TODO: Decide whether to remove this
err := event.WriteObservation(ctx, op,
event.WithHeader(
"auth-results", struct {
Msg string `json:"msg"`
Resource *perms.Resource `json:"resource"`
UserId string `json:"user_id"`
Action string `json:"action"`
}{
Msg: "failed authz info for request",
Resource: v.res,
UserId: ret.UserId,
Action: v.act.String(),
}))
if err != nil {
event.WriteError(ctx, op, err)
}
} else {
// If the anon user was used (either no token, or invalid (perhaps
// expired) token), return a 401. That way if it's an authn'd user
// that is not authz'd we'll return 403 to be explicit.
if ret.UserId == globals.AnonymousUserId {
ret.Error = handlers.UnauthenticatedError()
}
ea.UserInfo = &event.UserInfo{
UserId: ret.UserId,
}
return
}
}
grants := make([]event.Grant, 0, len(ret.grants))
for _, g := range ret.grants {
grants = append(grants, event.Grant{
Grant: g.Grant,
RoleId: g.RoleId,
ScopeId: g.GrantScopeId,
})
}
ea.UserInfo = &event.UserInfo{
UserId: ret.UserId,
}
if userData.Account.Id != nil {
ea.UserInfo.AuthAccountId = *userData.Account.Id
}
ea.GrantsInfo = &event.GrantsInfo{
Grants: grants,
}
if userData.User.FullName != nil {
ea.UserName = *userData.User.FullName
}
if userData.User.Email != nil {
ea.UserEmail = *userData.User.Email
}
if reqInfo != nil {
reqInfo.UserId = ret.UserId
reqInfo.OutputFields = authResults.OutputFields
}
ret.Error = nil
return
}
func (v *verifier) decryptToken(ctx context.Context) {
const op = "auth.(verifier).decryptToken"
switch v.requestInfo.TokenFormat {
case uint32(AuthTokenTypeUnknown):
// Nothing to decrypt
return
case uint32(AuthTokenTypeBearer), uint32(AuthTokenTypeSplitCookie):
if v.kms == nil {
event.WriteError(ctx, op, stderrors.New("no KMS object available to authz system"))
v.requestInfo.TokenFormat = uint32(AuthTokenTypeUnknown)
return
}
tokenRepo, err := v.authTokenRepoFn()
if err != nil {
event.WriteError(ctx, op, err, event.WithInfoMsg("failed to get authtoken repo"))
v.requestInfo.TokenFormat = uint32(AuthTokenTypeUnknown)
return
}
at, err := tokenRepo.LookupAuthToken(v.ctx, v.requestInfo.PublicId)
if err != nil {
event.WriteError(ctx, op, err, event.WithInfoMsg("failed to look up auth token by public ID"))
v.requestInfo.TokenFormat = uint32(AuthTokenTypeUnknown)
return
}
if at == nil {
event.WriteError(ctx, op, stderrors.New("nil result from looking up auth token by public ID"))
v.requestInfo.TokenFormat = uint32(AuthTokenTypeUnknown)
return
}
tokenWrapper, err := v.kms.GetWrapper(v.ctx, at.GetScopeId(), kms.KeyPurposeTokens)
if err != nil {
event.WriteError(ctx, op, err, event.WithInfoMsg("unable to get wrapper for tokens; continuing as anonymous user"))
v.requestInfo.TokenFormat = uint32(AuthTokenTypeUnknown)
return
}
version := v.requestInfo.EncryptedToken[0:len(globals.ServiceTokenV1)]
switch version {
case globals.ServiceTokenV1:
default:
event.WriteError(ctx, op, stderrors.New("unknown token encryption version; continuing as anonymous user"), event.WithInfo("version", version))
v.requestInfo.TokenFormat = uint32(AuthTokenTypeUnknown)
return
}
marshaledToken, err := base58.FastBase58Decoding(v.requestInfo.EncryptedToken[len(globals.ServiceTokenV1):])
if err != nil {
event.WriteError(ctx, op, err, event.WithInfoMsg("error unmarshaling base58 token; continuing as anonymous user"))
v.requestInfo.TokenFormat = uint32(AuthTokenTypeUnknown)
return
}
blobInfo := new(wrapping.BlobInfo)
if err := proto.Unmarshal(marshaledToken, blobInfo); err != nil {
event.WriteError(ctx, op, err, event.WithInfoMsg("error decoding encrypted token; continuing as anonymous user"))
v.requestInfo.TokenFormat = uint32(AuthTokenTypeUnknown)
return
}
s1Bytes, err := tokenWrapper.Decrypt(v.ctx, blobInfo, wrapping.WithAad([]byte(v.requestInfo.PublicId)))
if err != nil {
event.WriteError(ctx, op, err, event.WithInfoMsg("error decrypting encrypted token; continuing as anonymous user"))
v.requestInfo.TokenFormat = uint32(AuthTokenTypeUnknown)
return
}
var s1Info tokens.S1TokenInfo
if err := proto.Unmarshal(s1Bytes, &s1Info); err != nil {
event.WriteError(ctx, op, err, event.WithInfoMsg("error unmarshaling token info; continuing as anonymous user"))
v.requestInfo.TokenFormat = uint32(AuthTokenTypeUnknown)
return
}
if v.requestInfo.TokenFormat == uint32(AuthTokenTypeUnknown) || s1Info.Token == "" || v.requestInfo.PublicId == "" {
event.WriteError(ctx, op, stderrors.New("after parsing, could not find valid token; continuing as anonymous user"))
v.requestInfo.TokenFormat = uint32(AuthTokenTypeUnknown)
return
}
v.requestInfo.Token = s1Info.Token
return
case uint32(AuthTokenTypeRecoveryKms):
if v.kms == nil {
event.WriteError(ctx, op, stderrors.New("no KMS object available to authz system"))
v.requestInfo.TokenFormat = uint32(AuthTokenTypeUnknown)
return
}
wrapper := v.kms.GetExternalWrappers(ctx).Recovery()
if wrapper == nil {
event.WriteError(ctx, op, stderrors.New("no recovery KMS is available"))
v.requestInfo.TokenFormat = uint32(AuthTokenTypeUnknown)
return
}
info, err := recovery.ParseRecoveryToken(v.ctx, wrapper, v.requestInfo.EncryptedToken)
if err != nil {
event.WriteError(ctx, op, err, event.WithInfoMsg("decrypt recovery token: error parsing and validating recovery token"))
v.requestInfo.TokenFormat = uint32(AuthTokenTypeUnknown)
return
}
// If we add the validity period to the creation time (which we've
// verified is before the current time, with a minute of fudging), and
// it's before now, it's expired and might be a replay.
if info.CreationTime.Add(globals.RecoveryTokenValidityPeriod).Before(time.Now()) {
event.WriteError(ctx, op, stderrors.New("recovery token has expired (possible replay attack)"))
v.requestInfo.TokenFormat = uint32(AuthTokenTypeUnknown)
return
}
repo, err := v.serversRepoFn()
if err != nil {
event.WriteError(ctx, op, err, event.WithInfoMsg("decrypt recovery token: error fetching server repo"))
v.requestInfo.TokenFormat = uint32(AuthTokenTypeUnknown)
return
}
if err := repo.AddNonce(v.ctx, info.Nonce, server.NoncePurposeRecovery); err != nil {
event.WriteError(ctx, op, err, event.WithInfoMsg("decrypt recovery token: error adding nonce to database (possible replay attack)"))
v.requestInfo.TokenFormat = uint32(AuthTokenTypeUnknown)
return
}
event.WriteError(ctx, op, stderrors.New("recovery KMS was used to authorize a call"), event.WithInfo("url", v.requestInfo.Path, "method", v.requestInfo.Method))
}
}
func (v verifier) performAuthCheck(ctx context.Context, resourceType []resource.Type, isRecursiveRequest bool) (
aclResults perms.ACLResults,
userData template.Data,
scopeInfo *scopes.ScopeInfo,
retAcl perms.ACL,
grantTuples []perms.GrantTuple,
retErr error,
) {
const op = "auth.(verifier).performAuthCheck"
// Ensure we return an error by default if we forget to set this somewhere
retErr = errors.New(ctx, errors.Unknown, op, "default auth error", errors.WithoutEvent())
// Make the linter happy
_ = retErr
scopeInfo = new(scopes.ScopeInfo)
// This will always be set, so further down below we can switch on whether
// it's empty
userData.User.Id = util.Pointer(globals.AnonymousUserId)
// Validate the token and fetch the corresponding user ID
switch v.requestInfo.TokenFormat {
case uint32(AuthTokenTypeUnknown):
// Nothing; remain as the anonymous user
case uint32(AuthTokenTypeRecoveryKms):
// We validated the encrypted token in decryptToken and handled the
// nonces there, so just set the user
userData.User.Id = util.Pointer(globals.RecoveryUserId)
case uint32(AuthTokenTypeBearer), uint32(AuthTokenTypeSplitCookie):
if v.requestInfo.Token == "" {
// This will end up staying as the anonymous user
break
}
tokenRepo, err := v.authTokenRepoFn()
if err != nil {
retErr = errors.Wrap(ctx, err, op)
return
}
at, err := tokenRepo.ValidateToken(v.ctx, v.requestInfo.PublicId, v.requestInfo.Token)
if err != nil {
// Continue as the anonymous user as maybe this token is expired but
// we can still perform the action
event.WriteError(ctx, op, err, event.WithInfoMsg("error validating token; continuing as anonymous user"))
break
}
if at != nil {
userData.Account.Id = util.Pointer(at.GetAuthAccountId())
userData.User.Id = util.Pointer(at.GetIamUserId())
if *userData.User.Id == "" {
event.WriteError(ctx, op, stderrors.New("perform auth check: valid token did not map to a user, likely because no account is associated with the user any longer; continuing as u_anon"), event.WithInfo("token_id", at.GetPublicId()))
userData.User.Id = util.Pointer(globals.AnonymousUserId)
userData.Account.Id = nil
}
}
}
iamRepo, err := v.iamRepoFn()
if err != nil {
retErr = errors.Wrap(ctx, err, op, errors.WithMsg("failed to get iam repo"))
return
}
u, _, err := iamRepo.LookupUser(ctx, *userData.User.Id)
if err != nil {
retErr = errors.Wrap(ctx, err, op, errors.WithMsg("failed to lookup user"))
return
}
userData.User.Name = util.Pointer(u.Name)
userData.User.Email = util.Pointer(u.Email)
userData.User.FullName = util.Pointer(u.FullName)
if userData.Account.Id != nil && *userData.Account.Id != "" && v.passwordAuthRepoFn != nil && v.oidcAuthRepoFn != nil && v.ldapAuthRepoFn != nil {
const domain = "auth"
var acct auth.Account
var err error
switch globals.ResourceInfoFromPrefix(*userData.Account.Id).Subtype {
case password.Subtype:
repo, repoErr := v.passwordAuthRepoFn()
if repoErr != nil {
retErr = errors.Wrap(ctx, repoErr, op, errors.WithMsg("failed to get password auth repo"))
return
}
acct, err = repo.LookupAccount(ctx, *userData.Account.Id)
case oidc.Subtype:
repo, repoErr := v.oidcAuthRepoFn()
if repoErr != nil {
retErr = errors.Wrap(ctx, repoErr, op, errors.WithMsg("failed to get oidc auth repo"))
return
}
acct, err = repo.LookupAccount(ctx, *userData.Account.Id)
case ldap.Subtype:
repo, repoErr := v.ldapAuthRepoFn()
if repoErr != nil {
retErr = errors.Wrap(ctx, repoErr, op, errors.WithMsg("failed to get ldap auth repo"))
return
}
acct, err = repo.LookupAccount(ctx, *userData.Account.Id)
default:
retErr = errors.Wrap(ctx, err, op, errors.WithMsg("unrecognized account id type"))
return
}
if err != nil {
if errors.IsNotFoundError(err) {
retErr = errors.Wrap(ctx, err, op, errors.WithMsg("account doesn't exist"))
return
}
retErr = errors.Wrap(ctx, err, op, errors.WithMsg("error looking up account"))
return
}
userData.Account.Name = util.Pointer(acct.GetName())
userData.Account.Email = util.Pointer(acct.GetEmail())
userData.Account.LoginName = util.Pointer(acct.GetLoginName())
userData.Account.Subject = util.Pointer(acct.GetSubject())
}
// Look up scope details to return. We can skip a lookup when using the
// global scope
switch v.res.ScopeId {
case scope.Global.String():
scopeInfo = &scopes.ScopeInfo{
Id: scope.Global.String(),
Type: scope.Global.String(),
Name: scope.Global.String(),
Description: "Global Scope",
ParentScopeId: "",
}
default:
scp, err := iamRepo.LookupScope(v.ctx, v.res.ScopeId)
if err != nil {
retErr = errors.Wrap(ctx, err, op)
return
}
if scp == nil {
retErr = errors.New(ctx, errors.InvalidParameter, op, fmt.Sprint("non-existent scope $q", v.res.ScopeId))
return
}
scopeInfo = &scopes.ScopeInfo{
Id: scp.GetPublicId(),
Type: scp.GetType(),
Name: scp.GetName(),
Description: scp.GetDescription(),
ParentScopeId: scp.GetParentId(),
}
}
v.res.ParentScopeId = scopeInfo.ParentScopeId
// At this point we don't need to look up grants since it's automatically allowed
if v.requestInfo.TokenFormat == uint32(AuthTokenTypeRecoveryKms) {
aclResults.AuthenticationFinished = true
aclResults.Authorized = true
retErr = nil
return
}
var parsedGrants []perms.Grant
// Fetch and parse grants for this user ID (which may include grants for
// u_anon and u_auth)
iamOpt := []iam.Option{iam.WithRecursive(isRecursiveRequest)}
grantTuples, err = iamRepo.GrantsForUser(v.ctx, *userData.User.Id, resourceType, scopeInfo.Id, iamOpt...)
if err != nil {
retErr = errors.Wrap(ctx, err, op)
return
}
parsedGrants = make([]perms.Grant, 0, len(grantTuples))
// Note: Below, we always skip validation so that we don't error on formats
// that we've since restricted, e.g. "ids=foo;actions=create,read". These
// will simply not have an effect.
for _, tuple := range grantTuples {
permsOpts := []perms.Option{
perms.WithUserId(*userData.User.Id),
perms.WithSkipFinalValidation(true),
}
if userData.Account.Id != nil {
permsOpts = append(permsOpts, perms.WithAccountId(*userData.Account.Id))
}
parsed, err := perms.Parse(
ctx,
tuple,
permsOpts...)
if err != nil {
retErr = errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("failed to parse grant %#v", tuple.Grant)))
return
}
parsedGrants = append(parsedGrants, parsed)
}
retAcl = perms.NewACL(parsedGrants...)
aclResults = retAcl.Allowed(*v.res, v.act, *userData.User.Id)
// We don't set authenticated above because setting this but not authorized
// is used for further permissions checks, such as during recursive listing.
// So we want to make sure any code relying on that has the full set of
// grants successfully loaded.
aclResults.AuthenticationFinished = true
retErr = nil
return
}
// FetchActionSetForId returns the allowed actions for a given ID using the
// current set of ACLs and all other parameters the same (user, etc.)
func (r *VerifyResults) FetchActionSetForId(ctx context.Context, id string, availableActions action.ActionSet, opt ...Option) action.ActionSet {
return r.fetchActions(id, resource.Unknown, availableActions, opt...)
}
// FetchActionSetForType returns the allowed actions for a given collection type
// using the current set of ACLs and all other parameters the same (user, etc.)
func (r *VerifyResults) FetchActionSetForType(ctx context.Context, typ resource.Type, availableActions action.ActionSet, opt ...Option) action.ActionSet {
return r.fetchActions("", typ, availableActions, opt...)
}
func (r *VerifyResults) fetchActions(id string, typ resource.Type, availableActions action.ActionSet, opt ...Option) action.ActionSet {
switch {
case r.v.requestInfo.DisableAuthEntirely,
r.v.requestInfo.TokenFormat == uint32(AuthTokenTypeRecoveryKms):
// TODO: See if we can be better about what we return with the anonymous
// user and recovery KMS, perhaps given exclusionary options for each
return availableActions
}
// If there is no user ID set by definition there are no actions to fetch.
// This shouldn't happen because we should always fall back to at least the
// anonymous user so it's defense in depth.
if r.UserData.User.Id == nil {
return nil
}
opts := getOpts(opt...)
res := opts.withResource
// If not passed in, use what's already been populated through verification
if res == nil {
res = r.v.res
}
// If this is being called directly we may not have a resource yet
if res == nil {
res = new(perms.Resource)
}
if id != "" {
res.Id = id
}
if typ != resource.Unknown {
res.Type = typ
}
ret := make(action.ActionSet, len(availableActions))
for act := range availableActions {
if r.v.acl.Allowed(*res, act, *r.UserData.User.Id).Authorized {
ret.Add(act)
}
}
if len(ret) == 0 {
return nil
}
return ret
}
func (r *VerifyResults) FetchOutputFields(res perms.Resource, act action.Type) *perms.OutputFields {
var ret *perms.OutputFields
switch {
case r.v.requestInfo.TokenFormat == uint32(AuthTokenTypeRecoveryKms):
return ret.AddFields([]string{"*"})
case r.v.requestInfo.DisableAuthEntirely:
return ret
case r.UserData.User.Id == nil:
// If there is no user ID set by definition there are no actions to fetch.
// This shouldn't happen because we should always fall back to at least the
// anonymous user so it's defense in depth.
return ret
}
return r.v.acl.Allowed(res, act, *r.UserData.User.Id).OutputFields
}
// ACL returns the perms.ACL of the verifier.
func (r *VerifyResults) ACL() perms.ACL {
if r.v == nil {
return perms.ACL{}
}
return r.v.acl
}
// GetTokenFromRequest pulls the token from either the Authorization header or
// split cookies and parses it. If it cannot be parsed successfully, the issue
// is logged and we return blank, so logic will continue as the anonymous user.
// The public ID and _encrypted_ token are returned along with the token format.
func GetTokenFromRequest(ctx context.Context, kmsCache *kms.Kms, req *http.Request) (string, string, uint32) {
const op = "auth.GetTokenFromRequest"
// First, get the token, either from the authorization header or from split
// cookies
var receivedTokenType TokenFormat
var fullToken string
if authHeader := req.Header.Get("Authorization"); authHeader != "" {
headerSplit := strings.SplitN(strings.TrimSpace(authHeader), " ", 2)
if len(headerSplit) == 2 && strings.EqualFold(strings.TrimSpace(headerSplit[0]), "bearer") {
receivedTokenType = AuthTokenTypeBearer
fullToken = strings.TrimSpace(headerSplit[1])
}
}
if receivedTokenType != AuthTokenTypeBearer {
var httpCookiePayload string
var jsCookiePayload string
if hc, err := req.Cookie(handlers.HttpOnlyCookieName); err == nil {
httpCookiePayload = hc.Value
}
if jc, err := req.Cookie(handlers.JsVisibleCookieName); err == nil {
jsCookiePayload = jc.Value
}
if httpCookiePayload != "" && jsCookiePayload != "" {
receivedTokenType = AuthTokenTypeSplitCookie
fullToken = jsCookiePayload + httpCookiePayload
}
}
if receivedTokenType == AuthTokenTypeUnknown || fullToken == "" {
// We didn't find auth info or a client screwed up and put in a blank
// header instead of nothing at all, so return blank which will indicate
// the anonymous user
return "", "", uint32(AuthTokenTypeUnknown)
}
if strings.HasPrefix(fullToken, "r_") {
return "", fullToken, uint32(AuthTokenTypeRecoveryKms)
}
splitFullToken := strings.Split(fullToken, "_")
if len(splitFullToken) != 3 {
event.WriteError(ctx, op, stderrors.New("unexpected number of segments in token; continuing as anonymous user"), event.WithInfo("expected", 3, "found", len(splitFullToken)))
return "", "", uint32(AuthTokenTypeUnknown)
}
publicId := strings.Join(splitFullToken[0:2], "_")
encryptedToken := splitFullToken[2]
return publicId, encryptedToken, uint32(receivedTokenType)
}
// ScopesAuthorizedForList retrieves and returns all scopes where a user is authorized
// to perform a *list* action on. It looks recursively from `rootScopeId`.
func (r *VerifyResults) ScopesAuthorizedForList(ctx context.Context, rootScopeId string, resourceType resource.Type) (map[string]*scopes.ScopeInfo, error) {
const op = "auth.(VerifyResults).ScopesAuthorizedForList"
// Validation
switch {
case resourceType == resource.Unknown:
return nil, errors.New(ctx, errors.InvalidParameter, op, "unknown resource")
case r.v.iamRepoFn == nil:
return nil, errors.New(ctx, errors.InvalidParameter, op, "nil iam repo")
case rootScopeId == "":
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing root scope id")
case r.Scope == nil:
return nil, errors.New(ctx, errors.InvalidParameter, op, "nil scope in auth results")
}
repo, err := r.v.iamRepoFn()
if err != nil {
return nil, err
}
// Get all scopes recursively. Start at global because we need to take into
// account permissions in parent scopes even if they want to scale back the
// returned information to a child scope and its children.
scps, err := repo.ListScopesRecursively(ctx, scope.Global.String())
if err != nil {
return nil, err
}
var deferredScopes []*iam.Scope
var globalHasList bool // Store whether global has list permission
scopeResourceMap := make(map[string]*scopes.ScopeInfo) // Scope data per scope id
// For each scope, see if we have permission to list that resource in that scope
for _, scp := range scps {
scpId := scp.GetPublicId()
aSet := r.FetchActionSetForType(ctx,
resource.Unknown, // This is overridden by `WithResource` option.
action.NewActionSet(action.List),
WithResource(&perms.Resource{Type: resourceType, ScopeId: scpId, ParentScopeId: scp.GetParentId()}),
)
// We only expect the action set to be nothing, or list. In case
// this is not the case, we bail out.
switch {
case len(aSet) == 0:
// Defer until we've read all scopes. We do this because if the
// ordering coming back isn't in parent-first ordering our map
// lookup might fail.
deferredScopes = append(deferredScopes, scp)
case len(aSet) == 1 || r.UserId == globals.RecoveryUserId:
if !aSet.HasAction(action.List) {
return nil, errors.New(ctx, errors.Internal, op, "unexpected action in set")
}
if scopeResourceMap[scpId] == nil {
scopeResourceMap[scpId] = &scopes.ScopeInfo{
Id: scp.GetPublicId(),
Type: scp.GetType(),
Name: scp.GetName(),
Description: scp.GetDescription(),
ParentScopeId: scp.GetParentId(),
}
}
if scpId == scope.Global.String() {
globalHasList = true
}
default:
return nil, errors.New(ctx, errors.Internal, op, "unexpected number of actions back in set")
}
}
// Now go through the deferred scopes and see if a parent matches
for _, scp := range deferredScopes {
// If they had list on global scope anything else is automatically
// included; otherwise if they had list on the parent scope, this
// scope is included in the map and is sufficient here.
if globalHasList || scopeResourceMap[scp.GetParentId()] != nil {
scpId := scp.GetPublicId()
if scopeResourceMap[scpId] == nil {
scopeResourceMap[scpId] = &scopes.ScopeInfo{
Id: scp.GetPublicId(),
Type: scp.GetType(),
Name: scp.GetName(),
Description: scp.GetDescription(),
ParentScopeId: scp.GetParentId(),
}
}
}
}
// Elide out any that aren't under the root scope id
elideScopes := make([]string, 0, len(scopeResourceMap))
for scpId, scp := range scopeResourceMap {
switch rootScopeId {
// If the root is global, it matches
case scope.Global.String():
// If the current scope matches the root, it matches
case scpId:
// Or if the parent of this scope is the root (for orgs that would mean
// a root scope ID which is covered in the case above, so this is really
// projects matching an org used as the root)
case scp.ParentScopeId:
default:
elideScopes = append(elideScopes, scpId)
}
}
for _, scpId := range elideScopes {
delete(scopeResourceMap, scpId)
}
// If we have nothing in scopeInfoMap at this point, we aren't authorized
// anywhere so return 403.
if len(scopeResourceMap) == 0 {
return nil, handlers.ForbiddenError()
}
return scopeResourceMap, nil
}
// GrantsHash returns a stable hash of all the grants in the verify results.
func (r *VerifyResults) GrantsHash(ctx context.Context) ([]byte, error) {
return r.grants.GrantHash(ctx)
}
// GetRequestInfo extracts the request info stored in the context, if it exists.
// This returns nil, false if the request info could not be found.
func GetRequestInfo(ctx context.Context) (*authpb.RequestInfo, bool) {
v, ok := ctx.Value(verifierKey).(*verifier)
if !ok {
return nil, false
}
return v.requestInfo, true
}