Skip to content

Commit 3195432

Browse files
committed
eval-server uses X-OII-AUTH as authentication
1 parent 20c71f1 commit 3195432

2 files changed

Lines changed: 44 additions & 56 deletions

File tree

src/tools/eval_server.rs

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use axum::{
1212
Json, Router,
1313
};
1414
use bytes::Bytes;
15+
use clap::Parser;
1516
use serde::{Deserialize, Serialize};
1617
use task_maker_dag::{ExecutionDAG, ExecutionStatus, File};
1718
use task_maker_lang::{LanguageManager, SourceFile};
@@ -20,15 +21,47 @@ use tokio::net::TcpListener;
2021
use tower_http::trace::TraceLayer;
2122

2223
use crate::sandbox::ToolsSandboxRunner;
23-
use crate::tools::opt::EvalServerOpt;
2424

25-
#[derive(Debug, Serialize, Deserialize, Clone)]
26-
struct TokenList {
27-
tokens: Vec<String>,
25+
#[derive(Parser, Debug, Clone)]
26+
pub struct EvalServerOpt {
27+
/// Address to bind the server on
28+
#[clap(long, default_value = "127.0.0.1:3000")]
29+
pub addr: String,
30+
31+
/// Require authentication for the API endpoints.
32+
/// If true, the server will expect an "X-OII-AUTH" header with a token for each request.
33+
///
34+
/// The token is meaningless and is only used to prevent unauthorized access. You can set it to
35+
/// any value you want.
36+
#[clap(long)]
37+
pub require_header: bool,
38+
39+
/// List of allowed languages (names). If empty, all languages are allowed.
40+
#[clap(long)]
41+
pub allowed_languages: Vec<String>,
42+
43+
/// Maximum CPU time limit (seconds)
44+
#[clap(long, default_value = "10.0")]
45+
pub max_time_limit: f64,
46+
47+
/// Maximum memory limit (MB)
48+
#[clap(long, default_value = "512")]
49+
pub max_memory_limit: u64,
50+
51+
/// Compilation CPU time limit (seconds)
52+
#[clap(long, default_value = "60.0")]
53+
pub compilation_time_limit: f64,
54+
55+
/// Compilation memory limit (MB)
56+
#[clap(long, default_value = "1024")]
57+
pub compilation_memory_limit: u64,
58+
59+
#[clap(flatten, next_help_heading = Some("STORAGE"))]
60+
pub storage: crate::StorageOpt,
2861
}
2962

3063
struct AppState {
31-
tokens: Option<HashSet<String>>,
64+
require_header: bool,
3265
allowed_languages: HashSet<String>,
3366
opt: EvalServerOpt,
3467
}
@@ -106,18 +139,9 @@ struct LanguageInfo {
106139

107140
#[tokio::main]
108141
pub async fn main_eval_server(opt: EvalServerOpt) -> Result<(), Error> {
109-
let tokens = if let Some(path) = &opt.tokens_file {
110-
let content = std::fs::read_to_string(path).context("Failed to read tokens file")?;
111-
let list: TokenList =
112-
serde_json::from_str(&content).context("Failed to parse tokens file")?;
113-
Some(list.tokens.into_iter().collect::<HashSet<_>>())
114-
} else {
115-
None
116-
};
117-
118142
let allowed_languages = opt.allowed_languages.iter().cloned().collect();
119143
let state = Arc::new(AppState {
120-
tokens,
144+
require_header: opt.require_header,
121145
allowed_languages,
122146
opt,
123147
});
@@ -145,15 +169,14 @@ async fn auth_middleware(
145169
mut req: Request,
146170
next: Next,
147171
) -> Result<Response, StatusCode> {
148-
if let Some(tokens) = &state.tokens {
172+
if state.require_header {
149173
let auth_header = req
150174
.headers()
151-
.get("Authorization")
152-
.and_then(|h| h.to_str().ok())
153-
.and_then(|s| s.strip_prefix("Bearer "));
175+
.get("X-OII-AUTH")
176+
.and_then(|h| h.to_str().ok());
154177

155178
match auth_header {
156-
Some(token) if tokens.contains(token) => {
179+
Some(token) => {
157180
// Token is valid, continue to the next handler
158181
let token_str = token.to_string();
159182
req.extensions_mut().insert(token_str);

src/tools/opt.rs

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
use std::path::PathBuf;
2-
31
use clap::Parser;
42

53
use crate::tools::add_solution_checks::AddSolutionChecksOpt;
64
use crate::tools::booklet::BookletOpt;
75
use crate::tools::clear::ClearOpt;
86
use crate::tools::copy_competition_files::CopyCompetitionFilesOpt;
7+
use crate::tools::eval_server::EvalServerOpt;
98
use crate::tools::export_booklet::ExportBookletOpt;
109
use crate::tools::export_solution_checks::ExportSolutionChecksOpt;
1110
use crate::tools::find_bad_case::FindBadCaseOpt;
@@ -72,37 +71,3 @@ pub enum Tool {
7271
#[clap(hide = true)]
7372
InternalSandbox,
7473
}
75-
76-
#[derive(Parser, Debug, Clone)]
77-
pub struct EvalServerOpt {
78-
/// Address to bind the server on
79-
#[clap(long, default_value = "127.0.0.1:3000")]
80-
pub addr: String,
81-
82-
/// Path to a JSON file containing the allowed tokens
83-
#[clap(long)]
84-
pub tokens_file: Option<PathBuf>,
85-
86-
/// List of allowed languages (names). If empty, all languages are allowed.
87-
#[clap(long)]
88-
pub allowed_languages: Vec<String>,
89-
90-
/// Maximum CPU time limit (seconds)
91-
#[clap(long, default_value = "10.0")]
92-
pub max_time_limit: f64,
93-
94-
/// Maximum memory limit (MB)
95-
#[clap(long, default_value = "512")]
96-
pub max_memory_limit: u64,
97-
98-
/// Compilation CPU time limit (seconds)
99-
#[clap(long, default_value = "60.0")]
100-
pub compilation_time_limit: f64,
101-
102-
/// Compilation memory limit (MB)
103-
#[clap(long, default_value = "1024")]
104-
pub compilation_memory_limit: u64,
105-
106-
#[clap(flatten, next_help_heading = Some("STORAGE"))]
107-
pub storage: crate::StorageOpt,
108-
}

0 commit comments

Comments
 (0)