mirror of
https://github.com/roddhjav/apparmor.d.git
synced 2025-01-18 00:48:10 +01:00
build: simplify profile struct.
This commit is contained in:
parent
714971911a
commit
6ea2df19eb
3 changed files with 10 additions and 19 deletions
|
@ -29,24 +29,22 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
type AppArmorProfile struct {
|
type AppArmorProfile struct {
|
||||||
Content string
|
|
||||||
Variables map[string][]string
|
Variables map[string][]string
|
||||||
Attachments []string
|
Attachments []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewAppArmorProfile(content string) *AppArmorProfile {
|
func NewAppArmorProfile() *AppArmorProfile {
|
||||||
variables := make(map[string][]string)
|
variables := make(map[string][]string)
|
||||||
maps.Copy(variables, Tunables)
|
maps.Copy(variables, Tunables)
|
||||||
return &AppArmorProfile{
|
return &AppArmorProfile{
|
||||||
Content: content,
|
|
||||||
Variables: variables,
|
Variables: variables,
|
||||||
Attachments: []string{},
|
Attachments: []string{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParseVariables extract all variables from the profile
|
// ParseVariables extract all variables from the profile
|
||||||
func (p *AppArmorProfile) ParseVariables() {
|
func (p *AppArmorProfile) ParseVariables(content string) {
|
||||||
matches := regVariablesDef.FindAllStringSubmatch(p.Content, -1)
|
matches := regVariablesDef.FindAllStringSubmatch(content, -1)
|
||||||
for _, match := range matches {
|
for _, match := range matches {
|
||||||
if len(match) > 2 {
|
if len(match) > 2 {
|
||||||
key := match[1]
|
key := match[1]
|
||||||
|
|
|
@ -11,15 +11,12 @@ import (
|
||||||
|
|
||||||
func TestNewAppArmorProfile(t *testing.T) {
|
func TestNewAppArmorProfile(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
content string
|
want *AppArmorProfile
|
||||||
want *AppArmorProfile
|
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "aa",
|
name: "aa",
|
||||||
content: "",
|
|
||||||
want: &AppArmorProfile{
|
want: &AppArmorProfile{
|
||||||
Content: "",
|
|
||||||
Variables: map[string][]string{
|
Variables: map[string][]string{
|
||||||
"bin": {"/{usr/,}{s,}bin"},
|
"bin": {"/{usr/,}{s,}bin"},
|
||||||
"lib": {"/{usr/,}lib{,exec,32,64}"},
|
"lib": {"/{usr/,}lib{,exec,32,64}"},
|
||||||
|
@ -33,7 +30,7 @@ func TestNewAppArmorProfile(t *testing.T) {
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
if got := NewAppArmorProfile(tt.content); !reflect.DeepEqual(got, tt.want) {
|
if got := NewAppArmorProfile(); !reflect.DeepEqual(got, tt.want) {
|
||||||
t.Errorf("NewAppArmorProfile() = %v, want %v", got, tt.want)
|
t.Errorf("NewAppArmorProfile() = %v, want %v", got, tt.want)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -81,12 +78,11 @@ func TestAppArmorProfile_ParseVariables(t *testing.T) {
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
p := &AppArmorProfile{
|
p := &AppArmorProfile{
|
||||||
Content: tt.content,
|
|
||||||
Variables: map[string][]string{},
|
Variables: map[string][]string{},
|
||||||
Attachments: []string{},
|
Attachments: []string{},
|
||||||
}
|
}
|
||||||
|
|
||||||
p.ParseVariables()
|
p.ParseVariables(tt.content)
|
||||||
if !reflect.DeepEqual(p.Variables, tt.want) {
|
if !reflect.DeepEqual(p.Variables, tt.want) {
|
||||||
t.Errorf("AppArmorProfile.ParseVariables() = %v, want %v", p.Variables, tt.want)
|
t.Errorf("AppArmorProfile.ParseVariables() = %v, want %v", p.Variables, tt.want)
|
||||||
}
|
}
|
||||||
|
@ -111,7 +107,6 @@ func TestAppArmorProfile_resolve(t *testing.T) {
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
p := &AppArmorProfile{
|
p := &AppArmorProfile{
|
||||||
Content: "",
|
|
||||||
Variables: tt.variables,
|
Variables: tt.variables,
|
||||||
Attachments: []string{},
|
Attachments: []string{},
|
||||||
}
|
}
|
||||||
|
@ -179,7 +174,6 @@ func TestAppArmorProfile_ResolveAttachments(t *testing.T) {
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
p := &AppArmorProfile{
|
p := &AppArmorProfile{
|
||||||
Content: "",
|
|
||||||
Variables: tt.variables,
|
Variables: tt.variables,
|
||||||
Attachments: []string{},
|
Attachments: []string{},
|
||||||
}
|
}
|
||||||
|
@ -233,7 +227,6 @@ func TestAppArmorProfile_NestAttachments(t *testing.T) {
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
p := &AppArmorProfile{
|
p := &AppArmorProfile{
|
||||||
Content: "",
|
|
||||||
Variables: map[string][]string{},
|
Variables: map[string][]string{},
|
||||||
Attachments: tt.Attachments,
|
Attachments: tt.Attachments,
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,8 +45,8 @@ func BuildComplain(profile string) string {
|
||||||
|
|
||||||
// Bypass userspace tools restriction
|
// Bypass userspace tools restriction
|
||||||
func BuildUserspace(profile string) string {
|
func BuildUserspace(profile string) string {
|
||||||
p := aa.NewAppArmorProfile(profile)
|
p := aa.NewAppArmorProfile()
|
||||||
p.ParseVariables()
|
p.ParseVariables(profile)
|
||||||
p.ResolveAttachments()
|
p.ResolveAttachments()
|
||||||
att := p.NestAttachments()
|
att := p.NestAttachments()
|
||||||
matches := regAttachments.FindAllString(profile, -1)
|
matches := regAttachments.FindAllString(profile, -1)
|
||||||
|
|
Loading…
Reference in a new issue