11// Copyright (c) Microsoft Corporation. All rights reserved.
22
3- use super :: protocol_v2:: { MARKER , Registration , Request , VIEW_MARKER , ViewRegistration } ;
3+ use super :: protocol_v2:: {
4+ AUTH_MARKER , Authentication , MARKER , Registration , Request , VIEW_MARKER , ViewRegistration ,
5+ } ;
46use super :: server:: { Options , QuerySource } ;
57use anyhow:: { Context , Result , ensure} ;
68use notify:: { EventKind , Watcher } ;
@@ -106,7 +108,8 @@ fn hello(namespace: &Namespace) -> Result<Value> {
106108 "versioned-views" , "operation-replay" , "atomic-base-advancement" , "adaptive-evaluation" ,
107109 "instance-owner-guards" , "catalog-pages" , "reference-aware-collection" ,
108110 "versioned-allocations" , "bound-checkpoints" , "offline-maintenance" , "stop-if-idle" ,
109- "bounded-storage-inventory" , "namespace-discovery" , "maintenance-diagnostics"
111+ "bounded-storage-inventory" , "namespace-discovery" , "maintenance-diagnostics" ,
112+ "private-rpc-authentication"
110113 ] ,
111114 "profile" : IndexingProfile :: default ( ) ,
112115 "policy" : namespace. policy( ) ?, "allocation" : namespace. allocation( ) ?,
@@ -357,6 +360,7 @@ impl Drop for Observer {
357360
358361struct State {
359362 registration : Registration ,
363+ authentication : Authentication ,
360364 namespace : Arc < Namespace > ,
361365 views : ViewManager ,
362366 watches : WatchSettings ,
@@ -704,7 +708,7 @@ impl State {
704708 fn legacy_registration ( & self ) -> super :: protocol:: Registration {
705709 super :: protocol:: Registration {
706710 protocol : 1 ,
707- instance : self . registration . instance . to_string ( ) ,
711+ instance : self . authentication . legacy_token . to_string ( ) ,
708712 repository : self . registration . repository . clone ( ) ,
709713 pid : self . registration . pid ,
710714 port : self . registration . port ,
@@ -1467,7 +1471,7 @@ fn respond(mut stream: TcpStream, request: Option<&Request>, state: &State, resu
14671471 let response = match result {
14681472 Ok ( mut data) if request. is_some_and ( |request| request. protocol == 1 ) => {
14691473 data[ "protocol" ] = json ! ( 1 ) ;
1470- data[ "instance" ] = json ! ( state. registration . instance ) ;
1474+ data[ "instance" ] = json ! ( state. authentication . legacy_token ) ;
14711475 data[ "repository" ] = json ! ( state. registration. repository) ;
14721476 json ! ( { "jsonrpc" : "2.0" , "id" : id, "result" : data} )
14731477 }
@@ -1493,18 +1497,13 @@ fn respond(mut stream: TcpStream, request: Option<&Request>, state: &State, resu
14931497}
14941498
14951499struct DaemonRegistration {
1496- path : PathBuf ,
1497- identity : managed:: FileIdentity ,
1498- sentinel : ( PathBuf , managed:: FileIdentity ) ,
1500+ files : Vec < ( PathBuf , managed:: FileIdentity ) > ,
14991501 _git_lock : File ,
15001502}
15011503
15021504impl Drop for DaemonRegistration {
15031505 fn drop ( & mut self ) {
1504- for ( path, identity) in [
1505- ( & self . path , & self . identity ) ,
1506- ( & self . sentinel . 0 , & self . sentinel . 1 ) ,
1507- ] {
1506+ for ( path, identity) in self . files . iter ( ) . rev ( ) {
15081507 if let Err ( error) = managed:: remove_control_file ( path, identity)
15091508 && error. source_io_kind ( ) != Some ( std:: io:: ErrorKind :: NotFound )
15101509 {
@@ -1545,6 +1544,8 @@ pub(super) fn run(root: &Path, options: Options<'_>, policy: Policy) -> Result<(
15451544 port : listener. local_addr ( ) ?. port ( ) ,
15461545 storage : namespace. path ( ) . into ( ) ,
15471546 } ;
1547+ let authentication = Authentication :: new ( registration. clone ( ) ) ?;
1548+ let legacy_instance = authentication. legacy_token . clone ( ) ;
15481549 let monitors = Arc :: new ( Mutex :: new ( HashMap :: new ( ) ) ) ;
15491550 let observer_registration = registration. clone ( ) ;
15501551 let observer_registry = Arc :: clone ( & monitors) ;
@@ -1590,7 +1591,7 @@ pub(super) fn run(root: &Path, options: Options<'_>, policy: Policy) -> Result<(
15901591 json ! ( super :: protocol:: ViewRegistration {
15911592 daemon: super :: protocol:: Registration {
15921593 protocol: 1 ,
1593- instance: observer_registration . instance . to_string( ) ,
1594+ instance: legacy_instance . to_string( ) ,
15941595 repository: observer_registration. repository. clone( ) ,
15951596 pid: observer_registration. pid,
15961597 port: observer_registration. port,
@@ -1604,7 +1605,7 @@ pub(super) fn run(root: &Path, options: Options<'_>, policy: Policy) -> Result<(
16041605 json ! ( { "protocol" : 2 , "namespace" : observer_registration. namespace, "instance" : observer_registration. instance,
16051606 "managed" : true , "view" : slot. id( ) } )
16061607 } ;
1607- let sentinel_identity = managed:: publish_control_file ( & sentinel, & sentinel_value) ?;
1608+ let sentinel_identity = managed:: publish_private_control_file ( & sentinel, & sentinel_value) ?;
16081609 let monitor = Arc :: new ( Mutex :: new ( Monitor {
16091610 watcher : None ,
16101611 watched : HashSet :: new ( ) ,
@@ -1660,6 +1661,7 @@ pub(super) fn run(root: &Path, options: Options<'_>, policy: Policy) -> Result<(
16601661 let ( jobs, jobs_rx) = mpsc:: sync_channel ( policy. work . queue_items as usize ) ;
16611662 let state = Arc :: new ( State {
16621663 registration : registration. clone ( ) ,
1664+ authentication,
16631665 namespace,
16641666 views,
16651667 watches,
@@ -1675,21 +1677,29 @@ pub(super) fn run(root: &Path, options: Options<'_>, policy: Policy) -> Result<(
16751677 lifecycle_epoch : AtomicU64 :: new ( 1 ) ,
16761678 scheduler_snapshot : Mutex :: new ( json ! ( { "initializing" : true } ) ) ,
16771679 } ) ;
1680+ let mut registration_guard = DaemonRegistration {
1681+ files : Vec :: with_capacity ( 3 ) ,
1682+ _git_lock : git_lock,
1683+ } ;
1684+ let authentication_path = repository. common_dir ( ) . join ( AUTH_MARKER ) ;
1685+ let authentication_identity = managed:: publish_private_control_file (
1686+ & authentication_path,
1687+ & serde_json:: to_value ( & state. authentication ) ?,
1688+ ) ?;
1689+ registration_guard
1690+ . files
1691+ . push ( ( authentication_path, authentication_identity) ) ;
16781692 let marker = repository. common_dir ( ) . join ( MARKER ) ;
16791693 let marker_identity = managed:: publish_control_file ( & marker, & json ! ( registration) ) ?;
1694+ registration_guard. files . push ( ( marker, marker_identity) ) ;
16801695 let sentinel = repository. common_dir ( ) . join ( super :: MARKER ) ;
16811696 let sentinel_value = if policy. storage == managed:: policy:: StorageMode :: CompatibilityRetainAll {
16821697 json ! ( state. legacy_registration( ) )
16831698 } else {
16841699 json ! ( { "protocol" : 2 , "namespace" : registration. namespace, "instance" : registration. instance} )
16851700 } ;
1686- let sentinel_identity = managed:: publish_control_file ( & sentinel, & sentinel_value) ?;
1687- let _registration = DaemonRegistration {
1688- path : marker,
1689- identity : marker_identity,
1690- sentinel : ( sentinel, sentinel_identity) ,
1691- _git_lock : git_lock,
1692- } ;
1701+ let sentinel_identity = managed:: publish_private_control_file ( & sentinel, & sentinel_value) ?;
1702+ registration_guard. files . push ( ( sentinel, sentinel_identity) ) ;
16931703 eprintln ! (
16941704 "shared-v2 listening on {} (namespace {})" ,
16951705 registration. port, registration. namespace
@@ -1796,7 +1806,24 @@ pub(super) fn run(root: &Path, options: Options<'_>, policy: Policy) -> Result<(
17961806 & mut stream,
17971807 managed:: MAX_REQUEST_BYTES as u64 ,
17981808 ) ?;
1799- let value: Value = serde_json:: from_slice ( & line) ?;
1809+ let mut value: Value = serde_json:: from_slice ( & line) ?;
1810+ let legacy_protocol = value[ "protocol" ] == 1 ;
1811+ let authorization = if legacy_protocol {
1812+ value[ "instance" ] . as_str ( )
1813+ } else {
1814+ value[ "authorization" ] . as_str ( )
1815+ } ;
1816+ if !state. authentication . accepts ( authorization, legacy_protocol) {
1817+ return Err ( Error :: new (
1818+ ErrorCategory :: Permission ,
1819+ "rpc-authentication-failed" ,
1820+ "managed RPC requires the current private daemon credential" ,
1821+ )
1822+ . into ( ) ) ;
1823+ }
1824+ if !legacy_protocol && let Some ( object) = value. as_object_mut ( ) {
1825+ object. remove ( "authorization" ) ;
1826+ }
18001827 let request: Request = if value[ "protocol" ] == 1 {
18011828 if state. namespace . header ( ) . storage
18021829 != managed:: policy:: StorageMode :: CompatibilityRetainAll
@@ -1811,7 +1838,7 @@ pub(super) fn run(root: &Path, options: Options<'_>, policy: Policy) -> Result<(
18111838 jsonrpc : legacy. jsonrpc ,
18121839 protocol : legacy. protocol ,
18131840 namespace : state. registration . namespace . clone ( ) ,
1814- instance : Id :: parse ( legacy . instance ) ? ,
1841+ instance : state . registration . instance . clone ( ) ,
18151842 repository : legacy. repository ,
18161843 method : legacy. method ,
18171844 params : legacy. params ,
0 commit comments