Commit graph

27 commits

Author SHA1 Message Date
Achilleas Koutsou
00d473c802 container: create a SourceSpec for the resolve parameters
Create a struct to conveniently hold the source parameters for a
container resolve request and use it in the Add() function of the
resolver.
2023-05-31 16:40:07 +02:00
Achilleas Koutsou
3ff1b3beb0 container: update unit tests to match expected behaviour 2023-04-19 20:07:40 +02:00
Achilleas Koutsou
f9e3d8659d container: explicitly set LocalName
Set the LocalName for the spec using a separate argument in the
NewSpec() constructor instead of reusing the `source` arg.
The name is already available in the calling scope in the client's
Resolve() method.

If the LocalName is an empty string, default to the remote (source)
reference.  This is a change from the previous behaviour which only used
the base source.Name().  The full source corresponds to the
user-provided source value, which includes any specified tag or digest.

The `name` argument which is used in the `Resolve()` function should
always correspond to the user-provided container name.
2023-04-19 20:07:40 +02:00
Achilleas Koutsou
ab9e612c99 containers: update tests
Update tests to include the list digests of the test images.
2023-04-17 18:30:41 +02:00
Achilleas Koutsou
e5aaf3e514 container: save manifest list digest when resolving
While resolving a manifest list digest, store the list digest to return
with the resolvedIds.

This is done for both types of manifest list:
  application/vnd.docker.distribution.manifest.list.v2+json
and
  application/vnd.oci.image.index.v1+json
2023-04-17 18:30:41 +02:00
Achilleas Koutsou
f38eb8406a container: include TLSVerify and ListDigest in spec ctor 2023-04-17 18:30:41 +02:00
Achilleas Koutsou
a7dd8ff94f container: add ListDigest to the spec
Add the ListDigest to the container Spec struct and all its copies so we
can store list digests when they are available and pass them on to the
appropriate osbuild stages, sources, and inputs.

Copy the value whenever a spec is moved to a different representation.
2023-04-17 18:30:41 +02:00
Brian C. Lane
7a4bb863dd Update deprecated io/ioutil functions
ioutil has been deprecated since go 1.16, this fixes all of the
deprecated functions we are using:

ioutil.ReadFile -> os.ReadFile
ioutil.ReadAll -> io.ReadAll
ioutil.WriteFile -> os.WriteFile
ioutil.TempFile -> os.CreateTemp
ioutil.TempDir -> os.MkdirTemp

All of the above are a simple name change, the function arguments and
results are exactly the same as before.

ioutil.ReadDir -> os.ReadDir

now returns a os.DirEntry but the IsDir and Name functions work the
same. The difference is that the FileInfo must be retrieved with the
Info() function which can also return an error.

These were identified by running:
golangci-lint run --build-tags=integration ./...
2023-03-07 09:22:23 -08:00
Ondřej Budai
b997142db0 common: merge all *ToPtr methods to one generic ToPtr
After introducing Go 1.18 to a project, it's required by law to convert at
least one method to a generic one.

Everyone hates IntToPtr, StringToPtr, BoolToPtr and Uint64ToPtr, so let's
convert them to the ultimate generic ToPtr one.

Signed-off-by: Ondřej Budai <ondrej@budai.cz>
2023-01-09 14:03:18 +01:00
Tomáš Hozza
baa5e96734 internal/container: delete unreachable code
Signed-off-by: Tomáš Hozza <thozza@redhat.com>
2022-11-07 16:10:41 +01:00
Ygal Blum
64a3aac895 job/osbuild: move utility the container package
Extract a helper method that parses the reference and applies
defaults, i.e. registry and path, and move it to the contaier
package.
2022-08-01 21:50:03 +01:00
Ygal Blum
3231aabbc0 cloudapi: add support for uploading to a container registry
Worker
------
Add configuration for the default container registry.
Use the default container registry if not provided as part
of the image name.
When using the default registry use the configured values
Return the image url as part of the result.

Composer Worker API
-------------------
Add `ContainerTargetResultOptions` to return the image url

Composer API
------------
Add UploadOptions to allow setting of the image name and tag
Add UploadStatus to return the url of the uploaded image

Co-Developed-By: Christian Kellner <christian@kellner.me>
2022-08-01 21:50:03 +01:00
Tomas Hozza
322ae02f73 internal/container: delete leftover dead code
This issue was found by Coverity:

Error: DEADCODE (CWE-561): [#def1]
osbuild-composer-58/_build/src/github.com/osbuild/osbuild-composer/internal/container/client.go:386: cond_null: Condition "err != nil", taking false branch. Now the value of "err" is "nil".
osbuild-composer-58/_build/src/github.com/osbuild/osbuild-composer/internal/container/client.go:390: null: At condition "err != nil", the value of "err" must be "nil".
osbuild-composer-58/_build/src/github.com/osbuild/osbuild-composer/internal/container/client.go:390: dead_error_condition: The condition "err != nil" cannot be true.
osbuild-composer-58/_build/src/github.com/osbuild/osbuild-composer/internal/container/client.go:391: dead_error_line: Execution cannot reach this statement: "<temporary>.$0 = container....".
  389|
  390|   	if err != nil {
  391|-> 		return resolvedIds{}, nil
  392|   	}
  393|
2022-07-29 23:20:09 +02:00
Xiaofeng Wang
c71ae8f455 test: Update tests to support embeded container image feature
Container image can only be embeded on commit Use fedora image
instead of ubi8 and only run embeded image checking on  RHEL
8.7, 9.1, CS8 and CS9.
2022-07-26 17:07:31 +02:00
Christian Kellner
34c94ab92b container: rework GetDefaultAuthFile and don't cache its result
Instead of using a cached result `GetDefaultAuthFile`, always
do call the function when a new `Client` is created, since at
least `/run/containers` can get created as a side-effect by
one of the container. Now that we check eagerly and often the
path check function was reworked to only return paths that do
exist and are accessible.

Also check if `REGISTRY_AUTH_FILE` is set and if so, and it
is accessible use that.

To check accessability, use `unix.Access` instead of `os.Stat`,
since On Fedora/RHEL 9 `os.Stat` is implemented via `statx` and
will indeed return `EACCES` for inaccessible paths. But on RHEL
8 `lstat` is used and that will return `ENOENT` but then later
when trying to open the file we will get `EPERM`.
2022-07-26 17:07:31 +02:00
Christian Kellner
e290502a1d container: add a check to ensure auth file path is always set
We never want an empty path but always force a specific auth
file location, even if the location does not actually exist,
due to the peculiarities mentioned in the comment of the
`container.GetDefaultAuthFile` function.
2022-07-26 17:07:31 +02:00
Christian Kellner
e38e7c717d container: add a auth file path getter for Client
This is so we can check and verify what is set.
2022-07-26 17:07:31 +02:00
Christian Kellner
c2f3f76d96 container: ability to set AuthFilePath for Resolver
Add a new field `AuthFilePath` and if it is not the empty string,
it will be forwaded that information to `Client`.
2022-07-25 21:21:44 +02:00
Christian Kellner
b4f890a909 container: add tests for resolver
Add checks for the new `container.Resolver`, including the failure
case.
2022-07-21 13:32:07 +02:00
Christian Kellner
76d80295fa container: add new resolver helper
Add a new class `container.Resolver` which can be used to resolve
multiple container images to their respective ids in parallel.
It should make it easy for all existing tools and api endpoints
to adpot container resultion.
2022-07-21 13:32:07 +02:00
Christian Kellner
dcbdcf4419 container: simple client resolver tests
Add some basic checks for the new `client.Resolve` method.
Specifically that pinning down the ids is working as expected.
2022-07-21 13:32:07 +02:00
Christian Kellner
60135dd5df container: add mock container registry for testing
Create a small only mock container registry to test `Client`.
Currently the registry is read-only and thus cannot be used
for upload tests but it can and will be used for container
resolution checks.
2022-07-21 13:32:07 +02:00
Christian Kellner
60607af26c container: ability to resolve containers to specs
Add a new `Resolve` method to `Client` that will resolve its `Target`
to the corresponding manifest digest id and its corresponding iamge
identifier. The former can be used in the URL to fetch a specific
image from the registry via `<name>@<digest>` and the latter uniquely
identifies a container image via the hash of its configuration object.
This should stay the same across pulls and is also the id returned via
`podman pull` and `podman images`.
Since (most) container images are OS and architecture specific a tag
often points to a manifest list that contains all available options.
Therefore the resolve operation needs to choose the correct arch for
image. A new pair of getters `Set{Architecture,Variant}Choice` lets
the user control which architecture/variant is selected during the
resolution process.
2022-07-21 13:32:07 +02:00
Christian Kellner
bd42243882 container: set default auth file path to sane location
Ensure that the `Client.AuthFilePath` points to a sane location,
which here means that the location is either accessible by the
current user or does not exist. This is because any other error
opening the auth file with lead to a overall failure when trying
to access container registries, even if the target resources is
public.
The reason we have to set it ourselves is that by default the
containers library looks in a sub-path of `XDG_RUNTIME_DIR` and if
that variable is not set it falls-back to `/run/containers/<uid>`.
Since `XDG_RUNTIME_DIR` is indeed not set for the composer process
started via systemd, it will fall-back, but it does not have access
to `/run/containers` and finding the authorization info for any
request will fail with "permission denied".
Add a setter so that we can set the `Client.AuthFilePath` to a
different location than the default one.
2022-07-21 13:32:07 +02:00
Christian Kellner
e53b9c8bb2 container/client: rework credentials storage
Instead of keeping an extra field in `Client`, we just use the
existing `sysCtx.DockerAuthConfig` structure. When the context
is later copied during the upload operation the credentials
will be copied as well. It also saves us from syncing the
credentials if we directly use said `sysCtx` for operations.
2022-07-21 13:32:07 +02:00
Christian Kellner
865a899f70 container/client: rework tls settings
Instead of having an extra field, `TlsVerify`, on the `Client` and
then later setting the corresponding `SystemContext` options, use
the existing `SystemContext` field of `Client`. The corresponding
field is a tri-state: unset, true, false, which is represented as
a pointer to boolean in the `Client`'s new getter and setter. This
also inverts the boolean logic from verify TLS to skip TLS which
aligns very well with the corresponding fields in the upload target
struct.
In addition we properly capitalize some existing variables.
2022-07-21 13:32:07 +02:00
Christian Kellner
986f076276 container: add support for uploading to registries
Add a new generic container registry client via a new `container`
package. Use this to create a command line utility as well as a
new upload target for container registries.

The code uses the github.com/containers/* project and packages to
interact with container registires that is also used by skopeo,
podman et al. One if the dependencies is `proglottis/gpgme` that
is using cgo to bind libgpgme, so we have to add the corresponding
devel package to the BuildRequires as well as installing it on CI.

Checks will follow later via an integration test.
2022-06-29 10:02:46 +02:00