118 Commits

Author SHA1 Message Date
Jorge Sancho Larraz
265b7c44d1 sandbox/apparmor: aare exclusion rule generation (#13488)
* sandbox/apparmor: add GenerateAAREExclusionPatterns

This function is generic (and complex) enough to be able to handle all of the
overlapping and wildcard behavior we need in docker-support, and it could also
serve to replace numerous other places in the codebase where we need this sort
of complex behavior. It is a generalization of the existing
aareExclusionPatterns helper, though it's actually unclear if this exact
implementation will currently be able to serve the use case from that helper
directly or if more options/adjustments are needed to enable that use case as
well.

To keep the diff smaller, this patch does not actually change any of the
profiles/interfaces, just TODO's are left for where to use it.

Note that the generated rules are slightly more condensed in terms of number of
rules but significantly more verbose in terms of alternations, not sharing more
of repeated substrings between alternations inside the patterns. This was done
explicitly to keep the generating code simpler and easier to understand, but it
may prove to have performance effects, either detrimental or benevolent but
that should be measured before deciding to make the generation code even more
complex than it already is.

Signed-off-by: Ian Johnson <ian.johnson@canonical.com>

* interfaces/docker-support: generate AARE exclusion patterns with helper func

Signed-off-by: Ian Johnson <ian.johnson@canonical.com>

* sandbox/apparmor: unexport helper functions

These were not meant to be exported, only the fully generic one is meant to be
exported.

Signed-off-by: Ian Johnson <ian.johnson@canonical.com>

* sandbox/apparmor: fix bug mis-sorting capitalized letters in AARE exclude patt

Thanks to Alberto for spotting this :-)

Signed-off-by: Ian Johnson <ian.johnson@canonical.com>

* sandbox/apparmor: fix format issues introduced during rebase

* sandbox/apparmor: simplify generateAAREExclusionPatternsGenericImpl

* sandbox/apparmor: add checks for unsupported cases and improve documentation

* sandbox/apparmor: update tests to compare the apparmor binary instead of source

* interfaces/builtin/docker_support: check if userns is supported before adding it to the profile

* interfaces/builtin/docker_support: fix dependencies

* sandbox/apparmor: use placeholders

* i/b/docker_support_test: update TestGenerateAAREExclusionPatterns to use SnapAppSet

* testutil/apparmor: use go crypto/sha1 module instead of system sha1sum command

* {sandbox,testutil}/apparmor: minor format fixes

* move helper to find common prefix to strutil

* add copyright info

* use string builder

* i/b/docker_support_test.go: update accordingly to 277fbc266e (many: add components to interfaces.SnapAppSet (#13837))

* strutil/commonprefix.go: remove extra empty line

* sandbox/apparmor/apparmor.go: sort prefixes to ensure profile is always the same

* sandbox/apparmor/apparmor.go: remove extra empty line

* i/b/docker_support_test: skip TestGenerateAAREExclusionPatterns is apparmor_parser is not usable

---------

Signed-off-by: Ian Johnson <ian.johnson@canonical.com>
Co-authored-by: Ian Johnson <ian.johnson@canonical.com>
2024-07-04 12:23:08 +02:00
Ernest Lotter
fdc90dfe41 many: ensure-dir mounts for personal-files missing dirs (#13260)
* many: ensure-dir mount entries from personal-files write attrs

* many: review improvements

* strutil: make pathiter current path slash trimming use existing method

* osutil, strutil: more review fixes

* i, i/apparmor, i/builtin, osutil: improve unit test coverage

* i, i/apparmor, i/builtin, i/mount: review improvements

* strutil: improve comment

* interfaces/apparmor: allow snap-update-ns to open home directory

* tests: revert interfaces-personal-files changes to simplify merge

* interfaces/builtin: improve plug connect error message

Co-authored-by: Miguel Pires <miguelpires94@gmail.com>

* interfaces/builtin: fixed ut

---------

Co-authored-by: Miguel Pires <miguelpires94@gmail.com>
2023-12-15 15:25:49 +02:00
Ben Hoyt
8a9b69a61a strutil: add MultiCommaSeparatedList
This new function is useful for parsing `url.Values` where there may be
multiple values for the same key, and those values may themselves be
comma-separated values.

This is a port of a commit from the `x-go` package, which was originally
intended to be used in pebble. The commit can be found here:

32684ae6fb

Signed-off-by: Oliver Calder <oliver.calder@canonical.com>
2023-12-06 16:09:03 -06:00
Dimitri John Ledkov
d55f3123d4 go: go fix with 1.18
Apply `go fix ./...` to the whole code base using go-1.18 to remove
old build tags.
2023-09-25 13:22:14 +02:00
Miguel Pires
598df3621a daemon: add aspects endpoint to REST API
Signed-off-by: Miguel Pires <miguel.pires@canonical.com>
2023-06-13 17:36:48 +02:00
Philip Meulengracht
ae6489f798 golang: remove deadcode linter 2023-03-09 14:43:47 +01:00
Alfonso Sánchez-Beato
4d8f4df11c strutil: introduce JoinNonEmpty
JoinNonEmpty concatenates non-empty strings with the specified
separator. This overcomes a problem with strings.Join, which
introduces separators for empty strings.
2023-02-27 15:47:07 +01:00
Miguel Pires
d097436c1c many: fix formatting w/ gofmt 1.19
Go 1.19 includes some changes to gofmt which intend to make lists and
heading clearer when rendered (https://go.dev/doc/go1.19). This commit
is the result of running the new gofmt and manually fixing some of it.
This was necessary because the new gofmt assumed lines beginning w/ tabs
to start lists or examples. While this is often true in our codebase,
we occasionally also use tabs to indent the lines after a TODO or FIXME
prefix or in yaml (e.g., excerpts of a snap.yaml). This meant that a lot of the
reformatted comments were broken and had to be fixed manually.

Signed-off-by: Miguel Pires <miguel.pires@canonical.com>
2023-01-16 14:23:11 +01:00
Fred Lotter
bb2514455d strutil: go staticcheck (S1008) code simplification
strutil/version.go:104:2: should use 'return !matchEpoch(a)'
instead of 'if matchEpoch(a) { return false }; return true' (S1008)
2023-01-10 16:58:10 +00:00
Alberto Mardegan
c45782e846 many: don't concatenate non-constant format strings
We should be careful not to concatenate variable strings into the first
argument of Sprintf/Printf/Errorf: if these variable strings end up
containing a percent character, it will break the way that the printfs
arguments are interpreted. Luckily, golang is smarter than C and is able
to detect mismatches between the number of '%' and the number of
arguments, but it still can lead to unexpected results:

    $ sudo snap set core refresh.rate-limit='%[2]#v'
    error: cannot perform the following tasks:
    - Run configure hook of "core" snap (run hook "configure": cannot parse "%!#(BADINDEX)v": no numerical prefix)

also:

    $ sudo snap set core refresh.rate-limit='%#v'
    error: cannot perform the following tasks:
    - Run configure hook of "core" snap (run hook "configure": cannot parse "&errors.errorString{s:"no numerical prefix"}": %!s(MISSING))

Moreover, it appears that all the occurrences of such pattern in our
code are situated either on unprivileged processes (like the `snap`
client), or, when in snapd, can only be triggered by the root user
(notice the `sudo` in the commands above).

Nevertheless, let's be defensive and fix these.

There are also other occurrences of concatenations in formatting
strings, but those are only constants so they don't pose a problem. But
to avoid the risk of these strings getting updated in the future with a
mutable version, let's explicitly mark these format prefixes as `const`.
2022-09-20 17:16:22 +03:00
Michael Vogt
57952322d9 strutil: unexport VersionIsValid
We are not using `strutil.VersionIsValid()` anywhere outside of the
strutil package so unexport it.
2022-08-15 18:49:45 +02:00
Michael Vogt
c850c912cc strutil: fix VersionCompare() to allow multiple - in the version
The current code in strutil.VersionCompare() is not fully
compatible with what a debian version allows. Specifically
the version number of `libgcc-s1` in 22.04 is valid
(`12-20220319-1ubuntu1`) but it's rejected right now.

The debian policy [1] says:
```
The upstream_version may contain only alphanumerics[14] and the
characters . + - : (full stop, plus, hyphen, colon) and should
start with a digit. If there is no debian_revision then hyphens
are not allowed; if there is no epoch then colons are not allowed.
```
and python-apt/apt/dpkg are all happy with the version. Given that
we generally follow the debian policy for this we should fix this.

Thanks to Gustavo for pointing this out.

[1] https://www.debian.org/doc/debian-policy/ch-controlfields.html#version
2022-08-15 18:49:41 +02:00
Philip Meulengracht
2dec53312a strutil: replace manually trim logic with stdlib
review feedback
2022-03-31 12:04:05 +02:00
Philip Meulengracht
fad3dd0ea3 cmd/snap,strutil: move lineWrap to WordWrapPadded
moved to strutil as I need it for snapctl model
2022-03-31 11:00:48 +02:00
Philip Meulengracht
fc051378e1 cmd/snap,strutil: move unit test for WordWrap 2022-03-28 13:33:49 +02:00
Philip Meulengracht
a40188c56e strutil: move WordWrap function to strutil
will need this function for the model command in snapctl
2022-03-25 14:18:59 +01:00
Philip Meulengracht
0d1c1f24d5 multiple: review feedback
Always write CPUAccounting, improve usage of fmt and bytes.Buffer, improve docs, fix typos
2022-03-16 19:29:58 +01:00
Philip Meulengracht
b1d3d60f7a many: review feedback
update error messages for resource validation, add missing test for changing limits inside quota.go
2022-03-14 10:11:36 +01:00
Philip Meulengracht
1ce22daaf1 multiple: first part of review feedback
renamings, better docs
2022-02-28 16:55:15 +01:00
Philip Meulengracht
6f92ff61b3 strutil: add unit tests for SliceToCommaSeparatedString 2022-02-25 12:43:26 +01:00
Philip Meulengracht
be20d29a81 wrappers: review feedback
add new function []int => string in strutils, use that instead of chained string functions
2022-02-24 14:19:42 +01:00
Miguel Pires
615da7771d many: fix failing golangci checks
* Group 3rd party imports
* Remove unused vars
* Move var only used in tests into test
* Add 'nolint' to ignore warning on unused iotas (might've been
left to improve readability).
2022-02-09 10:12:09 +00:00
Miguel Pires
13209fc52c many: fix formatting w/ latest go version
Fix formatting when running the latest version (1.17) of gofmt.
2021-12-08 13:59:56 +00:00
Miguel Pires
6795a00dc6 strutil: improve test case 2021-10-21 11:01:02 +01:00
Miguel Pires
0e16c1040c o/snapstate: deduplicate snap names in install, remove and update 2021-10-20 15:48:08 +01:00