Repository navigation
Expand file tree
/
Copy pathexecutions_capabilities.go
More file actions
76 lines (72 loc) · 2.73 KB
/
Copy pathexecutions_capabilities.go
File metadata and controls
76 lines (72 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package kubeworkspaces
import (
"context"
"errors"
"time"
executions "github.com/kube-workspaces/api/gen/executions"
"github.com/kube-workspaces/api/internal/auth"
"github.com/kube-workspaces/api/internal/execution/qga"
)
type scriptSupportChecker interface {
CheckScriptSupport(context.Context, qga.Target) error
}
func (s *executionssrvc) Capabilities(ctx context.Context, p *executions.CapabilitiesPayload) (*executions.ExecutionCapabilities, error) {
user := auth.ExecutionPrincipal(ctx)
if user == nil {
return nil, executions.Unauthorized("sign in with a user-bound credential")
}
if p == nil || p.Namespace == "" || p.WorkspaceName == "" {
return nil, executions.Invalid("namespace and workspace name are required")
}
now := time.Now()
if !auth.HasExecutionPermission(user, p.Namespace, p.WorkspaceName, auth.PermissionExec, now) ||
!auth.HasExecutionPermission(user, p.Namespace, p.WorkspaceName, auth.PermissionElevated, now) {
return nil, executions.Forbidden("scoped execution and elevation permissions are required")
}
if s.plat == nil {
return nil, executions.Unavailable("execution policy unavailable")
}
policy, err := s.plat.GetVMExecutionPolicy(ctx)
if err != nil {
return nil, executions.Unavailable("execution policy unavailable")
}
if !policy.Enabled || !policy.AllowElevated {
return nil, executions.Forbidden("elevated execution is disabled by policy")
}
if s.execSvc.Ready(ctx) != nil {
return nil, executions.Unavailable("execution recording storage unavailable")
}
if s.qga == nil {
return nil, executions.Unavailable("the pilot execution transport is not enabled")
}
target, err := s.qga.Resolve(ctx, p.Namespace, p.WorkspaceName)
if errors.Is(err, qga.ErrDenied) {
return nil, executions.NotFound("not an eligible running agent-equipped VM")
}
if err != nil {
return nil, executions.Unavailable("workspace is temporarily unavailable")
}
result := &executions.ExecutionCapabilities{
Namespace: p.Namespace, WorkspaceName: p.WorkspaceName, WorkspaceUID: target.WorkspaceUID,
OsIdentity: target.OSIdentity(), Reason: "Guest does not advertise compatible script support.",
}
checker, ok := s.qga.(scriptSupportChecker)
if !ok {
return result, nil
}
if err := checker.CheckScriptSupport(ctx, target); err != nil {
if errors.Is(err, qga.ErrUnsupportedScripts) {
return result, nil
}
if errors.Is(err, qga.ErrDenied) {
return nil, executions.NotFound("workspace changed during capability discovery")
}
return nil, executions.Unavailable("script capability discovery unavailable")
}
result.ScriptSupported = true
result.ScriptRequestVersion = 2
result.InterpreterPolicy = qga.ScriptPolicy(target)
result.MaxScriptBytes = qga.MaxScriptBytes
result.Reason = ""
return result, nil
}