@@ -8,57 +8,87 @@ namespace K8sControlApi.Controllers;
8
8
public class K8sController : ControllerBase
9
9
{
10
10
private readonly K8sService _svc ;
11
+ private static readonly bool EnableAuth =
12
+ Environment . GetEnvironmentVariable ( "ENABLE_BASIC_AUTH" ) ? . ToLower ( ) == "true" ;
11
13
12
14
public K8sController ( K8sService svc )
13
15
{
14
16
_svc = svc ;
15
17
}
16
18
19
+ private IActionResult UnauthorizedIfNeeded ( )
20
+ {
21
+ if ( EnableAuth && ! User . Identity ? . IsAuthenticated == true )
22
+ return Unauthorized ( ) ;
23
+ return null ;
24
+ }
25
+
17
26
[ HttpPost ( "restart" ) ]
18
27
public async Task < IActionResult > RestartNamespace ( string @namespace )
19
28
{
29
+ var unauth = UnauthorizedIfNeeded ( ) ;
30
+ if ( unauth != null ) return unauth ;
31
+
20
32
await _svc . RestartNamespace ( @namespace ) ;
21
33
return Ok ( $ "Restarted pods in namespace '{ @namespace } '") ;
22
34
}
23
35
24
36
[ HttpPost ( "stop" ) ]
25
37
public async Task < IActionResult > StopNamespace ( string @namespace )
26
38
{
39
+ var unauth = UnauthorizedIfNeeded ( ) ;
40
+ if ( unauth != null ) return unauth ;
41
+
27
42
await _svc . StopNamespace ( @namespace ) ;
28
43
return Ok ( $ "Scaled all deployments in namespace '{ @namespace } ' to 0") ;
29
44
}
30
45
31
46
[ HttpPost ( "start" ) ]
32
47
public async Task < IActionResult > StartNamespace ( string @namespace )
33
48
{
49
+ var unauth = UnauthorizedIfNeeded ( ) ;
50
+ if ( unauth != null ) return unauth ;
51
+
34
52
await _svc . StartNamespace ( @namespace ) ;
35
53
return Ok ( $ "Scaled all deployments in namespace '{ @namespace } ' to last known replica count") ;
36
54
}
37
55
38
56
[ HttpPost ( "deployment/{deployment}/restart" ) ]
39
57
public async Task < IActionResult > RestartDeployment ( string @namespace , string deployment )
40
58
{
59
+ var unauth = UnauthorizedIfNeeded ( ) ;
60
+ if ( unauth != null ) return unauth ;
61
+
41
62
await _svc . RestartDeployment ( @namespace , deployment ) ;
42
63
return Ok ( $ "Restarted pods in deployment '{ deployment } '") ;
43
64
}
44
65
45
66
[ HttpPost ( "deployment/{deployment}/stop" ) ]
46
67
public async Task < IActionResult > StopDeployment ( string @namespace , string deployment )
47
68
{
69
+ var unauth = UnauthorizedIfNeeded ( ) ;
70
+ if ( unauth != null ) return unauth ;
71
+
48
72
await _svc . StopDeployment ( @namespace , deployment ) ;
49
73
return Ok ( $ "Scaled deployment '{ deployment } ' to 0") ;
50
74
}
51
75
52
76
[ HttpPost ( "deployment/{deployment}/start" ) ]
53
77
public async Task < IActionResult > StartDeployment ( string @namespace , string deployment )
54
78
{
79
+ var unauth = UnauthorizedIfNeeded ( ) ;
80
+ if ( unauth != null ) return unauth ;
81
+
55
82
await _svc . StartDeployment ( @namespace , deployment ) ;
56
83
return Ok ( $ "Started deployment '{ deployment } '") ;
57
84
}
58
85
59
86
[ HttpPost ( "pod/{pod}/restart" ) ]
60
87
public async Task < IActionResult > RestartPod ( string @namespace , string pod )
61
88
{
89
+ var unauth = UnauthorizedIfNeeded ( ) ;
90
+ if ( unauth != null ) return unauth ;
91
+
62
92
await _svc . RestartPod ( @namespace , pod ) ;
63
93
return Ok ( $ "Restarted pod '{ pod } '") ;
64
94
}
0 commit comments