Skip to content
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

Deal with various auth token types #247

Merged
merged 1 commit into from
Aug 1, 2023
Merged

Conversation

tgeoghegan
Copy link
Contributor

As of 1, the Janus aggregator API is explicit about which kind of authentication token it uses (Bearer or DapAuth) and also generates bearer tokens by default. This PR adopts changes to the aggregator API message definitions.

Copy link
Contributor

@jbr jbr left a comment

Choose a reason for hiding this comment

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

lgtm, one nit (typ)

Copy link
Contributor

Choose a reason for hiding this comment

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

Not related to this PR: This file is dead code and should have been removed in #246. No need to modify it in this PR, but it also doesn't hurt anything

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I was wondering about that when I had to fix up both this file and src/api_mocks/aggregator_api.rs! Let's nuke the file in a separate PR.

pub struct AuthenticationToken {
/// Type of the authentication token. Always "Bearer" in divviup-api.
#[serde(rename = "type")]
typ: String,
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit:

Suggested change
typ: String,
r#type: String,

avoids having to rename (and the awkward typ)

If you don't like that, maybe call it token_type in rust and rename to "type" in serde?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, cool, I didn't know about this bit of syntax. I'm happy to use r#type in this project if you like it better.

@@ -102,6 +103,8 @@ pub mod collector_auth_tokens {
let leader = task.leader_aggregator(&db).await?;
let client = leader.client(client);
let task_response = client.get_task(&task.id).await?;
Ok(Json([task_response.collector_auth_token]))
Ok(Json([task_response
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm surprised this is what rustfmt likes but who am I to second-guess the linter

@jbr
Copy link
Contributor

jbr commented Jun 29, 2023

Not necessarily in this PR, but should we have code that checks the token type and errors if it's not Bearer? Does our api specification allow for anything other than Bearer? If not, why not just call the fields aggregator_bearer_token and collector_bearer_token and avoid the complexity?

@tgeoghegan
Copy link
Contributor Author

Not necessarily in this PR, but should we have code that checks the token type and errors if it's not Bearer? Does our api specification allow for anything other than Bearer? If not, why not just call the fields aggregator_bearer_token and collector_bearer_token and avoid the complexity?

In the future, we may need to support partner aggregators that implement all sorts of different authentication schemes that aren't necessarily bearer tokens. For example, Daphne currently only supports the Dap-Auth-Token form. So formulating the API this way is my meager attempt at future-proofing, though divviup-api would need more work to support that sort of thing. For example, the representation of a partner aggregator would have to indicate what authentication mode it uses.

@jbr
Copy link
Contributor

jbr commented Jun 29, 2023

In that case it seems like we probably should error out if we encounter a token type that's not Bearer? #248

@tgeoghegan
Copy link
Contributor Author

Let's hold off on merging this until the corresponding Janus change lands. I'll take care of merging when appropriate (and rebase as needed).

@@ -101,12 +101,33 @@ impl From<Option<i64>> for QueryType {
}
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct AuthenticationToken {
Copy link
Contributor

Choose a reason for hiding this comment

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

Note that we use a Serde struct for this object here, and an internally-tagged enum in Janus. In follow-up work, I think it might make sense to treat the authentication token as an opaque JSON blob.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh! In thinking this through I realized we never need to use these tokens in this repo, only pass them between servers? In that case I can close #248 and I agree with this comment. Unless divviup-api needs to be the owner of acceptable tokens, we could just pass around a serde_json::Value

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That would work for the aggregator auth tokens, which are opaque to divviup-api, but not necessarily for collector auth tokens. The tension here is that the Janus aggregator API can express multiple token types, hence the { "type": <>, "token": <> } structure, but divviup-api represents collector auth tokens as a string which implicitly should be used as Bearer tokens. So when providing collector auth tokens to subscribers, divviup-api needs to extract the token field of the AuthenticationToken structure.

We could make this less awkward but using the AuthenticationToken representation in the API divviup-api offers to its clients.

@jbr
Copy link
Contributor

jbr commented Jul 28, 2023

Do we need to validate anything about the bearer token here? We are going to check if it works, which matters more than the format


#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
pub struct CollectorAuthenticationToken {
pub r#type: String,
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this currently always expected to be Bearer? Could this be an internally-tagged enum?

@tgeoghegan
Copy link
Contributor Author

Do we need to validate anything about the bearer token here? We are going to check if it works, which matters more than the format

I think that the aggregator capability/config query you're wiring up in divviup/janus#1646 / #340 is sufficient validation that the token is well formed and valid.

Copy link
Contributor

Choose a reason for hiding this comment

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

Are we at a point where we need to do this? The approach taken by #302 is just to delete all aggregators. If we need to do this, we probably also should do so in #302

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we're planning to blow away some database tables anyway, then yeah, let's skip the migrations. Let's talk tomorrow (Tuesday) about a deployment plan since we have a bunch of disruptive changes going out this week.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Based on discussion in Slack today, our plan is to delete all existing aggregators from the staging deploy of divviup-api before we deploy this change and #302, and then re-create any aggregators as needed, eliminating the need to migrate existing data. Accordingly, I've rolled back the commit that added the DB migration (and rebased for good measure).

@tgeoghegan tgeoghegan force-pushed the timg/agg-api-bearer-token branch 2 times, most recently from 9b7d350 to d86536f Compare August 1, 2023 17:37
As of [1], the Janus aggregator API is explicit about which kind of
authentication token it uses (`Bearer` or `DapAuth`) and also generates
bearer tokens by default. This PR adopts changes to the aggregator API
message definitions.

[1]: divviup/janus#1548
@tgeoghegan tgeoghegan merged commit fa6950e into main Aug 1, 2023
6 checks passed
@tgeoghegan tgeoghegan deleted the timg/agg-api-bearer-token branch August 1, 2023 23:00
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.

3 participants