Skip to content

feat: add session actor middleware #897

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 11 commits into from
Closed

feat: add session actor middleware #897

wants to merge 11 commits into from

Conversation

deansheather
Copy link
Member

@deansheather deansheather commented Apr 6, 2022

Adds the base actor interface types (without RBAC methods for now, Steven will add them later):

  • Actor
  • SystemActor
  • AnonymousActor
  • UserActor

And their basic implementations. Also adds middleware for extracting the actor from the request, which is implemented for UserActor and falls back to AnonymousActor if no cookie was set.

The existing apikey middleware was left in, and I will be making the transition to in a separate PR. I removed the OIDC/Oauth2 code when I copied the apikey middleware across and replaced it with a TODO.

#782

@deansheather deansheather requested review from Emyrk and kylecarbs April 6, 2022 21:12
@deansheather
Copy link
Member Author

Rationale for access/session import path is that I want to group together authentication, authorization and session code in a single parent package. Steven is onboard with this too

@codecov
Copy link

codecov bot commented Apr 6, 2022

Codecov Report

Merging #897 (dadef23) into main (4c1ef38) will increase coverage by 0.32%.
The diff coverage is 83.43%.

@@            Coverage Diff             @@
##             main     #897      +/-   ##
==========================================
+ Coverage   65.92%   66.25%   +0.32%     
==========================================
  Files         228      231       +3     
  Lines       14126    14293     +167     
  Branches      105      105              
==========================================
+ Hits         9313     9470     +157     
- Misses       3868     3877       +9     
- Partials      945      946       +1     
Flag Coverage Δ
unittest-go-macos-latest 53.78% <83.43%> (+0.56%) ⬆️
unittest-go-postgres- 66.10% <83.43%> (+0.19%) ⬆️
unittest-go-ubuntu-latest 55.98% <83.43%> (+0.46%) ⬆️
unittest-go-windows-2022 52.90% <83.43%> (+0.32%) ⬆️
unittest-js 58.25% <ø> (ø)
Impacted Files Coverage Δ
coderd/httpmw/actor.go 71.42% <71.42%> (ø)
coderd/access/session/user.go 81.25% <81.25%> (ø)
coderd/access/session/mw.go 100.00% <100.00%> (ø)
coderd/coderd.go 97.35% <100.00%> (+0.21%) ⬆️
peerbroker/dial.go 77.04% <0.00%> (-6.56%) ⬇️
provisioner/echo/serve.go 54.40% <0.00%> (-2.40%) ⬇️
coderd/provisionerdaemons.go 62.56% <0.00%> (ø)
provisionerd/provisionerd.go 80.76% <0.00%> (ø)
coderd/database/queries.sql.go 84.19% <0.00%> (+0.21%) ⬆️
peerbroker/proxy.go 58.13% <0.00%> (+0.58%) ⬆️
... and 4 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4c1ef38...dadef23. Read the comment docs.

@greyscaled greyscaled changed the title feat(cdr): add session actor types feat: add session actor types Apr 6, 2022
Copy link
Member

@kylecarbs kylecarbs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this is finished, so I won't be a complete stickler. I want to point out in it's current form, this violates the no unused packages rule, which makes this a lot harder to thoroughly review.

@deansheather deansheather requested a review from kylecarbs April 7, 2022 23:25
Copy link
Member

@kylecarbs kylecarbs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be helpful to understand the interaction of RBAC with the Actor. I think I get it, but I'm going to write it out to ensure I can give a thorough review:

  1. Actor interface gets sent into RBAC.
  2. RBAC uses the ID() function to abstractly lookup roles.
  3. That way all authentication types group into a single interface!

I see how we'd add actor types in the future and they'd be covered in this system, but would a function that returned a UUID for a resource perform similarly?

Comment on lines +22 to +26
// ID is the unique ID of the actor for logging purposes.
ID() string
// Name is a friendly, but consistent, name for the actor for logging
// purposes. E.g. "deansheather"
Name() string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this either be system or the user's name? Seems like certain routes should never be system, so I'm confused when the logging would come into play.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On each request I want to log the actor name at least, so it's easy to see which user made a request in the logs. I am removing the SystemActor though.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this ok @kylecarbs? The goal of this is so we can log the username without having to do a type cast because of the same reason as above (type switching just to read the username is very slow).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're just going to have the UserActor do we need to generalize the actor type at all? Could we just use database.User directly? That way we wouldn't have to type-cast at all.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're not just going to have the UserActor type. We will also have a WorkspaceActor and eventually a SatelliteActor.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we just have two or three though, could we just log the username / workspace / satellite name separately? If the actor type is primarily going to be used for that, I'm hesitant to say it's worth the grouping.

I'd think we'd have separate routes for users / workspaces / satellites anyways, similar to how agents can only access specific routes right now. In that pattern, there's no opportunity to have a user call an agent route, because they'll never need to do so.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is one way to separate things. But that makes each actor type more distinct.

RBAC means an agent token is just a UserActor with a reduced scope. I see the benefit in having 1 type and letting rbac decide what they can and can't do.

I see value in the separating routes approach too.

Both options require their own care. One thing I wonder about is will a provisionerd ever need to query coderd for anything? Like is there a case some provisionerd will want a system_user with some RBAC role/perms to query some extra endpoints? I have no idea if there is, but having 1 system is nice in the sense all actors are equivalent and just defer to rbac for drawing lines.

Comment on lines +60 to +61
// You should probably be calling session.ExtractActor as a middleware, or
// session.RequestActor instead.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is true, could we keep this function private?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No because then the linter got mad at me for having the tests be package session, and I have test helpers shared between the user tests and the middleware tests :(

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't testing the middleware test this function in that case? That's how it works in httpmw right now too.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to write tests for this function by itself instead of having a giant test file for the middleware when the middleware itself is very small.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our tests may be deceiving to a caller if they primarily test and exposed a function we don't recommend they use. The middleware is only valuable if it calls this other function, so I'd be hesitant to say the middleware's expectations are small.

That's not to say decomposition isn't right for this case, but I do think our tests should primarily test the primary functions we expose.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll do the internal tests for the entire package in that case then

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this ok? @kylecarbs

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's best to test the HTTP handler that's being exposed to the API; not being able to test that handler will likely result in a similar experience for callers. Is there a reason we can't test the handler with similar levels of functionality?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The middleware handler is tested to handle all possible output states from the user actor function. The user actor tests additionally also test each possible failure state individually.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kylecarbs I disagree that we should only test a package api imo.

I am all for testing individual functions in a package. I understand unit tests can help api design, and those tests should still exist in the xxxx_test.go file. But having internal tests to test more granular functions is a great way to test edge cases without excess state.

If the internal test fails, that is way more information for debugging than if an api level function that hides the internal functions fails.


So I think internal tests are great. But should not replace package_test functions that better test pkg api design

Copy link
Member

@johnstcn johnstcn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like the comprehensive unit tests here.

Comment on lines +60 to +61
// You should probably be calling session.ExtractActor as a middleware, or
// session.RequestActor instead.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@deansheather you can do user_internal_test.go for package session tests

}

// UserActorFromRequest tries to get a UserActor from the API key supplied in
// the request cookies. If the cookie doesn't exist, nil is returned. If there
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a note in the future we need to also support a custom Header for cli auth. Cookie auth will need to do CSRF in future.

@deansheather deansheather changed the title feat: add session actor types feat: add session actor middleware Apr 8, 2022
@deansheather deansheather requested a review from kylecarbs April 8, 2022 22:51
Comment on lines +22 to +26
// ID is the unique ID of the actor for logging purposes.
ID() string
// Name is a friendly, but consistent, name for the actor for logging
// purposes. E.g. "deansheather"
Name() string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is one way to separate things. But that makes each actor type more distinct.

RBAC means an agent token is just a UserActor with a reduced scope. I see the benefit in having 1 type and letting rbac decide what they can and can't do.

I see value in the separating routes approach too.

Both options require their own care. One thing I wonder about is will a provisionerd ever need to query coderd for anything? Like is there a case some provisionerd will want a system_user with some RBAC role/perms to query some extra endpoints? I have no idea if there is, but having 1 system is nice in the sense all actors are equivalent and just defer to rbac for drawing lines.

Comment on lines +10 to +13
"github.com/stretchr/testify/require"

"github.com/coder/coder/coderd/database/databasefake"
"github.com/coder/coder/coderd/httpapi"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious, are we still splitting coder and other github imports?

Comment on lines +60 to +61
// You should probably be calling session.ExtractActor as a middleware, or
// session.RequestActor instead.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kylecarbs I disagree that we should only test a package api imo.

I am all for testing individual functions in a package. I understand unit tests can help api design, and those tests should still exist in the xxxx_test.go file. But having internal tests to test more granular functions is a great way to test edge cases without excess state.

If the internal test fails, that is way more information for debugging than if an api level function that hides the internal functions fails.


So I think internal tests are great. But should not replace package_test functions that better test pkg api design

return NewUserActor(u, key), true
}

func parseAPIKey(apiKey string) (id, secret string, hashed []byte, err error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just and example @kylecarbs of a great function to unit test with an easy tabled test. To do this using the middleware would be such a pain because invalid apiKey strings might not even get far enough in the function to get to this parse.

Regardless if the invalid strings reach this in prod, I still think it's of value to know this function works as intended incase it's ever moved/reused.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this function is well-tested outside of the middleware, it could be argued the middleware doesn't have to be. If both are expected to be well-tested, then tests become duplicative. I'm of the opinion testing the user-expected behavior is much more important than if the code actually works, because the user just wants it to work for them.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The middleware unit test will not test this one function to all of it's edge cases. For one, the middleware does some basic validation above this usage, so any tokens that fall those checks won't make it to parseAPIKey.

The code might be able to be refactored to ensure all keys hit parseAPIKey, but I'm speaking more generally than this one case.

I'm of the opinion testing the user-expected behavior is much more important than if the code actually works, because the user just wants it to work for them.

I don't see how these are not related. These functions are the building blocks, and often they will be reused/adapted by a developer in the future (months out).

I am of the opinion if I write helper functions like this, it's very easy to build a tabled test to confirm my implementation is correct. And if someone comes in later, the tabled tests can often serve better than a comment for what should happen in given cases, because let's be honest sometimes comments are vague/ambiguous. A tabled test does not have this ambiguity. Worse case the tabled test is missing an edge case, in which case the new developer can easily add it.


I do agree if you sufficiently test all the building blocks of a function, then you do not need to test the, in this case, the middle function as exhaustively.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The functions build the API surface, but the most important part of the code is that the API works as expected. I'm not arguing against testing the internals, but we should try to get away with testing via externals first to reduce complexity.

Comment on lines +13 to +20
func RequestActor(r *http.Request) session.Actor {
return session.RequestActor(r)
}

// ExtractActor reexports session.ExtractActor for your convenience.
func ExtractActor(db database.Store) func(http.Handler) http.Handler {
return session.ExtractActor(db)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this also defined in the access package? why is there 2 ExtractActor functions?

@@ -76,6 +78,7 @@ func New(options *Options) (http.Handler, func()) {
})
r.Route("/files", func(r chi.Router) {
r.Use(
httpmw.RequireAuthentication(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do this at a top level? I'd hate to forget it in one of these routes.

@kylecarbs
Copy link
Member

Superseded by #1050

@kylecarbs kylecarbs closed this Apr 18, 2022
@kylecarbs kylecarbs deleted the actor-types branch April 18, 2022 15:06
@misskniss misskniss added this to the V2 Beta milestone May 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants