11from typing import Annotated , List
2- from datetime import datetime , timedelta
32
43from fastapi import APIRouter , Depends , HTTPException , Request
54from sqlmodel import Session , select
65from sqlalchemy .exc import IntegrityError
76
8- from fob_api import auth , engine
7+ from fob_api import auth , get_session
98from fob_api .models .database import User , HeadScalePolicyACL , HeadScalePolicyHost
109from fob_api .models .api import HeadScalePolicyAcl as HeadScalePolicyAclAPI
1110from fob_api .models .api import HeadScalePolicyAclCreate as HeadScalePolicyAclCreateAPI
1615router = APIRouter (prefix = "/headscale" )
1716
1817@router .get ("/acls/" , tags = ["vpn" ])
19- def list (user : Annotated [User , Depends (auth .get_current_user )]) -> List [HeadScalePolicyAclAPI ]:
18+ def list (
19+ user : Annotated [User , Depends (auth .get_current_user )],
20+ session : Session = Depends (get_session ),
21+ ) -> List [HeadScalePolicyAclAPI ]:
2022 """
2123 Return list of HeadScale ACLs in Policy
2224 """
23- if not user .is_admin :
24- raise HTTPException (status_code = 403 , detail = "Not an admin" )
25- with Session (engine ) as session :
26- acls = session .exec (select (HeadScalePolicyACL )).all ()
25+ auth .is_admin (user )
26+ acls = session .exec (select (HeadScalePolicyACL )).all ()
2727 return [HeadScalePolicyAclAPI (
2828 id = acl .id ,
2929 action = acl .action ,
@@ -34,114 +34,110 @@ def list(user: Annotated[User, Depends(auth.get_current_user)]) -> List[HeadScal
3434
3535@router .post ("/acls/" , tags = ["vpn" ])
3636def create (
37- acl : HeadScalePolicyAclCreateAPI ,
38- user : Annotated [User , Depends (auth .get_current_user )],
39- ) -> HeadScalePolicyAclAPI | None :
37+ acl : HeadScalePolicyAclCreateAPI ,
38+ user : Annotated [User , Depends (auth .get_current_user )],
39+ session : Session = Depends (get_session ),
40+ ) -> HeadScalePolicyAclAPI | None :
4041 """
4142 Create a new HeadScale ACL in Policy
4243 """
43- if not user .is_admin :
44- raise HTTPException (status_code = 403 , detail = "Not an admin" )
45- with Session (engine ) as session :
46- new_acl = HeadScalePolicyACL (** acl .model_dump ())
47- session .add (new_acl )
48- session .commit ()
49- session .refresh (new_acl )
44+ auth .is_admin (user )
45+ new_acl = HeadScalePolicyACL (** acl .model_dump ())
46+ session .add (new_acl )
47+ session .commit ()
48+ session .refresh (new_acl )
5049
51- try :
52- update_headscale_policy ()
53- except Exception as e :
54- session .delete (new_acl )
55- session .commit ()
56- raise HTTPException (status_code = 400 , detail = f"Failed to apply new policy: { e } " )
57- return HeadScalePolicyAclAPI (
58- id = new_acl .id ,
59- action = new_acl .action ,
60- src = new_acl .src .split ("," ),
61- dst = new_acl .dst .split ("," ),
62- proto = new_acl .proto ,
63- )
50+ try :
51+ update_headscale_policy ()
52+ except Exception as e :
53+ session .delete (new_acl )
54+ session .commit ()
55+ raise HTTPException (status_code = 400 , detail = f"Failed to apply new policy: { e } " )
56+ return HeadScalePolicyAclAPI (
57+ id = new_acl .id ,
58+ action = new_acl .action ,
59+ src = new_acl .src .split ("," ),
60+ dst = new_acl .dst .split ("," ),
61+ proto = new_acl .proto ,
62+ )
6463
6564@router .delete ("/acls/{acl_id}/" , tags = ["vpn" ])
6665def delete (
67- acl_id : int ,
68- user : Annotated [User , Depends (auth .get_current_user )],
69- ) -> None :
66+ acl_id : int ,
67+ user : Annotated [User , Depends (auth .get_current_user )],
68+ session : Session = Depends (get_session ),
69+ ) -> None :
7070 """
7171 Delete a HeadScale ACL from Policy
7272 """
73- if not user .is_admin :
74- raise HTTPException (status_code = 403 , detail = "Not an admin" )
75- with Session (engine ) as session :
76- acl = session .get (HeadScalePolicyACL , acl_id )
77- if not acl :
78- raise HTTPException (status_code = 404 , detail = "ACL not found" )
79- session .delete (acl )
73+ auth .is_admin (user )
74+ acl = session .get (HeadScalePolicyACL , acl_id )
75+ if not acl :
76+ raise HTTPException (status_code = 404 , detail = "ACL not found" )
77+ session .delete (acl )
78+ session .commit ()
79+ try :
80+ update_headscale_policy ()
81+ except Exception as e :
82+ session .add (acl )
8083 session .commit ()
81- try :
82- update_headscale_policy ()
83- except Exception as e :
84- session .add (acl )
85- session .commit ()
86- raise HTTPException (status_code = 400 , detail = f"Failed to apply new policy: { e } " )
84+ raise HTTPException (status_code = 400 , detail = f"Failed to apply new policy: { e } " )
8785
8886@router .get ("/host/" , tags = ["vpn" ])
89- def list_hosts (user : Annotated [User , Depends (auth .get_current_user )]) -> List [HeadScalePolicyHostAPI ]:
87+ def list_hosts (
88+ user : Annotated [User , Depends (auth .get_current_user )],
89+ session : Session = Depends (get_session ),
90+ ) -> List [HeadScalePolicyHostAPI ]:
9091 """
9192 Return list of HeadScale hosts
9293 """
93- if not user .is_admin :
94- raise HTTPException (status_code = 403 , detail = "Not an admin" )
95- with Session (engine ) as session :
96- headscale_policy_host = session .exec (select (HeadScalePolicyHost )).all ()
94+ auth .is_admin (user )
95+ headscale_policy_host = session .exec (select (HeadScalePolicyHost )).all ()
9796 return headscale_policy_host
9897
9998@router .post ("/host/" , tags = ["vpn" ])
10099def create_host (
101- host : HeadScalePolicyHostCreateAPI ,
102- user : Annotated [User , Depends (auth .get_current_user )],
103- ) -> HeadScalePolicyHostAPI | None :
100+ host : HeadScalePolicyHostCreateAPI ,
101+ user : Annotated [User , Depends (auth .get_current_user )],
102+ session : Session = Depends (get_session ),
103+ ) -> HeadScalePolicyHostAPI | None :
104104 """
105105 Create a new HeadScale host
106106 """
107- if not user .is_admin :
108- raise HTTPException (status_code = 403 , detail = "Not an admin" )
109- with Session (engine ) as session :
110-
111- new_host = HeadScalePolicyHost (** host .model_dump ())
112- session .add (new_host )
113- try :
114- session .commit ()
115- except IntegrityError :
116- raise HTTPException (status_code = 400 , detail = "This host binding already exists" )
117- session .refresh (new_host )
118- try :
119- update_headscale_policy ()
120- except Exception as e :
121- session .delete (new_host )
122- session .commit ()
123- raise HTTPException (status_code = 400 , detail = f"Failed to apply new policy: { e } " )
124- return new_host
107+ auth .is_admin (user )
108+ new_host = HeadScalePolicyHost (** host .model_dump ())
109+ session .add (new_host )
110+ try :
111+ session .commit ()
112+ except IntegrityError :
113+ raise HTTPException (status_code = 400 , detail = "This host binding already exists" )
114+ session .refresh (new_host )
115+ try :
116+ update_headscale_policy ()
117+ except Exception as e :
118+ session .delete (new_host )
119+ session .commit ()
120+ raise HTTPException (status_code = 400 , detail = f"Failed to apply new policy: { e } " )
121+ return new_host
125122
126123@router .delete ("/host/{host_id}/" , tags = ["vpn" ])
127124def delete_host (
128- host_id : int ,
129- user : Annotated [User , Depends (auth .get_current_user )],
130- ) -> None :
125+ host_id : int ,
126+ user : Annotated [User , Depends (auth .get_current_user )],
127+ session : Session = Depends (get_session ),
128+ ) -> None :
131129 """
132130 Delete a HeadScale host
133131 """
134- if not user .is_admin :
135- raise HTTPException (status_code = 403 , detail = "Not an admin" )
136- with Session (engine ) as session :
137- host = session .get (HeadScalePolicyHost , host_id )
138- if not host :
139- raise HTTPException (status_code = 404 , detail = "Host not found" )
140- session .delete (host )
132+ auth .is_admin (user )
133+ host = session .get (HeadScalePolicyHost , host_id )
134+ if not host :
135+ raise HTTPException (status_code = 404 , detail = "Host not found" )
136+ session .delete (host )
137+ session .commit ()
138+ try :
139+ update_headscale_policy ()
140+ except Exception as e :
141+ session .add (host )
141142 session .commit ()
142- try :
143- update_headscale_policy ()
144- except Exception as e :
145- session .add (host )
146- session .commit ()
147- raise HTTPException (status_code = 400 , detail = f"Failed to apply new policy: { e } " )
143+ raise HTTPException (status_code = 400 , detail = f"Failed to apply new policy: { e } " )
0 commit comments