Debian Codegraph @

nft/exec/exec.go

raw symbols SCIP
1 /*
2 * This file is part of the go-nft project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * Copyright 2021 Red Hat, Inc.
17 *
18 */
19
20 package exec
21
22 import (
23 "bytes"
24 "context"
25 "fmt"
26 "os/exec"
27 "strings"
28
29 nftconfig "github.com/networkplumbing/go-nft/nft/config"
30 )
31
32 const (
33 cmdBin = "nft"
34 cmdHandle = "-a"
35 cmdEcho = "-e"
36 cmdFile = "-f"
37 cmdJSON = "-j"
38 cmdList = "list"
39 cmdRuleset = "ruleset"
40 cmdStdin = "-"
41 )
42
43 // ReadConfig loads the nftables configuration from the system and
44 // returns it as a nftables config structure.
45 // The system is expected to have the `nft` executable deployed and nftables enabled in the kernel.
46 func ReadConfig(ctx context.Context, filterCommands ...string) (*nftconfig.Config, error) {
47 whatToList := cmdRuleset
48 if len(filterCommands) > 0 {
49 whatToList = strings.Join(filterCommands, " ")
50 }
51 stdout, err := execCommand(ctx, nil, cmdJSON, cmdList, whatToList)
52 if err != nil {
53 return nil, err
54 }
55
56 config := nftconfig.New()
57 if err := config.FromJSON(stdout.Bytes()); err != nil {
58 return nil, fmt.Errorf("failed to list ruleset: %v", err)
59 }
60
61 return config, nil
62 }
63
64 // ApplyConfig applies the given nftables config on the system.
65 // The system is expected to have the `nft` executable deployed and nftables enabled in the kernel.
66 func ApplyConfig(ctx context.Context, c *nftconfig.Config) error {
67 data, err := c.ToJSON()
68 if err != nil {
69 return err
70 }
71
72 if _, err := execCommand(ctx, data, cmdJSON, cmdFile, cmdStdin); err != nil {
73 return err
74 }
75
76 return nil
77 }
78
79 // ApplyConfigEcho applies the given nftables config on the system, echoing
80 // back the added elements with their assigned handles
81 // The system is expected to have the `nft` executable deployed and nftables enabled in the kernel.
82 func ApplyConfigEcho(ctx context.Context, c *nftconfig.Config) (*nftconfig.Config, error) {
83 data, err := c.ToJSON()
84 if err != nil {
85 return nil, err
86 }
87
88 stdout, err := execCommand(ctx, data, cmdHandle, cmdEcho, cmdJSON, cmdFile, cmdStdin)
89 if err != nil {
90 return nil, err
91 }
92
93 config := nftconfig.New()
94 if err := config.FromJSON(stdout.Bytes()); err != nil {
95 return nil, fmt.Errorf("failed to parse echo: %v", err)
96 }
97
98 return config, nil
99 }
100
101 func execCommand(ctx context.Context, input []byte, args ...string) (*bytes.Buffer, error) {
102 cmd := exec.CommandContext(ctx, cmdBin, args...)
103
104 var stdout, stderr bytes.Buffer
105 cmd.Stderr = &stderr
106 cmd.Stdout = &stdout
107
108 if input != nil {
109 var stdin bytes.Buffer
110 stdin.Write(input)
111 cmd.Stdin = &stdin
112 }
113
114 if err := cmd.Run(); err != nil {
115 return nil, fmt.Errorf(
116 "failed to execute %s %s: %v stdin:'%s' stdout:'%s' stderr:'%s'",
117 cmd.Path, strings.Join(cmd.Args, " "), err, string(input), stdout.String(), stderr.String(),
118 )
119 }
120
121 return &stdout, nil
122 }