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 execscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/exec`/
21
22 import (
23 "bytesscip-go gomod github.com/golang/go/src go1.16 bytes/"
24 "contextscip-go gomod github.com/golang/go/src go1.16 context/"
25 "fmtscip-go gomod github.com/golang/go/src go1.16 fmt/"
26 "os/execscip-go gomod github.com/golang/go/src go1.16 `os/exec`/"
27 "stringsscip-go gomod github.com/golang/go/src go1.16 strings/"
28
29 nftconfigscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/ "github.com/networkplumbing/go-nft/nft/configscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/"
30 )
31
32 const (
33 cmdBinscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/exec`/cmdBin. = "nft"
34 cmdHandlescip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/exec`/cmdHandle. = "-a"
35 cmdEchoscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/exec`/cmdEcho. = "-e"
36 cmdFilescip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/exec`/cmdFile. = "-f"
37 cmdJSONscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/exec`/cmdJSON. = "-j"
38 cmdListscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/exec`/cmdList. = "list"
39 cmdRulesetscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/exec`/cmdRuleset. = "ruleset"
40 cmdStdinscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/exec`/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 ReadConfigscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/exec`/ReadConfig().(ctx contextscip-go gomod github.com/golang/go/src go1.16 context/.Contextscip-go gomod github.com/golang/go/src go1.16 context/Context#, filterCommands ...string) (*nftconfigscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/.Configscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/Config#, error) {
47 whatToList := cmdRulesetscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/exec`/cmdRuleset.
48 if len(filterCommands) > 0 {
49 whatToList = stringsscip-go gomod github.com/golang/go/src go1.16 strings/.Joinscip-go gomod github.com/golang/go/src go1.16 strings/Join().(filterCommands, " ")
50 }
51 stdout, err := execCommandscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/exec`/execCommand().(ctx, nil, cmdJSONscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/exec`/cmdJSON., cmdListscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/exec`/cmdList., whatToList)
52 if err != nil {
53 return nil, err
54 }
55
56 config := nftconfigscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/.Newscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/New().()
57 if err := config.FromJSONscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/Config#FromJSON().(stdout.Bytesscip-go gomod github.com/golang/go/src go1.16 bytes/Buffer#Bytes().()); err != nil {
58 return nil, fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 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 ApplyConfigscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/exec`/ApplyConfig().(ctx contextscip-go gomod github.com/golang/go/src go1.16 context/.Contextscip-go gomod github.com/golang/go/src go1.16 context/Context#, c *nftconfigscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/.Configscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/Config#) error {
67 data, err := c.ToJSONscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/Config#ToJSON().()
68 if err != nil {
69 return err
70 }
71
72 if _, err := execCommandscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/exec`/execCommand().(ctx, data, cmdJSONscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/exec`/cmdJSON., cmdFilescip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/exec`/cmdFile., cmdStdinscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/exec`/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 ApplyConfigEchoscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/exec`/ApplyConfigEcho().(ctx contextscip-go gomod github.com/golang/go/src go1.16 context/.Contextscip-go gomod github.com/golang/go/src go1.16 context/Context#, c *nftconfigscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/.Configscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/Config#) (*nftconfigscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/.Configscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/Config#, error) {
83 data, err := c.ToJSONscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/Config#ToJSON().()
84 if err != nil {
85 return nil, err
86 }
87
88 stdout, err := execCommandscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/exec`/execCommand().(ctx, data, cmdHandlescip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/exec`/cmdHandle., cmdEchoscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/exec`/cmdEcho., cmdJSONscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/exec`/cmdJSON., cmdFilescip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/exec`/cmdFile., cmdStdinscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/exec`/cmdStdin.)
89 if err != nil {
90 return nil, err
91 }
92
93 config := nftconfigscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/.Newscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/New().()
94 if err := config.FromJSONscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/Config#FromJSON().(stdout.Bytesscip-go gomod github.com/golang/go/src go1.16 bytes/Buffer#Bytes().()); err != nil {
95 return nil, fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 fmt/Errorf().("failed to parse echo: %v", err)
96 }
97
98 return config, nil
99 }
100
101 func execCommandscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/exec`/execCommand().(ctx contextscip-go gomod github.com/golang/go/src go1.16 context/.Contextscip-go gomod github.com/golang/go/src go1.16 context/Context#, input []byte, args ...string) (*bytesscip-go gomod github.com/golang/go/src go1.16 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.16 bytes/Buffer#, error) {
102 cmd := execscip-go gomod github.com/golang/go/src go1.16 `os/exec`/.CommandContextscip-go gomod github.com/golang/go/src go1.16 `os/exec`/CommandContext().(ctx, cmdBinscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/exec`/cmdBin., args...)
103
104 var stdout, stderr bytesscip-go gomod github.com/golang/go/src go1.16 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.16 bytes/Buffer#
105 cmd.Stderrscip-go gomod github.com/golang/go/src go1.16 `os/exec`/Cmd#Stderr. = &stderr
106 cmd.Stdoutscip-go gomod github.com/golang/go/src go1.16 `os/exec`/Cmd#Stdout. = &stdout
107
108 if input != nil {
109 var stdin bytesscip-go gomod github.com/golang/go/src go1.16 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.16 bytes/Buffer#
110 stdin.Writescip-go gomod github.com/golang/go/src go1.16 bytes/Buffer#Write().(input)
111 cmd.Stdinscip-go gomod github.com/golang/go/src go1.16 `os/exec`/Cmd#Stdin. = &stdin
112 }
113
114 if err := cmd.Runscip-go gomod github.com/golang/go/src go1.16 `os/exec`/Cmd#Run().(); err != nil {
115 return nil, fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 fmt/Errorf().(
116 "failed to execute %s %s: %v stdin:'%s' stdout:'%s' stderr:'%s'",
117 cmd.Pathscip-go gomod github.com/golang/go/src go1.16 `os/exec`/Cmd#Path., stringsscip-go gomod github.com/golang/go/src go1.16 strings/.Joinscip-go gomod github.com/golang/go/src go1.16 strings/Join().(cmd.Argsscip-go gomod github.com/golang/go/src go1.16 `os/exec`/Cmd#Args., " "), err, string(input), stdout.Stringscip-go gomod github.com/golang/go/src go1.16 bytes/Buffer#String().(), stderr.Stringscip-go gomod github.com/golang/go/src go1.16 bytes/Buffer#String().(),
118 )
119 }
120
121 return &stdout, nil
122 }