<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>max dev blog &amp; other stuff</title><link>/</link><description>max dev blog &amp; other stuff</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><lastBuildDate>Tue, 20 Sep 2022 00:00:00 +0000</lastBuildDate><atom:link href="/index.xml" rel="self" type="application/rss+xml"/><item><title>Proxmox cloud backup</title><link>/blog/pve-cloud-backup/</link><pubDate>Tue, 20 Sep 2022 00:00:00 +0000</pubDate><guid>/blog/pve-cloud-backup/</guid><description>&lt;p>If you are running &lt;a href="https://www.proxmox.com/en/proxmox-ve">Proxmox Virtual Environment [0]&lt;/a> in your infra, chances are that you utilize the &lt;a href="https://pve.proxmox.com/wiki/Backup_and_Restore">Backup and Restore functionality [1]&lt;/a>. Like many other PVE features, its web GUI is devoid of a large number of controls for managing options described in the documentation.&lt;/p>
&lt;p>Within this short entry I want to demonstrate setup I&amp;rsquo;ve came up with for &lt;strong>synchronizing Proxmox VMs/CTs to Backblaze B2 cloud backup&lt;/strong> using hook scripts feature.&lt;/p>
&lt;h2 id="prerequisites" >
&lt;div>
&lt;a href="#prerequisites">
#
&lt;/a>
Prerequisites
&lt;/div>
&lt;/h2>
&lt;p>Before proceeding, you should be in control of a &lt;a href="https://www.backblaze.com/b2/cloud-storage.html">Backblaze B2 account [2]&lt;/a> with a configured bucket to upload dumps to. Alternatively, you should be fine using any other remote storage provided &lt;a href="https://rclone.org/#providers">supported by rclone [3]&lt;/a>. My choice of Backblaze is justified by the affordable pricing model they employ.&lt;/p>
&lt;p>I&amp;rsquo;m also assuming that you have a configured backup job in PVE. Specifics don&amp;rsquo;t matter; the job can cover any number of machines, have whichever schedule and use any other options. A single backup job is tied to a single storage device for storing resulting dumps, so we can be sure all backups will be kept in the same location.&lt;/p>
&lt;h2 id="backup-job-hook-scripts" >
&lt;div>
&lt;a href="#backup-job-hook-scripts">
#
&lt;/a>
Backup job hook scripts
&lt;/div>
&lt;/h2>
&lt;p>&lt;a href="https://pve.proxmox.com/wiki/Backup_and_Restore#_hook_scripts">Hook script [4]&lt;/a>, in terms of PVE&amp;rsquo;s backups functionality, is an optional script set per job called at various phases of its execution. An example Perl script provided with &lt;code>pve-manager&lt;/code> documentation enumerates the phases script might be called at:&lt;/p>
&lt;pre tabindex="0">&lt;code>job-start
job-end
job-abort
backup-start
backup-end
backup-abort
log-end
pre-stop
pre-restart
post-restart
&lt;/code>&lt;/pre>&lt;p>The phase is passed to the script as the parameter.&lt;/p>
&lt;h2 id="rclone" >
&lt;div>
&lt;a href="#rclone">
#
&lt;/a>
rclone
&lt;/div>
&lt;/h2>
&lt;p>We&amp;rsquo;re on the right track with integrating custom processes into PVE backup jobs. Let&amp;rsquo;s focus on preparing the process itself before &lt;em>hooking it up&lt;/em> to the job.&lt;/p>
&lt;p>Assuming that you&amp;rsquo;ve already created a Backblaze bucket, you should be in possession of API key and secret key.&lt;/p>
&lt;p>Install &lt;code>rclone&lt;/code> on the machine running PVE instance and run &lt;code>rclone config&lt;/code>. Use the interactive prompt to configure the access to the bucket. Try running commands like &lt;code>rclone ls &amp;lt;bucket-name&amp;gt;:&lt;/code> and &lt;code>rclone copy &amp;lt;sample-file&amp;gt; &amp;lt;bucket-name&amp;gt;:&lt;/code> to verify if it works correctly. &lt;code>rclone&lt;/code> should be capable of both reading and writing files.&lt;/p>
&lt;h2 id="preparing-the-sync-script" >
&lt;div>
&lt;a href="#preparing-the-sync-script">
#
&lt;/a>
Preparing the sync script
&lt;/div>
&lt;/h2>
&lt;p>The script should run only after the backup job is entirely finished. Make sure the job is configured in &lt;em>Datacenter &amp;gt; Backup&lt;/em> tab, then SSH into the server.&lt;/p>
&lt;p>Create a new file in a directory convenient for you. Then, paste in the following content:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#e2e4e5;background-color:#282a36;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#78787e">#!/bin/bash
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#78787e">&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#ff6ac1">if&lt;/span> &lt;span style="color:#ff6ac1">[&lt;/span> &lt;span style="color:#5af78e">&amp;#34;&lt;/span>&lt;span style="color:#ff5c57">$1&lt;/span>&lt;span style="color:#5af78e">&amp;#34;&lt;/span> &lt;span style="color:#ff6ac1">==&lt;/span> &lt;span style="color:#5af78e">&amp;#34;job-end&amp;#34;&lt;/span> &lt;span style="color:#ff6ac1">]&lt;/span>; &lt;span style="color:#ff6ac1">then&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> rclone --config /root/.config/rclone/rclone.conf --min-age 2w delete &amp;lt;bucket-name&amp;gt;:&amp;lt;bucket-path&amp;gt;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> rclone --config /root/.config/rclone/rclone.conf copy &amp;lt;storage-path&amp;gt; &amp;lt;bucket-name&amp;gt;:&amp;lt;bucket-path&amp;gt;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#ff6ac1">fi&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>&lt;code>if [ &amp;quot;$1&amp;quot; == &amp;quot;job-end&amp;quot; ]&lt;/code> is the script entrypoint safeguard. The script will be called on multiple stages of the execution of any backup job, but sync should only happen after it&amp;rsquo;s finished.&lt;/p>
&lt;p>All &lt;code>rclone&lt;/code> commands are appended with &lt;code>--config&lt;/code> option followed by the config file location. In my case, &lt;code>rclone&lt;/code> would fail to locate the config file when called by cron.&lt;/p>
&lt;p>First command, &lt;code>delete&lt;/code> with &lt;code>--min-age&lt;/code> option is my simple approach to implementing backups rotation mechanism. You can replace the value of 2 weeks with a retention time that suits you. Alternatively, you can apply &lt;a href="https://www.backblaze.com/b2/docs/file_versions.html">lifecycle rules[5]&lt;/a> to the bucket, so that the files will be purged automatically after specified period of time.&lt;/p>
&lt;p>&lt;img src="https://babarowski.com/images/2022/lifecycle_rules.png" alt="Screenshot showing a lifecycle settings of a sample B2 bucket">&lt;/p>
&lt;div class="note">
&lt;p>Pay attention to the `hard_delete` option in the `rclone` config of your B2 bucket. By default, it is set to `false` and the wizard prompts you about it during the configuration setup. If the bucket lifecycle policy is **not** set to *Keep only the last version of the file*, `rclone delete` will not in fact delete a file from the bucket. Instead, it is going to hide the file[6] with the hide marker.&lt;/p>
&lt;p>Hidden files consume storage space and you are charged for them. By default, B2 buckets keep all revisions of the file.&lt;/p>
&lt;/div>
&lt;p>Second command, &lt;code>copy&lt;/code>, actually synchronizes content of the &lt;code>&amp;lt;storage-path&amp;gt;&lt;/code> with &lt;code>&amp;lt;bucket-path&amp;gt;&lt;/code> of the bucket denoted by &lt;code>&amp;lt;bucket-name&amp;gt;&lt;/code>.&lt;/p>
&lt;p>Replace the script with the actual path of backup dumps location and target bucket name, then edit &lt;code>/etc/pve/vzdump.cron&lt;/code> file.&lt;/p>
&lt;p>Warning in the heading comment can be safely ignored. Making changes to the job using web front-end writes changes directly to this file. We will be adding the option that is not possible to add with the GUI. Any further changes made to the job using the GUI won&amp;rsquo;t affect it.&lt;/p>
&lt;p>Find the line responsible for scheduling the backup job of your choice and add the option &lt;code>--script &amp;lt;path-to-script&amp;gt;&lt;/code> with a path to the script you&amp;rsquo;ve just created. For example:&lt;/p>
&lt;pre tabindex="0">&lt;code>30 3 * * 7 root vzdump 100 --quiet 1 --mode snapshot --script /root/b2-sync.sh --node node-0 --storage vms-storage --compress ztsd --mailnotification always
&lt;/code>&lt;/pre>&lt;h2 id="whats-next" >
&lt;div>
&lt;a href="#whats-next">
#
&lt;/a>
What&amp;rsquo;s next?
&lt;/div>
&lt;/h2>
&lt;p>You&amp;rsquo;ve just configured Proxmox backup dumps synchronization with off-site cloud storage hooked with to the backup job finish. If you want to take this setup to the next level, try implementing complex backup retention rules. For example, desired rotation strategy would be:&lt;/p>
&lt;ul>
&lt;li>keep the latest 14 daily dumps,&lt;/li>
&lt;li>keep the latest 8 weekly dumps,
etc.&lt;/li>
&lt;/ul>
&lt;h2 id="links" >
&lt;div>
&lt;a href="#links">
#
&lt;/a>
Links
&lt;/div>
&lt;/h2>
&lt;pre tabindex="0">&lt;code>[0]: https://www.proxmox.com/en/proxmox-ve
[1]: https://pve.proxmox.com/wiki/Backup_and_Restore
[2]: https://www.backblaze.com/b2/cloud-storage.html
[3]: https://rclone.org/#providers
[4]: https://pve.proxmox.com/wiki/Backup_and_Restore#_hook_scripts
[5]: https://www.backblaze.com/b2/docs/file_versions.html
[6]: https://www.backblaze.com/b2/docs/b2_hide_file.html
&lt;/code>&lt;/pre></description></item><item><title>How to restrict network access of LXC container</title><link>/blog/how-to-restrict-network-in-lxc/</link><pubDate>Fri, 28 Jan 2022 00:00:00 +0000</pubDate><guid>/blog/how-to-restrict-network-in-lxc/</guid><description>&lt;p>So let&amp;rsquo;s say you have to run that sketchy or untrusted executable/project/binary/whatever and you want to do in a safe manner. Wide range of applicable solutions based on virtualization and contenerization technologies allow secure examination of suspicious software in a sandboxed environment.&lt;/p>
&lt;p>If you&amp;rsquo;re a Linux user, you&amp;rsquo;d probably point your attention to &lt;a href="https://linuxcontainers.org/">&lt;strong>LXC&lt;/strong>[0]&lt;/a>. Just a few commands to bring up shiny new environment based on a distro selected from wide variety of available options, running on the host kernel with no excessive emulation overhead.&lt;/p>
&lt;p>That alone provides decent separation from whatever you don&amp;rsquo;t want an object of research to touch in your workstation. &lt;strong>But what about connectivity?&lt;/strong>&lt;/p>
&lt;p>Permitting an unknown actor unrestricted access to the Internet or LAN poses serious threat. At the same time, the container is going to need it to some extent, for installing packages or remote access from host. Wouldn&amp;rsquo;t it be convenient to switch the network access restrictions of the container on and off at any time?&lt;/p>
&lt;h2 id="use-a-nat-bridge" >
&lt;div>
&lt;a href="#use-a-nat-bridge">
#
&lt;/a>
Use a NAT bridge
&lt;/div>
&lt;/h2>
&lt;p>Fairly simple way to achieve it is to use &lt;strong>iptables&lt;/strong> in conjunction with &lt;strong>lxc-net&lt;/strong>. Using &lt;em>lxc-net&lt;/em> assumes running the container behind a NAT bridge. If you want to bridge the container to LAN, the tool of your choice might provide a built-in mechanism to achieve similar effect&lt;a href="https://wiki.archlinux.org/title/Network_bridge">[1]&lt;/a>. In my opinion, having the container behind local NAT provides higher level of resilience and control. In case of having to expose the container to wider publicity, you can always redirect traffic from host to lxc.&lt;/p>
&lt;pre tabindex="0">&lt;code># iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 2221 -j DNAT --to-destination 10.0.3.100:22
&lt;/code>&lt;/pre>&lt;p>The example rule above forwards TCP traffic origination on port &lt;code>2221&lt;/code> of &lt;code>eth0&lt;/code> interface to lxc SSH server running on &lt;code>10.0.3.100:22&lt;/code>&lt;a href="https://wiki.archlinux.org/title/Linux_Containers#Example_iptables_rule">[2]&lt;/a>.&lt;/p>
&lt;p>Arch Linux Wiki &lt;a href="https://wiki.archlinux.org/title/Linux_Containers#Example_iptables_rule">article[2]&lt;/a> I took this rule from serves as a pretty good introduction to using &lt;em>lxc-net&lt;/em> and lxc as whole. There&amp;rsquo;s no point in me repeating most of its content, so give it a read if you&amp;rsquo;re not familiar with lxc. At the end of a day, you should have a lxc container running behind NAT managed by &lt;em>lxc-net&lt;/em>.&lt;/p>
&lt;h2 id="firewall-setup" >
&lt;div>
&lt;a href="#firewall-setup">
#
&lt;/a>
Firewall setup
&lt;/div>
&lt;/h2>
&lt;p>Assuming that you have used default configuration, CIDR of your lxc subnet is &lt;code>10.0.3.0/24&lt;/code>. Running &lt;code>lxc-ls -f&lt;/code> returns a list of containers similar to this:&lt;/p>
&lt;pre tabindex="0">&lt;code>NAME STATE AUTOSTART GROUPS IPV4 IPV6 UNPRIVILEGED
lxc0 RUNNING 0 - 10.0.3.250 - false
lxc1 STOPPED 0 - - - false
&lt;/code>&lt;/pre>&lt;p>We&amp;rsquo;ll be imposing restrictions on &lt;code>lxc0&lt;/code> container. Take note of its IP address – &lt;code>10.0.3.250&lt;/code> in my case.&lt;/p>
&lt;p>You should also know name of the interface managed by &lt;em>lxc-net&lt;/em>. It&amp;rsquo;s mentioned in the config file &lt;code>/etc/lxc/default.conf&lt;/code> by &lt;code>lxc.net.0.link&lt;/code> key and by default it&amp;rsquo;s &lt;code>lxcbr0&lt;/code>.&lt;/p>
&lt;p>If you run &lt;code>iptables -L FORWARD -v&lt;/code> you should see two rules related to it at top of the chain.&lt;/p>
&lt;pre tabindex="0">&lt;code>pkts bytes target prot opt in out source destination
0 0 ACCEPT all -- any lxcbr0 anywhere anywhere
0 0 ACCEPT all -- lxcbr0 any anywhere anywhere
&lt;/code>&lt;/pre>&lt;p>They allow all kinds of traffic on &lt;code>lxcbr0&lt;/code> interface in any direction.&lt;/p>
&lt;p>We want to forbid ANY incoming and outgoing traffic from the container except for traffic between the container and host. The address of the host can be obtained from &lt;code>/etc/default/lxc-net&lt;/code> file under &lt;code>LXC_ADDR&lt;/code> key and its default value is &lt;code>10.0.3.1&lt;/code>.&lt;/p>
&lt;p>Therefore, this rule should be applied on host &lt;em>iptables&lt;/em>:&lt;/p>
&lt;pre tabindex="0">&lt;code># iptables -I FORWARD 1 -i lxcbr0 ! -d 10.0.3.1 -j DROP
&lt;/code>&lt;/pre>&lt;p>It can be explained in a following way:&lt;/p>
&lt;ul>
&lt;li>&lt;code>-I FORWARD 1&lt;/code> inserts the rule at index 1 of &lt;code>FORWARD&lt;/code> chain&lt;/li>
&lt;li>&lt;code>-i lxcbr0&lt;/code> applies the rule to traffic received on interface called &lt;code>lxcbr0&lt;/code>&lt;/li>
&lt;li>&lt;code>! -d 10.0.3.1&lt;/code> applies the rule to traffic directed to any IP address, &lt;strong>except&lt;/strong> for &lt;code>10.0.3.1&lt;/code> which is host IP&lt;/li>
&lt;li>&lt;code>-j DROP&lt;/code> blocks all the traffic matching criteria above&lt;/li>
&lt;/ul>
&lt;p>Additionally, you might block inbound traffic:&lt;/p>
&lt;pre tabindex="0">&lt;code># iptables -I FORWARD 2 -i lxcbr0 ! -s 10.0.3.1 -j DROP
&lt;/code>&lt;/pre>&lt;p>In order to test the solution, try connecting to the container&amp;rsquo;s shell via SSH. It should work fine. Now try pinging any host inside or outside LAN. None of the packets should go through.&lt;/p>
&lt;p>Restriction can be lifted at any time with this command:&lt;/p>
&lt;pre tabindex="0">&lt;code># iptables -D FORWARD 1
&lt;/code>&lt;/pre>&lt;p>It removes the rule from chain FORWARD specified by the index. &lt;strong>Note that you need to run it twice if you applied both inbound and outbound blocking rules.&lt;/strong>&lt;/p>
&lt;h2 id="allowing-multiple-containers-to-communicate" >
&lt;div>
&lt;a href="#allowing-multiple-containers-to-communicate">
#
&lt;/a>
Allowing multiple containers to communicate
&lt;/div>
&lt;/h2>
&lt;p>If you want to run several containers in the same subnet and allow the traffic to occur between them, but not leak outside, you can modify the rule to drop anything except for packets sourced and addressed to &lt;code>10.0.3.0/24&lt;/code> subnet.&lt;/p>
&lt;p>This turns the first rule into:&lt;/p>
&lt;pre tabindex="0">&lt;code># iptables -I FORWARD 1 -i lxcbr0 ! -d 10.0.3.0/24 -j DROP
&lt;/code>&lt;/pre>&lt;p>Equivalent modification of &lt;code>-d&lt;/code> argument can be applied to the outbound rule.&lt;/p>
&lt;h2 id="links" >
&lt;div>
&lt;a href="#links">
#
&lt;/a>
Links
&lt;/div>
&lt;/h2>
&lt;pre tabindex="0">&lt;code>[0]: https://linuxcontainers.org/
[1]: https://wiki.archlinux.org/title/Network_bridge
[2]: https://wiki.archlinux.org/title/Linux_Containers#Example_iptables_rule
&lt;/code>&lt;/pre></description></item><item><title>Compiling Python &lt;3.5.3 in 2022</title><link>/blog/compiling-python34-in-2022/</link><pubDate>Sat, 15 Jan 2022 00:00:00 +0000</pubDate><guid>/blog/compiling-python34-in-2022/</guid><description>&lt;p>Despite Python 3.6 reaching end-of-life just 3 weeks ago&lt;a href="https://devguide.python.org/devcycle/#end-of-life-branches">[0]&lt;/a>, the amount of projects depending on older versions of Python 3 is exorbitant. Whilst working on a part of my media library workflow I found myself in need of using an old stalled project based on Python 3.4. Sadly, the code does not execute properly in newer runtimes and no one has ever undertook the task of refactoring it. Therefore, users are forced to run it with 3.4 version interpreter.&lt;/p>
&lt;p>&lt;strong>tldr; use openssl 1.0 and gcc 10&lt;/strong>&lt;/p>
&lt;h2 id="short-introduction-to-pyenv" >
&lt;div>
&lt;a href="#short-introduction-to-pyenv">
#
&lt;/a>
Short introduction to &lt;em>pyenv&lt;/em>
&lt;/div>
&lt;/h2>
&lt;p>Before getting right into the the crux of the matter, I&amp;rsquo;d like to interject for a moment in order to familiarize dear reader with Python runtime management tool called &lt;em>pyenv&lt;/em>. This entry is not closely connected with it, however chances of anyone still willing to manually juggle Python runtimes after learning about &lt;em>pyenv&lt;/em> are near to none. It&amp;rsquo;s just so convenient to use.&lt;/p>
&lt;p>My case isn&amp;rsquo;t one of its own. Python is notorious for providing poor interoperability between its different versions and the tool I&amp;rsquo;ve been using for years to mitigate caused inconveniences is &lt;a href="https://github.com/pyenv/pyenv">pyenv[1]&lt;/a>. Basically it allows you to install multiple version of Python runtime and switch between them with ease. Let&amp;rsquo;s say you want to use version &lt;code>3.7.12&lt;/code> – run &lt;code>pyenv install 3.7.12&lt;/code>. As Python offers source code tarballs as the only official way of distribution, &lt;em>pyenv&lt;/em> downloads the archive, builds the version of your choice and copies the output binaries to its own runtime directory. Then, switching to the specific version is as simple as executing &lt;code>pyenv global 3.7.12&lt;/code>. From now on, &lt;code>python&lt;/code> command should be resolved with &lt;code>3.7.12&lt;/code> binary.&lt;/p>
&lt;pre tabindex="0">&lt;code>$ python --version
Python 3.7.12
&lt;/code>&lt;/pre>&lt;p>&lt;em>pyenv&lt;/em> is very powerful utility on its own, and paired with extension called &lt;code>pyevn-virtualenv&lt;/code> has served me as a manager of runtimes for multiple Python projects.&lt;/p>
&lt;p>This time wasn&amp;rsquo;t supposed to be different and I was very surprised when &lt;code>pyenv install 3.4.10&lt;/code> greeted me with &lt;code>ERROR: The Python ssl extension was not compiled. Missing the OpenSSL lib&lt;/code>.&lt;/p>
&lt;h2 id="openssl-10" >
&lt;div>
&lt;a href="#openssl-10">
#
&lt;/a>
OpenSSL 1.0
&lt;/div>
&lt;/h2>
&lt;p>As per &lt;a href="https://github.com/pyenv/pyenv/wiki/Common-build-problems#2-your-openssl-version-is-incompatible-with-the-python-version-youre-trying-to-install">pyenv docs[2]&lt;/a>, old Python versions (for CPython, &amp;lt;3.5.3 and &amp;lt;2.7.13) require OpenSSL 1.0, while most of the newer systems ship with 1.1.x version by default. However, most also provide 1.0.2 version available as a separate package to cover incompatibility issues like this. For my distribution, which is Arch Linux, the package is called &lt;code>openssl-1.0&lt;/code>. Debian stretch and Ubuntu bionic users might use &lt;code>libssl1.0-dev&lt;/code>.&lt;/p>
&lt;pre tabindex="0">&lt;code>$ pacman -S openssl-1.0
&lt;/code>&lt;/pre>&lt;p>There is no need to uninstall package providing new version of OpenSSL. The compiler just needs to be told where to look for the headers. Run &lt;code>pyenv install&lt;/code> again with the following compilation flags:&lt;/p>
&lt;pre tabindex="0">&lt;code>$ CFLAGS=&amp;#34;-I/usr/include/openssl-1.0&amp;#34; LDFLAGS=&amp;#34;-L/usr/lib/openssl-1.0&amp;#34; CPPFLAGS=&amp;#34;-I/usr/include/openssl-1.0&amp;#34; pyenv install 3.4.10
&lt;/code>&lt;/pre>&lt;p>Verify the paths of OpenSSL 1.0 library beforehand. The command above includes the ones applicable for default Arch Linux setup. They might look different for other distributions.&lt;/p>
&lt;h2 id="segfault-and-other-compilation-errors" >
&lt;div>
&lt;a href="#segfault-and-other-compilation-errors">
#
&lt;/a>
SEGFAULT and other compilation errors
&lt;/div>
&lt;/h2>
&lt;p>Compiling older version of Python on Arch Linux and other bleeding edge distros might be interrupted by unexpected compilation error. In my case it was classic segmentation fault. The key is to use older version of gcc. For &lt;em>3.4.10&lt;/em> version, it&amp;rsquo;s enough to go back to gcc 10, still available as a regular package in Arch Linux.&lt;/p>
&lt;p>Install it:&lt;/p>
&lt;pre tabindex="0">&lt;code>$ pacman -S gcc10
&lt;/code>&lt;/pre>&lt;p>And add another flag to &lt;code>pyenv install&lt;/code> call – &lt;code>CC=gcc-10&lt;/code>:&lt;/p>
&lt;pre tabindex="0">&lt;code>$ CC=gcc-10 CFLAGS=&amp;#34;-I/usr/include/openssl-1.0&amp;#34; LDFLAGS=&amp;#34;-L/usr/lib/openssl-1.0&amp;#34; CPPFLAGS=&amp;#34;-I/usr/include/openssl-1.0&amp;#34; pyenv install 3.4.10
&lt;/code>&lt;/pre>&lt;p>Compilation should now succeed.&lt;/p>
&lt;h2 id="summary" >
&lt;div>
&lt;a href="#summary">
#
&lt;/a>
Summary
&lt;/div>
&lt;/h2>
&lt;p>To wrap everything up – compiling older version of Python on modern systems usually require two tweaks:&lt;/p>
&lt;ul>
&lt;li>providing OpenSSL 1.0 library as an alternative to any other version installed on the system&lt;/li>
&lt;li>using GCC 10 instead of recently released GCC 11&lt;/li>
&lt;/ul>
&lt;p>In Arch Linux it all narrows down to just two commands:&lt;/p>
&lt;pre tabindex="0">&lt;code>pacman -S openssl-1.0 gcc10
CC=gcc-10 CFLAGS=&amp;#34;-I/usr/include/openssl-1.0&amp;#34; LDFLAGS=&amp;#34;-L/usr/lib/openssl-1.0&amp;#34; CPPFLAGS=&amp;#34;-I/usr/include/openssl-1.0&amp;#34; pyenv install 3.4.10
&lt;/code>&lt;/pre>&lt;p>Happy coding, scripting or whatever you need old Python for. Don&amp;rsquo;t forget to consider refactoring the code to run correctly in newer runtime, as OpenSSL 1.0 embedded in legacy Python executable is EOL and any internet-facing applications should not rely on it anymore.&lt;/p>
&lt;h2 id="links" >
&lt;div>
&lt;a href="#links">
#
&lt;/a>
Links
&lt;/div>
&lt;/h2>
&lt;pre tabindex="0">&lt;code>[0]: https://devguide.python.org/devcycle/#end-of-life-branches
[1]: https://github.com/pyenv/pyenv
[2]: https://github.com/pyenv/pyenv/wiki/Common-build-problems#2-your-openssl-version-is-incompatible-with-the-python-version-youre-trying-to-install
&lt;/code>&lt;/pre>&lt;pre tabindex="0">&lt;code>&lt;/code>&lt;/pre></description></item><item><title>Wine not detecting drives in winecfg</title><link>/blog/wine-not-detecting-disks/</link><pubDate>Thu, 02 Dec 2021 00:00:00 +0000</pubDate><guid>/blog/wine-not-detecting-disks/</guid><description>&lt;p>Just wanted to drop this quick fix for anyone who runs into it the same issue as the viable solution is not trivial to find. My two evenings full of efforts prove it.&lt;/p>
&lt;p>When I run &lt;code>winecfg&lt;/code> and try to go into &lt;code>Drives&lt;/code> tab to make a change in the config, there&amp;rsquo;s an error message saying:&lt;/p>
&lt;pre tabindex="0">&lt;code>Failed to connect to the mount manager, the drive configuration cannot be edited.
&lt;/code>&lt;/pre>&lt;p>Looking at stdout of the process, this log entry can be spotted:&lt;/p>
&lt;pre tabindex="0">&lt;code>0009:err:winecfg:open_mountmgr failed to open mount manager err 2
&lt;/code>&lt;/pre>&lt;p>Several topics on WineHQ forums provide no insight into the cause of the problem, besides fact that it predominantly occurs on 64-bit systems running 32-bit Wine prefixes.&lt;/p>
&lt;p>The solution I found is to run this command:&lt;/p>
&lt;pre tabindex="0">&lt;code>wineboot --update
&lt;/code>&lt;/pre>&lt;p>Alerts about uninstalled Mono or other dependencies can be safely ignored. After the command finished running, I was more than happy to run &lt;code>winecfg&lt;/code> and see drives configuration in its right place.&lt;/p>
&lt;p>&lt;img src="https://babarowski.com/images/2021/winecfgdrives.png" alt="winecfg &amp;lsquo;Drives&amp;rsquo; tab">&lt;/p></description></item><item><title>Change GTKFileChooser to KDE Dolphin file chooser in Arch Linux</title><link>/blog/change-gtk-file-picker-to-kde-dolphin/</link><pubDate>Sun, 07 Nov 2021 01:00:00 +0000</pubDate><guid>/blog/change-gtk-file-picker-to-kde-dolphin/</guid><description>&lt;p>&lt;img src="https://babarowski.com/images/2021/gtkfilechooser.png" alt="GTKFileChooser window">&lt;/p>
&lt;p>GTK, while being widely adopted and utilized by thousands of apps is still suffering from that unresolved &lt;a href="https://gitlab.gnome.org/GNOME/gtk/-/issues/233">Add an &amp;ldquo;icon view with thumbnails&amp;rdquo;[0]&lt;/a> issue that has been residing in the project bug tracker for over 17 years (at the time of writing this article, November 2021). Personally, I&amp;rsquo;ve learned about it fairly recently reading &lt;a href="https://jayfax.neocities.org/mediocrity/gnome-has-no-thumbnails-in-the-file-picker.html">the article comparing GtkFileChooser to a clogged toilet[1]&lt;/a>, which explains how the inconvenience caused by the issue mostly went unnoticed by many users (including me) for years. It seems like it was a turning point in the life of the issue, as now its presence is being brought up on almost a daily basis in many informal tech communities. Most often, the purpose is scapegoating GTK as an entirety. The reasoning is that if such a conceptually simple, UX-crucial and obviously present at any notable competitor feature cannot be implemented since almost beginning of the project, then something is seriously wrong with the project itself. Perhaps it&amp;rsquo;s a convoluted codebase, bad maintaining or inadequate concepts fossilized into the core of the project.&lt;/p>
&lt;p>I had used GNOME as my DE for 4 years. This means I was obviously affected by the issue, but chose not to care about it for a while. Since a few months, I&amp;rsquo;ve been using only WMs for my daily work, which gave me a bit more flexibility in choosing the tools I like. This post explains how I installed alternative file chooser and set it to be the default one first in Firefox and (almost) globally on my Arch Linux setup.&lt;/p>
&lt;h2 id="installing-dolphin" >
&lt;div>
&lt;a href="#installing-dolphin">
#
&lt;/a>
Installing Dolphin
&lt;/div>
&lt;/h2>
&lt;p>A brief research conducts GNOME competitor – KDE includes a file chooser with said functionality implemented. File chooser is actually a part of KDE&amp;rsquo;s default file manager called &lt;a href="https://apps.kde.org/dolphin/">Dolphin[2]&lt;/a>.&lt;/p>
&lt;p>It can be installed on Arch as a separate package (without KDE and most of the dependencies). In my case, it required these dependencies to be installed:&lt;/p>
&lt;pre tabindex="0">&lt;code>$ sudo pacman -S dolphin
resolving dependencies...
:: There are 2 providers available for phonon-qt5-backend:
:: Repository extra
1) phonon-qt5-gstreamer 2) phonon-qt5-vlc
Enter a number (default=1): 1
looking for conflicting packages...
warning: dependency cycle detected:
warning: usbmuxd will be installed before its libimobiledevice dependency
warning: dependency cycle detected:
warning: phonon-qt5-gstreamer will be installed before its phonon-qt5 dependency
Packages (82) attica-5.87.0-1 baloo-5.87.0-1 baloo-widgets-21.08.3-1 cdparanoia-10.2-8
convertlit-1.8-10 dmraid-1.0.0.rc16.3-13 dosfstools-4.2-1
ebook-tools-0.2.2-7 exiv2-0.27.5-1 gptfdisk-1.0.8-1
gst-plugins-base-1.18.5-1 kactivities-5.87.0-1 karchive-5.87.0-1
kauth-5.87.0-1 kbookmarks-5.87.0-1 kcmutils-5.87.0-1 kcodecs-5.87.0-1
kcompletion-5.87.0-1 kconfig-5.87.0-1 kconfigwidgets-5.87.0-1
kcoreaddons-5.87.0-1 kcrash-5.87.0-1 kdbusaddons-5.87.0-1
kdeclarative-5.87.0-1 kded-5.87.0-1 kdnssd-5.87.0-1 kdsoap-2.0.0-1
kdsoap-ws-discovery-client-git20200927-2 kfilemetadata-5.87.0-1
kglobalaccel-5.87.0-1 kguiaddons-5.87.0-1 ki18n-5.87.0-1
kiconthemes-5.87.0-1 kidletime-5.87.0-1 kio-5.87.0-1 kio-extras-21.08.3-1
kitemviews-5.87.0-1 kjobwidgets-5.87.0-1 knewstuff-5.87.0-1
knotifications-5.87.0-1 kpackage-5.87.0-1 kparts-5.87.0-1 kservice-5.87.0-1
ktextwidgets-5.87.0-1 kuserfeedback-1.0.0-1 kwallet-5.87.0-1
kwidgetsaddons-5.87.0-1 kwindowsystem-5.87.0-1 kxmlgui-5.87.0-1
libatasmart-0.19-5 libblockdev-2.26-1 libbytesize-2.6-1
libimobiledevice-1.3.0-3 libinih-53-1 libmtp-1.1.19-1 libplist-2.2.0-3
libtommath-1.2.0-3 libusbmuxd-2.0.2-1 libvisual-0.4.0-8 libyaml-0.2.5-1
libzip-1.8.0-1 mdadm-4.1-2 media-player-info-24-2 ndctl-71.1-1 parted-3.4-2
phonon-qt5-4.11.1-2 phonon-qt5-gstreamer-4.10.0-2 polkit-qt5-0.114.0-1
poppler-qt5-21.11.0-1 qt5-multimedia-5.15.2-1 qt5-speech-5.15.2-1
solid-5.87.0-1 sonnet-5.87.0-1 syndication-5.87.0-1
syntax-highlighting-5.87.0-2 taglib-1.12-1 udisks2-2.9.4-1 upower-0.99.13-1
usbmuxd-1.1.1-1 volume_key-0.3.12-5 xfsprogs-5.13.0-1 dolphin-21.08.3-1
Total Download Size: 53.64 MiB
Total Installed Size: 200.73 MiB
&lt;/code>&lt;/pre>&lt;p>Those caring about &amp;ldquo;not bloating screenfetch&amp;rdquo; might stop reading right here. I don&amp;rsquo;t belong to that clique, and just continue.&lt;/p>
&lt;h2 id="enabling-dolphin-as-default-file-chooser" >
&lt;div>
&lt;a href="#enabling-dolphin-as-default-file-chooser">
#
&lt;/a>
Enabling Dolphin as default file chooser
&lt;/div>
&lt;/h2>
&lt;p>Now Dolphin is installed, but other applications needs to be instructed to use its component instead of GtkFileChooser. Unfortunately, whether GTK application is able to use alternative system dialogs or not depends on it implementing &lt;code>xdg-desktop-portal&lt;/code> front-end service. Good portion of modern software does it, but be prepared to keep using GtkFileChooser in some apps.&lt;/p>
&lt;p>Install packages &lt;code>xdg-desktop-portal xdg-desktop-portal-kde&lt;/code>. Set environment variable &lt;code>GTK_USE_PORTAL=1&lt;/code> in &lt;code>/etc/environment&lt;/code> to apply this solution globally, or in &lt;code>~/.bash_profile&lt;/code> to apply it for a user&lt;a href="https://wiki.archlinux.org/title/Uniform_look_for_Qt_and_GTK_applications#Consistent_file_dialog">[3]&lt;/a>.&lt;/p>
&lt;p>Reload session (log out and in). You should now be greeted with Dolphin file chooser in Firefox:
![Dolphin file chooser window]({{ site.baseurl }}images/2021/dolphinfilechooser.png)&lt;/p>
&lt;p>Some other apps I use on a daily basis started using Dolphin instead of GTK file chooser out of a box:&lt;/p>
&lt;ul>
&lt;li>Chromium&lt;/li>
&lt;li>telegram-desktop&lt;/li>
&lt;/ul>
&lt;p>And some didn&amp;rsquo;t change their behavior:&lt;/p>
&lt;ul>
&lt;li>Discord (GTK)&lt;/li>
&lt;li>VSCode (GTK)&lt;/li>
&lt;li>KeePassXC (Qt)&lt;/li>
&lt;/ul>
&lt;p>Let&amp;rsquo;s try briefly checking what&amp;rsquo;s the problem with each of them.&lt;/p>
&lt;h3 id="electron-apps-support-xdg_desktop_portal-since-14" >
&lt;div>
&lt;a href="#electron-apps-support-xdg_desktop_portal-since-14">
##
&lt;/a>
Electron apps support XDG_DESKTOP_PORTAL since 14
&lt;/div>
&lt;/h3>
&lt;p>Support for XDG_DESKTOP_PORTAL has been &lt;a href="https://github.com/electron/electron/pull/19159">merged to Electron in April 2021[4]&lt;/a>, however it was evaluated as not stable enough to be introduced in 13.x version and postponed for the release of Electron 14. &lt;a href="https://github.com/electron/electron/releases/tag/v14.0.0">The release happened on 31th August, 2021[5]&lt;/a>. Given the amount of time passed since then, it&amp;rsquo;s no wonder that few apps moved to Electron 14 from earlier versions.&lt;/p>
&lt;p>In case of VSCode, a version of Electron it&amp;rsquo;s running on can be easily checked by opening developer console (Help &amp;gt; Toggle Developer Options) and executing:&lt;/p>
&lt;pre tabindex="0">&lt;code>&amp;gt; process.versions.electron
&amp;lt; &amp;#34;13.5.1&amp;#34;
&lt;/code>&lt;/pre>&lt;p>As for now, neither Arch Linux package repositories or AUR provide any flavor of VSCode built with Electron 14. The only solution appears to be &lt;a href="https://github.com/Microsoft/vscode/wiki/How-to-Contribute#build-and-run">building VSCode from scratch with custom internals[6]&lt;/a>. To me, it&amp;rsquo;s not worth the hassle and I&amp;rsquo;m going to just wait for VSCode to adopt Electron 14.&lt;/p>
&lt;p>I had found no way of checking version of Electron Discord is running on, but I strongly suspect it&amp;rsquo;s the same cause as in VSCode case.&lt;/p>
&lt;h3 id="qt-uses-qfiledialog" >
&lt;div>
&lt;a href="#qt-uses-qfiledialog">
##
&lt;/a>
Qt uses QFileDialog
&lt;/div>
&lt;/h3>
&lt;p>And it seems to suffer from the same issue, as GTKFileChooser.&lt;/p>
&lt;p>![QFileDialog window]({{ site.baseurl }}images/2021/qfiledialog.png)&lt;/p>
&lt;p>&lt;a href="https://docs.flatpak.org/en/latest/portals-qt.html">Running Qt in Flatpak Sandbox might mitigate the issue[7]&lt;/a>. Debian provides this functionality on Flatpak/Snap based environments with &lt;a href="https://packages.debian.org/bullseye/qt5-xdgdesktopportal-platformtheme">&lt;code>qt5-xdgdesktopportal-platformtheme&lt;/code>[8]&lt;/a>.&lt;/p>
&lt;p>Unfortunately, I found no solution applicable for my setup.&lt;/p>
&lt;h2 id="tweaking-file-chooser" >
&lt;div>
&lt;a href="#tweaking-file-chooser">
#
&lt;/a>
Tweaking file chooser
&lt;/div>
&lt;/h2>
&lt;p>Dolphin file chooser works wherever it&amp;rsquo;s possible now. But it still suffers from some caveats. Let&amp;rsquo;s go through the list and try to fix them.&lt;/p>
&lt;h3 id="firefox-continuously-asks-to-be-set-as-default-browser-upon-launch" >
&lt;div>
&lt;a href="#firefox-continuously-asks-to-be-set-as-default-browser-upon-launch">
##
&lt;/a>
Firefox continuously asks to be set as default browser upon launch
&lt;/div>
&lt;/h3>
&lt;p>Setting &lt;code>XDG_DESKTOP_PORTAL=1&lt;/code> triggers another, long-lasting &lt;a href="https://bugzilla.mozilla.org/show_bug.cgi?id=1516290">bug[9]&lt;/a>. This time, it affects Mozilla Firefox. It can be silenced by setting &lt;code>browser.shell.checkDefaultBrowser&lt;/code> to &lt;code>false&lt;/code> in &lt;code>about:config&lt;/code>. Essentially equivalent of just going to &lt;em>Settings&lt;/em> page and unchecking &lt;em>Always check if Firefox is your default browser&lt;/em>.&lt;/p>
&lt;h3 id="thumbnails-for-other-media-types" >
&lt;div>
&lt;a href="#thumbnails-for-other-media-types">
##
&lt;/a>
Thumbnails for other media types
&lt;/div>
&lt;/h3>
&lt;p>Out of a box, Dolphin file chooser renders thumbnails for most of image file types. However, it&amp;rsquo;s not capable of generating thumbnails for videos.&lt;/p>
&lt;p>![Dolphin file chooser window showing video files with no thumbnails]({{ site.baseurl }}images/2021/novideothumbs.png)&lt;/p>
&lt;p>Install optional dependency &lt;code>ffmpegthumbs&lt;/code>. This provides functionality of rendering thumbnails for most video files.&lt;/p>
&lt;p>You might be tempted to install other optional dependencies, like &lt;code>kdegraphics-thumbnailers&lt;/code> or &lt;code>taglib&lt;/code> for generating thumbnails for other types of files. Keep in mind they work in main Dolphin file browser, not in the file chooser. Unless you&amp;rsquo;re going to use Dolphin file browser itself, installing them doesn&amp;rsquo;t make any difference.&lt;/p>
&lt;h3 id="icons" >
&lt;div>
&lt;a href="#icons">
##
&lt;/a>
Icons
&lt;/div>
&lt;/h3>
&lt;p>Dolphin installed separately from KDE does not feature themes. To use default icons set, install package &lt;code>breeze-icons&lt;/code>.&lt;/p>
&lt;h3 id="dark-theme-partially" >
&lt;div>
&lt;a href="#dark-theme-partially">
##
&lt;/a>
Dark theme (partially)
&lt;/div>
&lt;/h3>
&lt;p>Default theme of Dolphin file chooser is bright and it might not fit well with dark-color rices. While changing theme for Dolphin file browser is possible through Qt5 settings, &lt;del>theme settings do not apply to the file chooser&lt;/del>.&lt;/p>
&lt;p>&lt;em>UPDATE: It took several reboots for the file chooser to start respecting dark Qt5 theme, however I don&amp;rsquo;t know cause behind this behavior. Feel free to follow instructions below and it might work for you out-of-the-box or after a few days of some sort of transition.&lt;/em>&lt;/p>
&lt;p>Changing Qt5 apps theme without KDE Plasma is somewhat challenging on its own, so I&amp;rsquo;ll explain the method I used, but unfortunately it won&amp;rsquo;t be any of help for file chooser color scheme.&lt;/p>
&lt;p>Install &lt;code>breeze&lt;/code>, &lt;code>qt5ct&lt;/code> and run it. If you see a warning about program not properly configured, set &lt;code>QT_QPA_PLATFORMTHEME=qt5ct&lt;/code> environment variable and run it again.&lt;/p>
&lt;p>In the &lt;em>Appearance&lt;/em> tab, select style &lt;em>Breeze&lt;/em> and custom &lt;em>Palette&lt;/em> called &lt;em>darker&lt;/em>. Click &lt;em>Ok&lt;/em> and re-run Dolphin.&lt;/p>
&lt;p>It&amp;rsquo;s going to look pretty bad now. While most of the interface now has dark theme applied, background is solid white. Exit Dolphin and edit &lt;code>~/.config/kdeglobals&lt;/code> file adding:&lt;/p>
&lt;pre tabindex="0">&lt;code>[Colors:View]
BackgroundNormal=23,23,24
&lt;/code>&lt;/pre>&lt;p>(this setting might already be there, just replace the value then)&lt;/p>
&lt;p>Use RGB format for any color you like.&lt;/p>
&lt;p>To make GTK theme match new Breeze Darker, install &lt;code>breeze-gtk&lt;/code>. You can set it using a convenient GUI called &lt;code>lxappearance&lt;/code>.&lt;/p>
&lt;h2 id="alternatives" >
&lt;div>
&lt;a href="#alternatives">
#
&lt;/a>
Alternatives
&lt;/div>
&lt;/h2>
&lt;p>GNOME&amp;rsquo;s powerlessness in fixing this bug yielded many unofficial, fork- or plugin-based solutions. To name one, &lt;code>gtk3-patched-filechooser-icon-view&lt;/code> is available in AUR. If you&amp;rsquo;re all fine with GTK file chooser except for thumbnails matter, this might be a way to go. Personally, I find Dolphin file chooser much more appealing (even in its bright outfit) and admire UI transparency with full-fledged customization options. My recommendation is to try it out. In case you don&amp;rsquo;t like, just revert back to GTK.&lt;/p>
&lt;h2 id="links" >
&lt;div>
&lt;a href="#links">
#
&lt;/a>
Links
&lt;/div>
&lt;/h2>
&lt;pre tabindex="0">&lt;code>[0]: https://gitlab.gnome.org/GNOME/gtk/-/issues/233
[1]: https://jayfax.neocities.org/mediocrity/gnome-has-no-thumbnails-in-the-file-picker.html
[2]: https://apps.kde.org/dolphin/
[3]: https://wiki.archlinux.org/title/Uniform_look_for_Qt_and_GTK_applications#Consistent_file_dialog
[4]: https://github.com/electron/electron/pull/19159
[5]: https://github.com/electron/electron/releases/tag/v14.0.0
[6]: https://github.com/Microsoft/vscode/wiki/How-to-Contribute#build-and-run
[7]: https://docs.flatpak.org/en/latest/portals-qt.html
[8]: https://packages.debian.org/bullseye/qt5-xdgdesktopportal-platformtheme
[9]: https://bugzilla.mozilla.org/show_bug.cgi?id=1516290
&lt;/code>&lt;/pre>&lt;pre tabindex="0">&lt;code>&lt;/code>&lt;/pre></description></item><item><title>Fix JetBrains IDEA IntelliJ not working on bspwm (gray window issue)</title><link>/blog/bspwm-intellij-fix/</link><pubDate>Tue, 20 Jul 2021 01:00:00 +0000</pubDate><guid>/blog/bspwm-intellij-fix/</guid><description>&lt;p>An easily reproducible issue I&amp;rsquo;ve recently been running into is &lt;strong>JetBrains IDEs not being displayed properly in &lt;a href="https://github.com/baskerville/bspwm">bspwm[0]&lt;/a> environment&lt;/strong>. Instead of IDE graphical interface, only a gray solid window is being shown.&lt;/p>
&lt;p>There&amp;rsquo;s nothing innovatory about this fix, however it seems like it has never been mentioned in this particular context, meaning &lt;strong>running IntelliJ on bspwm and xorg&lt;/strong>.&lt;/p>
&lt;p>This issue has been described in &lt;a href="https://wiki.archlinux.org/title/Java#Gray_window,_applications_not_resizing_with_WM,_menus_immediately_closing">Troubleshooting section of Java article on ArchLinux wiki[1]&lt;/a>, although it doesn&amp;rsquo;t mention the fix method for bspwm.&lt;/p>
&lt;p>What needs to be done is &lt;strong>to export &lt;code>_JAVA_AWT_WM_NONREPARENTING=1&lt;/code>&lt;/strong>. Just like in the solution for Sway desktop environment.&lt;/p>
&lt;p>The easiest way to achieve this in xorg environment is to add this line in &lt;code>~/.xinitrc&lt;/code>:&lt;/p>
&lt;pre tabindex="0">&lt;code>export _JAVA_AWT_WM_NONREPARENTING=1
&lt;/code>&lt;/pre>&lt;h2 id="links" >
&lt;div>
&lt;a href="#links">
#
&lt;/a>
Links
&lt;/div>
&lt;/h2>
&lt;pre tabindex="0">&lt;code>[0]: https://github.com/baskerville/bspwm
[1]: https://wiki.archlinux.org/title/Java#Gray_window,_applications_not_resizing_with_WM,_menus_immediately_closing
&lt;/code>&lt;/pre></description></item><item><title>How I cracked obscure zlib compression format of old MySQL blobs</title><link>/blog/how-did-i-beat-obscure-zlib-compression-format/</link><pubDate>Sat, 03 Oct 2020 01:00:00 +0000</pubDate><guid>/blog/how-did-i-beat-obscure-zlib-compression-format/</guid><description>&lt;p>Looking around the web for old &lt;a href="https://en.wikipedia.org/wiki/Nuke_(warez)#Pre_database">pre databases[0]&lt;/a> I stumbled upon &lt;a href="http://rescene.wikidot.com/scene-databases">this nice collection of preDBs dumps[1]&lt;/a>. &lt;a href="http://rescene.wikidot.com/dump-islander">The one[2]&lt;/a> that caught my interest was shared by the user called Islander to celebrate him &lt;code>quitting this world and starting a real life&lt;/code>. Shared in October 2014, it was built across years 2005-2011, supposedly being the biggest database of nfos, sfvs and other metadata at 2011.&lt;/p>
&lt;p>7.7GB RAR archive includes 452MB SQL dump of pre entries and over 5.3GB SQL dump of nfo files. After importing these dumps into MySQL database server (8.0.21 hosted in Docker) I discovered that something is off with &lt;code>nfo&lt;/code> schema.&lt;/p>
&lt;p>To briefly give an idea on how the table looks like:&lt;/p>
&lt;pre tabindex="0">&lt;code>&amp;gt; SELECT COLUMN_NAME, ORDINAL_POSITION, COLUMNT_TYPE FROM information_schema.columns where TABLE_NAME=&amp;#39;nfo&amp;#39;
&amp;lt;
COLUMN_NAME ORDINAL_POSITION COLUMN_TYPE
id 1 int unsigned
rel_name 2 varchar
rel_nfo 3 longblog
rel_filename 4 varchar
&lt;/code>&lt;/pre>&lt;p>Cool. Let&amp;rsquo;s snatch one of those blobs:&lt;/p>
&lt;pre tabindex="0">&lt;code>&amp;gt; SELECT CONVERT(rel_nfo using ascii) from nfo where id=85443
&amp;lt; &amp;#34;$x????v??????M???b?J????&amp;amp;n?M?R????a$?bM?*E??...
&lt;/code>&lt;/pre>&lt;p>Huh? Turns out blobs are compressed. Since it&amp;rsquo;s MySQL, my first guess is of course &lt;strong>zlib&lt;/strong>.&lt;/p>
&lt;p>Trying to decompress the file using Python &lt;code>zlib&lt;/code> module:&lt;/p>
&lt;pre tabindex="0">&lt;code>&amp;gt;&amp;gt;&amp;gt; import zlib
&amp;gt;&amp;gt;&amp;gt; with open(&amp;#39;./nfo-rel_nfo.bin&amp;#39;, &amp;#39;rb&amp;#39;) as file:
... zlib.decompress(file)
...
Traceback (most recent call last):
File &amp;#34;&amp;lt;stdin&amp;gt;&amp;#34;, line 2, in &amp;lt;module&amp;gt;
zlib.error: Error -3 while decompressing data: incorrect header check
&lt;/code>&lt;/pre>&lt;p>No luck. Maybe it&amp;rsquo;s not zlib after all? I decide to verify that using &lt;a href="https://github.com/ReFirmLabs/binwalk">binwalk[3]&lt;/a>, a tool for heuristic binary-level analysis.&lt;/p>
&lt;pre tabindex="0">&lt;code>$ binwalk nfo-rel_nfo.bin
DECIMAL HEXADECIMAL DESCRIPTION
--------------------------------------------------------------------------------
4 0x4 Zlib compressed data, default compression
&lt;/code>&lt;/pre>&lt;p>It detects zlib signature at &lt;code>0x04&lt;/code> byte. How does it look like?&lt;/p>
&lt;pre tabindex="0">&lt;code>$ xxd nfo-rel_nfo.bin | head -1
00000000: 2224 0000 789c a599 dd76 dbb8 1180 eff5 &amp;#34;$..x....v......
&lt;/code>&lt;/pre>&lt;p>Perhaps it&amp;rsquo;s worth take a look at other file?&lt;/p>
&lt;pre tabindex="0">&lt;code>xxd nfo-rel_nfo-0.bin | head -1
00000000: 314e 0000 789c ed5c dd72 dbc6 92be dfaa 1N..x..\.r......
&lt;/code>&lt;/pre>&lt;p>After confirming it with numerous other files, it seems all of them share the same &lt;code>0x2, 0x3&lt;/code>, &lt;code>0x4&lt;/code> and &lt;code>0x5&lt;/code> bytes. What does it mean in terms of zlib compression?&lt;/p>
&lt;p>My first steps lead me to &lt;a href="https://tools.ietf.org/html/rfc1950">RFC 1950[4]&lt;/a> stating, that:&lt;/p>
&lt;ul>
&lt;li>&lt;code>0x0&lt;/code> defines CMF (Compression Method and flags)&lt;/li>
&lt;li>&lt;code>0x1&lt;/code> defines FLG (FLaGs)&lt;/li>
&lt;li>from &lt;code>0x2&lt;/code> to &lt;code>0x5&lt;/code> represent DICT if flag FDICT was set on 5th bit of &lt;code>0x1&lt;/code>&lt;/li>
&lt;li>everything beyond that point is compressed data&lt;/li>
&lt;li>files ends with Adler-32 checksum&lt;/li>
&lt;/ul>
&lt;p>Considering that all the files share the same bytes from 3rd to 6th, that are supposed to be either part of compression stream or FDICT while 1st and 2nd one are always different, it doesn&amp;rsquo;t make much of a sense.&lt;/p>
&lt;p>After hours of searching, I found &lt;a href="https://stackoverflow.com/q/59494980/6158307">this stackoverflow question[5]&lt;/a> asked by someone struggling with a similar problem. Upgrading zlib in some networking library broke the mutual compatibility between resources using old and new version of that library.&lt;/p>
&lt;p>They noticed that old zlib library (possibly 1.1.3) returns:&lt;/p>
&lt;pre tabindex="0">&lt;code>00000000: 1f00 0000 789c 0bc9 c82c 5600 a244 8592 ....x....,V..D..
&lt;/code>&lt;/pre>&lt;p>While 1.2.11 returns:&lt;/p>
&lt;pre tabindex="0">&lt;code>00000000: 78da 0bc9 c82c 5600 a244 8592 d4e2 1285 x....,V..D......
&lt;/code>&lt;/pre>&lt;p>Supposing the shared part starting with &lt;code>0x0bc9 c82c 5600 a244...&lt;/code> is compressed data stream, &lt;code>0x789c&lt;/code> are flags for 1.1.3 zlib compression and &lt;code>0x78da&lt;/code> are flags set by zlib 1.2.11. However, old zlib adds a little extra to the output - &lt;code>0x1f000000&lt;/code>. What&amp;rsquo;s the meaning behind that?&lt;/p>
&lt;p>That person eventually found the answer on the same day. Citing:&lt;/p>
&lt;pre tabindex="0">&lt;code>Turns out the older zlib - or possibly the older networking library - had a 32-bit &amp;#34;uncompressed output&amp;#34; size prepended to its compressed output.
&lt;/code>&lt;/pre>&lt;p>Let&amp;rsquo;s checkout that theory! Coincidentally, yet luckily I&amp;rsquo;m in possession of uncompressed version of this nfo file acquired somewhere else.&lt;/p>
&lt;pre tabindex="0">&lt;code>$ ls -l 00-va_-_big_dance_party_vol_1-\(185_312\)-1994-zzzz.nfo
-rw-r--r-- 1 max max 9250 Mar 9 01:02 &amp;#39;00-va_-_big_dance_party_vol_1-(185_312)-1994-zzzz.nfo&amp;#39;
&lt;/code>&lt;/pre>&lt;p>9250 bytes. Going back to that strange signature, first 32 bits are: &lt;code>0x22240000&lt;/code>, &lt;code>572784640&lt;/code> converted to decimal. Not even close to &lt;code>9250&lt;/code>. It took me a few moments to realize my mistake. Of course it needs to be read in little-endian order! After swapping - &lt;code>0x00002422&lt;/code> is &lt;code>9250&lt;/code>! Gotcha!!&lt;/p>
&lt;p>Let&amp;rsquo;s try removing first 32 bits of the file to fix flags offset and see what happens:&lt;/p>
&lt;pre tabindex="0">&lt;code>&amp;gt;&amp;gt;&amp;gt; with open(&amp;#39;./nfo-rel_nfo.bin&amp;#39;, &amp;#39;rb&amp;#39;) as input, open(&amp;#39;./out.nfo&amp;#39;, &amp;#39;wb&amp;#39;) as output:
... x = zlib.decompress(input.read())
... # no more errors yay! :D
... output.write(x)
...
9250
&lt;/code>&lt;/pre>&lt;p>It seems it worked. Let&amp;rsquo;s check out the output:
&lt;img src="https://babarowski.com/images/2020/zzzz.png" alt="Screenshot showing decompressed file">&lt;/p>
&lt;p>Victory! Now, in order to extract all the nfos from the database, I wrote this nice little Python script:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#e2e4e5;background-color:#282a36;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-python" data-lang="python">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#ff6ac1">from&lt;/span> pathlib &lt;span style="color:#ff6ac1">import&lt;/span> Path
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#ff6ac1">import&lt;/span> zlib
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#ff6ac1">import&lt;/span> mysql.connector
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#ff6ac1">from&lt;/span> tqdm &lt;span style="color:#ff6ac1">import&lt;/span> tqdm
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#ff6ac1">if&lt;/span> __name__ &lt;span style="color:#ff6ac1">==&lt;/span> &lt;span style="color:#5af78e">&amp;#39;__main__&amp;#39;&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> dump_path &lt;span style="color:#ff6ac1">=&lt;/span> &lt;span style="color:#5af78e">&amp;#39;/home/max/Projects/pre_archive/islander&amp;#39;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> cnx &lt;span style="color:#ff6ac1">=&lt;/span> mysql&lt;span style="color:#ff6ac1">.&lt;/span>connector&lt;span style="color:#ff6ac1">.&lt;/span>connect(user&lt;span style="color:#ff6ac1">=&lt;/span>&lt;span style="color:#5af78e">&amp;#39;root&amp;#39;&lt;/span>, password&lt;span style="color:#ff6ac1">=&lt;/span>&lt;span style="color:#5af78e">&amp;#39;root&amp;#39;&lt;/span>,
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> host&lt;span style="color:#ff6ac1">=&lt;/span>&lt;span style="color:#5af78e">&amp;#39;localhost&amp;#39;&lt;/span>, database&lt;span style="color:#ff6ac1">=&lt;/span>&lt;span style="color:#5af78e">&amp;#39;islander&amp;#39;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> cursor &lt;span style="color:#ff6ac1">=&lt;/span> cnx&lt;span style="color:#ff6ac1">.&lt;/span>cursor()
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> data_query &lt;span style="color:#ff6ac1">=&lt;/span> &lt;span style="color:#5af78e">&amp;#39;SELECT rel_name, rel_filename, rel_nfo FROM nfo;&amp;#39;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> count_query &lt;span style="color:#ff6ac1">=&lt;/span> &lt;span style="color:#5af78e">&amp;#39;SELECT COUNT(*) FROM nfo;&amp;#39;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> cursor&lt;span style="color:#ff6ac1">.&lt;/span>execute(count_query)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> nfo_count &lt;span style="color:#ff6ac1">=&lt;/span> cursor&lt;span style="color:#ff6ac1">.&lt;/span>fetchone()
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> cursor&lt;span style="color:#ff6ac1">.&lt;/span>execute(data_query)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff6ac1">with&lt;/span> tqdm(total&lt;span style="color:#ff6ac1">=&lt;/span>nfo_count[&lt;span style="color:#ff9f43">0&lt;/span>]) &lt;span style="color:#ff6ac1">as&lt;/span> pbar:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff6ac1">for&lt;/span> (rel_name, rel_filename, rel_nfo) &lt;span style="color:#ff6ac1">in&lt;/span> cursor:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> release_path &lt;span style="color:#ff6ac1">=&lt;/span> &lt;span style="color:#5af78e">f&lt;/span>&lt;span style="color:#5af78e">&amp;#39;&lt;/span>&lt;span style="color:#5af78e">{&lt;/span>dump_path&lt;span style="color:#5af78e">}&lt;/span>&lt;span style="color:#5af78e">/&lt;/span>&lt;span style="color:#5af78e">{&lt;/span>rel_name&lt;span style="color:#5af78e">}&lt;/span>&lt;span style="color:#5af78e">&amp;#39;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> filename &lt;span style="color:#ff6ac1">=&lt;/span> &lt;span style="color:#5af78e">f&lt;/span>&lt;span style="color:#5af78e">&amp;#39;&lt;/span>&lt;span style="color:#5af78e">{&lt;/span>rel_name&lt;span style="color:#5af78e">}&lt;/span>&lt;span style="color:#5af78e">.nfo&amp;#39;&lt;/span>&lt;span style="color:#ff6ac1">.&lt;/span>replace(&lt;span style="color:#5af78e">&amp;#39;/&amp;#39;&lt;/span>, &lt;span style="color:#5af78e">&amp;#39;&amp;#39;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> Path(release_path)&lt;span style="color:#ff6ac1">.&lt;/span>mkdir(parents&lt;span style="color:#ff6ac1">=&lt;/span>&lt;span style="color:#ff6ac1">True&lt;/span>, exist_ok&lt;span style="color:#ff6ac1">=&lt;/span>&lt;span style="color:#ff6ac1">True&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff6ac1">with&lt;/span> &lt;span style="color:#ff5c57">open&lt;/span>(&lt;span style="color:#5af78e">f&lt;/span>&lt;span style="color:#5af78e">&amp;#39;&lt;/span>&lt;span style="color:#5af78e">{&lt;/span>release_path&lt;span style="color:#5af78e">}&lt;/span>&lt;span style="color:#5af78e">/&lt;/span>&lt;span style="color:#5af78e">{&lt;/span>filename&lt;span style="color:#5af78e">}&lt;/span>&lt;span style="color:#5af78e">&amp;#39;&lt;/span>, &lt;span style="color:#5af78e">&amp;#39;wb&amp;#39;&lt;/span>) &lt;span style="color:#ff6ac1">as&lt;/span> nfo_file:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff6ac1">try&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> d &lt;span style="color:#ff6ac1">=&lt;/span> zlib&lt;span style="color:#ff6ac1">.&lt;/span>decompress(rel_nfo[&lt;span style="color:#ff9f43">4&lt;/span>:]) &lt;span style="color:#78787e"># trim 32 header and decompress&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> nfo_file&lt;span style="color:#ff6ac1">.&lt;/span>write(d)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> nfo_file&lt;span style="color:#ff6ac1">.&lt;/span>close()
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff6ac1">except&lt;/span> zlib&lt;span style="color:#ff6ac1">.&lt;/span>error:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff5c57">print&lt;/span>(&lt;span style="color:#5af78e">f&lt;/span>&lt;span style="color:#5af78e">&amp;#39;Error decompressing: &lt;/span>&lt;span style="color:#5af78e">{&lt;/span>rel_name&lt;span style="color:#5af78e">}&lt;/span>&lt;span style="color:#5af78e">&amp;#39;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> pbar&lt;span style="color:#ff6ac1">.&lt;/span>update()
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> cursor&lt;span style="color:#ff6ac1">.&lt;/span>close()
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> cnx&lt;span style="color:#ff6ac1">.&lt;/span>close()
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Crucial part is the line 32, where binary data is being read by &lt;code>zlib.decompress&lt;/code> omitting first 4 bytes. After adding some precautious steps to the script (some filenames has forbidden characters, plus ext4 doesn&amp;rsquo;t handle 72,923 files in a single directory well) I was able to decompress all the blobs in this database 🎉🥰&lt;/p>
&lt;h2 id="links" >
&lt;div>
&lt;a href="#links">
#
&lt;/a>
Links
&lt;/div>
&lt;/h2>
&lt;pre tabindex="0">&lt;code>[0]: https://en.wikipedia.org/wiki/Nuke_(warez)#Pre_database
[1]: http://rescene.wikidot.com/scene-databases
[2]: http://rescene.wikidot.com/dump-islander
[3]: https://github.com/ReFirmLabs/binwalk
[4]: https://tools.ietf.org/html/rfc1950
[5]: https://stackoverflow.com/q/59494980/6158307
&lt;/code>&lt;/pre></description></item><item><title>Mocking @AuthenticationPrincipal with custom UserDetails object</title><link>/blog/mock-authentication-with-custom-userdetails/</link><pubDate>Sun, 20 Sep 2020 19:00:00 +0000</pubDate><guid>/blog/mock-authentication-with-custom-userdetails/</guid><description>&lt;p>Lately I&amp;rsquo;ve been working on a project involving Spring Boot and Spring Security. It includes stateless API for authenticating users and restricting access to specific HTTP endpoints. I followed this &lt;a href="https://golb.hplar.ch/2019/05/stateless.html">Stateless Authentication with Spring Security[0]&lt;/a> article. Sessions associated with users are stored in the database table, while users authenticate with session ID in &lt;code>Authorization&lt;/code> request header. Pretty much what JWT does, except for I didn&amp;rsquo;t know about its existene at the moment, lol.&lt;/p>
&lt;p>&lt;strong>NOTE: This article follows my journey of debugging the problem, trying different wrong approaches and eventually finding the solution.
If it&amp;rsquo;s tldr for you, jump straight to &lt;a href="#final-solution">the solution&lt;/a>.&lt;/strong>&lt;/p>
&lt;p>Anyway, I took an advantage of &lt;code>Authentication&lt;/code> storing principal object and created a class implementing &lt;code>UserDetails&lt;/code> interface holding my DTO with mapped user entity:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#e2e4e5;background-color:#282a36;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-java" data-lang="java">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#ff9f43">@RequiredArgsConstructor&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#ff5c57">public&lt;/span> &lt;span style="color:#ff5c57">class&lt;/span> &lt;span style="color:#f3f99d">CustomUserDetails&lt;/span> &lt;span style="color:#ff5c57">implements&lt;/span> UserDetails {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff9f43">@Getter&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff5c57">private&lt;/span> &lt;span style="color:#ff5c57">final&lt;/span> UserDTO user;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff9f43">@Override&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff5c57">public&lt;/span> Collection&lt;span style="color:#ff6ac1">&amp;lt;?&lt;/span> &lt;span style="color:#ff5c57">extends&lt;/span> GrantedAuthority&lt;span style="color:#ff6ac1">&amp;gt;&lt;/span> &lt;span style="color:#57c7ff">getAuthorities&lt;/span>() {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> RoleDTO role &lt;span style="color:#ff6ac1">=&lt;/span> user.&lt;span style="color:#57c7ff">getRole&lt;/span>();
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> Set&lt;span style="color:#ff6ac1">&amp;lt;&lt;/span>GrantedAuthority&lt;span style="color:#ff6ac1">&amp;gt;&lt;/span> rolesSet &lt;span style="color:#ff6ac1">=&lt;/span> &lt;span style="color:#ff6ac1">new&lt;/span> HashSet&lt;span style="color:#ff6ac1">&amp;lt;&amp;gt;&lt;/span>();
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> rolesSet.&lt;span style="color:#57c7ff">add&lt;/span>(&lt;span style="color:#ff6ac1">new&lt;/span> SimpleGrantedAuthority(&lt;span style="color:#5af78e">&amp;#34;ROLE_&amp;#34;&lt;/span> &lt;span style="color:#ff6ac1">+&lt;/span> role.&lt;span style="color:#57c7ff">getRoleName&lt;/span>()));
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff6ac1">return&lt;/span> rolesSet;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff9f43">@Override&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff5c57">public&lt;/span> String &lt;span style="color:#57c7ff">getPassword&lt;/span>() {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff6ac1">return&lt;/span> user.&lt;span style="color:#57c7ff">getPassword&lt;/span>();
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff9f43">@Override&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff5c57">public&lt;/span> String &lt;span style="color:#57c7ff">getUsername&lt;/span>() {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff6ac1">return&lt;/span> user.&lt;span style="color:#57c7ff">getUsername&lt;/span>();
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff9f43">@Override&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff5c57">public&lt;/span> &lt;span style="color:#9aedfe">boolean&lt;/span> &lt;span style="color:#57c7ff">isAccountNonExpired&lt;/span>() {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff6ac1">return&lt;/span> user.&lt;span style="color:#57c7ff">getIsActive&lt;/span>();
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff9f43">@Override&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff5c57">public&lt;/span> &lt;span style="color:#9aedfe">boolean&lt;/span> &lt;span style="color:#57c7ff">isAccountNonLocked&lt;/span>() {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff6ac1">return&lt;/span> user.&lt;span style="color:#57c7ff">getIsActive&lt;/span>();
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff9f43">@Override&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff5c57">public&lt;/span> &lt;span style="color:#9aedfe">boolean&lt;/span> &lt;span style="color:#57c7ff">isCredentialsNonExpired&lt;/span>() {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff6ac1">return&lt;/span> user.&lt;span style="color:#57c7ff">getIsActive&lt;/span>();
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff9f43">@Override&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff5c57">public&lt;/span> &lt;span style="color:#9aedfe">boolean&lt;/span> &lt;span style="color:#57c7ff">isEnabled&lt;/span>() {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff6ac1">return&lt;/span> user.&lt;span style="color:#57c7ff">getIsActive&lt;/span>();
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Essentially, a proxy class implementing &lt;code>UserDetails&lt;/code> with a &lt;code>UserDTO&lt;/code> composed in and using its properties for resolving &lt;code>UserDetails&lt;/code> implemented methods. This approach allowed me to very easily access properties of a user accessing an endpoint. Consider this &lt;code>GET /user/images/&lt;/code> private endpoint, which is supposed to return authenticated user&amp;rsquo;s uploaded images:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#e2e4e5;background-color:#282a36;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-java" data-lang="java">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#ff5c57">public&lt;/span> ResponseEntity&lt;span style="color:#ff6ac1">&amp;lt;&lt;/span>List&lt;span style="color:#ff6ac1">&amp;lt;&lt;/span>ImageDTO&lt;span style="color:#ff6ac1">&amp;gt;&amp;gt;&lt;/span> &lt;span style="color:#57c7ff">userImages&lt;/span>(&lt;span style="color:#ff9f43">@AuthenticationPrincipal&lt;/span> CustomUserDetails userDetails) {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> UserDTO user &lt;span style="color:#ff6ac1">=&lt;/span> userDetails.&lt;span style="color:#57c7ff">getUser&lt;/span>();
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> Optional&lt;span style="color:#ff6ac1">&amp;lt;&lt;/span>List&lt;span style="color:#ff6ac1">&amp;lt;&lt;/span>ImageDTO&lt;span style="color:#ff6ac1">&amp;gt;&amp;gt;&lt;/span> images &lt;span style="color:#ff6ac1">=&lt;/span> Optional.&lt;span style="color:#57c7ff">ofNullable&lt;/span>(imageDTOService.&lt;span style="color:#57c7ff">findAllUploadedBy&lt;/span>(user));
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff6ac1">if&lt;/span> (images.&lt;span style="color:#57c7ff">isEmpty&lt;/span>()) {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff6ac1">return&lt;/span> ResponseEntity.&lt;span style="color:#57c7ff">status&lt;/span>(HttpStatus.&lt;span style="color:#57c7ff">OK&lt;/span>).&lt;span style="color:#57c7ff">body&lt;/span>(Collections.&lt;span style="color:#57c7ff">emptyList&lt;/span>());
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff6ac1">return&lt;/span> ResponseEntity.&lt;span style="color:#57c7ff">status&lt;/span>(HttpStatus.&lt;span style="color:#57c7ff">OK&lt;/span>).&lt;span style="color:#57c7ff">body&lt;/span>(images.&lt;span style="color:#57c7ff">get&lt;/span>());
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>To me it looks nice and elegant. This custom &lt;code>UserDetailsService&lt;/code> returns &lt;code>CustomUserDetails&lt;/code> based on what persistence layer returns, so the encapsulated DTO object can always be used for interaction with other DTOs or persistence services.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#e2e4e5;background-color:#282a36;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-java" data-lang="java">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#ff9f43">@Service&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#ff9f43">@RequiredArgsConstructor&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#ff5c57">public&lt;/span> &lt;span style="color:#ff5c57">class&lt;/span> &lt;span style="color:#f3f99d">CustomUserDetailsService&lt;/span> &lt;span style="color:#ff5c57">implements&lt;/span> UserDetailsService {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff5c57">private&lt;/span> &lt;span style="color:#ff5c57">final&lt;/span> UserDTOService userDTOService;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff9f43">@Override&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff5c57">public&lt;/span> UserDetails &lt;span style="color:#57c7ff">loadUserByUsername&lt;/span>(String username) &lt;span style="color:#ff5c57">throws&lt;/span> UsernameNotFoundException {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> Optional&lt;span style="color:#ff6ac1">&amp;lt;&lt;/span>UserDTO&lt;span style="color:#ff6ac1">&amp;gt;&lt;/span> user &lt;span style="color:#ff6ac1">=&lt;/span> Optional.&lt;span style="color:#57c7ff">ofNullable&lt;/span>(userDTOService.&lt;span style="color:#57c7ff">findByUsername&lt;/span>(username));
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff6ac1">if&lt;/span> (user.&lt;span style="color:#57c7ff">isEmpty&lt;/span>()) {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff6ac1">throw&lt;/span> &lt;span style="color:#ff6ac1">new&lt;/span> UsernameNotFoundException(&lt;span style="color:#5af78e">&amp;#34;Username &amp;#34;&lt;/span> &lt;span style="color:#ff6ac1">+&lt;/span> username &lt;span style="color:#ff6ac1">+&lt;/span> &lt;span style="color:#5af78e">&amp;#34; not found&amp;#34;&lt;/span>);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff6ac1">return&lt;/span> &lt;span style="color:#ff6ac1">new&lt;/span> CustomUserDetails(user.&lt;span style="color:#57c7ff">get&lt;/span>());
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="how-do-i-test-this-controller" >
&lt;div>
&lt;a href="#how-do-i-test-this-controller">
#
&lt;/a>
How do I test this controller?
&lt;/div>
&lt;/h2>
&lt;p>However, this approach implies some issues when it comes to testing the controller.&lt;/p>
&lt;p>While trying to test &lt;code>UserControllerImpl.userImages()&lt;/code> I realized I need to somehow mock the parameter &lt;code>@AuthenticationPrincipal CustomUserDetails userDetails&lt;/code>. Not knowing much about it, I did some googling and found out, that &lt;a href="https://stackoverflow.com/questions/46615504/springboottest-mock-authentication-principal-with-a-custom-user-does-not-work">others[1]&lt;/a> use &lt;a href="https://docs.spring.io/spring-security/site/docs/current/api/org/springframework/security/test/context/support/WithMockUser.html">@WithMockUser[2]&lt;/a> annotation in situations like that. However, this turned out unsatisfactionary for my case, since the method I want to test doesn&amp;rsquo;t just restrict an access to some commonly-used resource. If I was, for example, testing the accessibility of resources that should only be used by specified group of users, that would be perfect. Using &lt;code>@WithMockUser&lt;/code> I can specify roles and/or authorities required to access the resource, then test if access is granted (or properly rejected for mocked users with insufficient authorities). Or even a simpler case, having an endpoint returning user&amp;rsquo;s username. Just a quick &lt;code>@WithMockUser(username = &amp;quot;foo&amp;quot;)&lt;/code> and a job is done!&lt;/p>
&lt;p>Meanwhile, my resource uses the principal to pass the encapsulated user DTO to the service. It delegates the job to the persistence layer, which requires DTO object which can be later be mapped to full-fleged entity. Therefore, I needed full-fleged authentication principal mocking.&lt;/p>
&lt;h2 id="trying-to-mock-the-principal-by-interference-in-security-context" >
&lt;div>
&lt;a href="#trying-to-mock-the-principal-by-interference-in-security-context">
#
&lt;/a>
Trying to mock the principal by interference in Security Context
&lt;/div>
&lt;/h2>
&lt;p>What exactly is &lt;code>@AuthenticationPrincipal&lt;/code>? According to the &lt;a href="https://docs.spring.io/spring-security/site/docs/current/api/org/springframework/security/core/annotation/AuthenticationPrincipal.html">Spring Security documentation[3]&lt;/a>, it ties up a method argument with &lt;code>Authentication.getPrincipal()&lt;/code> return object. &lt;code>Authentication&lt;/code> object can be easily obtained from &lt;code>SecurityContextHolder&lt;/code>, so using some help of this &lt;a href="https://stackoverflow.com/a/46631015/6158307">stack overflow answer[4]&lt;/a> I came up with these few lines of code:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#e2e4e5;background-color:#282a36;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-java" data-lang="java">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#ff9f43">@SpringBootTest&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#ff9f43">@AutoConfigureMockMvc&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#ff5c57">class&lt;/span> &lt;span style="color:#f3f99d">UserControllerImplTest&lt;/span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff9f43">@Test&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff5c57">public&lt;/span> &lt;span style="color:#9aedfe">void&lt;/span> &lt;span style="color:#57c7ff">whenGetUserImages_andUserIsAuthorized_thenReturnListOfImages&lt;/span>() &lt;span style="color:#ff5c57">throws&lt;/span> Exception {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> String username &lt;span style="color:#ff6ac1">=&lt;/span> &lt;span style="color:#5af78e">&amp;#34;username&amp;#34;&lt;/span>;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> UserDTO userDTO &lt;span style="color:#ff6ac1">=&lt;/span> getSampleUser(username);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> CustomUserDetails userDetails &lt;span style="color:#ff6ac1">=&lt;/span> &lt;span style="color:#ff6ac1">new&lt;/span> CustomUserDetails(userDTO);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> Authentication authentication &lt;span style="color:#ff6ac1">=&lt;/span> Mockito.&lt;span style="color:#57c7ff">mock&lt;/span>(Authentication.&lt;span style="color:#57c7ff">class&lt;/span>);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> SecurityContext securityContext &lt;span style="color:#ff6ac1">=&lt;/span> Mockito.&lt;span style="color:#57c7ff">mock&lt;/span>(SecurityContext.&lt;span style="color:#57c7ff">class&lt;/span>);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> AuthCookieFilter authCookieFilter &lt;span style="color:#ff6ac1">=&lt;/span> Mockito.&lt;span style="color:#57c7ff">mock&lt;/span>(AuthCookieFilter.&lt;span style="color:#57c7ff">class&lt;/span>);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> Mockito.&lt;span style="color:#57c7ff">when&lt;/span>(securityContext.&lt;span style="color:#57c7ff">getAuthentication&lt;/span>()).&lt;span style="color:#57c7ff">thenReturn&lt;/span>(authentication);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> SecurityContextHolder.&lt;span style="color:#57c7ff">setContext&lt;/span>(securityContext);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> Mockito.&lt;span style="color:#57c7ff">when&lt;/span>(authentication.&lt;span style="color:#57c7ff">getPrincipal&lt;/span>()).&lt;span style="color:#57c7ff">thenReturn&lt;/span>(userDetails);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> Mockito.&lt;span style="color:#57c7ff">doNothing&lt;/span>().&lt;span style="color:#57c7ff">when&lt;/span>(authCookieFilter).&lt;span style="color:#57c7ff">doFilter&lt;/span>(Mockito.&lt;span style="color:#57c7ff">any&lt;/span>(ServletRequest.&lt;span style="color:#57c7ff">class&lt;/span>), Mockito.&lt;span style="color:#57c7ff">any&lt;/span>(ServletResponse.&lt;span style="color:#57c7ff">class&lt;/span>), Mockito.&lt;span style="color:#57c7ff">any&lt;/span>(FilterChain.&lt;span style="color:#57c7ff">class&lt;/span>));
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> List&lt;span style="color:#ff6ac1">&amp;lt;&lt;/span>ImageDTO&lt;span style="color:#ff6ac1">&amp;gt;&lt;/span> images &lt;span style="color:#ff6ac1">=&lt;/span> getSampleImages();
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> images.&lt;span style="color:#57c7ff">forEach&lt;/span>(imageDTO &lt;span style="color:#ff6ac1">-&amp;gt;&lt;/span> imageDTO.&lt;span style="color:#57c7ff">setUploader&lt;/span>(userDTO));
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> Mockito.&lt;span style="color:#57c7ff">when&lt;/span>(imageDTOService.&lt;span style="color:#57c7ff">findAllUploadedBy&lt;/span>(userDTO)).&lt;span style="color:#57c7ff">thenReturn&lt;/span>(images);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> mockMvc.&lt;span style="color:#57c7ff">perform&lt;/span>(get(&lt;span style="color:#5af78e">&amp;#34;/user/images/&amp;#34;&lt;/span>))
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> .&lt;span style="color:#57c7ff">andExpect&lt;/span>(status().&lt;span style="color:#57c7ff">isOk&lt;/span>())
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> .&lt;span style="color:#57c7ff">andExpect&lt;/span>(content().&lt;span style="color:#57c7ff">contentType&lt;/span>(MediaType.&lt;span style="color:#57c7ff">APPLICATION_JSON&lt;/span>))
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> .&lt;span style="color:#57c7ff">andExpect&lt;/span>(content().&lt;span style="color:#57c7ff">json&lt;/span>(&lt;span style="color:#5af78e">&amp;#34;expected json response content goes here&amp;#34;&lt;/span>);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff5c57">private&lt;/span> UserDTO &lt;span style="color:#57c7ff">getSampleUser&lt;/span>(String username) {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff6ac1">return&lt;/span> UserDTO.&lt;span style="color:#57c7ff">builder&lt;/span>()
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> .&lt;span style="color:#57c7ff">username&lt;/span>(username)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> .&lt;span style="color:#57c7ff">email&lt;/span>(username &lt;span style="color:#ff6ac1">+&lt;/span> &lt;span style="color:#5af78e">&amp;#34;@example.com&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> .&lt;span style="color:#57c7ff">password&lt;/span>(&lt;span style="color:#5af78e">&amp;#34;password&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> .&lt;span style="color:#57c7ff">registerTime&lt;/span>(LocalDateTime.&lt;span style="color:#57c7ff">of&lt;/span>(2020, 1, 1, 12, 0, 0, 0))
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> .&lt;span style="color:#57c7ff">isActive&lt;/span>(&lt;span style="color:#ff6ac1">true&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> .&lt;span style="color:#57c7ff">build&lt;/span>();
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff5c57">private&lt;/span> List&lt;span style="color:#ff6ac1">&amp;lt;&lt;/span>ImageDTO&lt;span style="color:#ff6ac1">&amp;gt;&lt;/span> &lt;span style="color:#57c7ff">getSampleImages&lt;/span>() {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> ImageDTO image1 &lt;span style="color:#ff6ac1">=&lt;/span> getSampleImage(1L, &lt;span style="color:#5af78e">&amp;#34;token1&amp;#34;&lt;/span>);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> ImageDTO image2 &lt;span style="color:#ff6ac1">=&lt;/span> getSampleImage(2L, &lt;span style="color:#5af78e">&amp;#34;token2&amp;#34;&lt;/span>);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> ImageDTO image3 &lt;span style="color:#ff6ac1">=&lt;/span> getSampleImage(3L, &lt;span style="color:#5af78e">&amp;#34;token3&amp;#34;&lt;/span>);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff6ac1">return&lt;/span> List.&lt;span style="color:#57c7ff">of&lt;/span>(image1, image2, image3);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff5c57">private&lt;/span> ImageDTO &lt;span style="color:#57c7ff">getSampleImage&lt;/span>(Long id, String token) {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff6ac1">return&lt;/span> ImageDTO.&lt;span style="color:#57c7ff">builder&lt;/span>()
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> .&lt;span style="color:#57c7ff">id&lt;/span>(id)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> .&lt;span style="color:#57c7ff">token&lt;/span>(token)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> .&lt;span style="color:#57c7ff">isActive&lt;/span>(&lt;span style="color:#ff6ac1">true&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> .&lt;span style="color:#57c7ff">isPublic&lt;/span>(&lt;span style="color:#ff6ac1">true&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> .&lt;span style="color:#57c7ff">uploadTime&lt;/span>(LocalDateTime.&lt;span style="color:#57c7ff">of&lt;/span>(2020, 1, 1, 12, 0, 0, 0))
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> .&lt;span style="color:#57c7ff">build&lt;/span>();
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>&lt;code>AuthCookieFilter&lt;/code> is my request filter (&lt;code>GenericFilterBean&lt;/code>), scanning requests for &lt;code>Authorization&lt;/code> token, looking their content up in database and setting &lt;code>Authentication&lt;/code> in &lt;code>SecurityContextHolder&lt;/code> if it matches any. I had to silence it, since I&amp;rsquo;m setting up the Security Context up manually.&lt;/p>
&lt;p>Unfortunately, it didn&amp;rsquo;t work. For some reason, I couldn&amp;rsquo;t get pass authorization, getting 401 responses all the time. After some time debugging, I found that one of the first culprits of failing the authentication is &lt;code>ProviderManager.authenticate()&lt;/code> throwing a &lt;a href="https://docs.spring.io/spring-security/site/docs/current/api/org/springframework/security/authentication/ProviderNotFoundException.html">ProviderNotFoundException[5]&lt;/a>. So, there&amp;rsquo;s no &lt;code>AuthenticationProvider&lt;/code> capable of handling my &lt;code>Authentication&lt;/code>, huh? What&amp;rsquo;s the &lt;code>Authentication&lt;/code> now, anyway? According to the debugger: &lt;code>Authentication$MockitoMock&lt;/code>. Honestly, I don&amp;rsquo;t really know much about how mocks work internally. How are public properties and methods made visible, how references to them are being passed and most importantly, how is such an object perceived by other components of the application? My guess was that &lt;code>ProviderManager&lt;/code> can&amp;rsquo;t match any provider with my not-so-ordinary object.&lt;/p>
&lt;p>And so, I decided to abadon this idea and keep looking further.&lt;/p>
&lt;h2 id="injecting-custom-userdetailsservice" >
&lt;div>
&lt;a href="#injecting-custom-userdetailsservice">
#
&lt;/a>
Injecting custom UserDetailsService
&lt;/div>
&lt;/h2>
&lt;p>Then I stumbled upon this &lt;a href="https://stackoverflow.com/a/43920932/6158307">stack overflow answer[6]&lt;/a>, that made me aware of &lt;a href="https://docs.spring.io/spring-security/site/docs/current/api/org/springframework/security/test/context/support/WithUserDetails.html">&lt;code>@WithUserDetails&lt;/code>[7]&lt;/a> annotation. Similarly to &lt;code>@WithMockUser&lt;/code> it allows to inject a mock user to the request, but delegating the job of creating &lt;code>UserDetails&lt;/code> object to the developer. &lt;code>@WithMockUser&lt;/code> is higher level functionality, creating a simple &lt;code>UserDetails&lt;/code> based on input parameters. With &lt;code>@WithUserDetails&lt;/code> one needs to provide own &lt;code>UserDetailsService&lt;/code>.&lt;/p>
&lt;p>Perfect! I created additional configuration class for such a custom service:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#e2e4e5;background-color:#282a36;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-java" data-lang="java">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#ff9f43">@TestConfiguration&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#ff5c57">public&lt;/span> &lt;span style="color:#ff5c57">class&lt;/span> &lt;span style="color:#f3f99d">SpringSecurityForUserControllerImplTestConfig&lt;/span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff9f43">@Bean&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff9f43">@Primary&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff5c57">public&lt;/span> UserDetailsService &lt;span style="color:#57c7ff">userDetailsService&lt;/span>() {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> UserDTO userDTO &lt;span style="color:#ff6ac1">=&lt;/span> UserDTO.&lt;span style="color:#57c7ff">builder&lt;/span>()
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> .&lt;span style="color:#57c7ff">username&lt;/span>(&lt;span style="color:#5af78e">&amp;#34;username&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> .&lt;span style="color:#57c7ff">email&lt;/span>(&lt;span style="color:#5af78e">&amp;#34;username@example.com&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> .&lt;span style="color:#57c7ff">password&lt;/span>(&lt;span style="color:#5af78e">&amp;#34;password&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> .&lt;span style="color:#57c7ff">registerTime&lt;/span>(LocalDateTime.&lt;span style="color:#57c7ff">of&lt;/span>(2020, 1, 1, 12, 0, 0, 0))
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> .&lt;span style="color:#57c7ff">isActive&lt;/span>(&lt;span style="color:#ff6ac1">true&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> .&lt;span style="color:#57c7ff">role&lt;/span>(RoleDTO.&lt;span style="color:#57c7ff">builder&lt;/span>().&lt;span style="color:#57c7ff">roleName&lt;/span>(&lt;span style="color:#5af78e">&amp;#34;USER&amp;#34;&lt;/span>).&lt;span style="color:#57c7ff">build&lt;/span>())
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> .&lt;span style="color:#57c7ff">build&lt;/span>();
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> CustomUserDetails userDetails &lt;span style="color:#ff6ac1">=&lt;/span> &lt;span style="color:#ff6ac1">new&lt;/span> CustomUserDetails(userDTO);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff6ac1">return&lt;/span> &lt;span style="color:#ff6ac1">new&lt;/span> InMemoryUserDetailsManager(List.&lt;span style="color:#57c7ff">of&lt;/span>(userDetails));
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>added &lt;code>@SpringBootTest(classes = SpringSecurityForUserControllerImplTestConfig.class)&lt;/code> above tests class and &lt;code>@WithUserDetails(&amp;quot;username&amp;quot;)&lt;/code> above the test. To retrieve &lt;code>UserDTO&lt;/code> object inside test method I used the following line:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#e2e4e5;background-color:#282a36;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-java" data-lang="java">&lt;span style="display:flex;">&lt;span>UserDTO userDTO &lt;span style="color:#ff6ac1">=&lt;/span> ((CustomUserDetails) SecurityContextHolder.&lt;span style="color:#57c7ff">getContext&lt;/span>().&lt;span style="color:#57c7ff">getAuthentication&lt;/span>().&lt;span style="color:#57c7ff">getPrincipal&lt;/span>()).&lt;span style="color:#57c7ff">getUser&lt;/span>();
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>which unfortunately resulted in ClassCastException between &lt;code>org.springframework.security.core.userdetails.User&lt;/code> and my &lt;code>CustomUserDetails&lt;/code>. It turns out, that &lt;code>InMemoryUserDetailsManager&lt;/code> method has this method:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#e2e4e5;background-color:#282a36;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-java" data-lang="java">&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff5c57">public&lt;/span> UserDetails &lt;span style="color:#57c7ff">loadUserByUsername&lt;/span>(String username) &lt;span style="color:#ff5c57">throws&lt;/span> UsernameNotFoundException {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> UserDetails user &lt;span style="color:#ff6ac1">=&lt;/span> (UserDetails)&lt;span style="color:#ff6ac1">this&lt;/span>.&lt;span style="color:#57c7ff">users&lt;/span>.&lt;span style="color:#57c7ff">get&lt;/span>(username.&lt;span style="color:#57c7ff">toLowerCase&lt;/span>());
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff6ac1">if&lt;/span> (user &lt;span style="color:#ff6ac1">==&lt;/span> &lt;span style="color:#ff6ac1">null&lt;/span>) {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff6ac1">throw&lt;/span> &lt;span style="color:#ff6ac1">new&lt;/span> UsernameNotFoundException(username);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> } &lt;span style="color:#ff6ac1">else&lt;/span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff6ac1">return&lt;/span> &lt;span style="color:#ff6ac1">new&lt;/span> User(user.&lt;span style="color:#57c7ff">getUsername&lt;/span>(), user.&lt;span style="color:#57c7ff">getPassword&lt;/span>(), user.&lt;span style="color:#57c7ff">isEnabled&lt;/span>(), user.&lt;span style="color:#57c7ff">isAccountNonExpired&lt;/span>(), user.&lt;span style="color:#57c7ff">isCredentialsNonExpired&lt;/span>(), user.&lt;span style="color:#57c7ff">isAccountNonLocked&lt;/span>(), user.&lt;span style="color:#57c7ff">getAuthorities&lt;/span>());
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>explictly casting found principals to Spring generic User class, despite my custom implementaion of UserDetails put into it. That&amp;rsquo;s sad.&lt;/p>
&lt;h2 id="final-solution" >
&lt;div>
&lt;a href="#final-solution">
#
&lt;/a>
Final solution
&lt;/div>
&lt;/h2>
&lt;p>Here&amp;rsquo;s test-scope class with custom &lt;code>UserDetailsManager&lt;/code> implementation altering &lt;code>InMemoryUserDetailsManager.loadUserByUsername&lt;/code> unwanted behavior. It could&amp;rsquo;ve been done better, this implementation assumes there is only one user object stored inside by the Manager. &lt;code>UserDTO&lt;/code> was turned into a Bean, to allow easy access to it via autowiring from the test class.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#e2e4e5;background-color:#282a36;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-java" data-lang="java">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#ff9f43">@TestConfiguration&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#ff5c57">public&lt;/span> &lt;span style="color:#ff5c57">class&lt;/span> &lt;span style="color:#f3f99d">SpringSecurityForUserControllerImplTestConfig&lt;/span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff9f43">@Bean&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff5c57">public&lt;/span> UserDTO &lt;span style="color:#57c7ff">testUser&lt;/span>() {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff6ac1">return&lt;/span> UserDTO.&lt;span style="color:#57c7ff">builder&lt;/span>()
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> .&lt;span style="color:#57c7ff">username&lt;/span>(&lt;span style="color:#5af78e">&amp;#34;username&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> .&lt;span style="color:#57c7ff">email&lt;/span>(&lt;span style="color:#5af78e">&amp;#34;username@example.com&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> .&lt;span style="color:#57c7ff">password&lt;/span>(&lt;span style="color:#5af78e">&amp;#34;password&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> .&lt;span style="color:#57c7ff">registerTime&lt;/span>(LocalDateTime.&lt;span style="color:#57c7ff">of&lt;/span>(2020, 1, 1, 12, 0, 0, 0))
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> .&lt;span style="color:#57c7ff">isActive&lt;/span>(&lt;span style="color:#ff6ac1">true&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> .&lt;span style="color:#57c7ff">role&lt;/span>(RoleDTO.&lt;span style="color:#57c7ff">builder&lt;/span>().&lt;span style="color:#57c7ff">roleName&lt;/span>(&lt;span style="color:#5af78e">&amp;#34;USER&amp;#34;&lt;/span>).&lt;span style="color:#57c7ff">build&lt;/span>())
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> .&lt;span style="color:#57c7ff">build&lt;/span>();
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff9f43">@Bean&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff9f43">@Primary&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff5c57">public&lt;/span> UserDetailsService &lt;span style="color:#57c7ff">userDetailsService&lt;/span>() {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> CustomUserDetails userDetails &lt;span style="color:#ff6ac1">=&lt;/span> &lt;span style="color:#ff6ac1">new&lt;/span> CustomUserDetails(testUser());
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff6ac1">return&lt;/span> &lt;span style="color:#ff6ac1">new&lt;/span> UserDetailsManager() {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff5c57">private&lt;/span> &lt;span style="color:#ff5c57">final&lt;/span> InMemoryUserDetailsManager inMemoryUserDetailsManager &lt;span style="color:#ff6ac1">=&lt;/span> &lt;span style="color:#ff6ac1">new&lt;/span> InMemoryUserDetailsManager(List.&lt;span style="color:#57c7ff">of&lt;/span>(userDetails));
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff9f43">@Override&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff5c57">public&lt;/span> &lt;span style="color:#9aedfe">void&lt;/span> &lt;span style="color:#57c7ff">createUser&lt;/span>(UserDetails userDetails) {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff6ac1">this&lt;/span>.&lt;span style="color:#57c7ff">inMemoryUserDetailsManager&lt;/span>.&lt;span style="color:#57c7ff">createUser&lt;/span>(userDetails);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff9f43">@Override&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff5c57">public&lt;/span> &lt;span style="color:#9aedfe">void&lt;/span> &lt;span style="color:#57c7ff">updateUser&lt;/span>(UserDetails userDetails) {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff6ac1">this&lt;/span>.&lt;span style="color:#57c7ff">inMemoryUserDetailsManager&lt;/span>.&lt;span style="color:#57c7ff">updateUser&lt;/span>(userDetails);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff9f43">@Override&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff5c57">public&lt;/span> &lt;span style="color:#9aedfe">void&lt;/span> &lt;span style="color:#57c7ff">deleteUser&lt;/span>(String s) {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff6ac1">this&lt;/span>.&lt;span style="color:#57c7ff">inMemoryUserDetailsManager&lt;/span>.&lt;span style="color:#57c7ff">deleteUser&lt;/span>(s);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff9f43">@Override&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff5c57">public&lt;/span> &lt;span style="color:#9aedfe">void&lt;/span> &lt;span style="color:#57c7ff">changePassword&lt;/span>(String s, String s1) {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff6ac1">this&lt;/span>.&lt;span style="color:#57c7ff">inMemoryUserDetailsManager&lt;/span>.&lt;span style="color:#57c7ff">changePassword&lt;/span>(s, s1);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff9f43">@Override&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff5c57">public&lt;/span> &lt;span style="color:#9aedfe">boolean&lt;/span> &lt;span style="color:#57c7ff">userExists&lt;/span>(String s) {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff6ac1">return&lt;/span> &lt;span style="color:#ff6ac1">this&lt;/span>.&lt;span style="color:#57c7ff">inMemoryUserDetailsManager&lt;/span>.&lt;span style="color:#57c7ff">userExists&lt;/span>(s);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff9f43">@Override&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff5c57">public&lt;/span> UserDetails &lt;span style="color:#57c7ff">loadUserByUsername&lt;/span>(String s) &lt;span style="color:#ff5c57">throws&lt;/span> UsernameNotFoundException {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff6ac1">return&lt;/span> &lt;span style="color:#ff6ac1">new&lt;/span> CustomUserDetails(testUser());
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> };
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Test class now looks as following:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#e2e4e5;background-color:#282a36;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-java" data-lang="java">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#ff9f43">@SpringBootTest&lt;/span>(
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> classes &lt;span style="color:#ff6ac1">=&lt;/span> SpringSecurityForUserControllerImplTestConfig.&lt;span style="color:#57c7ff">class&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#ff9f43">@AutoConfigureMockMvc&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#ff5c57">class&lt;/span> &lt;span style="color:#f3f99d">UserControllerImplTest&lt;/span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff9f43">@Autowired&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff5c57">private&lt;/span> MockMvc mockMvc;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff9f43">@MockBean&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff5c57">private&lt;/span> ImageDTOService imageDTOService;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff9f43">@Autowired&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> UserDTO testUser;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff9f43">@Test&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff9f43">@WithUserDetails&lt;/span>(&lt;span style="color:#5af78e">&amp;#34;username&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff5c57">public&lt;/span> &lt;span style="color:#9aedfe">void&lt;/span> &lt;span style="color:#57c7ff">whenGetUserImages_andUserIsAuthorized_thenReturnListOfImages&lt;/span>() &lt;span style="color:#ff5c57">throws&lt;/span> Exception {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> List&lt;span style="color:#ff6ac1">&amp;lt;&lt;/span>ImageDTO&lt;span style="color:#ff6ac1">&amp;gt;&lt;/span> images &lt;span style="color:#ff6ac1">=&lt;/span> getSampleImages();
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> images.&lt;span style="color:#57c7ff">forEach&lt;/span>(imageDTO &lt;span style="color:#ff6ac1">-&amp;gt;&lt;/span> imageDTO.&lt;span style="color:#57c7ff">setUploader&lt;/span>(testUser));
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> Mockito.&lt;span style="color:#57c7ff">when&lt;/span>(imageDTOService.&lt;span style="color:#57c7ff">findAllUploadedBy&lt;/span>(testUser)).&lt;span style="color:#57c7ff">thenReturn&lt;/span>(images);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> mockMvc.&lt;span style="color:#57c7ff">perform&lt;/span>(get(&lt;span style="color:#5af78e">&amp;#34;/user/images/&amp;#34;&lt;/span>))
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> .&lt;span style="color:#57c7ff">andExpect&lt;/span>(status().&lt;span style="color:#57c7ff">isOk&lt;/span>())
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> .&lt;span style="color:#57c7ff">andExpect&lt;/span>(content().&lt;span style="color:#57c7ff">contentType&lt;/span>(MediaType.&lt;span style="color:#57c7ff">APPLICATION_JSON&lt;/span>))
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> .&lt;span style="color:#57c7ff">andExpect&lt;/span>(content().&lt;span style="color:#57c7ff">json&lt;/span>(
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#5af78e">&amp;#34;[\n&amp;#34;&lt;/span> &lt;span style="color:#ff6ac1">+&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#5af78e">&amp;#34; {\n&amp;#34;&lt;/span> &lt;span style="color:#ff6ac1">+&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#5af78e">&amp;#34; \&amp;#34;id\&amp;#34;: 1,\n&amp;#34;&lt;/span> &lt;span style="color:#ff6ac1">+&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#5af78e">&amp;#34; \&amp;#34;isActive\&amp;#34;: true,\n&amp;#34;&lt;/span> &lt;span style="color:#ff6ac1">+&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#5af78e">&amp;#34; \&amp;#34;isPublic\&amp;#34;: true,\n&amp;#34;&lt;/span> &lt;span style="color:#ff6ac1">+&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#5af78e">&amp;#34; \&amp;#34;title\&amp;#34;: null,\n&amp;#34;&lt;/span> &lt;span style="color:#ff6ac1">+&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#5af78e">&amp;#34; \&amp;#34;token\&amp;#34;: \&amp;#34;token1\&amp;#34;,\n&amp;#34;&lt;/span> &lt;span style="color:#ff6ac1">+&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#5af78e">&amp;#34; \&amp;#34;uploadTime\&amp;#34;: \&amp;#34;2020-01-01T12:00:00\&amp;#34;,\n&amp;#34;&lt;/span> &lt;span style="color:#ff6ac1">+&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#5af78e">&amp;#34; \&amp;#34;uploader\&amp;#34;: \&amp;#34;username\&amp;#34;\n&amp;#34;&lt;/span> &lt;span style="color:#ff6ac1">+&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#5af78e">&amp;#34; },\n&amp;#34;&lt;/span> &lt;span style="color:#ff6ac1">+&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#5af78e">&amp;#34; {\n&amp;#34;&lt;/span> &lt;span style="color:#ff6ac1">+&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#5af78e">&amp;#34; \&amp;#34;id\&amp;#34;: 2,\n&amp;#34;&lt;/span> &lt;span style="color:#ff6ac1">+&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#5af78e">&amp;#34; \&amp;#34;isActive\&amp;#34;: true,\n&amp;#34;&lt;/span> &lt;span style="color:#ff6ac1">+&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#5af78e">&amp;#34; \&amp;#34;isPublic\&amp;#34;: true,\n&amp;#34;&lt;/span> &lt;span style="color:#ff6ac1">+&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#5af78e">&amp;#34; \&amp;#34;title\&amp;#34;: null,\n&amp;#34;&lt;/span> &lt;span style="color:#ff6ac1">+&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#5af78e">&amp;#34; \&amp;#34;token\&amp;#34;: \&amp;#34;token2\&amp;#34;,\n&amp;#34;&lt;/span> &lt;span style="color:#ff6ac1">+&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#5af78e">&amp;#34; \&amp;#34;uploadTime\&amp;#34;: \&amp;#34;2020-01-01T12:00:00\&amp;#34;,\n&amp;#34;&lt;/span> &lt;span style="color:#ff6ac1">+&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#5af78e">&amp;#34; \&amp;#34;uploader\&amp;#34;: \&amp;#34;username\&amp;#34;\n&amp;#34;&lt;/span> &lt;span style="color:#ff6ac1">+&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#5af78e">&amp;#34; },\n&amp;#34;&lt;/span> &lt;span style="color:#ff6ac1">+&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#5af78e">&amp;#34; {\n&amp;#34;&lt;/span> &lt;span style="color:#ff6ac1">+&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#5af78e">&amp;#34; \&amp;#34;id\&amp;#34;: 3,&amp;#34;&lt;/span> &lt;span style="color:#ff6ac1">+&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#5af78e">&amp;#34; \n&amp;#34;&lt;/span> &lt;span style="color:#ff6ac1">+&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#5af78e">&amp;#34; \&amp;#34;isActive\&amp;#34;:true,\n&amp;#34;&lt;/span> &lt;span style="color:#ff6ac1">+&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#5af78e">&amp;#34; \&amp;#34;isPublic\&amp;#34;: true,\n&amp;#34;&lt;/span> &lt;span style="color:#ff6ac1">+&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#5af78e">&amp;#34; \&amp;#34;title\&amp;#34;: null,\n&amp;#34;&lt;/span> &lt;span style="color:#ff6ac1">+&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#5af78e">&amp;#34; \&amp;#34;token\&amp;#34;: \&amp;#34;token3\&amp;#34;,\n&amp;#34;&lt;/span> &lt;span style="color:#ff6ac1">+&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#5af78e">&amp;#34; \&amp;#34;uploadTime\&amp;#34;: \&amp;#34;2020-01-01T12:00:00\&amp;#34;,\n&amp;#34;&lt;/span> &lt;span style="color:#ff6ac1">+&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#5af78e">&amp;#34; \&amp;#34;uploader\&amp;#34;: \&amp;#34;username\&amp;#34;&amp;#34;&lt;/span> &lt;span style="color:#ff6ac1">+&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#5af78e">&amp;#34; }\n&amp;#34;&lt;/span> &lt;span style="color:#ff6ac1">+&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#5af78e">&amp;#34;]&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> ));
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>And it passes the test at last! 🎉🥰&lt;/p>
&lt;h2 id="links" >
&lt;div>
&lt;a href="#links">
#
&lt;/a>
Links
&lt;/div>
&lt;/h2>
&lt;pre tabindex="0">&lt;code>[0]: https://golb.hplar.ch/2019/05/stateless.html
[1]: https://stackoverflow.com/questions/46615504/springboottest-mock-authentication-principal-with-a-custom-user-does-not-work
[2]: https://docs.spring.io/spring-security/site/docs/current/api/org/springframework/security/test/context/support/WithMockUser.html
[3]: https://docs.spring.io/spring-security/site/docs/current/api/org/springframework/security/core/annotation/AuthenticationPrincipal.html
[4]: https://stackoverflow.com/a/46631015/6158307
[5]: https://docs.spring.io/spring-security/site/docs/current/api/org/springframework/security/authentication/ProviderNotFoundException.html
[6]: https://stackoverflow.com/a/43920932/6158307
[7]: https://docs.spring.io/spring-security/site/docs/current/api/org/springframework/security/test/context/support/WithUserDetails.html
&lt;/code>&lt;/pre></description></item></channel></rss>