apparmorutils.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package apparmorutils
  15. import (
  16. "fmt"
  17. "os"
  18. "strings"
  19. "yunion.io/x/pkg/errors"
  20. "yunion.io/x/pkg/utils"
  21. "yunion.io/x/onecloud/pkg/util/procutils"
  22. )
  23. const (
  24. AppArmorEnabledFile = "/sys/module/apparmor/parameters/enabled"
  25. )
  26. func IsEnabled() bool {
  27. content, err := os.ReadFile(AppArmorEnabledFile)
  28. if err != nil {
  29. return false
  30. }
  31. return strings.TrimSpace(string(content)) == "Y"
  32. }
  33. func Parser(profile string) error {
  34. tmpFileName := fmt.Sprintf("/tmp/apparmor-profile-%s.tmp", utils.GenRequestId(12))
  35. err := procutils.FilePutContents(tmpFileName, profile)
  36. if err != nil {
  37. return errors.Wrap(err, "FilePutContents")
  38. }
  39. defer func() {
  40. procutils.NewRemoteCommandAsFarAsPossible("rm", "-f", tmpFileName).Run()
  41. }()
  42. err = procutils.NewRemoteCommandAsFarAsPossible("apparmor_parser", "-r", "-W", tmpFileName).Run()
  43. if err != nil {
  44. return errors.Wrap(err, "apparmor_parser")
  45. }
  46. return nil
  47. }