oci_pull.c 6.4 KB
Newer Older
W
WangFengTu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/******************************************************************************
* 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"

17 18 19 20 21
#include <isula_libutils/imagetool_image.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>

L
lifeng68 已提交
22
#include "isula_libutils/log.h"
W
WangFengTu 已提交
23
#include "utils.h"
L
lifeng68 已提交
24
#include "utils_images.h"
W
WangFengTu 已提交
25 26
#include "registry.h"
#include "isulad_config.h"
L
lifeng68 已提交
27
#include "err_msg.h"
28
#include "storage.h"
29 30 31
#include "utils_array.h"
#include "utils_base64.h"
#include "utils_string.h"
W
WangFengTu 已提交
32

L
lifeng68 已提交
33
static int decode_auth(const char *auth, char **username, char **password)
W
WangFengTu 已提交
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
{
    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;
}

W
WangFengTu 已提交
84
static void update_option_insecure_registry(registry_pull_options *options, char **insecure_registries, char *host)
W
WangFengTu 已提交
85 86 87 88 89 90 91 92 93
{
    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)) {
W
WangFengTu 已提交
94
            options->insecure_registry = true;
W
WangFengTu 已提交
95 96 97 98
        }
    }
}

99
static int pull_image(const im_pull_request *request, char **name)
W
WangFengTu 已提交
100 101 102 103 104 105 106
{
    int ret = -1;
    registry_pull_options *options = NULL;
    char **insecure_registries = NULL;
    char **registry_mirrors = NULL;
    char **mirror = NULL;
    char *host = NULL;
107
    char *with_tag = NULL;
W
WangFengTu 已提交
108 109 110 111 112 113

    options = (registry_pull_options *)util_common_calloc_s(sizeof(registry_pull_options));
    if (options == NULL) {
        ERROR("Out of memory");
        goto out;
    }
L
lifeng68 已提交
114

W
WangFengTu 已提交
115 116 117 118 119 120
    if (request->auth != NULL) {
        ret = decode_auth(request->auth, &options->auth.username, &options->auth.password);
        if (ret != 0) {
            ERROR("Decode auth failed");
            goto out;
        }
L
lifeng68 已提交
121 122 123
    } else {
        options->auth.username = util_strdup_s(request->username);
        options->auth.password = util_strdup_s(request->password);
W
WangFengTu 已提交
124 125 126 127 128 129 130 131 132
    }

    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);
W
WangFengTu 已提交
133
        update_option_insecure_registry(options, insecure_registries, host);
W
WangFengTu 已提交
134 135 136 137 138 139 140
        ret = registry_pull(options);
        if (ret != 0) {
            ERROR("pull image failed");
            goto out;
        }
    } else {
        registry_mirrors = conf_get_registry_list();
141 142 143 144 145 146
        if (registry_mirrors == NULL) {
            ERROR("Invalid image name %s, no host found", request->image);
            isulad_try_set_error_message("Invalid image name, no host found");
            goto out;
        }

W
WangFengTu 已提交
147
        for (mirror = registry_mirrors; (mirror != NULL) && (*mirror != NULL); mirror++) {
W
WangFengTu 已提交
148
            if (util_has_prefix(*mirror, HTTP_PREFIX)) {
W
WangFengTu 已提交
149
                options->insecure_registry = true;
W
WangFengTu 已提交
150 151
            }
            host = oci_host_from_mirror(*mirror);
W
WangFengTu 已提交
152
            update_option_insecure_registry(options, insecure_registries, host);
153 154 155 156
            with_tag = oci_default_tag(request->image);
            options->image_name = oci_add_host(host, with_tag);
            free(with_tag);
            with_tag = NULL;
W
WangFengTu 已提交
157 158
            free(host);
            host = NULL;
159
            options->dest_image_name = oci_normalize_image_name(request->image);
W
WangFengTu 已提交
160 161 162 163 164 165 166 167
            ret = registry_pull(options);
            if (ret != 0) {
                continue;
            }
            break;
        }
    }

168 169
    *name = util_strdup_s(options->dest_image_name);

W
WangFengTu 已提交
170 171 172 173 174 175 176 177 178 179 180 181 182
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;
}

183
int oci_do_pull_image(const im_pull_request *request, im_pull_response *response)
W
WangFengTu 已提交
184 185 186
{
    int ret = 0;
    imagetool_image *image = NULL;
187
    char *dest_image_name = NULL;
W
WangFengTu 已提交
188 189 190 191 192 193

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

194
    ret = pull_image(request, &dest_image_name);
W
WangFengTu 已提交
195 196
    if (ret != 0) {
        ERROR("pull image %s failed", request->image);
197 198
        ret = -1;
        goto out;
W
WangFengTu 已提交
199 200
    }

201
    image = storage_img_get(dest_image_name);
W
WangFengTu 已提交
202 203
    if (image == NULL) {
        ERROR("get image %s failed after pulling", request->image);
204 205
        ret = -1;
        goto out;
W
WangFengTu 已提交
206 207
    }

208
    response->image_ref = util_strdup_s(image->id);
W
WangFengTu 已提交
209 210 211

out:
    free_imagetool_image(image);
212
    free(dest_image_name);
W
WangFengTu 已提交
213 214
    return ret;
}