diff --git a/src/daemon/modules/image/oci/registry/registry_apiv2.c b/src/daemon/modules/image/oci/registry/registry_apiv2.c index a85851ba3789c6a1ff1845bf893384172d7a3e5e..6d5e7ca473118b83e2f487cf922b05458d8d9d21 100644 --- a/src/daemon/modules/image/oci/registry/registry_apiv2.c +++ b/src/daemon/modules/image/oci/registry/registry_apiv2.c @@ -1139,6 +1139,41 @@ out: return ret; } +int parse_login(char *http_head, char *host) +{ + int ret = 0; + struct parsed_http_message *message = NULL; + + message = get_parsed_message(http_head); + if (message == NULL) { + ERROR("Get parsed message failed. http response size %zu, response:%s", strlen(http_head), http_head); + isulad_try_set_error_message("login to registry for %s failed: parse response failed", host); + ret = -1; + goto out; + } + + if (message->status_code == StatusUnauthorized) { + ERROR("login to registry for %s failed: invalid username/password", host); + isulad_try_set_error_message("login to registry for %s failed: invalid username/password", host); + ret = -1; + goto out; + } + + if (message->status_code != StatusOK) { + ERROR("login to registry for %s failed: server response code %d", host, message->status_code); + isulad_try_set_error_message("login to registry for %s failed: server response code %d", host, + message->status_code); + ret = -1; + goto out; + } + +out: + + free_parsed_http_message(&message); + + return ret; +} + int login_to_registry(pull_descriptor *desc) { int ret = 0; @@ -1159,13 +1194,18 @@ int login_to_registry(pull_descriptor *desc) goto out; } - ret = registry_request(desc, path, NULL, NULL, &resp_buffer, HEAD_BODY, &errcode); + ret = registry_request(desc, path, NULL, NULL, &resp_buffer, HEAD_ONLY, &errcode); if (ret != 0) { ERROR("registry: Get %s failed, resp: %s", path, resp_buffer); isulad_try_set_error_message("login to registry for %s failed", desc->host); goto out; } + ret = parse_login(resp_buffer, desc->host); + if (ret != 0) { + goto out; + } + ret = auths_save(desc->host, desc->username, desc->password); if (ret != 0) { ERROR("failed to save auth of host %s, use decrypted key %d", desc->host, desc->use_decrypted_key);