From 4d2b0a37f4e976cbd3f433e7683355725abf8b7c Mon Sep 17 00:00:00 2001 From: Walid Boudebouda Date: Mon, 15 May 2023 12:25:12 +0200 Subject: [PATCH] cli: add -p argument to vcl.load_files This commit adds an optional -p argument to vcl.load_files cli command to allow the user to specify a path that will be prepended to the VCL_PATH parameter when loading the vcl files --- bin/varnishd/mgt/mgt.h | 2 +- bin/varnishd/mgt/mgt_vcc.c | 17 +++++++++++--- bin/varnishd/mgt/mgt_vcl.c | 25 +++++++++++++++----- bin/varnishtest/tests/c00124.vtc | 40 ++++++++++++++++++++++++++++++++ doc/changes.rst | 13 +++++++++++ include/tbl/cli_cmds.h | 7 ++++-- 6 files changed, 92 insertions(+), 12 deletions(-) create mode 100644 bin/varnishtest/tests/c00124.vtc diff --git a/bin/varnishd/mgt/mgt.h b/bin/varnishd/mgt/mgt.h index 11ce5954b78..7030a5b278a 100644 --- a/bin/varnishd/mgt/mgt.h +++ b/bin/varnishd/mgt/mgt.h @@ -227,7 +227,7 @@ void STV_Init(void); /* mgt_vcc.c */ void mgt_DumpBuiltin(void); char *mgt_VccCompile(struct cli *, struct vclprog *, const char *vclname, - const char *vclsrc, const char * const *vclsrcfiles, int C_flag); + const char *vclsrc, const char * const *vclsrcfiles, const char *vcl_path, int C_flag); void mgt_vcl_init(void); void mgt_vcl_startup(struct cli *, const char *vclsrc, const char *origin, diff --git a/bin/varnishd/mgt/mgt_vcc.c b/bin/varnishd/mgt/mgt_vcc.c index d2eae18f93c..8ee98605bf8 100644 --- a/bin/varnishd/mgt/mgt_vcc.c +++ b/bin/varnishd/mgt/mgt_vcc.c @@ -57,6 +57,7 @@ struct vcc_priv { #define VCC_PRIV_MAGIC 0x70080cb8 const char *vclsrc; const char * const *vclsrcfiles; + const char *vcl_path; struct vsb *dir; struct vsb *csrcfile; struct vsb *libfile; @@ -103,7 +104,7 @@ vcc_vext_iter_func(const char *filename, void *priv) static void v_noreturn_ v_matchproto_(vsub_func_f) run_vcc(void *priv) { - struct vsb *sb = NULL; + struct vsb *sb = NULL, *vsb = NULL; struct vclprog *vpg; struct vcc_priv *vp; struct vcc *vcc; @@ -118,8 +119,17 @@ run_vcc(void *priv) vcc = VCC_New(); AN(vcc); VCC_Builtin_VCL(vcc, builtin_vcl); - VCC_VCL_path(vcc, mgt_vcl_path); VCC_VMOD_path(vcc, mgt_vmod_path); + if (vp->vcl_path != NULL) { + vsb = VSB_new_auto(); + AN(vsb); + VSB_printf(vsb, "%s:%s", vp->vcl_path, mgt_vcl_path); + AZ(VSB_finish(vsb)); + VCC_VCL_path(vcc, VSB_data(vsb)); + VSB_destroy(&vsb); + } else { + VCC_VCL_path(vcc, mgt_vcl_path); + } #define VCC_FEATURE_BIT(U, l, d) \ VCC_Opt_ ## l(vcc, MGT_VCC_FEATURE(VCC_FEATURE_ ## U)); @@ -347,7 +357,7 @@ mgt_vcc_fini_vp(struct vcc_priv *vp, int leave_lib) char * mgt_VccCompile(struct cli *cli, struct vclprog *vcl, const char *vclname, - const char *vclsrc, const char * const *vclsrcfiles, int C_flag) + const char *vclsrc, const char * const *vclsrcfiles, const char *vcl_path, int C_flag) { struct vcc_priv vp[1]; struct vsb *sb; @@ -362,6 +372,7 @@ mgt_VccCompile(struct cli *cli, struct vclprog *vcl, const char *vclname, mgt_vcc_init_vp(vp); vp->vclsrc = vclsrc; vp->vclsrcfiles = vclsrcfiles; + vp->vcl_path = vcl_path; /* * The subdirectory must have a unique name to 100% certain evade diff --git a/bin/varnishd/mgt/mgt_vcl.c b/bin/varnishd/mgt/mgt_vcl.c index 8a69c48dd32..575755961dc 100644 --- a/bin/varnishd/mgt/mgt_vcl.c +++ b/bin/varnishd/mgt/mgt_vcl.c @@ -43,6 +43,7 @@ #include "mgt/mgt_vcl.h" #include "common/heritage.h" +#include "mgt_param.h" #include "vcli_serve.h" #include "vct.h" #include "vev.h" @@ -437,7 +438,7 @@ mgt_vcl_setstate(struct cli *cli, struct vclprog *vp, const struct vclstate *vs) static struct vclprog * mgt_new_vcl(struct cli *cli, const char *vclname, const char *vclsrc, - const char * const *vclsrcfiles, const char *state, int C_flag) + const char * const *vclsrcfiles, const char *state, const char *vcl_path, int C_flag) { unsigned status; char *lib, *p; @@ -463,7 +464,7 @@ mgt_new_vcl(struct cli *cli, const char *vclname, const char *vclsrc, return (NULL); vp = mgt_vcl_add(vclname, vs); - lib = mgt_VccCompile(cli, vp, vclname, vclsrc, vclsrcfiles, C_flag); + lib = mgt_VccCompile(cli, vp, vclname, vclsrc, vclsrcfiles, vcl_path, C_flag); if (lib == NULL) { mgt_vcl_del(vp); return (NULL); @@ -516,7 +517,7 @@ mgt_vcl_startup(struct cli *cli, const char *vclsrc, const char *vclname, bprintf(buf, "boot%d", n++); vclname = buf; } - vp = mgt_new_vcl(cli, vclname, vclsrc, vcls, NULL, C_flag); + vp = mgt_new_vcl(cli, vclname, vclsrc, vcls, NULL, NULL, C_flag); if (vp != NULL) { /* Last startup VCL becomes the automatically selected * active VCL. */ @@ -596,7 +597,7 @@ mcf_vcl_inline(struct cli *cli, const char * const *av, void *priv) if (!mcf_find_no_vcl(cli, av[2])) return; - vp = mgt_new_vcl(cli, av[2], av[3], vcls, av[4], 0); + vp = mgt_new_vcl(cli, av[2], av[3], vcls, av[4], NULL, 0); if (vp != NULL && !MCH_Running()) VCLI_Out(cli, "VCL compiled.\n"); } @@ -611,7 +612,7 @@ mcf_vcl_load(struct cli *cli, const char * const *av, void *priv) if (!mcf_find_no_vcl(cli, av[2])) return; - vp = mgt_new_vcl(cli, av[2], NULL, vcls, av[4], 0); + vp = mgt_new_vcl(cli, av[2], NULL, vcls, av[4], NULL, 0); if (vp != NULL && !MCH_Running()) VCLI_Out(cli, "VCL compiled.\n"); } @@ -620,14 +621,26 @@ static void v_matchproto_(cli_func_t) mcf_vcl_load_files(struct cli *cli, const char * const *av, void *priv) { struct vclprog *vp; + const struct parspec *pp; int i = 2; const char *state = NULL; + const char *vcl_path = NULL; while (av[i] != NULL) { if (!strcmp(av[i], "-s")) { FAIL_NO_ARG(av[i+1], "-s"); state = av[i+1]; i += 2; + } else if (!strcmp(av[i], "-p")) { + FAIL_NO_ARG(av[i+1], "-p"); + pp = MCF_FindPar("vcl_path"); + if (pp->flags & PROTECTED) { + VCLI_SetResult(cli, CLIS_AUTH); + VCLI_Out(cli, "parameter \"vcl_path\" is protected.\n"); + return; + } + vcl_path = av[i+1]; + i += 2; } else { break; } @@ -643,7 +656,7 @@ mcf_vcl_load_files(struct cli *cli, const char * const *av, void *priv) if (!mcf_find_no_vcl(cli, av[i])) return; - vp = mgt_new_vcl(cli, av[i], NULL, &av[i+1], state, 0); + vp = mgt_new_vcl(cli, av[i], NULL, &av[i+1], state, vcl_path, 0); if (vp != NULL && !MCH_Running()) VCLI_Out(cli, "VCL compiled.\n"); } diff --git a/bin/varnishtest/tests/c00124.vtc b/bin/varnishtest/tests/c00124.vtc new file mode 100644 index 00000000000..8796b3fe0d7 --- /dev/null +++ b/bin/varnishtest/tests/c00124.vtc @@ -0,0 +1,40 @@ +varnishtest "Test vcl.load_files -p arg" + +server s1 { + rxreq + txresp -status 202 +} -start + +varnish v1 -vcl+backend {} -start + +shell { + ( + echo 'vcl 4.0;' + echo 'backend default {' + echo ' .host="${s1_addr}";' + echo ' .port="${s1_port}";' + echo '}' + echo '' + echo 'include "inc.vcl";' + ) > ${tmpdir}/c00123.vcl +} + +shell { + ( + echo 'sub vcl_deliver {' + echo ' set resp.http.test = "c00123";' + echo '}' + ) > ${tmpdir}/inc.vcl +} + +varnish v1 -cliexpect "No such file or directory" {vcl.load_files foo c00123.vcl} + +varnish v1 -cliok {vcl.load_files -p ${tmpdir} foo c00123.vcl} +varnish v1 -cliok {vcl.use foo} + +client c1 { + txreq + rxresp + expect resp.http.test == "c00123" + expect resp.status == 202 +} -run diff --git a/doc/changes.rst b/doc/changes.rst index e30c24924ad..6ac525e6661 100644 --- a/doc/changes.rst +++ b/doc/changes.rst @@ -84,6 +84,19 @@ Varnish Cache NEXT (2023-09-15) .. _3908: https://github.com/varnishcache/varnish-cache/pull/3908 .. _3911: https://github.com/varnishcache/varnish-cache/issues/3911 +* A new ``vcl.load_files`` command has been added to cli. It has + the following syntax: + + ``vcl.load_file [-s auto|cold|warm] [-p vcl_path] + [filename2, ...]`` + + It is similar to ``vcl.load`` command except that it turns the state argument + into ``[-s auto|cold|warm]``, it has an optional ``-p`` argument that is prepended + to ``vcl_path`` parameter when loading the vcls, and it allows to specify multiple + vcl files to load, they are parsed in the order they are provided, and treated + as if they were concatenated and merged into a single vcl file. Note that this command + will fail when ``-p`` is specified and ``vcl_path`` is protected (read only). + ================================ Varnish Cache 7.3.0 (2023-03-15) ================================ diff --git a/include/tbl/cli_cmds.h b/include/tbl/cli_cmds.h index 42f8338b918..3e96334b75f 100644 --- a/include/tbl/cli_cmds.h +++ b/include/tbl/cli_cmds.h @@ -80,11 +80,14 @@ CLI_CMD(VCL_LOAD, CLI_CMD(VCL_LOAD_FILES, "vcl.load_files", - "vcl.load_files [-s auto|cold|warm] [filename2, ...]", + "vcl.load_files [-s auto|cold|warm] [-p vcl_path] [filename2, ...]", "Compile and load multiple VCL files under the name provided.", " When multiple files are specified, they are parsed in the order" " they were provided and treated as if they were concatenated and merged into" - " a single vcl file.", + " a single vcl file." + " The optional ``-p`` argument specifies the path of the vcl files (and included files)," + " it is prepended to the ``vcl_path`` parameter. This command will fail when ``-p`` is specified" + " and ``vcl_path`` is protected (read only).", 2, -1 )