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

Tonic tls termination #146

Closed
Closed
Changes from 1 commit
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
cfda00e
chore: adding initial TODO file to track the work we want to cover
sminez Sep 16, 2024
cde272c
refactor: restructuring as a monorepo for the rewrite
sminez Sep 23, 2024
be8efd5
refactor: tidying up stub crate structure for ratings_new
sminez Sep 23, 2024
a4b47ef
chore: update dependencies and enable features
d-loose Sep 30, 2024
500772b
chore: copy sql migrations from ratings crate
d-loose Sep 30, 2024
982a4cf
chore: copy config and migrator utils from ratings crate
d-loose Sep 30, 2024
d5f2812
feat: setup database
d-loose Sep 30, 2024
c548a0e
chore: copy common UserError from ratings crate
d-loose Sep 30, 2024
ccdb6c4
feat: add User struct + db methods
d-loose Sep 30, 2024
5108f9a
feat: setup Vote struct + db methods
d-loose Sep 30, 2024
770460a
test: test User + Vote roundtrip
d-loose Sep 30, 2024
b061c41
feat: add get_all_by_user to Vote and delete to User
d-loose Sep 30, 2024
9ed5867
refactor(db): remove `Unknown` user error
d-loose Sep 30, 2024
0d17df9
fix: remove modules from main.rs
d-loose Sep 30, 2024
fff4135
refactor: `UserError`->`DbError`, remove box errors where possible
d-loose Sep 30, 2024
91dedc2
refactor: cleanup imports
d-loose Sep 30, 2024
ce03e24
refactor: use OnceCell for Config
d-loose Sep 30, 2024
2df134e
refactor: add db::Result type
d-loose Sep 30, 2024
c80bdaf
refactor: rename PgConnection arg, move to end
d-loose Sep 30, 2024
3a653e3
feat: remove FromRow trait from User
d-loose Sep 30, 2024
be93383
fix: remove redundant get_all_by_user method
d-loose Sep 30, 2024
7d1bd69
feat: add categories module
d-loose Nov 19, 2024
9e381da
refactor: remove `Categories` namespace
d-loose Nov 20, 2024
422c063
Merge pull request #132 from ubuntu/db-layer
sminez Nov 20, 2024
80e5d22
feat: stubbing out the application layer
sminez Nov 20, 2024
67a437f
fix: ensuring that db tests only run when explicitly requested
sminez Nov 20, 2024
3ad624d
fix: remove internal Arcs from context
sminez Nov 20, 2024
e509cfd
Merge pull request #143 from ubuntu/rewrite-application-layer
sminez Nov 20, 2024
3df21e9
ci: codecov is not a failure in ci
matthew-hagemann Oct 16, 2024
eeeaae9
chore: adding initial TODO file to track the work we want to cover
sminez Sep 16, 2024
ad01227
refactor: restructuring as a monorepo for the rewrite
matthew-hagemann Nov 20, 2024
9d6db6b
refactor: tidying up stub crate structure for ratings_new
matthew-hagemann Nov 20, 2024
4254257
feat: export proto types in ratings_new
matthew-hagemann Oct 3, 2024
441c01b
fix: temporarly make methods in ratings pub for use in ratings_new re…
matthew-hagemann Oct 3, 2024
fa60c1b
feat: merge use_cases and services into grpc file
matthew-hagemann Nov 20, 2024
16049a5
feat: migrate authenticate request
matthew-hagemann Oct 4, 2024
0ea57fc
feat: migrate all user use_cases to grpc layer
matthew-hagemann Oct 4, 2024
70c7435
style: move helpers to bottom of file
matthew-hagemann Oct 7, 2024
5343ae6
feat: move jwt auth into ratings_new
matthew-hagemann Oct 8, 2024
8005d99
refactor: working on moving auth into ratings_new
matthew-hagemann Nov 20, 2024
16da50e
refactor: feedback from pairing session
matthew-hagemann Nov 20, 2024
274da3b
refactor: merging charts files into grpc/charts.rs
matthew-hagemann Nov 20, 2024
ebe385b
feat: log error before losing detail in chart.rs
matthew-hagemann Nov 20, 2024
04bbf7e
fix: add strum dependency
matthew-hagemann Nov 20, 2024
3ae08db
style: run the formatter
matthew-hagemann Nov 20, 2024
7efa346
refactor: merging ratings files into grpc/app.rs
matthew-hagemann Nov 20, 2024
5ca6a99
chore: additional post rebase cleanup
matthew-hagemann Nov 20, 2024
7809059
refactor: swap AppContext for Context in gPRC layer
matthew-hagemann Nov 20, 2024
c2f1064
refactor: user gRPC layer to use ratings_new functions
matthew-hagemann Nov 21, 2024
8ee2d59
refactor: chart gRPC layer to use ratings_new functions
matthew-hagemann Nov 21, 2024
637d75f
chore: re-adding tests accidentally deleted in rebase
matthew-hagemann Nov 21, 2024
f834f35
chore: removing auth.rs in favor of context.rs
matthew-hagemann Nov 21, 2024
f260583
refactor: make use of db types and common error where possible
matthew-hagemann Nov 21, 2024
93d2ddd
style: run the formatter
matthew-hagemann Nov 21, 2024
13cf86f
refactor: use conn!() macro, add FIXME's
matthew-hagemann Nov 21, 2024
4e9a392
Merge pull request #142 from matthew-hagemann/grpc-layer-refactor
sminez Nov 21, 2024
4a01fd8
refactor: wiring in the application layer
sminez Nov 21, 2024
910337a
fix: simplifying use of config
sminez Nov 21, 2024
ed61a26
refactor: simplifying match
sminez Nov 21, 2024
4571f30
Merge pull request #144 from ubuntu/rewrite-wiring-up
sminez Nov 21, 2024
571481a
refactor: copying proto files and tests into ratings_new so we can ru…
sminez Nov 22, 2024
65c4235
chore: updating docker-compose.yml to run the new version of the server
sminez Nov 22, 2024
2cde744
feat: adding in auth and server init
sminez Nov 22, 2024
f2590aa
fix: removing duplicated generated files
sminez Nov 22, 2024
07d8230
fix: removing duplicated Cargo.lock
sminez Nov 22, 2024
e5802f3
fix: updating gitignore
sminez Nov 22, 2024
f3f85a0
wip: tls termination
matthew-hagemann Nov 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor: swap AppContext for Context in gPRC layer
matthew-hagemann committed Nov 20, 2024

Verified

This commit was signed with the committer’s verified signature.
matthew-hagemann Matthew Hagemann
commit 7809059e8554eff2b937824ddd815f4b7cafdb2f
8 changes: 3 additions & 5 deletions crates/ratings_new/src/grpc/app.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
//! Contains generation and definitions for the [`AppService`]
use crate::proto::app::app_server::AppServer;
use crate::{proto::app::app_server::AppServer, Context};
use crate::proto::common::Rating;

//replace
use ratings::{app::AppContext, features::common::entities::Rating as OldRating};
use ratings::features::rating::infrastructure::get_votes_by_snap_id;
use tracing::error;

use crate::proto::app::{app_server::App, GetRatingRequest, GetRatingResponse};
@@ -37,7 +35,7 @@ impl App for RatingService {
&self,
request: Request<GetRatingRequest>,
) -> Result<tonic::Response<GetRatingResponse>, Status> {
let app_ctx = request.extensions().get::<AppContext>().unwrap().clone();
let ctx = request.extensions().get::<Context>().unwrap().clone();

let GetRatingRequest { snap_id } = request.into_inner();

@@ -47,7 +45,7 @@ impl App for RatingService {

// let result = use_cases::get_rating(&app_ctx, snap_id).await;

match get_votes_by_snap_id(&app_ctx, &snap_id).await {
match get_votes_by_snap_id(&ctx, &snap_id).await {
Ok(votes) => {
let rating = OldRating::new(votes);
let payload = GetRatingResponse {
17 changes: 4 additions & 13 deletions crates/ratings_new/src/grpc/charts.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
//! Definitions and utilities for building the [`ChartService`] for using the [`Chart`] feature.
//!
//! [`Chart`]: crate::features::chart::entities::Chart
use crate::proto::chart::{
use crate::{proto::chart::{
chart_server::{Chart, ChartServer},
ChartData, GetChartRequest, GetChartResponse,
};
use ratings::features::chart::{
entities::{Chart as OldChart, ChartData as OldChartData},
errors::ChartError,
};
}, Context};

use tonic::{Request, Response, Status};
use tracing::error;

use ratings::{
app::AppContext, features::chart::infrastructure::get_votes_summary,
features::pb::chart::Category, features::pb::chart::Timeframe,
};

/// An empty struct denoting that allows the building of a [`ChartServer`].
#[derive(Copy, Clone, Debug, Default)]
pub struct ChartService;
@@ -45,7 +36,7 @@ impl Chart for ChartService {
&self,
request: Request<GetChartRequest>,
) -> Result<Response<GetChartResponse>, Status> {
let app_ctx = request.extensions().get::<AppContext>().unwrap().clone();
let ctx = request.extensions().get::<Context>().unwrap().clone();

let GetChartRequest {
timeframe,
@@ -62,7 +53,7 @@ impl Chart for ChartService {

let timeframe = Timeframe::try_from(timeframe).unwrap_or(Timeframe::Unspecified);

let result = get_votes_summary(&app_ctx, timeframe, category)
let result = get_votes_summary(&ctx, timeframe, category)
.await
.map_err(|error| {
error!("{error:?}");
50 changes: 20 additions & 30 deletions crates/ratings_new/src/grpc/user.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::Context;

use crate::proto::user::{
user_server::{User, UserServer},
AuthenticateRequest, AuthenticateResponse, GetSnapVotesRequest, GetSnapVotesResponse,
@@ -7,18 +9,6 @@ use time::OffsetDateTime;
use tonic::{Request, Response, Status};
use tracing::{error, warn};

use ratings::{
app::AppContext,
features::user::{
entities::{User as OldUser, Vote as OldVote},
infrastructure::{
create_or_seen_user, delete_user_by_client_hash, find_user_votes,
get_snap_votes_by_client_hash, save_vote_to_db, update_categories,
},
},
utils::jwt::Claims,
};

/// The length we expect a client hash to be, in bytes
pub const EXPECTED_CLIENT_HASH_LENGTH: usize = 64;

@@ -53,9 +43,9 @@ impl User for UserService {
&self,
mut request: Request<AuthenticateRequest>,
) -> Result<Response<AuthenticateResponse>, Status> {
let app_ctx = request
let ctx = request
.extensions_mut()
.remove::<AppContext>()
.remove::<Context>()
.expect("Expected AppContext to be present");
let AuthenticateRequest { id } = request.into_inner();

@@ -69,9 +59,9 @@ impl User for UserService {

let user = OldUser::new(&id);

match create_or_seen_user(&app_ctx, user).await {
Ok(user) => app_ctx
.jwt_encoder()
match create_or_seen_user(&ctx, user).await {
Ok(user) => ctx
.jwt_encoder
.encode(user.client_hash)
// Match on the encode, build the ok / error varients in there, out of a chain.
.map(|token| AuthenticateResponse { token })
@@ -84,15 +74,15 @@ impl User for UserService {
#[tracing::instrument(level = "debug", skip_all)] // skip_all skips logging what all the
// arguments were
async fn delete(&self, mut request: Request<()>) -> Result<Response<()>, Status> {
let app_ctx = request
let ctx = request
.extensions_mut()
.remove::<AppContext>()
.remove::<Context>()
.expect("Expected AppContext to be present");
let Claims {
sub: client_hash, ..
} = claims(&mut request);

match delete_user_by_client_hash(&app_ctx, &client_hash).await {
match delete_user_by_client_hash(&ctx, &client_hash).await {
// FIXME (maybe?)
// favor a into pattern if we do this in many places
Ok(_) => Ok(Response::new(())),
@@ -105,9 +95,9 @@ impl User for UserService {

#[tracing::instrument(level = "debug", skip_all)]
async fn vote(&self, mut request: Request<VoteRequest>) -> Result<Response<()>, Status> {
let app_ctx = request
let ctx = request
.extensions_mut()
.remove::<AppContext>()
.remove::<Context>()
.expect("Expected AppContext to be present");
let Claims {
sub: client_hash, ..
@@ -127,7 +117,7 @@ impl User for UserService {
.await
.inspect_err(|e| warn!("{}", e));

match save_vote_to_db(&app_ctx, vote).await {
match save_vote_to_db(&ctx, vote).await {
Ok(_) => Ok(Response::new(())),

Err(e) => {
@@ -142,9 +132,9 @@ impl User for UserService {
&self,
mut request: Request<ListMyVotesRequest>,
) -> Result<Response<ListMyVotesResponse>, Status> {
let app_ctx = request
let ctx = request
.extensions_mut()
.remove::<AppContext>()
.remove::<Context>()
.expect("Expected AppContext to be present");
let Claims {
sub: client_hash, ..
@@ -156,7 +146,7 @@ impl User for UserService {
Some(snap_id_filter)
};

let result = find_user_votes(&app_ctx, client_hash, snap_id_filter).await;
let result = find_user_votes(&ctx, client_hash, snap_id_filter).await;

match result {
Ok(votes) => {
@@ -178,9 +168,9 @@ impl User for UserService {
mut request: Request<GetSnapVotesRequest>,
) -> Result<Response<GetSnapVotesResponse>, Status> {
// FIXME: will turn into con macro
let app_ctx = request
let ctx = request
.extensions_mut()
.remove::<AppContext>()
.remove::<Context>()
.expect("Expected AppContext to be present");
let Claims {
sub: client_hash, ..
@@ -189,11 +179,11 @@ impl User for UserService {
let GetSnapVotesRequest { snap_id } = request.into_inner();

// Ignore but log warning, it's not fatal
let _ = update_categories(&snap_id, &app_ctx)
let _ = update_categories(&snap_id, &ctx)
.await
.inspect_err(|e| warn!("{}", e));

let result = get_snap_votes_by_client_hash(&app_ctx, snap_id, client_hash).await;
let result = get_snap_votes_by_client_hash(&ctx, snap_id, client_hash).await;

match result {
Ok(votes) => {