-
Notifications
You must be signed in to change notification settings - Fork 252
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
Cosmos DB - Authorization policy #325
Cosmos DB - Authorization policy #325
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was skeptical of the proposal, but it's not too bad. I definitely think it's better than having an Any
that service specific policies can downcast. I just had a bunch of small notes.
resource_link, | ||
&time, | ||
) | ||
}; | ||
self.prepare_request_with_signature(uri_path, http_method, &time, &auth) | ||
} | ||
|
||
/// Prepares' an `azure_core::Request`. | ||
/// Prepares' an `azure_core::Request`. This function will |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: not sure if we need to make the comment lines so short. Means we quickly have lots of lines of comments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a vim plugin that inserts a cr to keep comments < 80 chars wide (yes, I'm old 🤷♂️). I agree it doesn't really matter since Rust's lines can easily go over that limit. What should we use as a limit?
Thank you @rylev for taking the time to review this! I'll try to address your suggestions and push another commit. |
commit 79556d4 Author: Francesco Cogno <[email protected]> Date: Tue Jul 13 10:09:16 2021 +0200 docs on pipeline commit 4a95aad Author: Francesco Cogno <[email protected]> Date: Tue Jul 13 09:59:02 2021 +0200 R -> C generic commit cac15f7 Author: Francesco Cogno <[email protected]> Date: Tue Jul 13 09:52:55 2021 +0200 better comments commit f047a82 Author: Francesco Cogno <[email protected]> Date: Mon Jul 12 21:11:21 2021 +0200 docs commit 876191c Author: Francesco Cogno <[email protected]> Date: Mon Jul 12 18:52:27 2021 +0200 bag to contents commit 62b7c76 Author: Francesco Cogno <[email protected]> Date: Mon Jul 12 18:43:46 2021 +0200 data_lake commit eff9c2a Author: Francesco Cogno <[email protected]> Date: Mon Jul 12 18:23:04 2021 +0200 from str to TimeNonce commit 7b63386 Author: Francesco Cogno <[email protected]> Date: Wed Jul 7 19:09:24 2021 +0200 rewritten code commit 2ad67a2 Author: Francesco Cogno <[email protected]> Date: Tue Jul 6 18:46:21 2021 +0200 removed unwrap commit 7105dab Author: Francesco Cogno <[email protected]> Date: Tue Jul 6 18:14:57 2021 +0200 errors to lowercase
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MindFlavor looks good. Shall we merge?
Yes, let's keep the ball rolling! 👍 |
This PR enhances the pipeline by adding the
AuthorizationPolicy
and migrates the preexisting CosmosDB pipeline operations to the new policy.AuthorizationPolicy
The
AuthorizationPolicy
cannot be inCore
since every service uses a different authorization mechanism. So this PR proposes to have a specific policy in each module (they can potentially be shared in case of duplication). This PR covers Cosmos DB only. While CosmosDB does not support (yet) the bearer token authentication, it's possibile to authenticate specific users with shared key.The
AuthorizationPolicy
will handle the authentication by injecting the required headers to the request passing through the pipeline. Since the resulting authorization token can depend on the preexisting headers theAuthorizationPolicy
must be executed right before theTransportPolicy
. Also, since the token depends on the current timestamp (to mitigate reply attacks),AuthorizationPolicy
must be run at every retry.Cosmos context
In CosmosDB, the authorization token depends on the resource type. This information must be somehow passed through the pipeline because the
AuthorizationPolicy
needs it.The information of which resource to authenticate must be passed by the operation builder prior starting the pipeline. For example, the
create_database
operation, knowing that it has to work onResourceType::Databases
, must be able to passResourceType::Databases
to theAuthorizationPolicy
. The pipeline has aContext
struct that must be instantiated by the SDK user (ie the code that callscreate_database
). TheContext
will then traverse the pipeline. As such is a good candidate for the resource type information.For this reason, this PR proposes an additional type, called
PipelineContext
that wraps the user-suppliedContext
and adds the module-dependent field. This is achieved by exposing a generic (constrained to beSend + Sync
).An alternative approach (shown here: https://github.com/MindFlavor/azure-sdk-for-rust/tree/cosmos/auth_policy%2Fdev) is to use Rust's runtime polymorphism in the form of
std::any::Any
. BasicallyContext
will have aBox<dyn Any>
field that can accept anything. It would be responsibility of the operation builder (ie thecreate_database
function) to store the relevant information in theBox<dyn Any>
instance. TheAuthorizationPolicy
will thendowncast_ref()
theAny
instance and get the expected type.While this approach is easy to do, it lacks any kind of type safety and relies upon conventions (which is very unidiomatic in Rust). Also, it's runtime enforced only (another thing unidiomatic in Rust).
The approach proposed in this PR, instead, is type safe and clearly self-explanatory.
Notable changes
PipelineContext<BAG>
discussed above (composition ofContext
and custom field of typeBAG
).Policy
trait to be generic on theBAG
above. This allows policies that accept genericPipelineContext<BAG>
.BAG
above.BAG
generic (calledR
for brevity, this is something we might want to correct to avoid confusion).Policy<CosmosContext>
forAuthorizationPolicy
.AuthorizationPolicy
(and to send the properPipelineContext<CosmosContext>
down the pipeline.cosmos_client::prepare_request2
.cosmos_client
calledprepare_request_pipeline
. This function will no longer perform authentication since it will delegated to the pipeline policyAuthorizationPolicy
.CosmosClient
toAuthorizationPolicy
(preexisting non-pipeline code has been rerouted pending removal).uninmplemented
fromadd_as_header2
that caused E2E tests to fail (fix CosmosDB E2E attachment tests fail withnot implemented
panic #324).add_as_header2
for all the required types.