forked from kata-containers/kata-containers
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathkata-policy.go
73 lines (60 loc) · 1.76 KB
/
kata-policy.go
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
// Copyright (c) 2023 Intel Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
package main
import (
"fmt"
"os"
containerdshim "github.com/kata-containers/kata-containers/src/runtime/pkg/containerd-shim-v2"
"github.com/kata-containers/kata-containers/src/runtime/pkg/katautils"
"github.com/kata-containers/kata-containers/src/runtime/pkg/utils/shimclient"
"github.com/urfave/cli"
)
var policySubCmds = []cli.Command{
setPolicyCommand,
}
var kataPolicyCommand = cli.Command{
Name: "policy",
Usage: "set policy within the Kata Containers guest",
Subcommands: policySubCmds,
Action: func(context *cli.Context) {
cli.ShowSubcommandHelp(context)
},
}
var setPolicyCommand = cli.Command{
Name: "set",
Usage: "set policy in a specifc Kata Containers guest based on file",
Flags: []cli.Flag{
cli.StringFlag{
Name: "sandbox-id",
Usage: "the target sandbox for setting the policy",
Required: true,
Destination: &sandboxID,
},
},
Action: func(c *cli.Context) error {
policyFile := c.Args().Get(0)
// verify sandbox exists:
if err := katautils.VerifyContainerID(sandboxID); err != nil {
return err
}
// verify policy were provided:
if policyFile == "" {
return fmt.Errorf("policy file not provided")
}
if !katautils.FileExists(policyFile) {
return fmt.Errorf("policy file does not exist: %s", policyFile)
}
// Read file into buffer, and make request to the appropriate shim
buf, err := os.ReadFile(policyFile)
if err != nil {
return err
}
url := containerdshim.PolicyUrl
if err = shimclient.DoPut(sandboxID, defaultTimeout, url, "application/octet-stream", buf); err != nil {
return fmt.Errorf("Error observed when making policy-set request(%s): %s", policyFile, err)
}
return nil
},
}