aaparser.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Package aaparser is a convenience package interacting with `apparmor_parser`.
  2. package aaparser // import "github.com/docker/docker/pkg/aaparser"
  3. import (
  4. "fmt"
  5. "os/exec"
  6. "strconv"
  7. "strings"
  8. )
  9. const (
  10. binary = "apparmor_parser"
  11. )
  12. // GetVersion returns the major and minor version of apparmor_parser.
  13. //
  14. // Deprecated: no longer used, and will be removed in the next release.
  15. func GetVersion() (int, error) {
  16. output, err := cmd("", "--version")
  17. if err != nil {
  18. return -1, err
  19. }
  20. return parseVersion(output)
  21. }
  22. // LoadProfile runs `apparmor_parser -Kr` on a specified apparmor profile to
  23. // replace the profile. The `-K` is necessary to make sure that apparmor_parser
  24. // doesn't try to write to a read-only filesystem.
  25. func LoadProfile(profilePath string) error {
  26. _, err := cmd("", "-Kr", profilePath)
  27. return err
  28. }
  29. // cmd runs `apparmor_parser` with the passed arguments.
  30. func cmd(dir string, arg ...string) (string, error) {
  31. c := exec.Command(binary, arg...)
  32. c.Dir = dir
  33. output, err := c.CombinedOutput()
  34. if err != nil {
  35. return "", fmt.Errorf("running `%s %s` failed with output: %s\nerror: %v", c.Path, strings.Join(c.Args, " "), output, err)
  36. }
  37. return string(output), nil
  38. }
  39. // parseVersion takes the output from `apparmor_parser --version` and returns
  40. // a representation of the {major, minor, patch} version as a single number of
  41. // the form MMmmPPP {major, minor, patch}.
  42. func parseVersion(output string) (int, error) {
  43. // output is in the form of the following:
  44. // AppArmor parser version 2.9.1
  45. // Copyright (C) 1999-2008 Novell Inc.
  46. // Copyright 2009-2012 Canonical Ltd.
  47. lines := strings.SplitN(output, "\n", 2)
  48. words := strings.Split(lines[0], " ")
  49. version := words[len(words)-1]
  50. // trim "-beta1" suffix from version="3.0.0-beta1" if exists
  51. version = strings.SplitN(version, "-", 2)[0]
  52. // also trim "~..." suffix used historically (https://gitlab.com/apparmor/apparmor/-/commit/bca67d3d27d219d11ce8c9cc70612bd637f88c10)
  53. version = strings.SplitN(version, "~", 2)[0]
  54. // split by major minor version
  55. v := strings.Split(version, ".")
  56. if len(v) == 0 || len(v) > 3 {
  57. return -1, fmt.Errorf("parsing version failed for output: `%s`", output)
  58. }
  59. // Default the versions to 0.
  60. var majorVersion, minorVersion, patchLevel int
  61. majorVersion, err := strconv.Atoi(v[0])
  62. if err != nil {
  63. return -1, err
  64. }
  65. if len(v) > 1 {
  66. minorVersion, err = strconv.Atoi(v[1])
  67. if err != nil {
  68. return -1, err
  69. }
  70. }
  71. if len(v) > 2 {
  72. patchLevel, err = strconv.Atoi(v[2])
  73. if err != nil {
  74. return -1, err
  75. }
  76. }
  77. // major*10^5 + minor*10^3 + patch*10^0
  78. numericVersion := majorVersion*1e5 + minorVersion*1e3 + patchLevel
  79. return numericVersion, nil
  80. }