HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/import/pull-oci.c

57,404 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 3d65271f20b5fc318c1351f039a1d359c486e7e5

Lines 1001–1643 of 1,643

                { "Env",          SD_JSON_VARIANT_ARRAY,         json_dispatch_strv_environment, offsetof(OciConfiguration, env),         0 },
                { "Entrypoint",   SD_JSON_VARIANT_ARRAY,         sd_json_dispatch_strv,          offsetof(OciConfiguration, entrypoint),  0 },
                { "Cmd",          SD_JSON_VARIANT_ARRAY,         sd_json_dispatch_strv,          offsetof(OciConfiguration, cmd),         0 },
                { "WorkingDir",   SD_JSON_VARIANT_STRING,        json_dispatch_oci_working_dir,  offsetof(OciConfiguration, working_dir), 0 },
                { "StopSignal",   SD_JSON_VARIANT_STRING,        sd_json_dispatch_signal,        offsetof(OciConfiguration, stop_signal), 0 },
 
                /* Currently unsupported fields that the OCI image spec defines but we cannot convert into
                 * .nspawn settings just yet. */
                { "ExposedPorts", _SD_JSON_VARIANT_TYPE_INVALID, dispatch_unsupported,           0,                                       0 },
                { "Volumes",      _SD_JSON_VARIANT_TYPE_INVALID, dispatch_unsupported,           0,                                       0 },
                { "Labels",       _SD_JSON_VARIANT_TYPE_INVALID, dispatch_unsupported,           0,                                       0 },
                { "ArgsEscaped",  _SD_JSON_VARIANT_TYPE_INVALID, dispatch_unsupported,           0,                                       0 },
                { "Memory",       _SD_JSON_VARIANT_TYPE_INVALID, dispatch_unsupported,           0,                                       0 },
                { "MemorySwap",   _SD_JSON_VARIANT_TYPE_INVALID, dispatch_unsupported,           0,                                       0 },
                { "CpuShares",    _SD_JSON_VARIANT_TYPE_INVALID, dispatch_unsupported,           0,                                       0 },
                { "Healthcheck",  _SD_JSON_VARIANT_TYPE_INVALID, dispatch_unsupported,           0,                                       0 },
                {}
        };
 
        r = sd_json_dispatch(config, dispatch_table, SD_JSON_ALLOW_EXTENSIONS|SD_JSON_LOG, &config_data);
        if (r < 0)
                return r;
 
        _cleanup_free_ char *fn = strjoin(i->local, ".nspawn");
        if (!fn)
                return log_oom();
 
        _cleanup_free_ char *j = path_join(i->image_root, fn);
        if (!j)
                return log_oom();
 
        _cleanup_fclose_ FILE *f = NULL;
        _cleanup_(unlink_and_freep) char *tmpfile = NULL;
        r = fopen_tmpfile_linkable(j, O_WRONLY|O_CLOEXEC, &tmpfile, &f);
        if (r < 0)
                return log_error_errno(r, "Failed to create '%s': %m", j);
 
        fprintf(f,
                "# Generated from OCI configuration object\n"
                "[Exec]\n"
                "Boot=no\n"
                "KillSignal=%s\n",
                signal_to_string(config_data.stop_signal));
 
        if (config_data.user && !STR_IN_SET(config_data.user, "root", "0")) {
                r = print_pair_escaped(f, "User", config_data.user);
                if (r < 0)
                        return r;
        }
 
        if (config_data.working_dir && !path_equal(config_data.working_dir, "/")) {
                r = print_pair_escaped(f, "WorkingDirectory", config_data.working_dir);
                if (r < 0)
                        return r;
        }
 
        STRV_FOREACH(e, config_data.env) {
                r = print_pair_escaped(f, "Environment", *e);
                if (r < 0)
                        return r;
        }
 
        _cleanup_strv_free_ char **cmdline = strv_copy(config_data.entrypoint);
        if (!cmdline)
                return log_oom();
        if (strv_extend_strv(&cmdline, config_data.cmd, /* filter_duplicates= */ false) < 0)
                return log_oom();
 
        if (!strv_isempty(cmdline)) {
                _cleanup_free_ char *ej = NULL;
 
                STRV_FOREACH(z, cmdline) {
                        _cleanup_free_ char *q = NULL;
                        const char *a;
 
                        if (isempty(*z))
                                a = "\"\"";
                        else if (!in_charset(*z, ALPHANUMERICAL "=+-_/;.:,;")) {
                                _cleanup_free_ char *e = shell_escape(*z, "\"");
                                if (!e)
                                        return log_oom();
 
                                q = strjoin("\"", e, "\"");
                                if (!q)
                                        return log_oom();
 
                                a = q;
                        } else
                                a = *z;
 
                        if (!strextend_with_separator(&ej, " ", a))
                                return log_oom();
                }
 
                fprintf(f, "Parameters=%s\n", ej);
        }
 
        r = flink_tmpfile(f, tmpfile, j,
                          LINK_TMPFILE_REPLACE|
                          (i->flags & IMPORT_SYNC ? LINK_TMPFILE_SYNC : 0));
        if (r < 0)
                return log_error_errno(r, "Failed to move '%s' into place: %m", j);
 
        log_info("systemd-nspawn settings file written to '%s'.", j);
        return 0;
}
 
static int oci_pull_save_oci_config(OciPull *i) {
        int r;
 
        assert(i);
        assert(iovec_is_set(&i->config));
 
        _cleanup_free_ char *fn = strjoin(i->local, ".oci-config");
        if (!fn)
                return log_oom();
 
        _cleanup_free_ char *j = path_join(i->image_root, fn);
        if (!j)
                return log_oom();
 
        _cleanup_close_ int fd = -EBADF;
        _cleanup_(unlink_and_freep) char *tmpfile = NULL;
        fd = open_tmpfile_linkable(j, O_WRONLY|O_CLOEXEC, &tmpfile);
        if (fd < 0)
                return log_error_errno(fd, "Failed to create '%s': %m", j);
 
        r = loop_write(fd, i->config.iov_base, i->config.iov_len);
        if (r < 0)
                return log_error_errno(r, "Failed to write '%s': %m", j);
 
        r = link_tmpfile(fd, tmpfile, j,
                         LINK_TMPFILE_REPLACE|
                         (i->flags & IMPORT_SYNC ? LINK_TMPFILE_SYNC : 0));
        if (r < 0)
                return log_error_errno(r, "Failed to move '%s' into place: %m", j);
 
        log_info("OCI config written to '%s'.", j);
        return 0;
}
 
static int oci_pull_save_mstack(OciPull *i) {
        int r;
 
        assert(i);
 
        _cleanup_free_ char *dn = strjoin(i->local, ".mstack");
        if (!dn)
                return log_oom();
 
        _cleanup_free_ char *j = path_join(i->image_root, dn);
        if (!j)
                return log_oom();
 
        log_notice("Creating '%s'", j);
 
        _cleanup_free_ char *_jt = NULL;
        r = tempfn_random(j, "oci", &_jt);
        if (r < 0)
                return log_oom();
 
        _cleanup_close_ int dir_fd = xopenat(AT_FDCWD, _jt, O_DIRECTORY|O_CREAT|O_CLOEXEC);
        if (dir_fd < 0)
                return log_error_errno(dir_fd, "Failed to create '%s': %m", j);
 
        _cleanup_(rm_rf_physical_and_freep) char *jt = TAKE_PTR(_jt);
        for (size_t k = 0; k < i->n_layers; k++) {
                _cleanup_free_ char *sl = NULL;
                if (asprintf(&sl, "layer@%zu", k) < 0)
                        return log_oom();
 
                _cleanup_free_ char *ldn = NULL;
                if (oci_layer_dirname_for_digest(i->layer_digests + k, &ldn) < 0)
                        return log_oom();
 
                _cleanup_free_ char *with_dot_dot = path_join("..", ldn);
                if (!with_dot_dot)
                        return log_oom();
 
                if (symlinkat(with_dot_dot, dir_fd, sl) < 0)
                        return log_error_errno(errno, "Failed to make symlink for layer '%s': %m", sl);
        }
 
        if (!FLAGS_SET(i->flags, IMPORT_READ_ONLY)) {
                /* If this shall be a mutable container let's also create an "rw" layer, that will become
                 * the upper layer in the overlayfs stack. */
 
                if (FLAGS_SET(i->flags, IMPORT_FOREIGN_UID)) {
                        r = import_make_foreign_userns(&i->userns_fd);
                        if (r < 0)
                                return r;
 
                        r = mountfsd_make_directory_fd(
                                        /* vl= */ NULL,
                                        dir_fd,
                                        "rw",
                                        0755,
                                        /* flags= */ 0,
                                        /* ret_directory_fd= */ NULL);
                        if (r < 0)
                                return r;
                } else {
                        _cleanup_close_ int rw_fd = open_mkdir_at(dir_fd, "rw", O_EXCL|O_CLOEXEC, 0755);
                        if (rw_fd < 0)
                                return log_error_errno(rw_fd, "Failed to create 'rw' layer: %m");
                }
        }
 
        r = install_file(
                        AT_FDCWD, jt,
                        AT_FDCWD, j,
                        (i->flags & IMPORT_FORCE ? INSTALL_REPLACE : 0) |
                        (i->flags & IMPORT_SYNC ? INSTALL_SYNCFS|INSTALL_GRACEFUL : 0));
        if (r < 0)
                return log_error_errno(r, "Failed to move '%s' into place: %m", j);
 
        jt = mfree(jt); /* Disarm rm_rf_physical_and_free() */
 
        log_info("Mount stack written to '%s'.", j);
        return 0;
}
 
static int oci_pull_make_local(OciPull *i) {
        int r;
 
        assert(i);
        assert(oci_pull_is_done(i));
 
        r = oci_pull_save_mstack(i);
        if (r < 0)
                return r;
 
        if (!iovec_is_set(&i->config) ||
            iovec_equal(&i->config, &IOVEC_MAKE_STRING("{}")))
                log_info("Image has no configuration, not saving.");
        else {
                r = oci_pull_save_nspawn_settings(i);
                if (r < 0)
                        return r;
 
                r = oci_pull_save_oci_config(i);
                if (r < 0)
                        return r;
        }
 
        return 0;
}
 
static int oci_pull_work(OciPull *i) {
        int r;
 
        assert(i);
 
        r = oci_pull_fetch_layers(i);
        if (r < 0)
                return r;
 
        if (!oci_pull_is_done(i))
                return 1; /* continue */
 
        r = oci_pull_make_local(i);
        if (r < 0)
                return r;
 
        log_info("Everything done.");
        return 0; /* done */
}
 
static void oci_pull_finish(OciPull *i, int r) {
        assert(i);
 
        if (i->on_finished)
                i->on_finished(i, r, i->userdata);
        else
                sd_event_exit(i->event, r);
}
 
static void oci_pull_job_on_finished_layer(PullJob *j) {
        int r;
 
        assert(j);
 
        OciLayerState *st = ASSERT_PTR(j->userdata);
        OciPull *i = st->pull;
 
        if (j->error != 0) {
                r = log_error_errno(j->error, "Failed to retrieve layer file: %m");
                goto finish;
        }
 
        pull_job_close_disk_fd(j);
 
        if (pidref_is_set(&st->tar_pid)) {
                r = pidref_wait_for_terminate_and_check("tar", &st->tar_pid, WAIT_LOG);
                if (r < 0)
                        goto finish;
 
                pidref_done(&st->tar_pid);
                if (r != EXIT_SUCCESS) {
                        r = -EIO;
                        goto finish;
                }
        }
 
        assert(st->temp_path);
        assert(st->final_path);
 
        r = install_file(AT_FDCWD, st->temp_path,
                         AT_FDCWD, st->final_path,
                         INSTALL_READ_ONLY|INSTALL_GRACEFUL);
        if (r < 0) {
                log_error_errno(r, "Failed to rename final image name to %s: %m", st->final_path);
                goto finish;
        }
 
        st->temp_path = mfree(st->temp_path);
 
        r = set_ensure_put(&i->done_layer_jobs, &pull_job_hash_ops, j);
        if (r < 0) {
                log_oom();
                goto finish;
        }
 
        assert_se(set_remove(i->active_layer_jobs, j) == j);
 
        r = oci_pull_work(i);
        if (r <= 0)
                goto finish;
 
        return;
 
finish:
        oci_pull_finish(i, r);
}
 
static void oci_pull_job_on_finished_config(PullJob *j) {
        int r;
 
        assert(j);
        OciPull *i = ASSERT_PTR(j->userdata);
        assert(i->config_job == j);
 
        if (j->error != 0) {
                if (j->error == -ENOMEDIUM) /* HTTP 404 */
                        r = log_error_errno(j->error, "Failed to retrieve config object. (Wrong URL?)");
                else
                        r = log_error_errno(j->error, "Failed to retrieve config object: %m");
                goto finish;
        }
 
        iovec_done(&i->config);
        i->config = TAKE_STRUCT(j->payload);
 
        r = oci_pull_work(i);
        if (r <= 0)
                goto finish;
 
        return;
 
finish:
        oci_pull_finish(i, r);
}
 
static void oci_pull_job_on_finished_bearer_token(PullJob *j) {
        int r;
 
        assert(j);
        OciPull *i = ASSERT_PTR(j->userdata);
        assert(i->bearer_token_job == j);
 
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
 
        if (j->error != 0) {
                if (j->error == -ENOMEDIUM) /* HTTP 404 */
                        r = log_error_errno(j->error, "Failed to retrieve bearer token. (Wrong URL?)");
                else
                        r = log_error_errno(j->error, "Failed to retrieve bearer token: %m");
                goto finish;
        }
 
        r = pull_job_payload_as_json_object(j, &v);
        if (r < 0)
                goto finish;
 
        sd_json_variant *tv = sd_json_variant_by_key(v, "token");
        if (!tv) {
                r = log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Bearer token lacks 'token' field.");
                goto finish;
        }
 
        if (!sd_json_variant_is_string(tv)) {
                r = log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "'token' field of bearer token is not a string.");
                goto finish;
        }
 
        r = free_and_strdup_warn(&i->bearer_token, sd_json_variant_string(tv));
        if (r < 0)
                goto finish;
 
        assert(i->manifest_job);
        r = pull_job_set_bearer_token(i->manifest_job, i->bearer_token);
        if (r < 0) {
                log_error_errno(r, "Failed to set bearer token on manifest job: %m");
                goto finish;
        }
 
        r = pull_job_restart(i->manifest_job, /* new_url= */ NULL);
        if (r < 0)
                goto finish;
 
        return;
 
finish:
        oci_pull_finish(i, r);
}
 
static int make_bearer_token_url(const char *realm, const char *service, const char *scope, char **ret) {
        assert(realm);
        assert(service);
        assert(scope);
        assert(ret);
 
        _cleanup_free_ char *rs = urlescape(service);
        if (!rs)
                return -ENOMEM;
 
        _cleanup_free_ char *ss = urlescape(scope);
        if (!ss)
                return -ENOMEM;
 
        _cleanup_free_ char *url = strjoin(realm, "?service=", rs, "&scope=", ss);
        if (!url)
                return -ENOMEM;
 
        *ret = TAKE_PTR(url);
        return 0;
}
 
static int oci_pull_process_authentication_challenge(OciPull *i, const char *challenge) {
        int r;
 
        assert(i);
        assert(challenge);
 
        /* We only know what to do with the bearer token challenge */
        const char *e = startswith_no_case(challenge, "bearer ");
        if (!e)
                return log_error_errno(SYNTHETIC_ERRNO(ENOKEY), "Authentication mechanism not recognized, cannot authenticate.");
 
        if (i->bearer_token_job)
                return log_error_errno(SYNTHETIC_ERRNO(EBUSY), "Two bearer token challenges requested, refusing.");
 
        e += strspn(e, WHITESPACE);
 
        _cleanup_strv_free_ char **l = NULL;
        r = strv_split_full(&l, e, ",", EXTRACT_KEEP_QUOTE);
        if (r < 0)
                return log_error_errno(r, "Failed to split bearer token parameters: %m");
 
        _cleanup_free_ char *realm = NULL, *scope = NULL, *service = NULL;
        struct {
                const char *name;
                char **value;
        } fields[] = {
                { "realm=\"",   &realm   },
                { "scope=\"",   &scope   },
                { "service=\"", &service },
        };
 
        STRV_FOREACH(k, l) {
                bool found = false;
 
                FOREACH_ELEMENT(f, fields) {
                        const char *v = startswith_no_case(*k, f->name);
                        if (!v)
                                continue;
 
                        const char *q = endswith(v,"\"");
                        if (!q) {
                                log_warning("Field isn't quoted properly: %s", *k);
                                continue;
                        }
 
                        _cleanup_free_ char *s = strndup(v, q - v);
                        if (!s)
                                return log_oom();
 
                        free_and_replace(*f->value, s);
                        found = true;
                        break;
                }
 
                if (!found)
                        log_debug("Ignoring bearer token challenge field: %s", *k);
        }
 
        if (!realm || !service || !scope)
                return log_error_errno(SYNTHETIC_ERRNO(ENOKEY), "Incomplete bearer token fields");
 
        if (!startswith_no_case(realm, "https://"))
                return log_error_errno(SYNTHETIC_ERRNO(ENOKEY), "Authentication realm is not an URL, don't know how to proceed.");
 
        _cleanup_free_ char *url = NULL;
        r = make_bearer_token_url(realm, service, scope, &url);
        if (r < 0)
                return log_error_errno(r, "Failed to make bearer token URL: %m");
 
        r = pull_job_new(&i->bearer_token_job, url, i->glue, i);
        if (r < 0)
                return r;
 
        i->bearer_token_job->on_finished = oci_pull_job_on_finished_bearer_token;
        i->bearer_token_job->description = strjoin("Bearer token (", url, ")");
 
        r = pull_job_begin(i->bearer_token_job);
        if (r < 0)
                return r;
 
        return 1; /* continue running */
}
 
static void oci_pull_job_on_finished_manifest(PullJob *j) {
        int r;
 
        assert(j);
        OciPull *i = ASSERT_PTR(j->userdata);
        assert(i->manifest_job == j);
 
        if (j->error != 0) {
                if (j->error == -ENOMEDIUM) /* HTTP 404 */
                        r = log_error_errno(j->error, "Failed to retrieve manifest/index file. (Wrong URL?)");
                else if (j->error == -ENOKEY) { /* HTTP 401 - Need authentication */
 
                        if (j->authentication_challenge) {
                                r = oci_pull_process_authentication_challenge(i, j->authentication_challenge);
                                if (r > 0)
                                        return;
 
                                goto finish;
                        } else
                                r = log_error_errno(j->error, "Failed to retrieve manifest/index file. (Needing authentication.)");
                } else
                        r = log_error_errno(j->error, "Failed to retrieve manifest/index file: %m");
                goto finish;
        }
 
        /* If we have no content type, let's assume it's a manifest, not an index. This can happen in case we
         * operate with file:// URLs, which in turn is really useful for testing purposes, to mock an OCI
         * registry without having to set up an HTTP service. */
        if (streq_ptr(j->content_type, "application/vnd.oci.image.manifest.v1+json") ||
            !j->content_type) {
                r = oci_pull_process_manifest(i, j);
                if (r <= 0)
                        goto finish;
 
                return;
        }
 
        if (streq_ptr(j->content_type, "application/vnd.oci.image.index.v1+json")) {
 
                if (i->refuse_index) {
                        r = log_error_errno(SYNTHETIC_ERRNO(EPROTO), "Already processed an OCI index, refusing to process another one.");
                        goto finish;
                }
 
                /* For now do not allow nested indexes */
                i->refuse_index = true;
 
                r = oci_pull_process_index(i, j);
                if (r <= 0)
                        goto finish;
 
                return;
        }
 
        r = log_error_errno(SYNTHETIC_ERRNO(EPROTO), "Unexpected content type '%s', refusing.", strna(j->content_type));
 
finish:
        oci_pull_finish(i, r);
}
 
int oci_pull_start(
                OciPull *i,
                const char *ref,
                const char *local,
                ImportFlags flags) {
 
        int r;
 
        assert(i);
        assert(ref);
 
        r = oci_ref_parse(ref, &i->repository, &i->image, &i->tag);
        if (r < 0)
                return r;
 
        r = oci_ref_normalize(&i->protocol, &i->repository, &i->image, &i->tag);
        if (r < 0)
                return r;
 
        if (local && !pull_validate_local(local, flags))
                return -EINVAL;
 
        if (i->manifest_job)
                return -EBUSY;
 
        r = free_and_strdup(&i->local, local);
        if (r < 0)
                return r;
 
        i->flags = flags;
 
        _cleanup_free_ char *url = NULL;
        r = oci_make_manifest_url(i->protocol, i->repository, i->image, i->tag, &url);
        if (r < 0)
                return r;
 
        /* Set up  */
        r = pull_job_new(&i->manifest_job, url, i->glue, i);
        if (r < 0)
                return r;
 
        if (i->bearer_token) {
                r = pull_job_set_bearer_token(i->manifest_job, i->bearer_token);
                if (r < 0)
                        return r;
        }
 
        r = pull_job_set_accept(
                        i->manifest_job,
                        STRV_MAKE("application/vnd.oci.image.manifest.v1+json",
                                  "application/vnd.oci.image.index.v1+json"));
        if (r < 0)
                return r;
 
        i->manifest_job->on_finished = oci_pull_job_on_finished_manifest;
        i->manifest_job->description = strjoin("Image Index (", url, ")");
        if (!i->manifest_job->description)
                return -ENOMEM;
 
        return pull_job_begin(i->manifest_job);
}