<div dir="ltr">I think it's close to optimal, given the current tools. I would probably try to move away from regsub() and use vmod_str (<a href="https://github.com/varnish/varnish-modules/blob/master/src/vmod_str.vcc#L42">https://github.com/varnish/varnish-modules/blob/master/src/vmod_str.vcc#L42</a>), and maaaaaaybe use multiple assignments rather than on big expressions, but that's a personal preference at this point.<div><br></div><div>It would look like something like this in my mind (highly untested, don't sue me if your computer explodes):</div><div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><font face="monospace">import var;</font></div><div><font face="monospace">import str;</font></div><div><font face="monospace">import blob;</font></div><div><font face="monospace"><br></font></div><div><font face="monospace">sub vcl_recv {</font></div><div><font face="monospace"> if (str.split(req.http.Authorization, 0) == "Basic") {</font></div><div><font face="monospace"> var.set("b64", str.split(req.http.Authorization, 1));</font></div><div><font face="monospace"> var.set("decoded", bob.transcode(<span style="background-color:unset">encoding </span><span style="background-color:unset">= </span><span style="background-color:unset">BASE64URL, </span><span style="background-color:unset">encoded = var.get("b64")</span>));</font></div><div><font face="monospace"> set req.http.X-Auth-User = str.split(var.get("decoded"), 0, ":");</font></div><div><font face="monospace"> }</font></div><div><font face="monospace">}</font></div></blockquote><div><br></div><div>everything in one expression:</div><div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><font face="monospace">set req.http.X-Auth-User = str.split(</font></div><div><font face="monospace"> blob.transcode(</font></div><div><font face="monospace"><span style="background-color:unset"> encoding </span><span style="background-color:unset">= </span><span style="background-color:unset">BASE64URL,</span></font></div><div><font face="monospace"> <span style="background-color:unset">encoded = </span>str.split(req.http.Authorization, 1))</font></div><div><font face="monospace"> ),</font></div><div><font face="monospace"> 0,</font></div><div><font face="monospace"> ":"</font></div><div><font face="monospace"> );</font></div></blockquote><div><br></div><div>You should possibly use blob.transcode() anyway.</div><div><br></div><div>--</div><div><div><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div>Guillaume Quintard<br></div></div></div></div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Oct 25, 2021 at 11:25 AM Justin Lloyd <<a href="mailto:justinl@arena.net">justinl@arena.net</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div lang="EN-US" style="overflow-wrap: break-word;">
<div class="gmail-m_-2746566859228366300WordSection1">
<p class="MsoNormal">In my dev environment, I have a few users configured to use Basic authorization (configured in the Nginx backend) and I’d like to be able to perform VSL queries based on the auth user. This is what I was able to come up with, but I’m wondering
if there is a simpler way that I’m just not seeing. <u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:Consolas">require blob;<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:Consolas">if (req.http.Authorization) {<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:Consolas"> set req.http.X-Auth-User = regsub(blob.encode(IDENTITY,<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:Consolas"> blob=blob.decode(BASE64,<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:Consolas"> encoded=regsub(req.http.Authorization, "^Basic (.*)", "\1"))),<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:Consolas"> ":.*$", "");<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:Consolas">}<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:Consolas"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:Consolas">varnishtop -I ReqHeader:X-Auth-User<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:Consolas">varnishlog -i ReqURL -q 'ReqHeader:X-Auth-User ~ “someuser”'<u></u><u></u></span></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">Thanks,<u></u><u></u></p>
<p class="MsoNormal">Justin<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
</div>
_______________________________________________<br>
varnish-misc mailing list<br>
<a href="mailto:varnish-misc@varnish-cache.org" target="_blank">varnish-misc@varnish-cache.org</a><br>
<a href="https://www.varnish-cache.org/lists/mailman/listinfo/varnish-misc" rel="noreferrer" target="_blank">https://www.varnish-cache.org/lists/mailman/listinfo/varnish-misc</a><br>
</blockquote></div>