Skip to content
This repository has been archived by the owner on Aug 16, 2023. It is now read-only.

Commit

Permalink
Restrict working only with compliant janus API version (#219)
Browse files Browse the repository at this point in the history
  • Loading branch information
feymartynov committed Feb 2, 2021
1 parent 5bb4217 commit 3534b64
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE janus_backend DROP COLUMN api_version;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE janus_backend ADD COLUMN api_version TEXT NOT NULL DEFAULT 'v1';
8 changes: 5 additions & 3 deletions src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use svc_authz::cache::{Cache as AuthzCache, ConnectionPool as RedisConnectionPoo
use crate::app::context::GlobalContext;
use crate::app::error::{Error as AppError, ErrorKind as AppErrorKind};
use crate::app::metrics::{DynamicStatsCollector, StatsRoute};
use crate::backend::janus::Client as JanusClient;
use crate::backend::janus::{Client as JanusClient, JANUS_API_VERSION};
use crate::config::{self, Config, KruonisConfig};
use crate::db::ConnectionPool;
use context::{AppContext, JanusTopics};
Expand Down Expand Up @@ -181,7 +181,8 @@ fn subscribe(agent: &mut Agent, agent_id: &AgentId, config: &Config) -> Result<J
.context("Error subscribing to multicast requests")?;

// Janus status events
let subscription = Subscription::broadcast_events(&config.backend.id, API_VERSION, "status");
let subscription =
Subscription::broadcast_events(&config.backend.id, JANUS_API_VERSION, "status");

agent
.subscribe(&subscription, QoS::AtLeastOnce, Some(&group))
Expand All @@ -192,7 +193,8 @@ fn subscribe(agent: &mut Agent, agent_id: &AgentId, config: &Config) -> Result<J
.context("Error building janus events subscription topic")?;

// Janus events
let subscription = Subscription::broadcast_events(&config.backend.id, API_VERSION, "events");
let subscription =
Subscription::broadcast_events(&config.backend.id, JANUS_API_VERSION, "events");

agent
.subscribe(&subscription, QoS::AtLeastOnce, Some(&group))
Expand Down
16 changes: 13 additions & 3 deletions src/db/janus_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use diesel::result::Error;
use svc_agent::AgentId;
use uuid::Uuid;

use crate::backend::janus::JANUS_API_VERSION;
use crate::schema::janus_backend;

pub(crate) type AllColumns = (
Expand All @@ -13,6 +14,7 @@ pub(crate) type AllColumns = (
janus_backend::created_at,
janus_backend::capacity,
janus_backend::balancer_capacity,
janus_backend::api_version,
);

pub(crate) const ALL_COLUMNS: AllColumns = (
Expand All @@ -22,6 +24,7 @@ pub(crate) const ALL_COLUMNS: AllColumns = (
janus_backend::created_at,
janus_backend::capacity,
janus_backend::balancer_capacity,
janus_backend::api_version,
);

////////////////////////////////////////////////////////////////////////////////
Expand All @@ -35,6 +38,7 @@ pub(crate) struct Object {
created_at: DateTime<Utc>,
capacity: Option<i32>,
balancer_capacity: Option<i32>,
api_version: String,
}

impl Object {
Expand Down Expand Up @@ -129,6 +133,7 @@ pub(crate) struct UpsertQuery<'a> {
session_id: i64,
capacity: Option<i32>,
balancer_capacity: Option<i32>,
api_version: String,
}

impl<'a> UpsertQuery<'a> {
Expand All @@ -139,6 +144,7 @@ impl<'a> UpsertQuery<'a> {
session_id,
capacity: None,
balancer_capacity: None,
api_version: JANUS_API_VERSION.to_string(),
}
}

Expand Down Expand Up @@ -236,17 +242,19 @@ const MOST_LOADED_SQL: &str = r#"
LEFT JOIN room AS r2
ON 1 = 1
WHERE r2.id = $1
AND COALESCE(jb.balancer_capacity, jb.capacity, 2147483647) - COALESCE(jbl.load, 0) >= COALESCE(r2.reserve, 0)
AND COALESCE(jb.balancer_capacity, jb.capacity, 2147483647) - COALESCE(jbl.load, 0) >= COALESCE(r2.reserve, 1)
AND jb.api_version = $2
ORDER BY COALESCE(jbl.load, 0) DESC
LIMIT 1
"#;

pub(crate) fn most_loaded(room_id: Uuid, conn: &PgConnection) -> Result<Option<Object>, Error> {
use diesel::prelude::*;
use diesel::sql_types::Uuid;
use diesel::sql_types::{Text, Uuid};

diesel::sql_query(MOST_LOADED_SQL)
.bind::<Uuid, _>(room_id)
.bind::<Text, _>(JANUS_API_VERSION)
.get_result(conn)
.optional()
}
Expand Down Expand Up @@ -293,16 +301,18 @@ const LEAST_LOADED_SQL: &str = r#"
LEFT JOIN room AS r2
ON 1 = 1
WHERE r2.id = $1
AND jb.api_version = $2
ORDER BY COALESCE(jb.balancer_capacity, jb.capacity, 2147483647) - COALESCE(jbl.load, 0) DESC
LIMIT 1
"#;

pub(crate) fn least_loaded(room_id: Uuid, conn: &PgConnection) -> Result<Option<Object>, Error> {
use diesel::prelude::*;
use diesel::sql_types::Uuid;
use diesel::sql_types::{Text, Uuid};

diesel::sql_query(LEAST_LOADED_SQL)
.bind::<Uuid, _>(room_id)
.bind::<Text, _>(JANUS_API_VERSION)
.get_result(conn)
.optional()
}
Expand Down
1 change: 1 addition & 0 deletions src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ table! {
created_at -> Timestamptz,
capacity -> Nullable<Int4>,
balancer_capacity -> Nullable<Int4>,
api_version -> Text,
}
}

Expand Down

0 comments on commit 3534b64

Please sign in to comment.