@@ -16,7 +16,6 @@ use std::path::Path;
16
16
use std:: net:: SocketAddr ;
17
17
use std:: sync:: atomic:: { AtomicBool , Ordering as AtomicOrdering } ;
18
18
use std:: cmp:: Ordering ;
19
- use std:: env;
20
19
21
20
use serde:: Serialize ;
22
21
use serde:: de:: DeserializeOwned ;
@@ -45,7 +44,7 @@ use util::{self, get_repo_path};
45
44
pub use api:: { self , github, status, nll_dashboard, dashboard, data, days, graph, info, CommitResponse , ServerResult } ;
46
45
use collector:: { Date , Run , version_supports_incremental} ;
47
46
use collector:: api:: collected;
48
- use load:: { CommitData , InputData } ;
47
+ use load:: { Config , CommitData , InputData } ;
49
48
use antidote:: RwLock ;
50
49
use load:: CurrentState ;
51
50
@@ -510,16 +509,17 @@ lazy_static! {
510
509
static ref BODY_TRY_COMMIT : Regex = Regex :: new( r#"(?:\b|^)@rust-timer\s+build\s+(\w+)(?:\b|$)"# ) . unwrap( ) ;
511
510
}
512
511
513
- pub fn post_comment ( request : & github:: Request , body : & str ) -> ServerResult < ( ) > {
514
- println ! ( "post comment: {}" , body ) ;
512
+ pub fn post_comment ( cfg : & Config , request : & github:: Request , body : & str ) -> ServerResult < ( ) > {
513
+ let timer_token = cfg . keys . github . clone ( ) . expect ( "needs rust-timer token" ) ;
515
514
let client = reqwest:: Client :: new ( ) ;
516
515
let mut req = client. post ( & request. issue . comments_url ) ;
517
516
req
518
517
. json ( & github:: PostComment {
519
518
body : body. to_owned ( ) ,
520
519
} )
521
520
. header ( UserAgent :: new ( "perf-rust-lang-org-server" ) )
522
- . basic_auth ( "rust-timer" , Some ( env:: var ( "RUST_TIMER_GH_TOKEN" ) . unwrap ( ) ) ) ;
521
+ . basic_auth ( "rust-timer" , Some ( timer_token) ) ;
522
+
523
523
let res = req. send ( ) ;
524
524
match res {
525
525
Ok ( _) => { }
@@ -531,15 +531,14 @@ pub fn post_comment(request: &github::Request, body: &str) -> ServerResult<()> {
531
531
}
532
532
533
533
pub fn handle_github ( request : github:: Request , data : & InputData ) -> ServerResult < github:: Response > {
534
- println ! ( "handle_github({:?})" , request) ;
535
534
if !request. comment . body . contains ( "@rust-timer " ) {
536
535
return Ok ( github:: Response ) ;
537
536
}
538
537
539
- // FIXME: Better auth / config
540
- if request. comment . author_association != github :: Association :: Owner {
541
- post_comment ( & request,
542
- "Only owners of the repository are permitted to issue commands to rust-timer." ) ?;
538
+ if request . comment . author_association != github :: Association :: Owner ||
539
+ data . config . users . contains ( & request. comment . user . login ) {
540
+ post_comment ( & data . config , & request,
541
+ "Insufficient permissions to issue commands to rust-timer." ) ?;
543
542
return Ok ( github:: Response ) ;
544
543
}
545
544
@@ -548,7 +547,7 @@ pub fn handle_github(request: github::Request, data: &InputData) -> ServerResult
548
547
if let Some ( captures) = BODY_TRY_COMMIT . captures ( & body) {
549
548
if let Some ( commit) = captures. get ( 1 ) . map ( |c| c. as_str ( ) ) {
550
549
if commit. len ( ) != 40 {
551
- post_comment ( & request, "Please provide the full 40 character commit hash." ) ?;
550
+ post_comment ( & data . config , & request, "Please provide the full 40 character commit hash." ) ?;
552
551
return Ok ( github:: Response ) ;
553
552
}
554
553
let client = reqwest:: Client :: new ( ) ;
@@ -557,7 +556,7 @@ pub fn handle_github(request: github::Request, data: &InputData) -> ServerResult
557
556
. send ( ) . map_err ( |_| String :: from ( "cannot get commit" ) ) ?
558
557
. json ( ) . map_err ( |_| String :: from ( "cannot deserialize commit" ) ) ?;
559
558
if commit_response. parents . len ( ) != 1 {
560
- post_comment ( & request,
559
+ post_comment ( & data . config , & request,
561
560
& format ! ( "Bors try commit {} unexpectedly has {} parents." ,
562
561
commit_response. sha, commit_response. parents. len( ) ) ) ?;
563
562
return Ok ( github:: Response ) ;
@@ -569,7 +568,7 @@ pub fn handle_github(request: github::Request, data: &InputData) -> ServerResult
569
568
}
570
569
persistent. write ( ) . expect ( "successful encode" ) ;
571
570
}
572
- post_comment ( & request,
571
+ post_comment ( & data . config , & request,
573
572
& format ! ( "Success: Queued {} with parent {}, [comparison URL]({})." ,
574
573
commit_response. sha, commit_response. parents[ 0 ] . sha,
575
574
format!( "https://perf.rust-lang.org/compare.html?start={}&end={}" ,
@@ -622,7 +621,6 @@ struct Server {
622
621
data : Arc < RwLock < InputData > > ,
623
622
pool : CpuPool ,
624
623
updating : Arc < AtomicBool > ,
625
- key : String ,
626
624
}
627
625
628
626
macro_rules! check_http_method {
@@ -667,7 +665,8 @@ impl Server {
667
665
668
666
fn check_auth ( & self , req : & Request ) -> bool {
669
667
if let Some ( auth) = req. headers ( ) . get :: < Authorization < Bearer > > ( ) {
670
- if auth. 0 . token == self . key {
668
+ let data = self . data . read ( ) ;
669
+ if auth. 0 . token == * data. config . keys . secret . as_ref ( ) . unwrap ( ) {
671
670
return true ;
672
671
}
673
672
}
@@ -741,10 +740,10 @@ impl Server {
741
740
futures:: future:: ok :: < _ , <Self as Service >:: Error > ( acc)
742
741
} )
743
742
. map ( move |body| {
744
- if gh && !verify_gh_sig ( gh_header. unwrap ( ) , & body) . unwrap_or ( false ) {
743
+ let data = data. read ( ) ;
744
+ if gh && !verify_gh_sig ( & data. config , gh_header. unwrap ( ) , & body) . unwrap_or ( false ) {
745
745
return Response :: new ( ) . with_status ( StatusCode :: Unauthorized ) ;
746
746
}
747
- let data = data. read ( ) ;
748
747
let body: D = match serde_json:: from_slice ( & body) {
749
748
Ok ( d) => d,
750
749
Err ( err) => {
@@ -934,10 +933,10 @@ impl Service for Server {
934
933
}
935
934
}
936
935
937
- fn verify_gh_sig ( header : HubSignature , body : & [ u8 ] ) -> Option < bool > {
936
+ fn verify_gh_sig ( cfg : & Config , header : HubSignature , body : & [ u8 ] ) -> Option < bool > {
938
937
let key = hmac:: VerificationKey :: new (
939
938
& digest:: SHA1 ,
940
- env :: var ( "PERF_SECRET_KEY" ) . unwrap ( ) . as_bytes ( ) ,
939
+ cfg . keys . secret . as_ref ( ) . unwrap ( ) . as_bytes ( ) ,
941
940
) ;
942
941
let sha = header. 0 . get ( 5 ..) ?; // strip sha1=
943
942
let sha = hex:: decode ( sha) . ok ( ) ?;
@@ -948,12 +947,11 @@ fn verify_gh_sig(header: HubSignature, body: &[u8]) -> Option<bool> {
948
947
Some ( false )
949
948
}
950
949
951
- pub fn start ( data : InputData , port : u16 , key : String ) {
950
+ pub fn start ( data : InputData , port : u16 ) {
952
951
let server = Arc :: new ( Server {
953
952
data : Arc :: new ( RwLock :: new ( data) ) ,
954
953
pool : CpuPool :: new_num_cpus ( ) ,
955
954
updating : Arc :: new ( AtomicBool :: new ( false ) ) ,
956
- key,
957
955
} ) ;
958
956
let mut server_address: SocketAddr = "0.0.0.0:2346" . parse ( ) . unwrap ( ) ;
959
957
server_address. set_port ( port) ;
0 commit comments