Skip to content

Commit 4baa187

Browse files
committed
contrib/apparmor: remove code related to apparmor_parser version
This code was no longer used now that the version-dependent rules were removed from the template in 30c893e. Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 8d4f9b6 commit 4baa187

2 files changed

Lines changed: 2 additions & 151 deletions

File tree

contrib/apparmor/apparmor_test.go

Lines changed: 2 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -22,92 +22,9 @@ import (
2222
"testing"
2323
)
2424

25-
type versionExpected struct {
26-
output string
27-
version int
28-
}
29-
30-
func TestParseVersion(t *testing.T) {
31-
versions := []versionExpected{
32-
{
33-
output: `AppArmor parser version 2.10
34-
Copyright (C) 1999-2008 Novell Inc.
35-
Copyright 2009-2012 Canonical Ltd.
36-
`,
37-
version: 210000,
38-
},
39-
{
40-
output: `AppArmor parser version 2.8
41-
Copyright (C) 1999-2008 Novell Inc.
42-
Copyright 2009-2012 Canonical Ltd.
43-
`,
44-
version: 208000,
45-
},
46-
{
47-
output: `AppArmor parser version 2.20
48-
Copyright (C) 1999-2008 Novell Inc.
49-
Copyright 2009-2012 Canonical Ltd.
50-
`,
51-
version: 220000,
52-
},
53-
{
54-
output: `AppArmor parser version 2.05
55-
Copyright (C) 1999-2008 Novell Inc.
56-
Copyright 2009-2012 Canonical Ltd.
57-
`,
58-
version: 205000,
59-
},
60-
{
61-
output: `AppArmor parser version 2.9.95
62-
Copyright (C) 1999-2008 Novell Inc.
63-
Copyright 2009-2012 Canonical Ltd.
64-
`,
65-
version: 209095,
66-
},
67-
{
68-
output: `AppArmor parser version 3.14.159
69-
Copyright (C) 1999-2008 Novell Inc.
70-
Copyright 2009-2012 Canonical Ltd.
71-
`,
72-
version: 314159,
73-
},
74-
{
75-
output: `AppArmor parser version 3.0.0-beta1
76-
Copyright (C) 1999-2008 Novell Inc.
77-
Copyright 2009-2018 Canonical Ltd.
78-
`,
79-
version: 300000,
80-
},
81-
{
82-
output: `AppArmor parser version 3.0.0-beta1-foo-bar
83-
Copyright (C) 1999-2008 Novell Inc.
84-
Copyright 2009-2018 Canonical Ltd.
85-
`,
86-
version: 300000,
87-
},
88-
{
89-
output: `AppArmor parser version 2.7.0~rc2
90-
Copyright (C) 1999-2008 Novell Inc.
91-
Copyright 2009-2018 Canonical Ltd.
92-
`,
93-
version: 207000,
94-
},
95-
}
96-
97-
for _, v := range versions {
98-
version, err := parseVersion(v.output)
99-
if err != nil {
100-
t.Fatalf("expected error to be nil for %#v, got: %v", v, err)
101-
}
102-
if version != v.version {
103-
t.Fatalf("expected version to be %d, was %d, for: %#v\n", v.version, version, v)
104-
}
105-
}
106-
}
107-
10825
func TestDumpDefaultProfile(t *testing.T) {
109-
if _, err := getVersion(); err != nil {
110-
t.Skipf("AppArmor not available: %+v", err)
26+
if _, err := aaParser("--version"); err != nil {
27+
t.Skipf("apparmor_parser not available: %+v", err)
11128
}
11229
name := "test-dump-default-profile"
11330
prof, err := DumpDefaultProfile(name)

contrib/apparmor/template.go

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"io"
2727
"os"
2828
"path"
29-
"strconv"
3029
"strings"
3130
"text/template"
3231

@@ -91,7 +90,6 @@ type data struct {
9190
Imports []string
9291
InnerImports []string
9392
DaemonProfile string
94-
Version int
9593
}
9694

9795
func cleanProfileName(profile string) string {
@@ -117,11 +115,6 @@ func loadData(name string) (*data, error) {
117115
if macroExists("abstractions/base") {
118116
p.InnerImports = append(p.InnerImports, "#include <abstractions/base>")
119117
}
120-
ver, err := getVersion()
121-
if err != nil {
122-
return nil, fmt.Errorf("get apparmor_parser version: %w", err)
123-
}
124-
p.Version = ver
125118

126119
// Figure out the daemon profile.
127120
currentProfile, err := os.ReadFile("/proc/self/attr/current")
@@ -162,65 +155,6 @@ func aaParser(args ...string) (string, error) {
162155
return string(out), err
163156
}
164157

165-
func getVersion() (int, error) {
166-
out, err := aaParser("--version")
167-
if err != nil {
168-
return -1, err
169-
}
170-
return parseVersion(out)
171-
}
172-
173-
// parseVersion takes the output from `apparmor_parser --version` and returns
174-
// a representation of the {major, minor, patch} version as a single number of
175-
// the form MMmmPPP {major, minor, patch}.
176-
func parseVersion(output string) (int, error) {
177-
// output is in the form of the following:
178-
// AppArmor parser version 2.9.1
179-
// Copyright (C) 1999-2008 Novell Inc.
180-
// Copyright 2009-2012 Canonical Ltd.
181-
182-
version, _, _ := strings.Cut(output, "\n")
183-
if i := strings.LastIndex(version, " "); i >= 0 {
184-
version = version[i+1:]
185-
}
186-
187-
// trim "-beta1" suffix from version="3.0.0-beta1" if exists
188-
version, _, _ = strings.Cut(version, "-")
189-
// also trim tilde
190-
version, _, _ = strings.Cut(version, "~")
191-
192-
// split by major minor version
193-
v := strings.SplitN(version, ".", 4)
194-
if len(v) == 0 || len(v) > 3 {
195-
return -1, fmt.Errorf("parsing version failed for output: `%s`", output)
196-
}
197-
198-
// Default the versions to 0.
199-
var majorVersion, minorVersion, patchLevel int
200-
201-
majorVersion, err := strconv.Atoi(v[0])
202-
if err != nil {
203-
return -1, err
204-
}
205-
206-
if len(v) > 1 {
207-
minorVersion, err = strconv.Atoi(v[1])
208-
if err != nil {
209-
return -1, err
210-
}
211-
}
212-
if len(v) > 2 {
213-
patchLevel, err = strconv.Atoi(v[2])
214-
if err != nil {
215-
return -1, err
216-
}
217-
}
218-
219-
// major*10^5 + minor*10^3 + patch*10^0
220-
numericVersion := majorVersion*1e5 + minorVersion*1e3 + patchLevel
221-
return numericVersion, nil
222-
}
223-
224158
func isLoaded(name string) (bool, error) {
225159
f, err := os.Open("/sys/kernel/security/apparmor/profiles")
226160
if err != nil {

0 commit comments

Comments
 (0)