erofs-utils: lib: oci: support reading credentials from docker config
PATCH 1/1 erofs-utils: lib: oci: support reading credentials from docker config
From: Chengyu Zhu <[email protected]>
This patch adds support for reading authentication credentials from Docker's config.json (typically in ~/.docker/config.json or via DOCKER_CONFIG).
If no username/password is provided via command-line arguments, the implementation will attempt to look up the registry in the docker config file and use the stored credentials if found.
--- lib/Makefile.am | 2 +- lib/liberofs_dockerconfig.h | 28 +++++ lib/remotes/docker_config.c | 243 ++++++++++++++++++++++++++++++++++++ lib/remotes/oci.c | 18 ++- 4 files changed, 286 insertions(+), 5 deletions(-) create mode 100644 lib/liberofs_dockerconfig.h create mode 100644 lib/remotes/docker_config.c
lib/Makefile.am +1 -1
@@ -95,7 +95,7 @@ liberofs_la_LDFLAGS += ${liburing_LIBS} liberofs_la_SOURCES += backends/ublk.c endif endif -liberofs_la_SOURCES += remotes/oci.c +liberofs_la_SOURCES += remotes/oci.c remotes/docker_config.c liberofs_la_CFLAGS += ${json_c_CFLAGS} liberofs_la_LDFLAGS += ${json_c_LIBS} liberofs_la_SOURCES += gzran.c
lib/liberofs_dockerconfig.h new file +28 -0
@@ -0,0 +1,28 @@ +/* SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0 */ +/* +* Copyright (C) 2026 Tencent, Inc. +* http://www.tencent.com/ +*/ +#ifndef __EROFS_DOCKER_CONFIG_H +#define __EROFS_DOCKER_CONFIG_H + +#define DOCKER_REGISTRY "docker.io" +#define DOCKER_API_REGISTRY "registry-1.docker.io" +#define DOCKER_HUB_AUTH_KEY "https://index.docker.io/v1/" + +struct erofs_docker_credential { +char *username; +char *password; +}; + +/** +* erofs_docker_config_lookup - look up registry credentials from Docker config +* @registry: the registry hostname (e.g. "index.docker.io") +* @cred: output credential structure +*/ +int erofs_docker_config_lookup (const char *registry, +struct erofs_docker_credential *cred); + +void erofs_docker_credential_free (struct erofs_docker_credential *cred); + +#endif
lib/remotes/docker_config.c new file +243 -0
243 lines not on this page · raw message
lib/remotes/oci.c +14 -4
@@ -30,6 +30,7 @@#include "erofs/tar.h" #include "liberofs_base64.h" #include "liberofs_oci.h" +#include "liberofs_dockerconfig.h" #include "liberofs_private.h" #include "liberofs_gzran.h"
@@ -39,9 +40,6 @@#ifdef OCIEROFS_ENABLED -#define DOCKER_REGISTRY "docker.io" -#define DOCKER_API_REGISTRY "registry-1.docker.io" -#define DOCKER_MEDIATYPE_MANIFEST_V2 \ "application/vnd.docker.distribution.manifest.v2+json"#define DOCKER_MEDIATYPE_MANIFEST_V1 \
@@ -985,9 +983,21 @@ static int ocierofs_find_layer_by_digest(struct ocierofs_ctx *ctx, const char *dstatic int ocierofs_prepare_layers (struct ocierofs_ctx *ctx,const struct ocierofs_config *config) { +struct erofs_docker_credential dcred = {NULL ,NULL }; +const char *username = config->username; +const char *password = config->password;int ret; - ret =ocierofs_prepare_auth (ctx, config->username, config->password); +/* Fallback to Docker config.json if no CLI credentials provided */ +if ((!username || !*username) && (!password || !*password)) { +if (!erofs_docker_config_lookup (ctx->registry, &dcred)) { + username = dcred.username; + password = dcred.password; + } + } + + ret =ocierofs_prepare_auth (ctx, username, password); +erofs_docker_credential_free (&dcred);if (ret)return ret;
Signature · 1 line
2.51.0
On 2026/2/9 15:53, ChengyuZhu6 wrote:
..
Quoted 45 lines
lib/remotes/docker_config.c new file +42
-0@@ -0,0 +1,243 @@ +// SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0 +/* +* Copyright (C) 2026 Tencent, Inc. +* http://www.tencent.com/ +*/ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/stat.h> + +#ifdef HAVE_JSON_C_JSON_H +#include <json-c/json.h> +#endif + +#include "erofs/defs.h" +#include "erofs/print.h" +#include "liberofs_base64.h" +#include "liberofs_dockerconfig.h" + +#ifndef HAVE_JSON_C_JSON_H + +int erofs_docker_config_lookup (const char *registry, +struct erofs_docker_credential *cred) +{ + (void )registry; + (void )cred; +return -EOPNOTSUPP ; +} + +void erofs_docker_credential_free (struct erofs_docker_credential *cred) +{ + (void )cred; +} + +#else /* HAVE_JSON_C_JSON_H */ + +static char *docker_config_path (void )
lib/remotes/docker_config.c line 52
Quoted 10 lines
lib/remotes/docker_config.c new file +10
-0+{ +const char *dir; +char *path =NULL ; + + dir =getenv ("DOCKER_CONFIG" ); +if (dir && *dir) { +if (asprintf (&path,"%s/config.json" , dir) <0 ) +return NULL ; +return path; + }
Is it possible to make DOCKER_CONFIG as "" to ignore $HOME/.docker/config.json for script usage?
Quoted 178 lines
lib/remotes/docker_config.c new file +178
-0+ + dir =getenv ("HOME" ); +if (!dir || !*dir) { +erofs_dbg ("HOME is not set, cannot locate docker config" ); +return NULL ; + } + +if (asprintf (&path,"%s/.docker/config.json" , dir) <0 ) +return NULL ; +return path; +} + +static char *read_file_to_string (const char *path) +{ +FILE *fp; +struct stat st; +char *buf; +size_t nread; + +if (stat (path, &st) <0 ) +return NULL ; + +if (st.st_size <=0 || st.st_size > (1 <<22 )) +return NULL ; + + fp =fopen (path,"r" ); +if (!fp) +return NULL ; + + buf =malloc (st.st_size +1 ); +if (!buf) { +fclose (fp); +return NULL ; + } + + nread =fread (buf,1 , st.st_size, fp); +fclose (fp); + +if ((off_t )nread != st.st_size) { +free (buf); +return NULL ; + } + buf[nread] ='\0' ; +return buf; +} + +/* +* Check if @key (an auths entry key) matches @registry. +* +* For Docker Hub: @registry is docker.io or registry-1.docker.io. +* The auths key in config.json is always "https://index.docker.io/v1/". +* For other registries: the auths key is an exact match against @registry. +*/ +static bool registry_match (const char *key,const char *registry) +{ +if (!key || !registry) +return false; + +if (!strcasecmp (registry,DOCKER_REGISTRY ) || + !strcasecmp (registry,DOCKER_API_REGISTRY )) +return !strcmp (key,DOCKER_HUB_AUTH_KEY ); + +return !strcasecmp (key, registry); +} + +static int decode_auth_field (const char *b64,char **out_user,char **out_pass) +{ +int b64_len =strlen (b64); +int decoded_max = b64_len; +u8 *decoded; +int decoded_len; +char *colon; + + decoded =malloc (decoded_max +1 ); +if (!decoded) +return -ENOMEM ; + + decoded_len =erofs_base64_decode (b64, b64_len, decoded); +if (decoded_len <=0 ) { +free (decoded); +return -EINVAL ; + } + decoded[decoded_len] ='\0' ; + + colon =strchr ((char *)decoded,':' ); +if (!colon) { +memset (decoded,0 , decoded_len); +free (decoded); +return -EINVAL ; + } + + *colon ='\0' ; + *out_user =strdup ((char *)decoded); + *out_pass =strdup (colon +1 ); + +memset (decoded,0 , decoded_len); +free (decoded); + +if (!*out_user || !*out_pass) { +free (*out_user); +free (*out_pass); + *out_user =NULL ; + *out_pass =NULL ; +return -ENOMEM ; + } +return 0 ; +} + +int erofs_docker_config_lookup (const char *registry, +struct erofs_docker_credential *cred) +{ +char *path =NULL ; +char *content =NULL ; +struct json_object *root =NULL , *auths_obj =NULL ; +int ret = -ENOENT ; + +memset (cred,0 ,sizeof (*cred)); + + path =docker_config_path (); +if (!path) +return -ENOENT ; + + content =read_file_to_string (path); +if (!content) { +erofs_dbg ("cannot read docker config: %s" , path); +free (path); +return -ENOENT ; + } +free (path); + + root =json_tokener_parse (content); +memset (content,0 ,strlen (content)); +free (content); + +if (!root) { +erofs_warn ("failed to parse docker config.json" ); +return -EINVAL ; + } + +if (!json_object_object_get_ex (root,"auths" , &auths_obj)) { +erofs_dbg ("no \"auths\" in docker config.json" ); +json_object_put (root); +return -ENOENT ; + } + +struct json_object_iterator it =json_object_iter_begin (auths_obj); +struct json_object_iterator end =json_object_iter_end (auths_obj); + +while (!json_object_iter_equal (&it, &end)) { +const char *key =json_object_iter_peek_name (&it); +struct json_object *entry, *auth_field; +const char *b64; + +if (!registry_match (key, registry)) { +json_object_iter_next (&it); +continue ; + } + + entry =json_object_iter_peek_value (&it); +if (json_object_object_get_ex (entry,"auth" , &auth_field)) { + b64 =json_object_get_string (auth_field); +if (b64 && *b64) { + ret =decode_auth_field (b64, &cred->username, + &cred->password); +if (!ret) +erofs_dbg ("found docker credentials for %s" , + registry); + } + } +break ; + } + +json_object_put (root); +return ret; +} + +void erofs_docker_credential_free (struct erofs_docker_credential *cred) +{
lib/remotes/docker_config.c line 240
Quoted 10 lines
lib/remotes/docker_config.c new file +10
-0+if (cred->username) { +memset (cred->username,0 ,strlen (cred->username)); +free (cred->username); + cred->username =NULL ; + } +if (cred->password) { +memset (cred->password,0 ,strlen (cred->password)); +free (cred->password); + cred->password =NULL ; + }
maybe introduce erofs_free_sensitive() for these usage.
Thanks,
Gao Xiang
PATCH v2 1/1 erofs-utils: lib: oci: support reading credentials from docker config
In reply to ChengyuZhu6From: Chengyu Zhu <[email protected]>
This patch adds support for reading authentication credentials from Docker's config.json (typically in ~/.docker/config.json or via DOCKER_CONFIG).
If no username/password is provided via command-line arguments, the implementation will attempt to look up the registry in the docker config file and use the stored credentials if found.
--- include/erofs/internal.h | 10 ++ lib/Makefile.am | 2 +- lib/liberofs_dockerconfig.h | 30 +++++ lib/remotes/docker_config.c | 240 ++++++++++++++++++++++++++++++++++++ lib/remotes/oci.c | 18 ++- 5 files changed, 295 insertions(+), 5 deletions(-) create mode 100644 lib/liberofs_dockerconfig.h create mode 100644 lib/remotes/docker_config.c
include/erofs/internal.h +10 -0
@@ -25,6 +25,8 @@ typedef unsigned short umode_t;#ifdef HAVE_PTHREAD_H #include <pthread.h> #endif +#include <stdlib.h> +#include <string.h> #include "atomic.h" #include "io.h"
@@ -542,6 +544,14 @@ static inline int erofs_blk_read(struct erofs_sb_info *sbi, int device_id, erofs_pos(sbi, nblocks)); } +static inline void erofs_free_sensitive (void *ptr,size_t len) +{ +if (!ptr) +return ; +memset (ptr,0 , len); +free (ptr); +} +/* vmdk.c */ int erofs_dump_vmdk_desc (FILE *f,struct erofs_sb_info *sbi);
lib/Makefile.am +1 -1
@@ -95,7 +95,7 @@ liberofs_la_LDFLAGS += ${liburing_LIBS} liberofs_la_SOURCES += backends/ublk.c endif endif -liberofs_la_SOURCES += remotes/oci.c +liberofs_la_SOURCES += remotes/oci.c remotes/docker_config.c liberofs_la_CFLAGS += ${json_c_CFLAGS} liberofs_la_LDFLAGS += ${json_c_LIBS} liberofs_la_SOURCES += gzran.c
lib/liberofs_dockerconfig.h new file +30 -0
@@ -0,0 +1,30 @@ +/* SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0 */ +/* +* Copyright (C) 2026 Tencent, Inc. +* http://www.tencent.com/ +*/ +#ifndef __EROFS_DOCKER_CONFIG_H +#define __EROFS_DOCKER_CONFIG_H + +#include "erofs/internal.h" + +#define DOCKER_REGISTRY "docker.io" +#define DOCKER_API_REGISTRY "registry-1.docker.io" +#define DOCKER_HUB_AUTH_KEY "https://index.docker.io/v1/" + +struct erofs_docker_credential { +char *username; +char *password; +}; + +/** +* erofs_docker_config_lookup - look up registry credentials from Docker config +* @registry: the registry hostname (e.g. "index.docker.io") +* @cred: output credential structure +*/ +int erofs_docker_config_lookup (const char *registry, +struct erofs_docker_credential *cred); + +void erofs_docker_credential_free (struct erofs_docker_credential *cred); + +#endif
lib/remotes/docker_config.c new file +240 -0
240 lines not on this page · raw message
lib/remotes/oci.c +14 -4
@@ -30,6 +30,7 @@#include "erofs/tar.h" #include "liberofs_base64.h" #include "liberofs_oci.h" +#include "liberofs_dockerconfig.h" #include "liberofs_private.h" #include "liberofs_gzran.h"
@@ -39,9 +40,6 @@#ifdef OCIEROFS_ENABLED -#define DOCKER_REGISTRY "docker.io" -#define DOCKER_API_REGISTRY "registry-1.docker.io" -#define DOCKER_MEDIATYPE_MANIFEST_V2 \ "application/vnd.docker.distribution.manifest.v2+json"#define DOCKER_MEDIATYPE_MANIFEST_V1 \
@@ -985,9 +983,21 @@ static int ocierofs_find_layer_by_digest(struct ocierofs_ctx *ctx, const char *dstatic int ocierofs_prepare_layers (struct ocierofs_ctx *ctx,const struct ocierofs_config *config) { +struct erofs_docker_credential dcred = {NULL ,NULL }; +const char *username = config->username; +const char *password = config->password;int ret; - ret =ocierofs_prepare_auth (ctx, config->username, config->password); +/* Fallback to Docker config.json if no CLI credentials provided */ +if ((!username || !*username) && (!password || !*password)) { +if (!erofs_docker_config_lookup (ctx->registry, &dcred)) { + username = dcred.username; + password = dcred.password; + } + } + + ret =ocierofs_prepare_auth (ctx, username, password); +erofs_docker_credential_free (&dcred);if (ret)return ret;
Signature · 1 line
2.51.0
Hi Chengyu,
Quoted 11 lines · On Tue, Feb 10, 2026 at 05:37:26PM +0800, ChengyuZhu6 wrote:
From: Chengyu Zhu <[email protected]>
This patch adds support for reading authentication credentials from Docker's
config.json(typically in~/.docker/config.jsonor viaDOCKER_CONFIG).If no username/password is provided via command-line arguments, the implementation will attempt to look up the registry in the docker config file and use the stored credentials if found.
Signed-off-by Chengyu Zhu <[email protected]>
I cannot apply this patch, which commit is this commit based on?
Could you try to base on the latest -experimental or -dev commit?
Thanks,
Gao Xiang
My bad. Let me see what’s going on.
Thanks,
Chengyu
Quoted 23 lines
2026年2月12日 09:27,Gao Xiang <[email protected]> 写道:
Hi Chengyu,
Quoted 11 lines · On Tue, Feb 10, 2026 at 05:37:26PM +0800, ChengyuZhu6 wrote:
From: Chengyu Zhu <[email protected]>
This patch adds support for reading authentication credentials from Docker's
config.json(typically in~/.docker/config.jsonor viaDOCKER_CONFIG).If no username/password is provided via command-line arguments, the implementation will attempt to look up the registry in the docker config file and use the stored credentials if found.
Signed-off-by Chengyu Zhu <[email protected]>I cannot apply this patch, which commit is this commit based on?
Could you try to base on the latest -experimental or -dev commit?
Thanks,
Gao Xiang
PATCH v3 1/1 erofs-utils: lib: oci: support reading credentials from docker config
In reply to ChengyuZhu6From: Chengyu Zhu <[email protected]>
This patch adds support for reading authentication credentials from Docker's config.json (typically in ~/.docker/config.json or via DOCKER_CONFIG).
If no username/password is provided via command-line arguments, the implementation will attempt to look up the registry in the docker config file and use the stored credentials if found.
--- include/erofs/internal.h | 10 ++ lib/Makefile.am | 2 +- lib/liberofs_dockerconfig.h | 30 +++++ lib/remotes/docker_config.c | 240 ++++++++++++++++++++++++++++++++++++ lib/remotes/oci.c | 18 ++- 5 files changed, 295 insertions(+), 5 deletions(-) create mode 100644 lib/liberofs_dockerconfig.h create mode 100644 lib/remotes/docker_config.c
include/erofs/internal.h +10 -0
@@ -25,6 +25,8 @@ typedef unsigned short umode_t;#ifdef HAVE_PTHREAD_H #include <pthread.h> #endif +#include <stdlib.h> +#include <string.h> #include "atomic.h" #include "io.h"
@@ -542,6 +544,14 @@ static inline int erofs_blk_read(struct erofs_sb_info *sbi, int device_id, erofs_pos(sbi, nblocks)); } +static inline void erofs_free_sensitive (void *ptr,size_t len) +{ +if (!ptr) +return ; +memset (ptr,0 , len); +free (ptr); +} +/* vmdk.c */ int erofs_dump_vmdk_desc (FILE *f,struct erofs_sb_info *sbi);
lib/Makefile.am +1 -1
@@ -89,7 +89,7 @@ liberofs_la_CFLAGS += ${libnl3_CFLAGS} liberofs_la_LDFLAGS += ${libnl3_LIBS} liberofs_la_SOURCES += backends/nbd.c endif -liberofs_la_SOURCES += remotes/oci.c +liberofs_la_SOURCES += remotes/oci.c remotes/docker_config.c liberofs_la_CFLAGS += ${json_c_CFLAGS} liberofs_la_LDFLAGS += ${json_c_LIBS} liberofs_la_SOURCES += gzran.c
lib/liberofs_dockerconfig.h new file +30 -0
@@ -0,0 +1,30 @@ +/* SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0 */ +/* +* Copyright (C) 2026 Tencent, Inc. +* http://www.tencent.com/ +*/ +#ifndef __EROFS_DOCKER_CONFIG_H +#define __EROFS_DOCKER_CONFIG_H + +#include "erofs/internal.h" + +#define DOCKER_REGISTRY "docker.io" +#define DOCKER_API_REGISTRY "registry-1.docker.io" +#define DOCKER_HUB_AUTH_KEY "https://index.docker.io/v1/" + +struct erofs_docker_credential { +char *username; +char *password; +}; + +/** +* erofs_docker_config_lookup - look up registry credentials from Docker config +* @registry: the registry hostname (e.g. "index.docker.io") +* @cred: output credential structure +*/ +int erofs_docker_config_lookup (const char *registry, +struct erofs_docker_credential *cred); + +void erofs_docker_credential_free (struct erofs_docker_credential *cred); + +#endif
lib/remotes/docker_config.c new file +240 -0
240 lines not on this page · raw message
lib/remotes/oci.c +14 -4
@@ -26,14 +26,12 @@#include "erofs/tar.h" #include "liberofs_base64.h" #include "liberofs_oci.h" +#include "liberofs_dockerconfig.h" #include "liberofs_private.h" #include "liberofs_gzran.h" #ifdef OCIEROFS_ENABLED -#define DOCKER_REGISTRY "docker.io" -#define DOCKER_API_REGISTRY "registry-1.docker.io" -#define DOCKER_MEDIATYPE_MANIFEST_V2 \ "application/vnd.docker.distribution.manifest.v2+json"#define DOCKER_MEDIATYPE_MANIFEST_V1 \
@@ -964,9 +962,21 @@ static int ocierofs_find_layer_by_digest(struct ocierofs_ctx *ctx, const char *dstatic int ocierofs_prepare_layers (struct ocierofs_ctx *ctx,const struct ocierofs_config *config) { +struct erofs_docker_credential dcred = {NULL ,NULL }; +const char *username = config->username; +const char *password = config->password;int ret; - ret =ocierofs_prepare_auth (ctx, config->username, config->password); +/* Fallback to Docker config.json if no CLI credentials provided */ +if ((!username || !*username) && (!password || !*password)) { +if (!erofs_docker_config_lookup (ctx->registry, &dcred)) { + username = dcred.username; + password = dcred.password; + } + } + + ret =ocierofs_prepare_auth (ctx, username, password); +erofs_docker_credential_free (&dcred);if (ret)return ret;
Signature · 1 line
2.51.0