oci_pull.c 5.9 KB
Newer Older
W
WangFengTu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
/******************************************************************************
* Copyright (c) Huawei Technologies Co., Ltd. 2019. All rights reserved.
* iSulad licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*     http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
* Author: wangfengtu
* Create: 2020-05-07
* Description: isula image pull operator implement
*******************************************************************************/
#include "oci_pull.h"

#include "log.h"
#include "utils.h"
#include "oci_common_operators.h"
#include "registry.h"
#include "isulad_config.h"

static int decode_auth(char *auth, char **username, char **password)
{
    int nret = 0;
    int ret = 0;
    unsigned char *decoded = NULL;
    size_t decoded_len = 0;
    char **auth_parts = NULL;

    if (auth == NULL || username == NULL || password == NULL) {
        ERROR("invalid NULL pointer");
        return -1;
    }

    decoded_len = util_base64_decode_len(auth, strlen(auth));
    if (decoded_len < 0) {
        return -1;
    }
    decoded = util_common_calloc_s(decoded_len + 1);
    if (decoded == NULL) {
        ERROR("out of memory");
        return -1;
    }

    nret = util_base64_decode(auth, strlen(auth), decoded, decoded_len);
    if (nret < 0) {
        ERROR("decode auth from base64 failed");
        ret = -1;
        goto out;
    }

    auth_parts = util_string_split((char *)decoded, ':');
    if (auth_parts == NULL || util_array_len((const char **)auth_parts) != 2) {
        ERROR("Invalid auth format");
        ret = -1;
        goto out;
    }

    *username = util_strdup_s(auth_parts[0]);
    *password = util_strdup_s(auth_parts[1]);
    (void)memset(auth_parts[0], 0, strlen(auth_parts[0]));
    (void)memset(auth_parts[1], 0, strlen(auth_parts[1]));

out:
    free_sensitive_string((char *)decoded);
    decoded = NULL;
    util_free_array(auth_parts);
    auth_parts = NULL;

    return ret;
}

static void update_option_skip_tls_verify(registry_pull_options *options, char **insecure_registries, char *host)
{
    char **registry = NULL;

    if (insecure_registries == NULL || options == NULL || host == NULL) {
        return;
    }

    for (registry = insecure_registries; (registry != NULL) && (*registry != NULL); registry++) {
        if (!strcmp(*registry, host)) {
            options->skip_tls_verify = true;
        }
    }
}

static int pull_image(const im_pull_request *request)
{
    int ret = -1;
    registry_pull_options *options = NULL;
    char **insecure_registries = NULL;
    char **registry_mirrors = NULL;
    char **mirror = NULL;
    char *host = NULL;
    char *dest_image_name = NULL;

    options = (registry_pull_options *)util_common_calloc_s(sizeof(registry_pull_options));
    if (options == NULL) {
        ERROR("Out of memory");
        goto out;
    }
    options->auth.username = request->username;
    options->auth.password = request->password;
    if (request->auth != NULL) {
        ret = decode_auth(request->auth, &options->auth.username, &options->auth.password);
        if (ret != 0) {
            ERROR("Decode auth failed");
            goto out;
        }
    }

    options->skip_tls_verify = conf_get_skip_insecure_verify_flag();
    insecure_registries = conf_get_insecure_registry_list();

    host = oci_get_host(request->image);
    if (host != NULL) {
        options->image_name = oci_default_tag(request->image);
        options->dest_image_name = util_strdup_s(options->image_name);
        update_option_skip_tls_verify(options, insecure_registries, host);
        ret = registry_pull(options);
        if (ret != 0) {
            ERROR("pull image failed");
            goto out;
        }
    } else {
        registry_mirrors = conf_get_registry_list();
        for (mirror = registry_mirrors; (mirror != NULL) && (*mirror != NULL); mirror++) {
            if (util_has_prefix(*mirror, HTTPS_PREFIX)) {
                options->skip_tls_verify = true;
            }
            host = oci_host_from_mirror(*mirror);
            update_option_skip_tls_verify(options, insecure_registries, host);
            dest_image_name = oci_default_tag(request->image);
            options->image_name = oci_add_host(host, dest_image_name);
            free(host);
            host = NULL;
            options->dest_image_name = dest_image_name;
            ret = registry_pull(options);
            if (ret != 0) {
                continue;
            }
            break;
        }
    }

out:
    free(host);
    host = NULL;
    util_free_array(registry_mirrors);
    registry_mirrors = NULL;
    util_free_array(insecure_registries);
    insecure_registries = NULL;
    free_registry_pull_options(options);
    options = NULL;

    return ret;
}

int oci_do_pull_image(const im_pull_request *request, im_pull_response **response)
{
    int ret = 0;
    imagetool_image *image = NULL;

    if (request == NULL || request->image == NULL || response == NULL) {
        ERROR("Invalid NULL param");
        return -1;
    }

    ret = pull_image(request);
    if (ret != 0) {
        ERROR("pull image %s failed", request->image);
        goto err_out;
    }

    image = storage_img_get(request->image);
    if (image == NULL) {
        ERROR("get image %s failed after pulling", request->image);
        goto err_out;
    }

    *response = (im_pull_response *)util_common_calloc_s(sizeof(im_pull_response));
    if (*response == NULL) {
        ERROR("Out of memory");
        goto err_out;
    }
    (*response)->image_ref = util_strdup_s(image->id);

    goto out;
err_out:
    free_im_pull_response(*response);
    *response = NULL;
    ret = -1;
out:
    free_imagetool_image(image);
    image = NULL;
    return ret;
}