mirror of
https://github.com/roddhjav/apparmor.d.git
synced 2025-01-27 21:38:21 +01:00
refractor: rename some path util function.
This commit is contained in:
parent
ebdeef152c
commit
982c2c66aa
13 changed files with 87 additions and 82 deletions
|
@ -13,7 +13,6 @@ import (
|
||||||
"github.com/roddhjav/apparmor.d/pkg/aa"
|
"github.com/roddhjav/apparmor.d/pkg/aa"
|
||||||
"github.com/roddhjav/apparmor.d/pkg/logging"
|
"github.com/roddhjav/apparmor.d/pkg/logging"
|
||||||
"github.com/roddhjav/apparmor.d/pkg/paths"
|
"github.com/roddhjav/apparmor.d/pkg/paths"
|
||||||
"github.com/roddhjav/apparmor.d/pkg/util"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const usage = `aa [-h] [--lint | --format | --tree] [-s] [-F file] [profiles...]
|
const usage = `aa [-h] [--lint | --format | --tree] [-s] [-F file] [profiles...]
|
||||||
|
|
|
@ -10,7 +10,6 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/roddhjav/apparmor.d/pkg/paths"
|
"github.com/roddhjav/apparmor.d/pkg/paths"
|
||||||
"github.com/roddhjav/apparmor.d/pkg/util"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -21,7 +20,7 @@ var (
|
||||||
// mustReadProfileFile read a file and return its content as a slice of string.
|
// mustReadProfileFile read a file and return its content as a slice of string.
|
||||||
// It panics if an error occurs. It removes the last comment line.
|
// It panics if an error occurs. It removes the last comment line.
|
||||||
func mustReadProfileFile(path *paths.Path) string {
|
func mustReadProfileFile(path *paths.Path) string {
|
||||||
res := strings.Split(util.MustReadFile(path), "\n")
|
res := strings.Split(path.MustReadFileAsString(), "\n")
|
||||||
return strings.Join(res[:len(res)-2], "\n")
|
return strings.Join(res[:len(res)-2], "\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -108,7 +107,7 @@ func TestAppArmorProfileFile_String(t *testing.T) {
|
||||||
},
|
},
|
||||||
}},
|
}},
|
||||||
},
|
},
|
||||||
want: util.MustReadFile(testData.Join("string.aa")),
|
want: testData.Join("string.aa").MustReadFileAsString(),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
|
|
|
@ -8,8 +8,6 @@ import (
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/roddhjav/apparmor.d/pkg/util"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_tokenizeRule(t *testing.T) {
|
func Test_tokenizeRule(t *testing.T) {
|
||||||
|
@ -919,7 +917,7 @@ var (
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "string.aa",
|
name: "string.aa",
|
||||||
raw: util.MustReadFile(testData.Join("string.aa")),
|
raw: testData.Join("string.aa").MustReadFileAsString(),
|
||||||
apparmor: &AppArmorProfileFile{
|
apparmor: &AppArmorProfileFile{
|
||||||
Preamble: Rules{
|
Preamble: Rules{
|
||||||
&Comment{Base: Base{Comment: " Simple test profile for the AppArmorProfileFile.String() method", IsLineRule: true}},
|
&Comment{Base: Base{Comment: " Simple test profile for the AppArmorProfileFile.String() method", IsLineRule: true}},
|
||||||
|
@ -1017,7 +1015,7 @@ var (
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "full.aa",
|
name: "full.aa",
|
||||||
raw: util.MustReadFile(testData.Join("full.aa")),
|
raw: testData.Join("full.aa").MustReadFileAsString(),
|
||||||
apparmor: &AppArmorProfileFile{
|
apparmor: &AppArmorProfileFile{
|
||||||
Preamble: Rules{
|
Preamble: Rules{
|
||||||
&Comment{Base: Base{IsLineRule: true, Comment: " Simple test profile with all rules used"}},
|
&Comment{Base: Base{IsLineRule: true, Comment: " Simple test profile with all rules used"}},
|
||||||
|
|
|
@ -10,7 +10,6 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/roddhjav/apparmor.d/pkg/paths"
|
"github.com/roddhjav/apparmor.d/pkg/paths"
|
||||||
"github.com/roddhjav/apparmor.d/pkg/util"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -149,7 +148,7 @@ func (f *AppArmorProfileFile) resolveInclude(include *Include) error {
|
||||||
|
|
||||||
iFile := &AppArmorProfileFile{}
|
iFile := &AppArmorProfileFile{}
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
raw, err := util.ReadFile(file)
|
raw, err := file.ReadFileAsString()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,9 +35,12 @@ import (
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/roddhjav/apparmor.d/pkg/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Path represents a path
|
// Path represents a path
|
||||||
|
@ -360,6 +363,31 @@ func (p *Path) CopyTo(dst *Path) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CopyTo recursivelly copy all files from a source path to a destination path.
|
||||||
|
func CopyTo(src *Path, dst *Path) error {
|
||||||
|
files, err := src.ReadDirRecursiveFiltered(nil,
|
||||||
|
FilterOutDirectories(),
|
||||||
|
FilterOutNames("README.md"),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, file := range files {
|
||||||
|
destination, err := file.RelFrom(src)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
destination = dst.JoinPath(destination)
|
||||||
|
if err := destination.Parent().MkdirAll(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := file.CopyTo(destination); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// CopyDirTo recursively copies the directory denoted by the current path to
|
// CopyDirTo recursively copies the directory denoted by the current path to
|
||||||
// the destination path. The source directory must exist and the destination
|
// the destination path. The source directory must exist and the destination
|
||||||
// directory must NOT exist (no implicit destination name allowed).
|
// directory must NOT exist (no implicit destination name allowed).
|
||||||
|
@ -460,6 +488,24 @@ func WriteToTempFile(data []byte, dir *Path, prefix string) (res *Path, err erro
|
||||||
return New(f.Name()), nil
|
return New(f.Name()), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ReadFileAsString read a file and return its content as a string.
|
||||||
|
func (p *Path) ReadFileAsString() (string, error) {
|
||||||
|
content, err := p.ReadFile()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return string(content), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// MustReadFileAsString read a file and return its content as a string. Panic if an error occurs.
|
||||||
|
func (p *Path) MustReadFileAsString() string {
|
||||||
|
content, err := p.ReadFile()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return string(content)
|
||||||
|
}
|
||||||
|
|
||||||
// ReadFileAsLines reads the file named by filename and returns it as an
|
// ReadFileAsLines reads the file named by filename and returns it as an
|
||||||
// array of lines. This function takes care of the newline encoding
|
// array of lines. This function takes care of the newline encoding
|
||||||
// differences between different OS
|
// differences between different OS
|
||||||
|
@ -473,6 +519,33 @@ func (p *Path) ReadFileAsLines() ([]string, error) {
|
||||||
return strings.Split(txt, "\n"), nil
|
return strings.Split(txt, "\n"), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MustReadFileAsLines read a file and return its content as a slice of string. Panic if an error occurs.
|
||||||
|
func (p *Path) MustReadFileAsLines() []string {
|
||||||
|
lines, err := p.ReadFileAsLines()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return lines
|
||||||
|
}
|
||||||
|
|
||||||
|
// MustReadFilteredFileAsLines read a file and return its content as a slice of string.
|
||||||
|
// It filter out comments and empty lines. Panic if an error occurs.
|
||||||
|
func (p *Path) MustReadFilteredFileAsLines() []string {
|
||||||
|
data, err := p.ReadFile()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
txt := string(data)
|
||||||
|
txt = strings.Replace(txt, "\r\n", "\n", -1)
|
||||||
|
txt = util.Filter(txt)
|
||||||
|
res := strings.Split(txt, "\n")
|
||||||
|
if slices.Contains(res, "") {
|
||||||
|
idx := slices.Index(res, "")
|
||||||
|
res = slices.Delete(res, idx, idx+1)
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
// Truncate create an empty file named by path or if the file already
|
// Truncate create an empty file named by path or if the file already
|
||||||
// exist it truncates it (delete all contents)
|
// exist it truncates it (delete all contents)
|
||||||
func (p *Path) Truncate() error {
|
func (p *Path) Truncate() error {
|
||||||
|
|
|
@ -15,7 +15,6 @@ import (
|
||||||
"github.com/roddhjav/apparmor.d/pkg/prebuild/builder"
|
"github.com/roddhjav/apparmor.d/pkg/prebuild/builder"
|
||||||
"github.com/roddhjav/apparmor.d/pkg/prebuild/directive"
|
"github.com/roddhjav/apparmor.d/pkg/prebuild/directive"
|
||||||
"github.com/roddhjav/apparmor.d/pkg/prebuild/prepare"
|
"github.com/roddhjav/apparmor.d/pkg/prebuild/prepare"
|
||||||
"github.com/roddhjav/apparmor.d/pkg/util"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -138,7 +137,7 @@ func Build() error {
|
||||||
if !file.Exist() {
|
if !file.Exist() {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
profile, err := util.ReadFile(file)
|
profile, err := file.ReadFileAsString()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,7 +13,6 @@ import (
|
||||||
|
|
||||||
"github.com/roddhjav/apparmor.d/pkg/aa"
|
"github.com/roddhjav/apparmor.d/pkg/aa"
|
||||||
"github.com/roddhjav/apparmor.d/pkg/prebuild"
|
"github.com/roddhjav/apparmor.d/pkg/prebuild"
|
||||||
"github.com/roddhjav/apparmor.d/pkg/util"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Exec struct {
|
type Exec struct {
|
||||||
|
@ -44,7 +43,7 @@ func (d Exec) Apply(opt *Option, profileRaw string) (string, error) {
|
||||||
|
|
||||||
rules := aa.Rules{}
|
rules := aa.Rules{}
|
||||||
for name := range opt.ArgMap {
|
for name := range opt.ArgMap {
|
||||||
profiletoTransition := util.MustReadFile(prebuild.RootApparmord.Join(name))
|
profiletoTransition := prebuild.RootApparmord.Join(name).MustReadFileAsString()
|
||||||
dstProfile := aa.DefaultTunables()
|
dstProfile := aa.DefaultTunables()
|
||||||
if _, err := dstProfile.Parse(profiletoTransition); err != nil {
|
if _, err := dstProfile.Parse(profiletoTransition); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
|
|
|
@ -55,7 +55,7 @@ func (s Stack) Apply(opt *Option, profile string) (string, error) {
|
||||||
|
|
||||||
res := ""
|
res := ""
|
||||||
for name := range opt.ArgMap {
|
for name := range opt.ArgMap {
|
||||||
stackedProfile := util.MustReadFile(prebuild.RootApparmord.Join(name))
|
stackedProfile := prebuild.RootApparmord.Join(name).MustReadFileAsString()
|
||||||
m := regRules.FindStringSubmatch(stackedProfile)
|
m := regRules.FindStringSubmatch(stackedProfile)
|
||||||
if len(m) < 2 {
|
if len(m) < 2 {
|
||||||
return "", fmt.Errorf("No profile found in %s", name)
|
return "", fmt.Errorf("No profile found in %s", name)
|
||||||
|
|
|
@ -8,7 +8,6 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/roddhjav/apparmor.d/pkg/paths"
|
"github.com/roddhjav/apparmor.d/pkg/paths"
|
||||||
"github.com/roddhjav/apparmor.d/pkg/util"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Default content of debian/apparmor.d.hide. Whonix has special addition.
|
// Default content of debian/apparmor.d.hide. Whonix has special addition.
|
||||||
|
@ -29,7 +28,7 @@ func (f Flagger) Read(name string) map[string][]string {
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
lines := util.MustReadFileAsLines(path)
|
lines := path.MustReadFilteredFileAsLines()
|
||||||
for _, line := range lines {
|
for _, line := range lines {
|
||||||
manifest := strings.Split(line, " ")
|
manifest := strings.Split(line, " ")
|
||||||
profile := manifest[0]
|
profile := manifest[0]
|
||||||
|
@ -49,7 +48,7 @@ func (i Ignorer) Read(name string) []string {
|
||||||
if !path.Exist() {
|
if !path.Exist() {
|
||||||
return []string{}
|
return []string{}
|
||||||
}
|
}
|
||||||
return util.MustReadFileAsLines(path)
|
return path.MustReadFilteredFileAsLines()
|
||||||
}
|
}
|
||||||
|
|
||||||
type DebianHider struct {
|
type DebianHider struct {
|
||||||
|
|
|
@ -10,7 +10,6 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/roddhjav/apparmor.d/pkg/prebuild"
|
"github.com/roddhjav/apparmor.d/pkg/prebuild"
|
||||||
"github.com/roddhjav/apparmor.d/pkg/util"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -44,7 +43,7 @@ func (p SetFlags) Apply() ([]string, error) {
|
||||||
// Overwrite profile flags
|
// Overwrite profile flags
|
||||||
if len(flags) > 0 {
|
if len(flags) > 0 {
|
||||||
flagsStr := " flags=(" + strings.Join(flags, ",") + ") {\n"
|
flagsStr := " flags=(" + strings.Join(flags, ",") + ") {\n"
|
||||||
out, err := util.ReadFile(file)
|
out, err := file.ReadFileAsString()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,7 +35,7 @@ func (p FullSystemPolicy) Apply() ([]string, error) {
|
||||||
|
|
||||||
// Set systemd profile name
|
// Set systemd profile name
|
||||||
path := prebuild.RootApparmord.Join("tunables/multiarch.d/system")
|
path := prebuild.RootApparmord.Join("tunables/multiarch.d/system")
|
||||||
out, err := util.ReadFile(path)
|
out, err := path.ReadFileAsString()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
@ -47,7 +47,7 @@ func (p FullSystemPolicy) Apply() ([]string, error) {
|
||||||
|
|
||||||
// Fix conflicting x modifiers in abstractions - FIXME: Temporary solution
|
// Fix conflicting x modifiers in abstractions - FIXME: Temporary solution
|
||||||
path = prebuild.RootApparmord.Join("abstractions/gstreamer")
|
path = prebuild.RootApparmord.Join("abstractions/gstreamer")
|
||||||
out, err = util.ReadFile(path)
|
out, err = path.ReadFileAsString()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,6 @@ import (
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/roddhjav/apparmor.d/pkg/prebuild"
|
"github.com/roddhjav/apparmor.d/pkg/prebuild"
|
||||||
"github.com/roddhjav/apparmor.d/pkg/util"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const ext = ".apparmor.d"
|
const ext = ".apparmor.d"
|
||||||
|
@ -44,7 +43,7 @@ func (p Overwrite) Apply() ([]string, error) {
|
||||||
if !path.Exist() {
|
if !path.Exist() {
|
||||||
return res, fmt.Errorf("%s not found", path)
|
return res, fmt.Errorf("%s not found", path)
|
||||||
}
|
}
|
||||||
for _, name := range util.MustReadFileAsLines(path) {
|
for _, name := range path.MustReadFilteredFileAsLines() {
|
||||||
origin := prebuild.RootApparmord.Join(name)
|
origin := prebuild.RootApparmord.Join(name)
|
||||||
dest := prebuild.RootApparmord.Join(name + ext)
|
dest := prebuild.RootApparmord.Join(name + ext)
|
||||||
if !dest.Exist() && p.OneFile {
|
if !dest.Exist() && p.OneFile {
|
||||||
|
|
|
@ -7,10 +7,6 @@ package util
|
||||||
import (
|
import (
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"regexp"
|
"regexp"
|
||||||
"slices"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/roddhjav/apparmor.d/pkg/paths"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -67,61 +63,7 @@ func DecodeHexInString(str string) string {
|
||||||
return str
|
return str
|
||||||
}
|
}
|
||||||
|
|
||||||
// CopyTo recursivelly copy all files from a source path to a destination path.
|
|
||||||
func CopyTo(src *paths.Path, dst *paths.Path) error {
|
|
||||||
files, err := src.ReadDirRecursiveFiltered(nil,
|
|
||||||
paths.FilterOutDirectories(),
|
|
||||||
paths.FilterOutNames("README.md"),
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
for _, file := range files {
|
|
||||||
destination, err := file.RelFrom(src)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
destination = dst.JoinPath(destination)
|
|
||||||
if err := destination.Parent().MkdirAll(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := file.CopyTo(destination); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Filter out comments and empty line from a string
|
// Filter out comments and empty line from a string
|
||||||
func Filter(src string) string {
|
func Filter(src string) string {
|
||||||
return regFilter.Replace(src)
|
return regFilter.Replace(src)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadFile read a file and return its content as a string.
|
|
||||||
func ReadFile(path *paths.Path) (string, error) {
|
|
||||||
content, err := path.ReadFile()
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
return string(content), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// MustReadFile read a file and return its content as a string. Panic if an error occurs.
|
|
||||||
func MustReadFile(path *paths.Path) string {
|
|
||||||
content, err := path.ReadFile()
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
return string(content)
|
|
||||||
}
|
|
||||||
|
|
||||||
// MustReadFileAsLines read a file and return its content as a slice of string.
|
|
||||||
// It panics if an error occurs and filter out comments and empty lines.
|
|
||||||
func MustReadFileAsLines(path *paths.Path) []string {
|
|
||||||
res := strings.Split(Filter(MustReadFile(path)), "\n")
|
|
||||||
if slices.Contains(res, "") {
|
|
||||||
idx := slices.Index(res, "")
|
|
||||||
res = slices.Delete(res, idx, idx+1)
|
|
||||||
}
|
|
||||||
return res
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue