<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>MuSigma</title>
    <description>A blog about programming mostly.
</description>
    <link>https://musigma.blog/</link>
    <atom:link href="https://musigma.blog/feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Sat, 16 Dec 2023 16:49:33 -0600</pubDate>
    <lastBuildDate>Sat, 16 Dec 2023 16:49:33 -0600</lastBuildDate>
    <generator>Jekyll v4.3.2</generator>
    
      <item>
        <title>Retrospective on CVE-2021-44228 (Log4Shell)</title>
        <description>&lt;p&gt;&lt;a href=&quot;https://logging.apache.org/log4j/2.x/&quot;&gt;Apache Log4j 2&lt;/a&gt; is an open source Java logging library that I’ve been contributing to and maintaining since 2014 as a member of the Apache Logging Services Project Management Committee (aka &lt;a href=&quot;https://www.apache.org/dev/pmc.html&quot;&gt;PMC&lt;/a&gt;).
Up until late 2021, Log4j 2 was a fairly low profile project in contrast with its widespread usage across the industry.
On 2021-11-24, the Logging PMC received a vulnerability report that would put Log4j 2 on the global stage just a couple of weeks later.
That email was just one link in the chain of events leading to Log4Shell, one of the most impactful security vulnerabilities in over a decade.
In this post, we’ll reflect on the history leading up to &lt;a href=&quot;https://logging.apache.org/log4j/2.x/security.html#CVE-2021-44228&quot;&gt;CVE-2021-44228&lt;/a&gt; and &lt;a href=&quot;https://logging.apache.org/log4j/2.x/security.html#CVE-2021-45046&quot;&gt;CVE-2021-45046&lt;/a&gt;, how the &lt;a href=&quot;https://www.apache.org/theapacheway/&quot;&gt;Apache Way&lt;/a&gt; of open source software development prevailed in rapidly responding to these vulnerabilities, and what else we’re doing in Log4j 2 to help prevent any similar issue from happening in the future.
Most of the information here was documented over a couple of weeks after the incident, and as we come close to the second anniversary of Log4Shell, I wanted to publish this publicly with some updates.&lt;/p&gt;

&lt;h2 id=&quot;background&quot;&gt;Background&lt;/h2&gt;

&lt;p&gt;The Log4Shell vulnerability works through a combination of features added over time that ultimately exploits design flaws in the &lt;a href=&quot;https://docs.oracle.com/javase/jndi/tutorial/getStarted/overview/index.html&quot;&gt;Java Naming and Directory Interface&lt;/a&gt; (JNDI) API—a standard component of Java—to allow an attacker to execute remote code fairly easily.
Dating back to the first release of Log4j 2 version 2.0-alpha1 while the project was still an experiment, a &lt;a href=&quot;https://s.apache.org/log4shell-origins-1&quot;&gt;commit was added&lt;/a&gt; that changed how substituting property placeholders in configuration files work to support a similar configuration feature from Log4j version 1.2.x.
Unfortunately, the way this was implemented applied property substitution to both configuration files and log messages when it was only intended to apply to configuration data.
As this functionality was introduced during the experimental period of development, there aren’t any Jira issues to refer to for further historical context.
While this functionality was not initially dangerous as the types of property lookup plugins available then were fairly benign, this was certainly unexpected behavior which wasn’t discovered until long after version 2.0 was released.
The Log4j 2 team has always taken backwards compatibility seriously, and the scope of this compatibility included both the logging API and the default logging configuration behavior.
There are other configuration options we’d love to make the default behavior—particularly around performance options—but broad changes like that will have to wait for version 3.0.&lt;/p&gt;

&lt;p&gt;After several more pre-2.0 releases, a &lt;a href=&quot;https://s.apache.org/log4shell-origins-2&quot;&gt;new feature was contributed&lt;/a&gt; to support property substitution for strings from the JNDI API.
The provided use case was to support a configuration where log messages would be appended to different files depending on which web application context the logs were coming from.
Specifying a common logging config for an entire Java EE cluster was a great way to reduce toil involved in updating logging configurations across a broad deployment.
Being July 2013, the exploitability of the JNDI API was still unknown, so this feature was accepted and released as part of version 2.0-beta9.
Combined with the log message property substitution behavior, the foundation was laid for making the default logging configuration susceptible to Log4Shell.&lt;/p&gt;

&lt;p&gt;Later that year, I discovered the Log4j 2 project while researching what logging library to use in Java applications and began contributing to the project.
In early 2014, I was invited to become a committer, and in later 2014, I was invited to join the PMC.
It was that year when we finally released version 2.0 for general availability and set the stable compatibility point for the API and default configuration.
Near the end of 2014, an &lt;a href=&quot;https://s.apache.org/log4shell-origins-3&quot;&gt;issue was filed&lt;/a&gt; that discovered the log message property substitution behavior while debugging strange errors encountered from Apache Camel.
The details of this issue weren’t made apparent until 2016 when the root cause was discovered and partially fixed in version 2.7.&lt;/p&gt;

&lt;p&gt;One year later, another &lt;a href=&quot;https://s.apache.org/log4shell-origins-4&quot;&gt;issue was filed&lt;/a&gt; and implemented to add a global option to disable message property substitution.
This was released in version 2.10.0, and this flag was one of the early mitigations available for Log4Shell five years later.
The flag was initially justified for performance reasons as a quadratic lookup function was being executed in a hot loop which didn’t seem to be necessary, though details of the exploitability of JNDI were already publicly known by the security community ever since a &lt;a href=&quot;https://www.blackhat.com/docs/us-16/materials/us-16-Munoz-A-Journey-From-JNDI-LDAP-Manipulation-To-RCE.pdf&quot;&gt;Blackhat 2016 talk&lt;/a&gt; published details around an earlier zero-day vulnerability exploited during &lt;a href=&quot;https://www.trendmicro.com/vinfo/us/security/news/cyber-attacks/operation-pawn-storm-fast-facts&quot;&gt;Operation Pawn Storm&lt;/a&gt;.
This zero-day was a vulnerability that was being exploited by attackers in the wild before Oracle were ever informed about the issue.&lt;/p&gt;

&lt;h2 id=&quot;the-first-vulnerability-report&quot;&gt;The First Vulnerability Report&lt;/h2&gt;

&lt;p&gt;On 2021-11-24, one day before the American holiday Thanksgiving Day, the Logging PMC received an email with the first vulnerability report.
Included were exploitation details and an attached proof of concept project to demonstrate the exploit.
Over the next 24 hours, the PMC discussed the implications of the vulnerability, potential solutions to patch the issue, and further details on how the exploit worked.
On 2021-11-25, the PMC acknowledged the vulnerability with the reporter and continued internal discussions on how to address the issue over the next few days.
During that time, the original reporter followed up with confirmations that Apache Flink, Apache Druid, and Apache Struts 2 were all impacted by this vulnerability.
By the 29th of November, the PMC had settled on a root cause of the problem and solution, so a &lt;a href=&quot;https://s.apache.org/log4shell-origins-5&quot;&gt;public issue was filed&lt;/a&gt; along with a pull request which made no reference to a security vulnerability, though astute observers may have deduced the seriousness of this issue before the CVE was later published.
During the following week, we began preparing the codebase for a 2.15.0 release.&lt;/p&gt;

&lt;p&gt;On 2021-12-05, the proposed fix for the vulnerability was merged, and version 2.15.0-rc1 was cut the following day.
While the release candidate was being reviewed by the PMC during a standard 72-hour voting period, the original reporter emailed us on the 8th to warn that they discovered public discussions among security teams on social media about a Log4j RCE vulnerability, though they weren’t sure if they knew the full extent of the problem.
Given the lack of evidence for anyone having exploited the issue before they had first reported the issue back in November, this would disqualify the use of “zero-day” vulnerability, though it did lead to zero-day vulnerabilities in tons of existing software.
On the 9th, the reporter emailed us again to show that the proposed fix was insufficient, so the release candidate vote was cancelled.
Later that day, a new release candidate was cut with a more comprehensive fix for the issue along with an expedited release vote process.
During this release vote, we received more vulnerability reports related to the failed rc1 vote, though these were all the same problem identified by the original reporter earlier that day.
The vote passed, and Log4j 2.15.0 was officially released with many items in the changelog, most notably a fix for a CVE rated at 10/10 in severity.
The following day, I woke up to a bursting inbox and a handful of text messages from various journalists looking for details on the breaking story that eventually gained the moniker of Log4Shell.&lt;/p&gt;

&lt;h2 id=&quot;the-second-vulnerability-report&quot;&gt;The Second Vulnerability Report&lt;/h2&gt;

&lt;p&gt;Over the next few days, we received two independent vulnerability reports that the fixes provided for CVE-2021-44228 were insufficient to protect from exploitation in certain non-default configurations.
During this time, the Logging PMC gathered in a long-running private video chat while the impact of Log4Shell began to spread throughout the world.
It was discovered that the property substitution was still being applied to a couple other Pattern Layout components which could contain attacker-controlled input.
Methods to bypass the newly introduced filtering for the JNDI lookup were demonstrated, and other places where JNDI could potentially be abused were identified and hardened.
We quickly addressed these issues and created versions 2.16.0 (for Java 8+), 2.12.2 (for Java 7), and 2.3.1 (for Java 6) to publish the fix along with a backport of the two CVEs respectively.
CVE-2021-45046 was initially published with a lower severity than it currently has at 9.0, and additional details were later reported to us independently by four separate sources that demonstrated how CVE-2021-45046 was a remote code execution vulnerability.
Various other reporters informed us on more variants of this later on, but they were all either caused by the same issue or were less severe than the RCE aspect.&lt;/p&gt;

&lt;p&gt;Of course, this wasn’t the final security vulnerability we fixed in December 2021, but these two issues were the only ones that led to serious problems like remote code execution.
Since then, an issue related to denial of service and another related to unexpected behavior when using JNDI for configuring other components were fixed in CVE-2021-45105 and CVE-2021-44832 respectively.
For more information about these CVEs, see &lt;a href=&quot;https://logging.apache.org/log4j/2.x/security.html&quot;&gt;the Log4j security page&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;future-of-log4j&quot;&gt;Future of Log4j&lt;/h2&gt;

&lt;p&gt;These security vulnerabilities raised a lot of questions and laid bare the consequences of the industry not keeping inventory of the software they use let alone the more detailed inventory of subcomponents that make up each piece of software.
The &lt;a href=&quot;https://www.apache.org/security/committers.html&quot;&gt;security process established at the Apache Software Foundation&lt;/a&gt; worked great at delivering security updates for the public as quickly as possible, though it turned out that many of our users were unaware they were even users of Log4j 2.
When I first wrote this for a limited audience, 35% of recent downloads of Log4j 2 from Maven central were for vulnerable releases.
Nearly two years later, this has reduced to &lt;a href=&quot;https://www.sonatype.com/resources/log4j-vulnerability-resource-center&quot;&gt;23% of recent downloads&lt;/a&gt; which is an improvement.
In the Log4j project, we have a couple ideas we’re working on that will help prevent similar problems in the future.&lt;/p&gt;

&lt;p&gt;A primary cause of Log4Shell was a packaging philosophy shared by Java itself until Java 9 finally broke itself up into well-defined modules.
To simplify upstream packaging and downstream use, many optional features were included in the standard distribution.
During the early 2010s when Log4j 2 was initially developed, build tools like Apache Maven and Gradle were not as dominant while tools like Apache Ant or even Make were used with manually checked-in copies of third party library jar files.
The previous major version of Log4j was packaged as a single jar; while convenient for the even more primitive build tooling of the late ’90s and early 2000s, this also had the design flaw of mixing the external and internal APIs together.
Version 2.0 was initially packaged into two main jars: the frontend &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;log4j-api&lt;/code&gt; and the backend &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;log4j-core&lt;/code&gt;.
The API has strong compatibility support from version to version, and the core backend contains all the implementations available as plugins for use in a logging configuration.
Many optional plugins are included here which can only be used when their required dependencies are present on the classpath.
This packaging format was chosen to maximize simplicity of deployment, though even this split-jar setup has caused its own large amounts of confusion to Java developers of all experience levels; some users are first learning what jars are while other users don’t even know the difference between &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;log4j-api&lt;/code&gt; (the frontend) and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;log4j-core&lt;/code&gt; (the backend).&lt;/p&gt;

&lt;p&gt;For version 3.0, &lt;a href=&quot;https://issues.apache.org/jira/browse/LOG4J2-2226&quot;&gt;we are planning&lt;/a&gt; to break up &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;log4j-core&lt;/code&gt; into several additional modules while leaving a slim, secure kernel.
We’ve published one alpha release of 3.0 so far, and we want to make a beta release before 3.0.0.
While the updated &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;log4j-core&lt;/code&gt; will still contain many common plugins, this reduces the required module dependencies to a base requirement of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;java.base&lt;/code&gt;.
Users will have to opt in to fancier features that rely on dependencies outside the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;java.base&lt;/code&gt; module provided by Java 11 as those plugins will be packaged in their own modules.
We are also adding basic feature flags to select plugins so that they can require explicit opt-in even when the module is present at runtime to further protect from abuse of potentially insecure plugins.&lt;/p&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;In the time since these issues were fixed, the project has received various levels of sponsorship and donations.
This has helped a lot as we’ve been able to dedicate more time to working on improvements to our release process, security metadata, code and test quality, and several other aspects of the project.
As world governments debate how to regulate software engineering, we continue to lead by example both in the Logging Services PMC and the Apache Software Foundation more broadly.&lt;/p&gt;

&lt;p&gt;Log4j 2 continues to improve over time with a healthy community and a fresh wave of widespread interest in reviewing the project for any sign of potential security issues.
As always, we encourage others to contribute to the project and other projects at Apache; community is the power behind the Apache Way.
It’s important that everyone using the project make sure they’re using the latest version.
Users still on Log4j 1 should upgrade to Log4j 2 or use our &lt;a href=&quot;https://logging.apache.org/log4j/2.x/manual/migration.html&quot;&gt;Log4j 1 compatibility features&lt;/a&gt; which are supported by the PMC.&lt;/p&gt;

</description>
        <pubDate>Fri, 10 Nov 2023 16:00:00 -0600</pubDate>
        <link>https://musigma.blog/2023/11/10/log4shell-history.html</link>
        <guid isPermaLink="true">https://musigma.blog/2023/11/10/log4shell-history.html</guid>
        
        
        <category>log4j</category>
        
        <category>java</category>
        
      </item>
    
      <item>
        <title>An abridged guide to using ed25519 PGP keys with GnuPG and SSH</title>
        <description>&lt;p&gt;I came across a &lt;a href=&quot;https://github.com/drduh/YubiKey-Guide&quot;&gt;great guide to using a YubiKey with SSH and GPG&lt;/a&gt; a couple years ago which helped push me over the fence and acquire my own YubiKey.
Following that setup guide, I set up my keys offline using a Tails Linux boot USB with a &lt;a href=&quot;https://onerng.info&quot;&gt;OneRNG hardware random number generator&lt;/a&gt;.
While a fun exercise, I must note that it’s not for the faint of heart, especially if done on a recent MacBook Pro (with a touchbar) or incompatible hardware.
However, it did help explain some of the features available in GnuPG, and this came in handy recently while exploring the &lt;a href=&quot;https://www.yubico.com/blog/whats-new-in-yubikey-firmware-5-2-3/&quot;&gt;new support for elliptic curve cryptography in YubiKey firmware 5.2.3&lt;/a&gt;, the version installed in my later YubiKey 5Ci purchase.
While I originally created PGP keys using the same guide last year with RSA keys, since those keys were expiring soon, it seemed like a good idea to look in to switching to &lt;a href=&quot;https://cr.yp.to/ecdh.html&quot;&gt;Curve25519&lt;/a&gt; keys.
GnuPG has added some improved support for this algorithm along with supporting this updated YubiKey firmware to transfer these keys to a YubiKey.
In this brief guide, I’ll go over how to generate an appropriate PGP key that can be used both in a YubiKey and for use with SSH.
For more general info about using smartcards with GnuPG, see &lt;a href=&quot;https://gnupg.org/howtos/card-howto/en/smartcard-howto.html&quot;&gt;this guide from GnuPG&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;about-pgp-keys&quot;&gt;About PGP Keys&lt;/h2&gt;

&lt;p&gt;PGP keys are slightly more complicated than a single keypair, and they’re fairly flexible.
A key has one or more user ids (name, email address, and an optional comment), one of which is flagged as the primary uid (by default, this is the original uid used on creation).
Keys have an optional expiration date along with a list of usages allowed for that specific key.
Keys have one or more subkeys which allow for different keys for different use cases all on the same key.
These use cases include certification (to create and certify subkeys typically), signatures, encryption, and authentication.
Each key or subkey can allow one or more of those capabilities, though certification is typically reserved for the primary key.
When using RSA, this is fairly simple as RSA supports all four of those capabilities.
However, elliptic curves typically use dual algorithms for signing and encryption, so this will be slightly more nuanced.&lt;/p&gt;

&lt;h2 id=&quot;generating-pgp-keys&quot;&gt;Generating PGP Keys&lt;/h2&gt;

&lt;p&gt;Our master key will be created for certification with no expiration date.
This is justified by the fact that the certification key will be physically tied to a YubiKey for which we can use a revocation certificate if the key is lost or stolen.
The subkeys will have expiration dates to allow for key rotation as explained in the linked guide, though that is out of scope for this post.
We’ll split up the remaining usage capabilities into three subkeys: a signing subkey, an encryption subkey, and an authentication subkey.
Note that certification and authentication keys use signature algorithms internally, thus for our key, we’ll use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ed25519&lt;/code&gt; for all but our encryption subkey which will instead use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cv25519&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The following commands will help us avoid using the UI for most of the work.
Use &lt;a href=&quot;https://raw.githubusercontent.com/drduh/config/master/gpg.conf&quot;&gt;this gpg.conf file&lt;/a&gt; in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.gnupg/gpg.conf&lt;/code&gt; for a more secure default.
Then, create a certification master key (note the optional comment can be omitted entirely, no parenthesis necessary) and specify a password for the key when prompted:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gpg --quick-generate-key \
    &apos;Your Name &amp;lt;your.email@example.com&amp;gt; (optional comment)&apos; \
    ed25519 cert never
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Next, save the key fingerprint without spaces to an environment variable:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;export KEYFP=123456789ABCDEF0...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now add subkeys for signing, encryption, and authentication.
These will have expiration times to allow for key rotation.
For example, using a one year expiration time:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gpg --quick-add-key $KEYFP ed25519 sign 1y
gpg --quick-add-key $KEYFP cv25519 encr 1y
gpg --quick-add-key $KEYFP ed25519 auth 1y
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Next, verify the keys have been added to your local keyring:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gpg -K
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This should give output like:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sec   ed25519/0x0123456789ABCDEF 2021-01-01 [C]
      Key fingerprint = 0000 1111 2222 3333 4444  5555 0123 4567 89AB CDEF
uid                   [ultimate] Matt Sicker &amp;lt;mattsicker@apache.org&amp;gt;
ssb   ed25519/0x9876543212345678 2021-01-01 [S] [expires: 2022-01-01]
ssb   cv25519/0x3141592653589793 2021-01-01 [E] [expires: 2022-01-01]
ssb   ed25519/0x8888ABCDFDEC8765 2021-01-01 [A] [expires: 2022-01-01]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;using-ssh&quot;&gt;Using SSH&lt;/h2&gt;

&lt;p&gt;The authentication subkey can be used for authentication in SSH.
There are a few different ways to export the PGP key into an OpenSSH format, and we’ll cover a simple one here.
This method will use GnuPG as an SSH agent which allows us to both use PGP keys for SSH as well as to import and encrypt existing SSH key files into a GPG-managed SSH keyring.
First, obtain a copy of &lt;a href=&quot;https://raw.githubusercontent.com/drduh/config/master/gpg-agent.conf&quot;&gt;this gpg-agent.conf file&lt;/a&gt; and save it to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.gnupg/gpg-agent.conf&lt;/code&gt;.
Uncomment the appropriate &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pinentry&lt;/code&gt; program for your platform (and don’t forget to install it if you haven’t already).
Next, add the following lines to your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.bashrc&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.zshrc&lt;/code&gt;, or whatever shell rc file you use:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;export GPG_TTY=&quot;$(tty)&quot;
export SSH_AUTH_SOCK=$(gpgconf --list-dirs agent-ssh-socket)
gpgconf --launch gpg-agent
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Note that this configuration is only relevant for your local machine.
If you’re using SSH agent forwarding, don’t copy this configuration to remote machines.
After restarting your shell, run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ssh-add -L&lt;/code&gt; to list the currently known SSH keys in GnuPG.
Your locally stored keys should be listed here as well as your YubiKey if you’ve transferred the key there and have it plugged in.
These lines correspond to SSH public keys which can be used in your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.ssh/authorized_keys&lt;/code&gt; file on relevant remote machines to SSH into.
Alternatively, you can export a GPG’s authentication key into an SSH format directly using the following command:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gpg --export-ssh-key 0x1234ABCD1234ABCD
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;For use with GitHub and other git+ssh providers, add this public key to your account’s SSH keys.&lt;/p&gt;

</description>
        <pubDate>Sun, 09 May 2021 11:00:00 -0500</pubDate>
        <link>https://musigma.blog/2021/05/09/gpg-ssh-ed25519.html</link>
        <guid isPermaLink="true">https://musigma.blog/2021/05/09/gpg-ssh-ed25519.html</guid>
        
        
        <category>security</category>
        
      </item>
    
      <item>
        <title>Exploring the ChaCha stream cipher</title>
        <description>&lt;p&gt;Stream ciphers form the basis for simpler encryption and decryption algorithms than traditional block ciphers like AES.
In particular, the &lt;a href=&quot;https://cr.yp.to/chacha.html&quot;&gt;ChaCha family of stream ciphers&lt;/a&gt; form the basis of the encryption functionality in &lt;a href=&quot;https://github.com/o1c-dev/o1c&quot;&gt;O(1) Cryptography&lt;/a&gt; which we’ll explore in more detail.
Originally published as the &lt;a href=&quot;https://cr.yp.to/snuffle/salsafamily-20071225.pdf&quot;&gt;Salsa family (PDF)&lt;/a&gt; of ciphers, &lt;a href=&quot;https://cr.yp.to/chacha/chacha-20080128.pdf&quot;&gt;ChaCha (PDF)&lt;/a&gt; makes some small modifications to Salsa for increased security while maintaining equivalent performance.
ChaCha has since been widely standardized in various networking standards and programming language standard cryptography libraries as an alternative to AES and other ciphers.
Recall that a stream cipher is an algorithm that takes a secret key and an input stream which returns an output stream of the same size of the input stream.
Stream ciphers work by taking the secret key (and usually some sort of nonce or initial value which cannot be reused for the same key) and generating a stream of deterministic random bits called the &lt;em&gt;keystream&lt;/em&gt; which is used for multiple purposes.
The primary use of this keystream is to xor it with an input stream of plaintext or ciphertext to produce an output ciphertext or plaintext respectively.
Given this mode of operation, compared to block ciphers which require complicated key scheduling algorithms, it can be hard to imagine why block ciphers have been so popular historically speaking.
Surely a stream cipher isn’t &lt;em&gt;that&lt;/em&gt; simple, is it?&lt;/p&gt;

&lt;!-- more --&gt;

&lt;h2 id=&quot;overview&quot;&gt;Overview&lt;/h2&gt;

&lt;p&gt;ChaCha and Salsa are stream ciphers that expand a 256-bit key into 2&lt;sup&gt;64&lt;/sup&gt; randomly accessible streams of 2&lt;sup&gt;64&lt;/sup&gt; randomly accessible 64-byte (512-bit) blocks.
They are parameterized by a &lt;em&gt;round number&lt;/em&gt; suffix, recommended at &lt;em&gt;20&lt;/em&gt; (as in ChaCha20 or Salsa20), but also available in &lt;em&gt;8&lt;/em&gt; and &lt;em&gt;12&lt;/em&gt; round variants with reduced security margin.
This round number controls the number of times the &lt;em&gt;round function&lt;/em&gt; is applied to the cipher’s internal state and must be an even number.
Each round applies a sequence of constant-time operations on an array of 16 32-bit words consisting of four addition, xor, and constant-distance left shift and rotate operations each.
The choice for these operations relies on the mechanical sympathy of how CPUs physically implement addition, xor, and shift/rotate instructions, all of which are both fast and operate in constant time regardless of input.
This set of operations is also &lt;a href=&quot;https://en.wikipedia.org/wiki/Functional_completeness&quot;&gt;functionally complete&lt;/a&gt;, so despite seeming simple, they can simulate any other Boolean expression or logic gate which makes them sufficiently powerful.&lt;/p&gt;

&lt;h2 id=&quot;internal-state&quot;&gt;Internal State&lt;/h2&gt;

&lt;p&gt;The internal state of a ChaCha cipher consists of a 512-bit block of data addressed as 16 little endian 32-bit unsigned integers.
Keeping the entirety of the cipher state inside this buffer along with the operations performed on it allows CPUs to keep the state in its cache which helps maximize software performance while simultaneously preventing timing attacks based on memory access patterns common to optimized software implementations of AES.
This state is initialized with a secret key, a constant value, and some input data formed from a nonce and initial counter integer.
The constant value is what is known as a &lt;em&gt;nothing up my sleeve number&lt;/em&gt; and is needed as part of the standard initialization of the cipher state, and in ChaCha, this constant is the 16 byte ASCII-encoded string “&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;expand 32-byte k&lt;/code&gt;” which fills the first four little endian integer values of this state.
A “nothing up my sleeve number” is an arbitrarily chosen initialization constant value that is used in a cipher where one is needed such that it’s clear that the choice of constant was arbitrary and is therefore unlikely to have intentional backdoors, a problem faced by the secrecy behind parameter choices in DES recommended by the NSA in the 1990s.
Such numbers are usually encodings of mathematical constants or ASCII strings.
The next eight integers consist of the 32-byte secret key interpreted as an array of eight little endian 32-bit integers &lt;em&gt;key&lt;/em&gt;&lt;sub&gt;0&lt;/sub&gt;, …, &lt;em&gt;key&lt;/em&gt;&lt;sub&gt;7&lt;/sub&gt;.
Finally, the last four integers consist of the initial counter and nonce encoded similarly.
This last aspect has three main variants on how to encode the input data: a 64-bit initial counter with a 64-bit nonce; a 32-bit initial counter with a 96-bit nonce; or a 128-bit nonce.
These correspond to the original ChaCha cipher, the &lt;a href=&quot;https://tools.ietf.org/html/rfc8439&quot;&gt;IETF standardized variant&lt;/a&gt;, and HChaCha respectively, the latter being used in the XChaCha variant of ChaCha which uses an extended nonce.
Laying this out in a matrix, the internal state looks like this:&lt;/p&gt;

&lt;table&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;em&gt;0x61707865&lt;/em&gt;&lt;/td&gt;
      &lt;td&gt;&lt;em&gt;0x3320646E&lt;/em&gt;&lt;/td&gt;
      &lt;td&gt;&lt;em&gt;0x79622D32&lt;/em&gt;&lt;/td&gt;
      &lt;td&gt;&lt;em&gt;0x6B206574&lt;/em&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;em&gt;key&lt;/em&gt;&lt;sub&gt;0&lt;/sub&gt;&lt;/td&gt;
      &lt;td&gt;&lt;em&gt;key&lt;/em&gt;&lt;sub&gt;1&lt;/sub&gt;&lt;/td&gt;
      &lt;td&gt;&lt;em&gt;key&lt;/em&gt;&lt;sub&gt;2&lt;/sub&gt;&lt;/td&gt;
      &lt;td&gt;&lt;em&gt;key&lt;/em&gt;&lt;sub&gt;3&lt;/sub&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;em&gt;key&lt;/em&gt;&lt;sub&gt;4&lt;/sub&gt;&lt;/td&gt;
      &lt;td&gt;&lt;em&gt;key&lt;/em&gt;&lt;sub&gt;5&lt;/sub&gt;&lt;/td&gt;
      &lt;td&gt;&lt;em&gt;key&lt;/em&gt;&lt;sub&gt;6&lt;/sub&gt;&lt;/td&gt;
      &lt;td&gt;&lt;em&gt;key&lt;/em&gt;&lt;sub&gt;7&lt;/sub&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;em&gt;input&lt;/em&gt;&lt;sub&gt;0&lt;/sub&gt;&lt;/td&gt;
      &lt;td&gt;&lt;em&gt;input&lt;/em&gt;&lt;sub&gt;1&lt;/sub&gt;&lt;/td&gt;
      &lt;td&gt;&lt;em&gt;input&lt;/em&gt;&lt;sub&gt;2&lt;/sub&gt;&lt;/td&gt;
      &lt;td&gt;&lt;em&gt;input&lt;/em&gt;&lt;sub&gt;3&lt;/sub&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;These bottom &lt;em&gt;input&lt;/em&gt; values correspond to attacker-controlled input which are positioned to reduce the flexibility attackers have in cryptanalysis of this cipher compared to Salsa which organizes the initial state in a different configuration with attacker-controlled input values in cells 6 through 9.
This state is updated by a sequence of invertible operations defined by applying a round function the round number of times.&lt;/p&gt;

&lt;h2 id=&quot;round-function&quot;&gt;Round Function&lt;/h2&gt;

&lt;p&gt;ChaCha defines its round function using a smaller sub-operation known as the &lt;em&gt;quarter-round&lt;/em&gt; which is applied four times per round using a specified permutation.
This quarter-round is responsible for the entirety of the underlying bit-shuffling taking place to produce a keystream and is defined using the following algorithm.&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;quarterRound&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;];&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;rotateLeft&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;^&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;];&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;rotateLeft&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;^&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;];&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;rotateLeft&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;^&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;];&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;rotateLeft&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;^&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Finally, a round consists of a column round or a diagonal round in alternating sequence.
A column round consists of a quarter-round applied to each of the four columns.
A diagonal round consists of a quarter-round applied to four diagonal permutations of the state.&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;columnRound&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;quarterRound&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;quarterRound&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;13&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;quarterRound&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;14&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;quarterRound&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;15&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;diagonalRound&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;quarterRound&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;15&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;quarterRound&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;quarterRound&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;13&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;quarterRound&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;14&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;ChaCha20 consists of 20 rounds, and this block shows the application of two subsequent rounds.
Therefore, a complete application of ChaCha20 will apply the above two rounds 10 times to produce a 64-byte block in the keystream.&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;permute&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;columnRound&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;diagonalRound&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;At the end of each key block, we interpret cells 12 and 13 as a little endian 64-bit counter which is incremented in place before generating the next 64-byte keystream block.&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;incrementCounter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(++&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;13&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;];&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Combined with functions to decode and encode bytes to and from arrays of integers, data streams and keystreams can be fairly easily combined.
These ChaCha functions are also used for deriving subkeys in the case of ChaCha20-Poly1305 authenticated encryption and for deriving subkeys and sub-nonce data for implementing XChaCha20-Poly1305.
Using the keystream output from ChaCha can be used to &lt;a href=&quot;/2021/01/30/drbg.html&quot;&gt;implement a deterministic random bit generator&lt;/a&gt;.
When combined with a message authentication function like Poly1305, ChaCha can form the basis for an authenticated encryption with authenticated data algorithm as standardized in &lt;a href=&quot;https://tools.ietf.org/html/rfc8439&quot;&gt;RFC 8439&lt;/a&gt;.
The ultimate advantage to using a cipher like ChaCha that permits efficient secure software implementations is that it can be widely used by less powerful devices or devices lacking intrinsic AES-related instructions for efficient secure hardware implementations.
Many existing software AES implementations are vulnerable to a considerable number of attacks depending on the threat model, and proper software implementations without appropriate operating system or hardware support may suffer performance problems leading to deployment of insecure code in practice.
Several of these attacks are detailed in &lt;a href=&quot;https://cr.yp.to/antiforgery/cachetiming-20050414.pdf&quot;&gt;Cache-timing attacks on AES (PDF)&lt;/a&gt; along with advice on how to mitigate this in software, though one of the key conclusions you might come to is that AES is something to be avoided where possible.&lt;/p&gt;

&lt;p&gt;In a future post, we’ll go over how Poly1305 works, combine it with ChaCha, and ultimately define the authenticated encryption with authenticated data (AEAD) algorithm XChaCha20-Poly1305 used in O(1) Cryptography.
In the meantime, you can have a look at some of the &lt;a href=&quot;https://github.com/o1c-dev/o1c/wiki/References&quot;&gt;academic papers and references&lt;/a&gt; cited in O(1) Cryptography.&lt;/p&gt;

</description>
        <pubDate>Sat, 06 Feb 2021 18:00:00 -0600</pubDate>
        <link>https://musigma.blog/2021/02/06/chacha.html</link>
        <guid isPermaLink="true">https://musigma.blog/2021/02/06/chacha.html</guid>
        
        
        <category>crypto</category>
        
      </item>
    
      <item>
        <title>Building a Deterministic Random Bit Generator</title>
        <description>&lt;p&gt;As part of developing the &lt;a href=&quot;https://github.com/o1c-dev/o1c&quot;&gt;O(1) Cryptography library&lt;/a&gt;, I initially relied upon the standard Java cryptography APIs for cryptographic random bit generation.
Unlike most of the cryptographic APIs in Java that are frequently misused, &lt;a href=&quot;https://docs.oracle.com/javase/8/docs/api/java/security/SecureRandom.html&quot;&gt;java.security.SecureRandom&lt;/a&gt; is about as simple as it gets for defining all the relevant operations of a secure random number generator.
Naturally, like most of the standard APIs, this one, too, can be misused if configured incorrectly, most importantly in the underlying seed generation strategies which are platform-specific.
Java provides a few different strategies out of the box, and in &lt;a href=&quot;https://github.com/o1c-dev/o1c/blob/81c2248a5ed696f11a1784ceb90afd573ab35822/src/main/java/dev/o1c/internal/SystemSeedGenerator.java#L29&quot;&gt;pure Java code&lt;/a&gt;, I rely on this API to seed the O(1) Cryptography deterministic random bit generators.
In C, there are some nicer integrations with OS-specific system calls and even hardware-specific integrations more readily available, though Java can access some of these if they’re provided via some PKCS11 library.
As a technical note, the only relevant limitation we have in Java compared to C is that the Java operations will generally require opening a file or accessing some resource which can potentially fail due to file descriptor leaks or other resource exhaustion, while the C code can make use of system calls that bypass the file interface entirely.
Depending on the underlying threat model, this can be a vulnerability if the process cannot open &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/random&lt;/code&gt; or equivalent device files.
With that in mind, let’s take a look at how a random bit generator works.&lt;/p&gt;

&lt;!-- more --&gt;

&lt;p&gt;The canonical U.S. standard for cryptographic random bit generators is &lt;a href=&quot;https://doi.org/10.6028/NIST.SP.800-90Ar1&quot;&gt;NIST Special Publication 800-90A Revision 1&lt;/a&gt; which specifies three mechanisms to do so using standard cryptographic primitives.
These mechanisms include one based on plain hash functions, one based on keyed hash functions (HMAC specifically), and a third based on block ciphers such as AES.
In O(1) Cryptography, I’ve implemented two strategies using the primitives available here: one based on a &lt;a href=&quot;https://github.com/o1c-dev/o1c/blob/81c2248a5ed696f11a1784ceb90afd573ab35822/src/main/java/dev/o1c/modern/blake3/Blake3RandomBytesGenerator.java#L30&quot;&gt;keyed BLAKE3&lt;/a&gt; hash function, and another &lt;a href=&quot;https://github.com/o1c-dev/o1c/blob/81c2248a5ed696f11a1784ceb90afd573ab35822/src/main/java/dev/o1c/modern/chacha20/ChaCha20RandomBytesGenerator.java#L32&quot;&gt;using ChaCha20&lt;/a&gt; as a pseudo-block cipher.
Both strategies have some commonality beyond their underlying permutations (BLAKE3 uses the same quarter-round mixing function as ChaCha20).&lt;/p&gt;

&lt;p&gt;First, a DRBG instance is lazily initialized on a per-thread basis using system-specific seed entropy.
Java makes this fairly simple with the &lt;a href=&quot;https://docs.oracle.com/javase/8/docs/api/java/lang/ThreadLocal.html&quot;&gt;ThreadLocal&lt;/a&gt; class and relying on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SecureRandom&lt;/code&gt; for accessing system-specific entropy sources for generating a seed.
C raises the challenge of not having a standard runtime, though it does have standardized thread-local storage support.
On the other hand, C gives us access to lower level APIs which have their own advantages to the Java equivalents.&lt;/p&gt;

&lt;p&gt;One of these APIs is the function &lt;a href=&quot;https://man7.org/linux/man-pages/man3/getentropy.3.html&quot;&gt;getentropy&lt;/a&gt; which uses the Linux system call &lt;a href=&quot;https://man7.org/linux/man-pages/man2/getrandom.2.html&quot;&gt;getrandom&lt;/a&gt;, a function that is used as the basis for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/random&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/urandom&lt;/code&gt;.
On BSDs and macOS, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;getentropy&lt;/code&gt; is itself the system call with the same signature that libc borrowed.
For older POSIX platforms that don’t expose a system call, reading seed data from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/random&lt;/code&gt; can be supported, though I’ve elided support for it currently as it involves file IO which starts to bloat the C library beyond what’s minimally necessary to support the Java API.
On Windows, there appears to be a long history of APIs here, the most promising sufficiently low level one being &lt;a href=&quot;https://docs.microsoft.com/en-us/windows/win32/api/ntsecapi/nf-ntsecapi-rtlgenrandom&quot;&gt;RtlGenRandom&lt;/a&gt;, a function from the advapi32 library which has been a fairly standard base library on Windows since the XP days.
An interesting source to look at would be non-standard hardware entropy sources like the &lt;a href=&quot;https://onerng.info/&quot;&gt;OneRNG&lt;/a&gt;, an open source hardware RNG which is typically accessible in a platform-independent manner via serial port access APIs besides any of the integrations offered specifically for Linux.&lt;/p&gt;

&lt;p&gt;Each implementation uses this seed data slightly differently, but they both use the seed as an initial key.
The ChaCha20 implementation also uses the seed for a nonce and initial counter.
In order to generate random bytes, this uses the keystream output (the resulting ciphertext of encrypting null bytes) for each request and then ratchets itself by using an incrementing nonce to provide forward secrecy.
The BLAKE3 implementation generates bytes by finalizing the hash output of an empty input with the first 32 bytes being used solely to rekey the hash function as its ratchet after using the subsequent bytes as the output bytes.
Both implementations maintain internal counters to track when reseeding is necessary.&lt;/p&gt;

&lt;p&gt;It may be interesting to note how simply the concepts from the underlying stream cipher and extensible output function primitives made implementing a DRBG much simpler than the required steps to do the same with AES or HMAC-SHA2.
Since O(1) Cryptography is an opinionated cryptography library with the goal of being easy to use and hard to misuse, this philosophy is apparent in both its APIs and its choice of cryptographic primitives.
Using the current NIST standards, there is currently only one primitive available that can be as easily used: &lt;a href=&quot;https://en.wikipedia.org/wiki/SHA-3&quot;&gt;SHAKE128/SHAKE256&lt;/a&gt;.
SHAKE is the extensible output function variant of SHA-3 which has a comparable API to BLAKE3 while being fairly slower.
Using this same pattern, it’s fairly simple to build a DRBG using any cryptographic sponge function such as &lt;a href=&quot;https://keccak.team/xoodyak.html&quot;&gt;Xoodyak&lt;/a&gt; or any sponge-style permutation like &lt;a href=&quot;https://ascon.iaik.tugraz.at/&quot;&gt;Ascon&lt;/a&gt; as either cipher-based generators or keyed hash ones.
Some of these ideas are also included, though it remains to be seen where lightweight cryptography standards converge, so they are only available as experimental implementations.&lt;/p&gt;

&lt;p&gt;I hope this helps demystify how a cryptographic random bit generator can be made using cryptographic primitives.
DRBGs can get more complicated than this by adding an interface to accept user-provided seed data to include in its seed input, maintaining buffers, and gathering various system state to use in the reseed algorithm, though I’ve tried to keep things simple by standing on the shoulders of operating systems and hardware support instead.
Plus, Java’s standard &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SecureRandom&lt;/code&gt; implementation already handles this for us where necessary.&lt;/p&gt;

&lt;p&gt;These random generators will be an important tool in our cryptographic toolbox later on when I go over the design of other parts of O(1) Cryptography in subsequent blog posts.
Random data are integral to our ability to generate keys and challenges, and the strength of our cryptosystem is only as good as its fundamental parts.
Ensuring that random data can be generated fast and in parallel is a clear requirement for any proper use of cryptography, so perhaps it may help to keep in mind one of the implicit design goals of O(1) Cryptography: speed.
Performance problems are a common source of security vulnerabilities when security measures get disabled due to interference with core application logic.
Using high performance cryptographic primitives prevents the need to tweak security parameters improperly; using primitives that avoid overly complex configuration options goes a step further by preventing insecure tweaks in the first place.&lt;/p&gt;

</description>
        <pubDate>Sat, 30 Jan 2021 17:00:00 -0600</pubDate>
        <link>https://musigma.blog/2021/01/30/drbg.html</link>
        <guid isPermaLink="true">https://musigma.blog/2021/01/30/drbg.html</guid>
        
        
        <category>crypto</category>
        
      </item>
    
      <item>
        <title>Introducing the O(1) Cryptography Project</title>
        <description>&lt;p&gt;Cryptography is a fascinating subject at the intersection of pure math and computer science that has become nearly ubiquitous over the past several years.
Similar to functional programming, cryptography holds a particular interest to me because of the pure math connections, and my work in security software engineering has brought me back into the topic over the past year or so.
Diving in to the standard Java cryptography APIs, I quickly discovered a tangle of strange naming conventions and poorly documented cryptographic primitives.
There have been numerous academic papers written studying the widespread phenomenon of misuse of Java cryptography APIs, and after spending a bit of time with them, I can easily see why this is.
Java, in its quest to remain low level and generic, oftentimes provides overly complicated APIs that, while flexible for low level use, offer very little guidance in their &lt;em&gt;correct&lt;/em&gt; use.
Some of this might be attributed to typical overengineering common to Java APIs prior to Java 8, but this particular API is more like a C API ported directly to Java.
In fact, there’s a fairly innocuous explanation for it: it’s the same basic pattern present in most historical cryptographic libraries, most of which are indeed written in C by academics with little care for engineering.
Combined with the historical baggage of having to deal with cryptographic export controls back in the 1990’s and early 2000’s, the Java cryptography API, like most cryptographic software written before export controls were relaxed, suffered from security vulnerabilities by design.&lt;/p&gt;

&lt;!-- more --&gt;

&lt;p&gt;Much has been written and discovered since these dark days, though most software still struggles with outdated cryptography practices and misuse.
In an effort to help software developers incorporate strong cryptography into their applications, an effort must be made to create and use cryptographic software components that are both easy to use and hard to misuse.
One of the longest running efforts with a similar philosophy is the &lt;a href=&quot;https://www.openbsd.org/&quot;&gt;OpenBSD operating system&lt;/a&gt; which has incorporated strong cryptography and security by default for the past 25 years.
Unconstrained by American cryptographic export controls in its home of Canada, the project has been one of the great examples of pervasive use of cryptography and secure design throughout its codebase.
With simplified export controls, particularly for free and open source software, this philosophy must expand and help improve the security, privacy, and safety of the software we all write.&lt;/p&gt;

&lt;p&gt;Using a corpus of public domain algorithms and cryptographic knowledge, I’ve started the &lt;a href=&quot;https://github.com/o1c-dev/o1c&quot;&gt;O(1) Cryptography Project&lt;/a&gt; where I’m developing an opinionated cryptography library that aims to be easy to use and hard to misuse.
O(1) Cryptography is a Java and C library that bundles the latest best practices in cryptography by providing abstractions for common cryptographic use cases.
The name is a pun on the idea that cryptographic algorithms should generally run in constant-time and with minimal differentiation in its observable state.
These properties seem to be somewhat modern to cryptography in that computers are fast enough and powerful enough now to statistically differentiate non-constant-time cryptographic algorithmic output, though they weren’t as big a concern back at the turn of the 21st century when AES was standardized.
In fact, most standardized cryptography until recently was developed almost entirely detached from the reality that one day, non-cryptographers might need to use this stuff, too.
There simply aren’t enough cryptographers in existence to write custom cryptographic routines for every application that needs it, and older cryptographic libraries were not designed for non-experts.
Maybe you’ve heard of the various acronyms from yesteryear like AES, DES, RC4, RSA, DSA, MD5, SHA1, and many more.
Some of these primitives are still useful and secure, but they all have problems of one form or another.
In particular, they all fail the two-prong test of being easy to use and hard to misuse.&lt;/p&gt;

&lt;p&gt;For example, AES is hard to use: as it was standardized before the consensus formed that &lt;em&gt;authenticated&lt;/em&gt; encryption was the way to go, it requires pairing with an authentication algorithm which is commonly forgotten in practice.
Implementers of AES are not discouraged from writing various optimizations that leak information about the underlying encryption key.
It is fairly easy to misuse AES on both the implementer side and the application side.
Being a block cipher, in order to encrypt data longer than the length of the encryption key, a mode of operation must be specified, and many commonly implemented modes have their own security vulnerabilities besides a lack of authentication.
Another example is RSA, the pair of signature and asymmetric encryption algorithms that are frequently misused and improperly implemented.
Due to its large key size, temptations to cut corners have run rampant through history in many implementations.
Misunderstandings in the use of symmetric versus asymmetric encryption have led to people using RSA to directly encrypt data, direct use of the Diffie-Hellman shared secret result for encryption, duplicate use of keys, and many other security failures.&lt;/p&gt;

&lt;p&gt;Between 2005 and 2010, &lt;a href=&quot;https://cr.yp.to/djb.html&quot;&gt;Prof. Daniel J. Bernstein&lt;/a&gt; at University of Illinois at Chicago published a few papers that form the basis for much of the underlying cryptographic primitives central to O(1) Cryptography.
In particular, the Salsa20/ChaCha20 family of stream ciphers and extended nonce versions, the Poly1305 one-time authenticator, and the elliptic curve Curve25519, were all detailed during this time.
A more detailed listing of these various foundational papers are &lt;a href=&quot;https://github.com/o1c-dev/o1c/wiki/References&quot;&gt;listed in the O(1) wiki&lt;/a&gt;, though the common theme behind the choice of primitives for this library are that they, too, are designed with the philosophy that they should be easy to use and hard to misuse.
Another interesting theme is that many of these algorithm choices have been included into various IETF standards such as TLS 1.3 and SSH, so their use has clearly become far more widespread than their initial years.&lt;/p&gt;

&lt;p&gt;Naturally, I am not the first to develop such a library, and there is prior art that inspires this library.
The general idea behind making a cryptographic library that is &lt;em&gt;easy to use&lt;/em&gt; and &lt;em&gt;hard to misuse&lt;/em&gt; is the central concept of the polyglot &lt;a href=&quot;https://github.com/cossacklabs/themis&quot;&gt;Themis cryptographic framework&lt;/a&gt;.
The choice of algorithms featured have been strongly influnced by Prof. Bernstein’s old NaCl library which formed the basis for &lt;a href=&quot;https://github.com/jedisct1/libsodium&quot;&gt;libsodium&lt;/a&gt;, the essential C library with a similar philosophy (or at least as easy to use as C can be given that it’s C).
Special thanks to Frank Denis, the maintainer of libsodium and developer of much of the &lt;a href=&quot;https://github.com/ziglang/zig&quot;&gt;zig standard crypto library&lt;/a&gt;, for further widening the rabbit hole of cryptographic primitives to explore and support, particularly in the field of lightweight cryptography.&lt;/p&gt;

&lt;p&gt;This library is still under heavy development, particularly in the area of documentation and the higher level APIs.
Much of the primary cryptographic primitives have been ported to Java where needed, and native implementations are also available.
While the primary concern is to develop the Java API first, due to the eventual inclusion of C code from the &lt;a href=&quot;https://github.com/mit-plv/fiat-crypto&quot;&gt;Fiat Cryptography project&lt;/a&gt; for formally verified elliptic curve functions, I am also considering what other languages make sense to provide facades for, especially non-JVM ones that would benefit more from the native code.
Pure Java versions of the algorithms are all available, though optimized native versions are also provided as an option.
Similar to Apache Log4j, it may make sense to create Scala and Kotlin APIs, but those are currently out of scope for initial release.&lt;/p&gt;

</description>
        <pubDate>Thu, 28 Jan 2021 18:00:00 -0600</pubDate>
        <link>https://musigma.blog/2021/01/28/o1c.html</link>
        <guid isPermaLink="true">https://musigma.blog/2021/01/28/o1c.html</guid>
        
        
        <category>crypto</category>
        
      </item>
    
      <item>
        <title>Using Travis CI for Continuous Delivery</title>
        <description>&lt;p&gt;For almost two years, I’ve been using &lt;a href=&quot;https://pages.github.com/&quot;&gt;GitHub Pages&lt;/a&gt; to host my blog.
This tool handles a lot of things for you behind the scene: it builds your site and deploys it to a web server.
GitHub implements this as a great example of continuous delivery, the marriage of continuous integration with continuous deployment.
GitHub also lets you use your own custom domain name, but it doesn’t support TLS connections over them.
Thus, I had to take matters into my own hands and go back to self hosting.&lt;/p&gt;

&lt;p&gt;Enter my old pals &lt;a href=&quot;https://www.nearlyfreespeech.net/&quot;&gt;NearlyFreeSpeech.NET&lt;/a&gt;, &lt;a href=&quot;https://letsencrypt.org/&quot;&gt;Let’s Encrypt&lt;/a&gt;, and &lt;a href=&quot;https://travis-ci.org/&quot;&gt;Travis CI&lt;/a&gt;.
This host provides SSH access, and Travis provides easy ways to set up CI/CD pipelines.
Let’s get started on building the GitHub Pages site first which itself is based on &lt;a href=&quot;https://jekyllrb.com/&quot;&gt;Jekyll&lt;/a&gt;.
This static site generator is written in Ruby, so in order to build this, we need a recent Ruby environment.
Then a few commands can get you bootstrapped:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gem &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;bundler
bundle &lt;span class=&quot;nb&quot;&gt;install
&lt;/span&gt;bundle &lt;span class=&quot;nb&quot;&gt;exec &lt;/span&gt;jekyll serve
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This command serves an incrementally compiling view of the site.
We can change &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;serve&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;build&lt;/code&gt; to simply get the output.
Now we need to deploy these output files via SSH.
There are several different ways to copy files over SSH, and in today’s post, we’ll be piping data directly through an SSH pipe.&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;tar&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-czf&lt;/span&gt; - &lt;span class=&quot;nt&quot;&gt;-C&lt;/span&gt; _site &lt;span class=&quot;nb&quot;&gt;.&lt;/span&gt; | ssh deploy@ssh.example.org &lt;span class=&quot;s1&quot;&gt;&apos;tar -xzf - -C /var/www&apos;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;By using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tar&lt;/code&gt;, we bundle up the entire &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_site/&lt;/code&gt; directory and compress it, and we output to stdout.
By changing directories into what we want to compress (using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-C&lt;/code&gt;), we’ll be able to easily extract it wherever we want.
Now that we have the basics, let’s integrate it with Travis.&lt;/p&gt;

&lt;p&gt;First, let’s get started with continuous integration of our website.
We need to add a minimal &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.travis.yml&lt;/code&gt; config file to declare our pipeline:&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;language&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;ruby&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;cache&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;bundler&lt;/span&gt;

&lt;span class=&quot;na&quot;&gt;before_install&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;gem install bundler&lt;/span&gt;

&lt;span class=&quot;na&quot;&gt;script&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;bundle exec jekyll build&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In our minimal config, we declare the programming language in use (Ruby), enable package caching for Bundler, setup commands to prepare the build environment, and build commands.
After enabling our repository in Travis, we can now see our site get built on commit.
This can help detect errors in configuration or anything verifiable that we add tests for.&lt;/p&gt;

&lt;p&gt;Next, we need to configure deployment.
Travis provides a simple way to encrypt files to store in a git repository which can be decrypted inside the running Travis build container.
We’ll use this to encrypt an SSH private key file to be used for copying our site build artifacts.
Let’s set up a deploy key.&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; .travis_deploy_key &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;.gitignore
ssh-keygen &lt;span class=&quot;nt&quot;&gt;-C&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;deploy@travis-ci.org&apos;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-f&lt;/span&gt; .travis_deploy_key
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Next, we need to install the Travis command line tool and log in.&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gem &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;travis
travis login
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Next, we encrypt the file and update our Travis config.&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;travis encrypt-file .travis_deploy_key &lt;span class=&quot;nt&quot;&gt;-a&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This will output a file named &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.travis_deploy_key.enc&lt;/code&gt; which can be safely committed to git.
The unencrypted file should not be committed, and the provided example adds it to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.gitignore&lt;/code&gt; to prevent it as such.
Now we can add some more settings to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.travis.yml&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;language&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;ruby&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;cache&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;bundler&lt;/span&gt;

&lt;span class=&quot;na&quot;&gt;addons&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;ssh_known_hosts&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;ssh.example.org&lt;/span&gt;

&lt;span class=&quot;na&quot;&gt;before_install&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;openssl aes-256-cbc ...&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;gem install bundler&lt;/span&gt;

&lt;span class=&quot;na&quot;&gt;script&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;bundle exec jekyll build&lt;/span&gt;

&lt;span class=&quot;na&quot;&gt;after_success&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;eval &quot;$(ssh-agent -s)&quot;&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;chmod 600 .travis_deploy_key&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;ssh-add .travis_deploy_key&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;tar&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;-czf&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;-C&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;_site&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;ssh&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;deploy@ssh.example.org&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;tar&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;-xzf&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;-C&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;/var/www&apos;&quot;&lt;/span&gt;

&lt;span class=&quot;na&quot;&gt;branches&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;only&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;master&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Of note here is the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;after_success&lt;/code&gt; section.
The first three commands are used to set up &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ssh-agent&lt;/code&gt; so that any &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ssh&lt;/code&gt; program can use its keys without user input.
The last command is the same one from earlier for deploying the site artifacts.
We also have a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;branches&lt;/code&gt; section which we use to restrict this pipeline to only execute on the master branch.
This way we can use branches to store blog post drafts and other things without them being deployed as well.&lt;/p&gt;

&lt;p&gt;We can adapt this process to build unsupported programming languages as well such as LaTeX.
To do so, we can use the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apt&lt;/code&gt; addon for Travis.&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;addons&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;ssh_known_hosts&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;ssh.example.org&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;apt&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;packages&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;texlive-full&lt;/span&gt;

&lt;span class=&quot;na&quot;&gt;before_install&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;openssl aes-256-cbc ...&lt;/span&gt;

&lt;span class=&quot;na&quot;&gt;script&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;pdflatex cv.tex&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;pdflatex cv.tex&lt;/span&gt;

&lt;span class=&quot;na&quot;&gt;after_success&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;eval &quot;$(ssh-agent -s)&quot;&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;chmod 600 .travis_deploy_key&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;ssh-add .travis_deploy_key&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;scp cv.pdf deploy@ssh.example.org:/var/www/&lt;/span&gt;

&lt;span class=&quot;na&quot;&gt;branches&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;only&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;master&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

</description>
        <pubDate>Tue, 27 Mar 2018 17:00:00 -0500</pubDate>
        <link>https://musigma.blog/2018/03/27/travis.html</link>
        <guid isPermaLink="true">https://musigma.blog/2018/03/27/travis.html</guid>
        
        
        <category>cicd</category>
        
      </item>
    
      <item>
        <title>The Art of Logging</title>
        <description>&lt;p&gt;All developers have attempted to debug their programs by printing
lifecycle and state information to the console.  This concept,
sometimes known as &lt;em&gt;printf debugging&lt;/em&gt;, can be far more powerful of
a tool than one might first expect.  The essence of this debugging
technique is the concept of &lt;em&gt;logging&lt;/em&gt;, where developers add relevant
information about the state of the running program to a log.  The
use of logging is vital to both developers and operators, and it
is important to understand how and why to use logging from both
perspectives.&lt;/p&gt;

&lt;h2 id=&quot;logging-fundamentals&quot;&gt;Logging Fundamentals&lt;/h2&gt;

&lt;p&gt;Logging is far more than just printing to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;stderr&lt;/code&gt;, however.  Typical
logging systems are divided into a set of logging levels that
generally define the audience and semantics of the log event.  For
example, in &lt;a href=&quot;https://logging.apache.org/log4j/2.x/&quot;&gt;Apache Log4j 2&lt;/a&gt;, logging levels are divided
into the following set:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;em&gt;Fatal&lt;/em&gt;: error messages that indicate that some subsystem or the
entire program cannot continue execution and will terminate.&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;Error&lt;/em&gt;: error messages regarding a problem that should be handled
by a human. These are generally useful for operators to alert on.&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;Warn&lt;/em&gt;: warning messages regarding potential problems that may need
to be handled by a human. This level is often misused and ignored
as a result.&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;Info&lt;/em&gt;: informative messages about the state of a program. These
types of messages tend to be related to the lifecycle of a program
and can be viewed as a way to debug the macro state of the program.&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;Debug&lt;/em&gt;: debugging information about internal states of the program.
These messages are usually only helpful to the developers maintaining
a program.&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;Trace&lt;/em&gt;: messages tracing the execution flow of a program. These
messages are usually very low level and simply mirror the micro
state of a program and generally don’t offer more information than
a debugger would.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Some logging systems define other levels, but most logging systems
categorize their log messages into similar buckets with similar use
cases.  Each level can be selectively enabled or disabled, though
generally disabling one level will disable all levels below it as
well.  For example, if we used a logging configuration that was set
to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;WARN&lt;/code&gt; as its level, then only warnings, errors, and fatal
messages would be shown.&lt;/p&gt;

&lt;p&gt;By simply adding severity information to log messages, we have
already surpassed the functionality offered by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;printf&lt;/code&gt;, but we’ve
only scratched the surface.  Any given program is generally large
enough to be made up of some sort of concept of modules or subsystems,
so it seems like it would be useful to extend this configurable
flexibility to subsystems as well.  In Java programs, these tend
to be separated by packages and classes, though the important concept
to use here is that of a named logger.  By naming the loggers used
in a program, each subsystem can be independently configured to
only output logs that are desired.  For example, suppose a third
party library is misusing the warning level and causing operations
to be concerned about the health of your application.  After verifying
that every warning log message under the logger name prefix
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;com.example.subsystem&lt;/code&gt; are not real warnings, we can use a higher
level threshold for that set of loggers specifically while not
having to disable warnings globally or modify the third party
library’s source code.  This also relates to the idea that logger
names form a hierarchy; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;com.example&lt;/code&gt; is the parent of
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;com.example.subsystem&lt;/code&gt;.  This allows for simpler ways to configure
entire subsystems in one setting.&lt;/p&gt;

&lt;p&gt;At this point, we have a rather powerful abstraction over log event
filtering using both a level and a name, but we can do better!  An
additional piece of metadata can be attached to log events: &lt;em&gt;markers&lt;/em&gt;.
A marker is a simple text string to mark some sort of cross-cutting
concern of a particular log message.  This can be used to help route
specific log messages to different logging systems.  For example,
suppose a log message is marked with the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ALERT&lt;/code&gt; marker.  The logging
configuration could have a filter for that marker which would route
these messages regardless of level or logger name to a particular
destination.  This might be an alerts channel in Slack or an alerts
mailing list.&lt;/p&gt;

&lt;p&gt;In some programming languages such as Java, string manipulation is
considered a somewhat low level operation, thus there are certain
string templating features not present here that would be useful
for logging.  For example, logging a message that contains values
from some local variables would normally require string concatenation,
and if that log message is never displayed, then said concatenation
was wasted CPU effort.  Little things like this can add up over
time to form a significant performance overhead, so we can certainly
do better!  Enter the parameterized log message which is quite
similar to a parameterized SQL query in spirit.  In Log4j and many
other logging systems, parameters are specified by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{}&lt;/code&gt; placeholders
in the log message and provided as additional parameters to the
logging method.  For example:&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;debug&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;User {} logged in&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getName&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The placeholder is filled in only if debug logging is enabled, so
the full string is never computed unless absolutely necessary.  This
technique is mostly relevant to languages like Java.  In the Scala
version of Log4j, for example, string templates are a built in
feature to the language, and macros are used behind the scenes to
avoid the template rendering when logging is disabled.  Example:&lt;/p&gt;

&lt;div class=&quot;language-scala highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;logger&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;debug&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;User ${user.getName} logged in&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;One more related API that is handy to know is that a lambda function
can be provided instead of a string in order to defer some code
needed to assemble a log message only when enabled.  For example,
suppose we wish to go fetch some additional metadata from a database
for some debug log message.  This overhead might be unacceptable
most of the time, but we may wish to selectively enable it once in
a while.  The entire body of the function can be encapsulated into
a lambda function and passed to the logger.  This is generally
cleaner than surrounding the code with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;if&lt;/code&gt; checks for the relevant
log level or other noisy techniques.&lt;/p&gt;

&lt;p&gt;There are far more features that can be covered regarding how to
use a logging API from the developer’s point of view, but these are
mostly convenience features regarding repetitive things like
thread-local information always included in a log message, or
structured log messages, generic event logging, and others.  Far
more information about these features are available in the &lt;a href=&quot;https://logging.apache.org/log4j/2.x/manual/api.html&quot;&gt;Log4j
manual&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;where-do-log-events-go&quot;&gt;Where Do Log Events Go?&lt;/h2&gt;

&lt;p&gt;Now that we’ve established a general framework for writing and
filtering log events, what can we do with them?  The simplest
implementation of handling log events would be to print each log
message to the console separated by new lines.  Since quite a bit
of context would be lost doing it this way, we generally include
additional information from the log event such as the timestamp,
log level, marker (if defined), logger name, and thread name for
multithreaded programs.  All the fields we wish to output should
be configurable, and in fact, there are &lt;a href=&quot;https://logging.apache.org/log4j/2.x/manual/layouts.html#PatternLayout&quot;&gt;several different fields
available&lt;/a&gt; which we can add to provide context about the
log message.  The output format could also use a structured format
such as JSON which is more easily parsed than line-oriented log
messages, though all log aggregation and search tools have powerful
tools to extract log event information from all sorts of formats.&lt;/p&gt;

&lt;p&gt;In some use cases, writing log events to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;stderr&lt;/code&gt; is acceptable.
For example, during development of a program, the developer may
wish to view log events while running the program in the console.
On the other hand, perhaps you’re using an orchestration framework
such as &lt;a href=&quot;https://mesos.apache.org/&quot;&gt;Apache Mesos&lt;/a&gt; to execute all your applications.
Such a framework can be configured to watch the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;stderr&lt;/code&gt; streams
of all running applications in order to collect log messages to a
central location.&lt;/p&gt;

&lt;p&gt;For many use cases, however, simply printing to a console that
nobody looks at is not a valid strategy.  Any website that requires
a reasonable SLA and has more than, say, a few hundred users,
generally requires multiple servers to distribute load.  As the
number of nodes increase, it simply becomes infeasible to watch the
console.  In fact, each node may be executing multiple applications,
so without a program like &lt;a href=&quot;https://github.com/tmux/tmux/wiki&quot;&gt;tmux&lt;/a&gt;, we’d have to redirect the
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;stderr&lt;/code&gt; of each program to a file anyways.  With that in mind, we
can directly configure the logging framework to output log events
to a file instead of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;stderr&lt;/code&gt;.  Each file can be monitored using a
program such as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tail&lt;/code&gt; to continually watch for new log events being
appended to the file.  This style of logging is pervasive in typical
GNU/Linux and BSD systems where many running services will output
log information to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/var/log/&lt;/code&gt; directories.  However, if this is
not configured to periodically rotate log files and delete old ones,
then the server’s disk space can eventually fill up with log
information!  This job is typically filled by a program such as
&lt;a href=&quot;https://github.com/logrotate/logrotate&quot;&gt;logrotate&lt;/a&gt;, though Log4j has a &lt;a href=&quot;https://logging.apache.org/log4j/2.x/manual/appenders.html#RollingRandomAccessFileAppender&quot;&gt;rolling file
appender&lt;/a&gt; which provides similar functionality.&lt;/p&gt;

&lt;p&gt;Simply outputting to a log file can be a good strategy for operators
who are still stuck in the “do it by hand” mindset, but we can do
better!  Our main goal here should be to collect logs from all our
servers into a central, searchable location.  One such way to
accomplish this is by using a product such as &lt;a href=&quot;https://www.elastic.co/&quot;&gt;ELK&lt;/a&gt;,
&lt;a href=&quot;https://www.fluentd.org/&quot;&gt;Fluentd&lt;/a&gt;, or &lt;a href=&quot;https://www.graylog.org/&quot;&gt;Graylog&lt;/a&gt;.  These tools offer more
than just log aggregation; they offer ways to filter, sort, search,
and alert based on the contents of the logs.  However, by relying
on log files, we’re also relying on the stability of the individual
servers.  Obtaining logs in disaster scenarios is generally more
difficult but also far more important, so let’s improve on that.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://flume.apache.org/&quot;&gt;Apache Flume&lt;/a&gt; is a project for collecting and aggregating
large volumes of log events in a distributed computing environment.
This is very useful in cluster scenarios such as running dozens or
hundreds of Apache Hadoop or Apache Spark nodes for example.
Individual nodes can pass along log events to a Flume agent, and
each agent is responsible for reliably delivering the log events
elsewhere.  Combined with the &lt;a href=&quot;https://logging.apache.org/log4j/2.x/manual/appenders.html#FlumeAppender&quot;&gt;Flume appender&lt;/a&gt;, this can
be easily utilized in a distributed environment to collect all log
events to a central log aggregator.  Said aggregator may be something
complex like Logstash or Graylog, or perhaps it may be something
simple like a single master log file.&lt;/p&gt;

&lt;p&gt;Now that we have our logs all in one place, we can really step up
the operations game.  We can set up alerts based on log level
thresholds, number of messages, frequency of messages, and triggers
based on any metadata contained within.  If we want to get really
fancy, we can train some machine learning models via &lt;a href=&quot;https://spark.apache.org/mllib/&quot;&gt;Spark&lt;/a&gt;
or &lt;a href=&quot;https://mahout.apache.org/&quot;&gt;Apache Mahout&lt;/a&gt; combined with any other exported metrics
data to attempt to predict failure of our services.  Such a technique
could also be used for all sorts of observability of clusters and
microservices.  Combined with scripts to automatically scale or
restart services, operations can become more proactive in maintaining
their systems.&lt;/p&gt;

&lt;p&gt;There are dozens more frameworks, libraries, and tools that could
be covered here.  Logging is something all developers do whether
they’re using the proper tools or not, so it’s a great idea to get
familiar with the tools and concepts in order to improve the metadata
being created by applications.  Developers should work closely with
operators (devops) in order to find a good balance of logging
verbosity and observability.  Managing logs is a complex topic that
many people tend to overlook, but having a good logging architecture
in place can help save the day during a production issue.  As a
final note, to those using the Java Platform, Apache Log4j 2 is the
premier logging library for Java, Scala, Groovy, Kotlin, and any
other JVM language.  It is common for logging to add noticeable
overhead to applications, and the typical solution is to simply
disable logging, but this removes all the advantages to logging in
the first place!  Instead, take a look at &lt;a href=&quot;https://logging.apache.org/log4j/2.x/performance.html&quot;&gt;the numbers&lt;/a&gt; and
see how Log4j can be used with very minimal overhead, even in high
frequency trading applications.&lt;/p&gt;

</description>
        <pubDate>Mon, 06 Nov 2017 08:00:00 -0600</pubDate>
        <link>https://musigma.blog/2017/11/06/logging.html</link>
        <guid isPermaLink="true">https://musigma.blog/2017/11/06/logging.html</guid>
        
        
        <category>logging</category>
        
      </item>
    
      <item>
        <title>Using Akka and Scala for CQRS and Event Sourcing</title>
        <description>&lt;p&gt;In the past, we covered &lt;a href=&quot;http://musigma.org/java/2016/11/14/lagom.html&quot;&gt;using Lagom&lt;/a&gt; to implement Java microservices
using the event sourcing and CQRS patterns that framework relies on. Today, we’ll
be revisiting our blog microservice example using the &lt;a href=&quot;http://scala-lang.org/&quot;&gt;Scala programming language&lt;/a&gt;
and &lt;a href=&quot;http://akka.io/&quot;&gt;Akka&lt;/a&gt;, one of the main components underlying &lt;a href=&quot;https://www.lagomframework.com/&quot;&gt;Lagom&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;event-sourcing&quot;&gt;Event Sourcing&lt;/h2&gt;

&lt;p&gt;First, let’s take a quick review of what event sourcing and CQRS are along with
the general design patterns to use to implement these ideas. &lt;a href=&quot;https://martinfowler.com/eaaDev/EventSourcing.html&quot;&gt;Event sourcing&lt;/a&gt;
is an architectural pattern where all activity in a system is captured by
immutable event objects, and these events are stored and published in a sequential
log. The state of a system at a given time can therefore be derived from the sequence of events
prior to the state. For example, most SQL databases are implemented using this
sort of architecture where all data mutation operations are stored as events in a
&lt;a href=&quot;https://en.wikipedia.org/wiki/Write-ahead_logging&quot;&gt;write-ahead log&lt;/a&gt;. This pattern works very well with the physical storage
underlying these devices as they tend to be based on magnetic disks which can
work very fast in sequential access but become a lot slower in random access.&lt;/p&gt;

&lt;p&gt;Using the concept of event sourcing, &lt;a href=&quot;https://martinfowler.com/bliki/CQRS.html&quot;&gt;Command Query Responsibility Segregation&lt;/a&gt;
is an architectural pattern that is rather self-describing: the responsibility
of a system to handle commands and queries are separated rather than intertwined
as in typical applications. Commands are used to update state while queries are
used to inspect state. The separation of these two concerns allows for much
greater scalability in distributed systems than traditional approaches such as
a single backing database for both reading and writing data. While CQRS is not
required in order to implement a write-side and read-side pair of databases,
it does offer a strong pattern to follow to do so. For example, an application
like Twitter may wish to store all tweets in a distributed database such as
&lt;a href=&quot;https://cassandra.apache.org/&quot;&gt;Cassandra&lt;/a&gt;, but in order to effectively search those tweets, it
would be useful to copy written data into &lt;a href=&quot;https://www.elastic.co/products/elasticsearch&quot;&gt;Elasticsearch&lt;/a&gt; for later querying.
Provided that we follow an event sourcing pattern, then the source streams of
data are well defined and make it much simpler to implement this responsibility
segregation.&lt;/p&gt;

&lt;p&gt;In CQRS, we can issue a command to a system to update its state. This command,
after validation, will create an immutable event which is appended to the event
log. After persisting this event, the state of the system can be updated in
response. By doing this, the system can always be brought back to any given
state by replaying the events in the event log. For performance reasons, the
state of a system is periodically snapshotted and persisted so that less events
from the main event log need to be replayed when restarting the system.
The state obtained from the event log can be as simple as the denormalized
representation of entities, or it could be more complex views of the event
stream which can be further combined down the line. It’s important to note
that following this pattern means we need to manually reimplement some things
we take for granted in a normal RDBMS such as transactions, indexing, joins,
constraints, and triggers. Ideally, these sorts of things can be abstracted
well enough to be provided mostly through libraries or frameworks, but some
things such as transactions are still application-specific in how to make
&lt;a href=&quot;https://docs.microsoft.com/en-us/azure/architecture/patterns/compensating-transaction&quot;&gt;compensating transactions&lt;/a&gt; to roll back erroneous data.&lt;/p&gt;

&lt;h2 id=&quot;actor-model-and-akka&quot;&gt;Actor Model and Akka&lt;/h2&gt;

&lt;p&gt;The &lt;a href=&quot;https://en.wikipedia.org/wiki/Actor_model&quot;&gt;actor model&lt;/a&gt; is essentially a model of distributed systems where
processes are modeled as “actors”, things that have an inbox for receiving
messages that are processed asynchronously. Actors can only communicate with
other actors via messages, and this allows actors to be scaled outward into
fully distributed systems. A simple way to think of an actor is as a thread
that cannot directly access the state of any other thread and must pass
immutable message objects to other threads to coordinate anything. This avoids
the use of synchronization, locks, and all the extremely difficult to debug
concurrency issues that plague typical concurrent code.&lt;/p&gt;

&lt;p&gt;Actors are lightweight and can be spawned and restarted many times with very
little overhead (far less than an actual thread). Thus, following the ideals
of programming languages like &lt;a href=&quot;https://www.erlang.org/&quot;&gt;Erlang&lt;/a&gt;, actors are small enough to allow
for a more robust form of error handling known as “let it crash”. Forming a
suitable hierarchy of actors, parent actors can automatically restart child
actors when errors occur or perform other fallback strategies. This is
particularly useful for long-running systems where either a full restart is
infeasible or the cost of resources to double up the system for blue green
deployments is prohibitive. It may also be the case that recovering the full
state of the system would take too long, so being able to selectively restart
only single components without affecting the rest of the system is very useful.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://akka.io/&quot;&gt;Akka&lt;/a&gt; is an implementation of this actor model inspired by Erlang.
Akka is written in Scala and provides both a Scala and Java API for actors,
clustering, persistence, distributed data, reactive streams, HTTP, and
integration with various external systems.&lt;/p&gt;

&lt;p&gt;All code samples in this post are from &lt;a href=&quot;https://github.com/jvz/akka-blog-example&quot;&gt;my GitHub repository&lt;/a&gt;. We’ll be
using Scala 2.12 which requires Java 8. Note that if you’re still stuck on
Java 6 or 7, Scala 2.10 and 2.11 both only require Java 6. Code samples may be
adaptable to previous releases of Scala with little or no modifications.
Anyways, let’s jump in to using Scala!&lt;/p&gt;

&lt;h2 id=&quot;blog-api&quot;&gt;Blog API&lt;/h2&gt;

&lt;p&gt;Our API to implement today will be the same as in my &lt;a href=&quot;http://musigma.org/java/2016/11/14/lagom.html&quot;&gt;Lagom tutorial&lt;/a&gt;.
As such, we’ll have a blog data type consisting of three fields: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;title&lt;/code&gt;,
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;author&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;body&lt;/code&gt;. Blog posts are identified by UUIDs. The REST API will
consist of a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GET&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;POST&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PUT&lt;/code&gt; endpoint for looking up, creating, and
updating blog posts respectively. The first thing to implement, then will be
our blog data type.&lt;/p&gt;

&lt;div class=&quot;language-scala highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PostContent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;author&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You may note that there isn’t much to it, and you’re right! Let’s review the
syntax here. First of all, we don’t need to define things as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;public&lt;/code&gt; in Scala
as they’re all assumed to be &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;public&lt;/code&gt; by default. In fact, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;public&lt;/code&gt; is not even
a keyword in Scala. After a class name comes its parameters which can be thought
of as the parameters to its constructor. A class can still have multiple
constructors by defining methods named &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;this&lt;/code&gt; within the class, but we have no
need for that here. Parameters and variables are declared in the opposite order
as Java; in Java, we would say &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;String foo&lt;/code&gt;, whereas in Scala we would say
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;foo: String&lt;/code&gt;. The other keywords used here are &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;final&lt;/code&gt; which makes the class
final as in Java, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;case&lt;/code&gt; which makes this a “case class”.&lt;/p&gt;

&lt;p&gt;Recall that in Java, we can use Lombok to automatically create an all-args
constructor, getters for all fields, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;equals&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;toString&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hashCode&lt;/code&gt;,
builders, withers, and other boilerplate code. In Scala, we can simply add
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;case&lt;/code&gt; to a class to get a lot of that for free. A case class makes all its
class parameters (fields) available with something similar to getters, adds
sensible &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;equals&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hashCode&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;toString&lt;/code&gt; method implementations, and
handles some other Scala-specific features we’ll cover later. Essentially, a
case class can be thought of as a data value class, and we’ll be using it as
such.&lt;/p&gt;

&lt;p&gt;Next, we’ll define a post id class instead of directly using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UUID&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;language-scala highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PostId&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;UUID&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;AnyVal&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;toString&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;toString&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PostId&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;apply&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;PostId&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PostId&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;UUID&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;randomUUID&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;())&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;apply&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;UUID&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;PostId&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PostId&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;implicit&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;postIdDecoder&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Decoder&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;PostId&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;Decoder&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;decodeUUID&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;PostId&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;implicit&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;postIdEncoder&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Encoder&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;PostId&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;Encoder&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;encodeUUID&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;contramap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We have several new keywords to discuss here. First, by adding &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;val&lt;/code&gt; to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;id&lt;/code&gt;
class parameter, this will add a Scala-style getter for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;id&lt;/code&gt; by providing a way
to access the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;id&lt;/code&gt; field directly. Classes use the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;extends&lt;/code&gt; keyword to extend
other classes or implement traits (interfaces). This &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AnyVal&lt;/code&gt; class is a special
class in Scala that works essentially as a boxed value type. This value class can
only contain a single public field, and at compile time, the compiler will attempt
to remove all indirect access of that field and replace it with direct access.
Thus, we can create a wrapper type for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UUID&lt;/code&gt; without sacrificing performance
at runtime.&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;override&lt;/code&gt; keyword is used just like the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Override&lt;/code&gt; annotation in Java:
while not required, if the keyword is present but the method doesn’t actually
override anything, this will cause a compiler error. This can be useful for
catching typos or unknown API changes. The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;def&lt;/code&gt; keyword is used for defining
methods. When a method has zero arguments, the parenthesis are optional. The
conventional style here is that parentheses are omitted in pure functions
while they remain in side-effecting functions. As with variables, the types of
parameters and method return values come afterwards.&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;object&lt;/code&gt; keyword here defines what is essentially a singleton instance of a
class of the same name. Scala does not have &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;static&lt;/code&gt;, but these objects provide
an equivalent feature. When an object is named the same as a class, it is
called the class’s “companion object”. A class and its companion object have
equal access to internals of each other similar to how static and non-static
members work in Java.&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apply&lt;/code&gt; methods here are used as factory methods to create &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PostId&lt;/code&gt;
instances. The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apply&lt;/code&gt; method has special meaning in Scala: we can omit the
name of the method when calling it. For example, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PostId.apply()&lt;/code&gt; is the same
as calling just &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PostId()&lt;/code&gt;. As can be seen here, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;return&lt;/code&gt; keyword is optional
when the last line of executing code of a method is the return value.&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;implicit&lt;/code&gt; keyword is used for a lot of things in Scala, and in this case,
an implicit variable is one that is available for implicit parameters to methods
that take them. We’ll be using it here to fill in custom encoder and decoder
implementations for our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PostId&lt;/code&gt; class to ensure that the JSON marshaller and
unmarshaller do not think that this is a complex object.&lt;/p&gt;

&lt;p&gt;The square bracket syntax, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Decoder[PostId]&lt;/code&gt;, is Scala’s generic type parameter
syntax. The equivalent in Java would be &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Decoder&amp;lt;PostId&amp;gt;&lt;/code&gt;. Finally, these codecs
are derived from existing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UUID&lt;/code&gt; codecs, so we map and contramap them using
some lambda functions. Both of these lambda functions use anonymous parameter
syntax, both of which can be expanded to:&lt;/p&gt;

&lt;div class=&quot;language-scala highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;Decoder&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;decodeUUID&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PostId&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;Encoder&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;encodeUUID&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;contramap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;postId&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;postId&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Next, let’s stub out a service interface. We’ll fill in the details later.&lt;/p&gt;

&lt;div class=&quot;language-scala highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;trait&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;BlogService&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;getPost&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;PostId&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;addPost&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;content&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;PostContent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;updatePost&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;PostId&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;content&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;PostContent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trait&lt;/code&gt; keyword is very similar to an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;interface&lt;/code&gt; in Java. In fact, as
written, this trait will compile directly into an interface. Traits are far
more powerful than Java interfaces, so this isn’t always possible, but Java
has started making interfaces more powerful starting with default methods in
Java 8, so these two concepts may converge one day. The main difference between
a trait and a class in Scala is that a trait cannot have its own parameters,
but a class can extend (or mix in) multiple traits. So far, this is still
very similar to Java. However, traits can contain fields, private and
protected members, default implementations, and even constraints on what the
concrete implementing class must conform to. We’ll omit the return types of the
methods for now, but we’ll return to add them later once we’ve defined them.&lt;/p&gt;

&lt;h2 id=&quot;blog-entity&quot;&gt;Blog Entity&lt;/h2&gt;

&lt;p&gt;Next, let’s dive down to the entity level. Recall that in Lagom, a persistence
entity is associated with three related things: commands, events, and state.
Following a similar pattern here, we’ll implement a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BlogEntity&lt;/code&gt; class by
first defining our commands, events, and state classes.&lt;/p&gt;

&lt;div class=&quot;language-scala highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;BlogEntity&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;sealed&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;trait&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;BlogCommand&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;GetPost&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;PostId&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;BlogCommand&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;AddPost&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;content&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;PostContent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;BlogCommand&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;UpdatePost&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;PostId&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;content&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;PostContent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;BlogCommand&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;sealed&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;trait&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;BlogEvent&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;PostId&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;content&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;PostContent&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PostAdded&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;PostId&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;content&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;PostContent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;BlogEvent&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PostUpdated&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;PostId&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;content&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;PostContent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;BlogEvent&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PostNotFound&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;PostId&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;RuntimeException&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Blog post not found with id $id&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;MaybePost&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;+A&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Either&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;PostNotFound&lt;/span&gt;, &lt;span class=&quot;kt&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;BlogState&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;posts&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Map&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;PostId&lt;/span&gt;, &lt;span class=&quot;kt&quot;&gt;PostContent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;apply&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;PostId&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;MaybePost&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;PostContent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt;
      &lt;span class=&quot;nv&quot;&gt;posts&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;toRight&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;PostNotFound&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;BlogEvent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;BlogState&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;
      &lt;span class=&quot;nc&quot;&gt;BlogState&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;posts&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;updated&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;content&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;BlogState&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;apply&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;BlogState&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;
      &lt;span class=&quot;nc&quot;&gt;BlogState&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;Map&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;empty&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The first new thing of note here is the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sealed&lt;/code&gt; keyword. A trait or class
marked &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sealed&lt;/code&gt; indicates that all subclasses of that trait or class must be
contained in the same file. This makes pattern matching on these types of
classes easier as it aids the compiler in detecting missing patterns checked
by the programmer and can help prevent certain classes of bugs.&lt;/p&gt;

&lt;p&gt;We have three types of commands: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GetPost&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AddPost&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UpdatePost&lt;/code&gt;. These
are all rather trivial and mirror the same commands from the Lagom version of
this microservice.&lt;/p&gt;

&lt;p&gt;Next, we defined a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sealed trait BlogEvent&lt;/code&gt; which contains two public values.
The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;val&lt;/code&gt; keyword is similar to marking a variable as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;final&lt;/code&gt; in Java, while
the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;var&lt;/code&gt; keyword is similar to a normal variable in Java. When defining a
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;val&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;var&lt;/code&gt; on a class, this is technically creating a field in the class
along with a getter-style method (named the same as the field) and a setter-style
method if using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;var&lt;/code&gt;. What this effectively does is allows the variable to be
accessed as if it were a public field of the class. Note that by not giving a
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;val&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;var&lt;/code&gt; an initial value, this makes them abstract. It may be worth
noting here that semicolons are optional in Scala and tend to be omitted;
otherwise, this would have been our first usage of them.&lt;/p&gt;

&lt;p&gt;For our events, we only care about commands, not queries (in the CQRS sense of
the words, not a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BlogCommand&lt;/code&gt; type of command), so we defined two events:
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PostAdded&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PostUpdated&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Next, we abuse the case class feature to implement our own exception class,
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PostNotFound&lt;/code&gt;, to obtain some handy features for free. This demonstrates
the syntax to call the constructor of a superclass as well. There is one
other nifty feature in use here: interpolated strings. Scala provides an
extensible feature to make interpolated strings which are expanded out and
filled in at compile time. The syntax &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;s&quot;Hello, $foo!&quot;&lt;/code&gt; would be
essentially the same as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;Hello, &quot; + foo + &quot;!&quot;&lt;/code&gt; at compile time. We could use
more advanced expressions inside the string by wrapping the variable name
in curly braces, so for instance, we could say &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;s&quot;Hello, ${foo.capitalize}!&quot;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;After that, we define a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;type&lt;/code&gt; alias named &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MaybePost[+A]&lt;/code&gt;. There are a few
things going on in this, so let’s break it down. First of all, Scala allows us
to define type aliases which can be used to alias a larger type into something
more readable, or it can even be used simply to rename types. In the generic
type parameter, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+A&lt;/code&gt; bit indicates that, if &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;B&lt;/code&gt; is a subclass of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;A&lt;/code&gt;, then
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MaybePost[B]&lt;/code&gt; is a subclass of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MaybePost[A]&lt;/code&gt;. This is called a covariant type
parameter. If we omitted the plus, we’d have an invariant type parameter which
means that regardless of how &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;A&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;B&lt;/code&gt; are related, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Foo[A]&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Foo[B]&lt;/code&gt;
would not be related. There is a third type of variance available called
contravariant type parameters which use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-A&lt;/code&gt; and imply the opposite subclass
relationship between &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Foo[A]&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Foo[B]&lt;/code&gt; from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;A&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;B&lt;/code&gt;. We alias this
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MaybePost&lt;/code&gt; from the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Either[A, B]&lt;/code&gt; type from the Scala standard library which
is a type that can be either a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Left&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Right&lt;/code&gt; value containing a type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;A&lt;/code&gt; or
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;B&lt;/code&gt; respectively.  This class is normally used as a more powerful form of
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Option&lt;/code&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Optional&lt;/code&gt; in Java) where the left side type is an exception type and
the right side type is the success value type.&lt;/p&gt;

&lt;p&gt;Finally, we come to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BlogState&lt;/code&gt; class and companion object. We have some
strange names chosen here for methods: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apply&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+&lt;/code&gt;. Wait, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+&lt;/code&gt;? Yup! In
Scala, you can name a method pretty much anything you like. Scala will translate
the names into valid Java identifiers at compile time. As explained above, the
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apply&lt;/code&gt; method is treated specially by allowing the word &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apply&lt;/code&gt; to be omitted
when calling the method. There are several other special method names that allow
for syntax features in Scala such as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;update&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;unapply&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;flatMap&lt;/code&gt;,
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;filter&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;withFilter&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;foreach&lt;/code&gt;, and methods named after mathematical
operators such as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;*&lt;/code&gt;.  We will not be covering most of them, but
it’s worth noting their names.  One other thing to note here is that the default
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Map[K, V]&lt;/code&gt; type used here is an immutable hash map, and in keeping with the
spirit of immutability, we will be updating our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BlogState&lt;/code&gt; by returning a new
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BlogState&lt;/code&gt; with a new &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Map&lt;/code&gt; which contains the old map plus an additional item.
Thus, the use of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;posts.updated(key, value)&lt;/code&gt; returns a new map with the addition
or modification of the provided key value mapping.&lt;/p&gt;

&lt;p&gt;With all that out of the way, let’s move on to the entity implementation.&lt;/p&gt;

&lt;div class=&quot;language-scala highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;BlogEntity&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PersistentActor&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;BlogEntity._&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;context._&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;BlogState&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;persistenceId&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;blog&quot;&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;receiveCommand&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Receive&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;GetPost&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nf&quot;&gt;sender&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;AddPost&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;content&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nf&quot;&gt;handleEvent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;PostAdded&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;PostId&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;content&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pipeTo&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;sender&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;UpdatePost&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;content&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nf&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;match&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;response&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;@&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Left&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;sender&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;response&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Right&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt;
          &lt;span class=&quot;nf&quot;&gt;handleEvent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;PostUpdated&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;content&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pipeTo&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;sender&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;
          &lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;handleEvent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;E&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;&amp;lt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;BlogEvent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;E&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Future&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;E&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Promise&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;E&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;persist&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;event&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nv&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;success&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;
      &lt;span class=&quot;nv&quot;&gt;system&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;eventStream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;publish&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;nf&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lastSequenceNr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lastSequenceNr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;saveSnapshot&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;future&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;receiveRecover&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Receive&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;BlogEvent&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SnapshotOffer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;snapshot&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;BlogState&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;snapshot&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Note that we need to extend &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PersistentActor&lt;/code&gt; to use akka-persistence, the
mechanism we’ll be using to save blog events to disk. This class is dense with
syntax we haven’t covered yet, so let’s go over it all. First of all, note how
we imported all the members of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BlogEntity&lt;/code&gt; object. The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_&lt;/code&gt; in the import is
equivalent to using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;*&lt;/code&gt; in Java (Scala doesn’t use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;*&lt;/code&gt; here because &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;*&lt;/code&gt; is a
valid class and method name in Scala). Note that we don’t ever use an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;import
static&lt;/code&gt; like in Java as Scala does not have static things (though it can import
static things from Java without having to specify it’s a static import). Since
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BlogEntity&lt;/code&gt; is already in scope, we did not have to specify the full package
name preceding it in the import. We also import the members of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;context&lt;/code&gt;
variable which is defined in a superclass. We do this to gain access to some
implicitly available variables used in the asynchronous code below.&lt;/p&gt;

&lt;p&gt;Next, note that we were able to construct a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BlogState&lt;/code&gt; object by omitting
the keyword &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;new&lt;/code&gt; because we defined a method named &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apply&lt;/code&gt;. Thus, we are
actually calling &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BlogState.apply()&lt;/code&gt; here. In the next line, we override an
abstract method to identify this aggregate root for persistence purposes. An
alternative name here may be the fully qualified class name.&lt;/p&gt;

&lt;p&gt;Another neat syntax to note here is that in Scala, a zero-arg method can omit
its parenthesis. This feature allows for a lot of neat things such as allowing a
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;val&lt;/code&gt; to override an arg-less &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;def&lt;/code&gt; in the parent class. Our next method,
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;receiveCommand&lt;/code&gt;, returns an Akka type called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Receive&lt;/code&gt; which is a type alias
for a lambda function that takes a single parameter of type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Any&lt;/code&gt; and returns
nothing. This warrants a quick overview of some of the standard Scala types.
The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Any&lt;/code&gt; class is the root class, and from there are &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AnyVal&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AnyRef&lt;/code&gt;.
The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AnyVal&lt;/code&gt; class is for types like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Int&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Boolean&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Double&lt;/code&gt;, etc., along
with user-defined value classes as explained in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PostId&lt;/code&gt; description, while
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AnyRef&lt;/code&gt; is equivalent to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Object&lt;/code&gt; in Java. There is also the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Unit&lt;/code&gt; class which
is equivalent to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;void&lt;/code&gt; in Java. The difference here is that technically, all
methods must return a value in Scala, so for an empty return type, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Unit&lt;/code&gt; can be
used which has only one possible value: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;()&lt;/code&gt;. There are also two bottom-most
types in Scala: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Nothing&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Null&lt;/code&gt;. The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Nothing&lt;/code&gt; type is generally used as a
return value for a method that does not actually return (e.g., it always throws
an exception). The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Null&lt;/code&gt; type is the type of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;null&lt;/code&gt; reference which is a
subtype of everything. Try not to confuse these with the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;None&lt;/code&gt; type of an empty
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Option&lt;/code&gt; or the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Nil&lt;/code&gt; instance of an empty &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;List&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The receive method of an actor tends to be implemented as a pattern match expression.
A pattern match takes the form of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;foo match { case a =&amp;gt; ...; case b =&amp;gt; ...; ... }&lt;/code&gt;,
and this is a very powerful feature used pervasively in Scala. In our use case
here, we can omit the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;foo match&lt;/code&gt; part to create a lambda function that matches
an anonymous value. In our cases, we’ll be looking for messages that match our
command types. Since our commands are all case classes, Scala has generated an
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;unapply&lt;/code&gt; method for each which makes the classes destructurable so to say within
pattern match expressions. For example, the expression &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;case GetPost(id) =&amp;gt;&lt;/code&gt;
will match when the matched object is an instance of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GetPost&lt;/code&gt;, and it will
subsequently bind its one field to the new variable named &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;id&lt;/code&gt;. We can use
nested patterns here if we wanted, but that is a more advanced feature.&lt;/p&gt;

&lt;p&gt;In the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GetPost&lt;/code&gt; example, we already have a handy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BlogState.apply(id)&lt;/code&gt; method
available as a lookup to find the content for a given id. This returns our
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MaybePost[PostContent]&lt;/code&gt; type which can be used to determine whether or not we
got the content. Using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sender()&lt;/code&gt; method, we can send a message back to the
actor that sent the initial message. The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;!&lt;/code&gt; method used here is an alias for
the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tell&lt;/code&gt; method. Note that we omitted the dot and parenthesis here by using
the infix notatation syntax of calling methods. This syntax allows, for example,
the method call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1.+(1)&lt;/code&gt; to be written as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1 + 1&lt;/code&gt;. This is particularly useful
for methods that have an operator syntax like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+&lt;/code&gt;, but it can also be useful in
functional programming contexts as well. Thus, our line of code is equivalent to
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sender().!(state.apply(id))&lt;/code&gt; when expanded out.&lt;/p&gt;

&lt;p&gt;Next, to handle the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AddPost&lt;/code&gt; command, we create a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PostAdded&lt;/code&gt; event with a new
id and its content. The result of handling the event is a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Future[PostAdded]&lt;/code&gt;,
so we use the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pipeTo&lt;/code&gt; pattern in Akka to send the result of that future back to
the sender. As mentioned for the infix method call syntax, this line can be
equivalently written as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;handleEvent(PostAdded(PostId(), content)).pipeTo(sender())&lt;/code&gt;.
We’ll be reusing the event handling logic in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UpdatePost&lt;/code&gt;, so we’ll come back to
that. The next line contains a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Unit&lt;/code&gt; to return explicitly.  We do this because
we are using a set of strict compiler flags which would make discarding a return
value an error otherwise. Note that this is not required in the default Scala
compiler settings, but we’re trying to stick to high quality Scala code.&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UpdatePost&lt;/code&gt; command requires a bit more work than &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AddPost&lt;/code&gt; did. This is
to validate that the id provided already exists. Thus, first we look up in our
state for an existing post. If it does not exist, we’ll get a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Left(error)&lt;/code&gt;
value with some exception &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;error&lt;/code&gt;; if it does exist, we’ll get a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Right(content)&lt;/code&gt;
value with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;content&lt;/code&gt; being the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PostContent&lt;/code&gt; value. Thus, we combine this with
a pattern match to check for both types. The syntax, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;case response @ Left(_)&lt;/code&gt;,
uses two pattern types concurrently: binding the matched expression to the new
variable named &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;response&lt;/code&gt;, and matching that it is a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Left&lt;/code&gt; type with any
content (the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_&lt;/code&gt; is a wildcard match in this context as a throwaway variable).
If the post doesn’t exist, we send back the error. Otherwise, we create a
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PostUpdated&lt;/code&gt; event, handle it, and send it back.&lt;/p&gt;

&lt;p&gt;Next, we look at handling the event. Let’s break down the new syntax. The
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[E &amp;lt;: BlogEvent]&lt;/code&gt; bit is a generic method type parameter, where &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;:&lt;/code&gt; is
equivalent to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;extends&lt;/code&gt; in a Java generic method. Conversely, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;gt;:&lt;/code&gt; generic
syntax would be equivalent to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;super&lt;/code&gt; in Java. For example, this method
may be written in Java as
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;private &amp;lt;E extends BlogEvent&amp;gt; Future&amp;lt;E&amp;gt; handleEvent(E event)&lt;/code&gt;. The other
syntax of note here is the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;e: =&amp;gt; E&lt;/code&gt; parameter. This is similar to a zero-arg
lambda function, but when called as such, does not require being wrapped in a
lambda. Had we used &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;e: () =&amp;gt; E&lt;/code&gt; as the parameter instead, then we would have
had to call the method as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;handleEvent(() =&amp;gt; PostAdded(...))&lt;/code&gt; instead.&lt;/p&gt;

&lt;p&gt;In order to create a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Future&lt;/code&gt; here, we’re using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Promise&lt;/code&gt; class. A Scala
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Promise&lt;/code&gt; works rather similarly to promises in JavaScript and other languages.
To handle the event, we call the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;persist&lt;/code&gt; method defined in the
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PersistentActor&lt;/code&gt; superclass. We use another new syntax here where a method
with a single lambda function parameter can be replaced with curly braces.
A lambda function can always be surrounded in curly braces, so this syntax
is taking advantage of some infix method call syntax features.
This syntax is rather similar to how &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Closure&lt;/code&gt;s work in Groovy. The lambda
provided to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;persist&lt;/code&gt; here is called after the event has been successfully
persisted. In this, we complete our promise which is returned as a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Future&lt;/code&gt;
to the caller. After that, we call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;state += event&lt;/code&gt;. Since we never defined a
method called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+=&lt;/code&gt; on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BlogState&lt;/code&gt;, Scala sees that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;state&lt;/code&gt; is a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;var&lt;/code&gt;, so it
can rewrite that expression into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;state = state + event&lt;/code&gt;. Since we do in fact
have such a method available, the whole expression is equivalent to calling
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;state = state.+(event)&lt;/code&gt;. After that, we publish the event to the system
event stream which can be used as an application-wide event bus. Finally, we
add in a periodic call to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;saveSnapshot&lt;/code&gt; every 1000 events which will allow
us to restart and recover the state a lot faster than rereading the entire
event log.&lt;/p&gt;

&lt;p&gt;The last bit of code we needed to implement in this actor was the
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;receiveRecover&lt;/code&gt; method which is used to recover the latest snapshot on
startup if available. We can populate this actor’s state directly from the
snapshot, and then subsequently handle all the events that weren’t included
in that snapshot. The only new syntax used here is the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;case event: BlogEvent&lt;/code&gt;
pattern match syntax which matches when the object is an instance of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BlogEvent&lt;/code&gt;
and binds it to the new variable &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;event&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;blog-service&quot;&gt;Blog Service&lt;/h2&gt;

&lt;p&gt;Now that we’ve written our entity actor, we can fill in the stub &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BlogService&lt;/code&gt;
trait defined earlier.&lt;/p&gt;

&lt;div class=&quot;language-scala highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;trait&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;BlogService&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;AkkaConfiguration&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;BlogEntity._&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;blogEntity&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;actorRefFactory&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;actorOf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Props&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;BlogEntity&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;])&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;getPost&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;PostId&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Future&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;MaybePost&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;PostContent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;blogEntity&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;GetPost&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)).&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;mapTo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;MaybePost&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;PostContent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]]&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;addPost&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;content&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;PostContent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Future&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;PostAdded&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;blogEntity&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;AddPost&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;content&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)).&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;mapTo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;PostAdded&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;updatePost&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;PostId&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;content&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;PostContent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Future&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;MaybePost&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;PostUpdated&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;blogEntity&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;UpdatePost&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;content&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)).&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;mapTo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;MaybePost&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;PostUpdated&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]]&lt;/span&gt;

&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AkkaConfiguration&lt;/code&gt; trait we mix in here is a trait we defined for easy
access to Akka-related objects such as an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ActorRefFactory&lt;/code&gt; to create actors,
a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Materializer&lt;/code&gt; which is used to run Akka Streams, an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ExecutionContext&lt;/code&gt; which
is used for coordinating &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Future&lt;/code&gt;s and other asynchronous functionality, and a
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Timeout&lt;/code&gt; which is used for a default timeout value when making request/reply-style
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ask&lt;/code&gt; calls to actors.&lt;/p&gt;

&lt;p&gt;As noted before, we can import all the members of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BlogEntity&lt;/code&gt; companion
object here. It’s also worth noting that Scala allows you to import things to
whatever scope you want, so we can limit our imports to the closest spot it is
actually used.&lt;/p&gt;

&lt;p&gt;To use our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BlogEntity&lt;/code&gt; actor, we need to spawn it first. Using our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ActorRefFactory&lt;/code&gt;,
we can spawn a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BlogEntity&lt;/code&gt; using the default &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Props&lt;/code&gt; of a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BlogEntity&lt;/code&gt;. Spawning
the actor will create it and start it asynchronously, returning an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ActorRef&lt;/code&gt;
instance which can be used to interact with the actor. Since actors can be restarted,
replaced, or even located on completely different processes or machines, we never
directly access an actor’s instance and instead use its &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ActorRef&lt;/code&gt; to send messages,
watch it, etc. Since actors are written in a rather type-unsafe fashion, we’re wrapping
the actor’s messaging API into a typesafe trait.&lt;/p&gt;

&lt;p&gt;In order to receive responses from our actor when sending a message, we must do
so asynchronously. Thus, our API returns futures. We utilize the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ask&lt;/code&gt; pattern
to send a message to the actor and wait for a response message. Since actors
are not typed, the message response comes back as a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Future[Any]&lt;/code&gt;, so we use the
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mapTo[A]&lt;/code&gt; method on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Future&lt;/code&gt; to verify it matches our expected type and cast it.
Other than that, this trait is rather self-explanatory based on our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BlogEntity&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;blog-rest-api&quot;&gt;Blog REST API&lt;/h2&gt;

&lt;p&gt;Next up is defining our REST API. We’ll be using the high level Akka HTTP
route DSL for this. Using our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BlogService&lt;/code&gt; trait, we’ll mix that in to
another trait to define our API.&lt;/p&gt;

&lt;div class=&quot;language-scala highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;trait&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;BlogRestApi&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;RestApi&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;BlogService&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;route&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Route&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;pathPrefix&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;api&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;blog&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pathEndOrSingleSlash&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;post&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;// POST /api/blog/&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;entity&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;as&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;PostContent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;content&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt;
          &lt;span class=&quot;nf&quot;&gt;onSuccess&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;addPost&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;content&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;added&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt;
            &lt;span class=&quot;nf&quot;&gt;complete&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;StatusCodes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;Created&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;added&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;
          &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;~&lt;/span&gt;
      &lt;span class=&quot;nf&quot;&gt;pathPrefix&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;JavaUUID&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;PostId&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;pathEndOrSingleSlash&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;get&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;c1&quot;&gt;// GET /api/blog/:id&lt;/span&gt;
            &lt;span class=&quot;nf&quot;&gt;onSuccess&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;getPost&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
              &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Right&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;content&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;complete&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;StatusCodes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;OK&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;content&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;
              &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Left&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;complete&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;StatusCodes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;NotFound&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
          &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;~&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;put&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;c1&quot;&gt;// PUT /api/blog/:id&lt;/span&gt;
            &lt;span class=&quot;nf&quot;&gt;entity&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;as&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;PostContent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;content&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt;
              &lt;span class=&quot;nf&quot;&gt;onSuccess&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;updatePost&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;content&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Right&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;updated&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;complete&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;StatusCodes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;OK&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;updated&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Left&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;complete&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;StatusCodes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;NotFound&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;
              &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
          &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;There are a couple new syntax features here along with many DSL-specific
things going on. First of all, this introduces the keyword &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;with&lt;/code&gt; which is used
when extending multiple traits. The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RestApi&lt;/code&gt; trait is one we made that mixes
in the Akka HTTP route DSL along with support for marshalling and unmarshalling
our case classes and primitive types into JSON. The only other new syntax here
is the tuple syntax. A tuple is an abstraction of an ordered pair. A tuple can
have two or more values that do not have to be the same type. They are contained
in parenthesis and separated by commas. Due to syntax ambiguity, when sending an
inline tuple to a method, we need to double up on the parenthesis to avoid it
being interpreted as a call to a method with multiple parameters. All other
syntax in this trait are features of the route DSL. For example, URI paths can
be matched using implicit conversions from strings into URI patterns, and those
patterns can be composed with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/&lt;/code&gt; which matches a slash in the URI. Segments
of the path can be extracted into parameters in the lambda function provided
after. We can easily unmarshal request bodies using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;entity(as[A]) { a: A =&amp;gt; ... }&lt;/code&gt;
DSL. Using our service mixin, we can forward these requests and get response
which can be chained back into the HTTP response. Finally, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~&lt;/code&gt; function is
used to chain two routes together into a single route. Far more comprehensive
information about the routing DSL can be found &lt;a href=&quot;http://doc.akka.io/docs/akka-http/current/scala/http/routing-dsl/overview.html&quot;&gt;in the Akka documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In order to run this code, we still need to create our HTTP server and set up
Akka in general. That code is not very interesting in itself and is included in
the &lt;a href=&quot;https://github.com/jvz/akka-blog-example&quot;&gt;code samples&lt;/a&gt;. While this post only scratches the surface of Scala,
it provides a broad overview of various Scala-specific syntax features that
really differentiate it from Java. There is one other feature that has only
been mentioned by name so far, and that is &lt;em&gt;implicits&lt;/em&gt;. Implicits are a
powerful feature specific to Scala that helps reduce boilerplate typing in
various scenarios. Implicits can generally be used for a few different things:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Implicit values can be used to provide parameters to functions or class
constructors without being explicitly written as long as it’s in scope. This
is useful for passing around parameters that are needed by tons of methods
such as the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ExecutionContext&lt;/code&gt; object mentioned above.&lt;/li&gt;
  &lt;li&gt;Implicit parameters can be used to automatically be filled in by an
implicit value that is in scope so that the parameter doesn’t have to be typed
out over and over again.&lt;/li&gt;
  &lt;li&gt;Implicit methods can be used to convert from one type to another when the
original type is not compatible where it is used. For example, this feature
is used to automatically convert an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Int&lt;/code&gt; to a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Long&lt;/code&gt; when passed to a
method parameter that takes a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Long&lt;/code&gt;. This can be a very dangerous feature
when misused.&lt;/li&gt;
  &lt;li&gt;Implicit classes can be used to provide extension methods to an existing API.
This is used to add methods to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;java.lang.String&lt;/code&gt;, for example, which cannot
normally be done as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;String&lt;/code&gt; is a final class that cannot be extended.
Extension methods provide a compositional way to extend APIs in a type safe
fashion without having to explicitly wrap the API everywhere an extension
method is desired.&lt;/li&gt;
  &lt;li&gt;Implicits can be used to provide type classes to Scala, a feature common to
functional programming languages. Type classes are used very seldomly in
object oriented programming languages. An example of a simple type class in
Java is the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Comparable&amp;lt;T&amp;gt;&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Enum&amp;lt;E&amp;gt;&lt;/code&gt; interface.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Overall, Scala is a fantastic programming language with a simple core set of
features and tons of extensibility. Staying true to the “scalable language”
concept behind its name, Scala works well in small scripts all the way up to
large distributed applications. The Akka framework provides the building
blocks for writing distributed systems using event sourcing and CQRS, while
Scala itself enables much more concise, readable DSLs and removal of verbose
boilerplate common to many Java frameworks. Akka is far more flexible than its
opinionated cousin, Lagom, and it works well with various technologies common
to event sourced systems like &lt;a href=&quot;https://kafka.apache.org/&quot;&gt;Kafka&lt;/a&gt; and &lt;a href=&quot;https://cassandra.apache.org/&quot;&gt;Cassandra&lt;/a&gt;.&lt;/p&gt;

</description>
        <pubDate>Mon, 03 Jul 2017 09:00:00 -0500</pubDate>
        <link>https://musigma.blog/2017/07/03/akka-cqrs.html</link>
        <guid isPermaLink="true">https://musigma.blog/2017/07/03/akka-cqrs.html</guid>
        
        
        <category>scala</category>
        
      </item>
    
      <item>
        <title>Why Use a Microservice Architecture</title>
        <description>&lt;p&gt;Microservice architecture is the latest fad in software development, and as such
it comes with numerous conflicting definitions. To help clear up this confusion,
we’ll discuss what microservices are, how they relate to older development
architecture patterns, and why and when they are useful. In the enterprise
world, &lt;a href=&quot;https://en.wikipedia.org/wiki/Service-oriented_architecture&quot;&gt;service oriented architecture&lt;/a&gt; was the established paradigm in
software development, and this typically incorporated the SOAP standard and
web services. Some concepts from SOA, such as loose coupling and &lt;a href=&quot;https://en.wikipedia.org/wiki/Domain-driven_design&quot;&gt;domain-driven
design&lt;/a&gt;, translate well into microservices, but other concepts such as
global transactions and global data consistency do not scale well in such
an architecture. One major pain point in SOA that microservices helps to
address are monolithic applications and the pains behind deploying new
versions of them. Another problem stems from incompatible implementations
of SOAP being widely used which tend to nullify the benefits of using a
web services standard for loose coupling. Expensive proprietary software is
also a large downside to typical SOA style development which has not yet
reappeared to a similar extent in microservices. Data scalability and
schema evolution are another hard issue in monolithic architectures which tend
to be more manageable in a microservices architecture.&lt;/p&gt;

&lt;p&gt;Due in part to the existing complexity of SOA usage, many developers eventually
found themselves trapped by applications that could not be iterated on rapidly
in an &lt;a href=&quot;http://agilemanifesto.org/&quot;&gt;agile development environment&lt;/a&gt;. Deployments required complicated
build systems easily described as “magic”, and entire teams would need to be
formed just to ensure a single deployment worked properly. This sort of process
worked well enough in the waterfall development days (or at least as well as
waterfall development worked itself), but as the software industry migrated to
agile development methodologies, the friction of maintenance, deployment, and
onboarding new developers into overly complicated monoliths became untenable.&lt;/p&gt;

&lt;p&gt;At this point, some developers began experimenting with what would later be
named microservices. Standardizing on the ubiquitous web standards, individual
services were written to communicate over HTTP using XML and later &lt;a href=&quot;http://www.json.org/&quot;&gt;JSON&lt;/a&gt;,
relying on extensive library support for these standards. This borrowed the
concept of &lt;a href=&quot;https://en.wikipedia.org/wiki/Representational_state_transfer&quot;&gt;RESTful APIs&lt;/a&gt;, where services provided resources which were
identifiable by &lt;a href=&quot;https://en.wikipedia.org/wiki/Uniform_Resource_Identifier&quot;&gt;URIs&lt;/a&gt;, and each resource defined &lt;a href=&quot;https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods&quot;&gt;HTTP
methods&lt;/a&gt; (or verbs) that could be performed on those resources.
There are other ideas on how to implement microservices besides via HTTP (e.g.,
&lt;a href=&quot;https://www.osgi.org/&quot;&gt;OSGi&lt;/a&gt; in the Java world provides their own style of microservices which
can communicate in-memory, but are still physically separated by the JVM via
separate &lt;a href=&quot;https://docs.oracle.com/javase/8/docs/api/java/lang/ClassLoader.html&quot;&gt;ClassLoaders&lt;/a&gt;), but the general concept behind them all
is that each service requires physical separation and must communicate over
well-defined boundaries such as through a network socket. This provides several
advantages over traditional monoliths, many of which we will discuss.&lt;/p&gt;

&lt;p&gt;It’s important to note before we go into detail that in general, microservice
architecture provides far more benefits the larger an organization becomes. A
small enterprise with only a single team or two working on these services may
find it overly complicated to break their systems into microservices,
particularly with purely &lt;a href=&quot;https://en.wikipedia.org/wiki/Create,_read,_update_and_delete&quot;&gt;CRUD-style applications&lt;/a&gt;. Many of the advantages
discussed here can also be extended into small monolithic applications with
proper developer discipline towards modularity, &lt;a href=&quot;https://en.wikipedia.org/wiki/Loose_coupling&quot;&gt;loose coupling of
functionality&lt;/a&gt;, and &lt;a href=&quot;https://en.wikipedia.org/wiki/Cohesion_(computer_science)&quot;&gt;high cohesion&lt;/a&gt; of individual units of code
as organized into modules.&lt;/p&gt;

&lt;p&gt;First of all, breaking up a monolith into smaller pieces simplifies the
individual build and deployment systems of each microservice. While the overall
complexity of the system may increase, the savings in developer time and pain is
well worth the cost. Each individual microservice is much simpler to understand
in isolation, and in theory, this decreases the amount of time it takes to
onboard new developers into a system. This does generally require the adoption
of &lt;a href=&quot;https://en.wikipedia.org/wiki/DevOps&quot;&gt;devops&lt;/a&gt; into an organization, but this is generally considered a
good thing to adopt regardless.&lt;/p&gt;

&lt;p&gt;By breaking into microservices, another advantage is provided in that each
service may have different technologies that are more appropriate to use
depending on the domain in question. For example, simple CRUD microservices may
be more rapidly developed using a language like &lt;a href=&quot;https://kotlinlang.org/&quot;&gt;Kotlin&lt;/a&gt;, &lt;a href=&quot;https://www.ruby-lang.org/en/&quot;&gt;Ruby&lt;/a&gt;,
or even &lt;a href=&quot;https://nodejs.org/en/&quot;&gt;JavaScript&lt;/a&gt;, while more complicated asynchronous message-oriented
services may be more appropriately developed in &lt;a href=&quot;https://www.scala-lang.org/&quot;&gt;Scala&lt;/a&gt;,
&lt;a href=&quot;http://elixir-lang.org/&quot;&gt;Elixir&lt;/a&gt;, or another functional programming language. Choosing
appropriate frameworks for each service is also an important advantage as
developers no longer have to rely on overly broad enterprise style frameworks.
This allows more room for experimentation with newer technology and design
patterns which can then be shared with other developers to help improve the
overall system.&lt;/p&gt;

&lt;p&gt;Choosing appropriate programming languages, libraries, and frameworks are only
one part of the many choices required for each microservice. Since most
applications contain some sort of persistent state, most applications require
some sort of database or general persistent storage area. When separating an
application into microservices, each microservice can and should have ownership
over the data it needs while providing access to it through its own public APIs.
A common problem in many actual implementations of microservices is sharing
databases between services. Not only does this tightly couple those applications
in regards to scaling, schema management, and other administrative tasks, but it
also destroys a lot of the advantages of using a microservice architecture.
Though in practice, a single database cluster can be used to serve multiple
microservices, as long as each microservice uses its own database within that
cluster, the choice to physically separate that database is much easier than
when multiple applications share a database, even if they use their own tables.&lt;/p&gt;

&lt;p&gt;The choice of database, cache, and other infrastructural addons to applications
should be left to the individual microservices, though in order to make the
operations sides of things at least somewhat practical, it is generally a good
idea to limit the number of different choices to use. For example, an
organization may wish to standardize on a single type of &lt;a href=&quot;https://www.postgresql.org/&quot;&gt;relational
database&lt;/a&gt;, a single &lt;a href=&quot;https://cassandra.apache.org/&quot;&gt;NoSQL database&lt;/a&gt;, a single &lt;a href=&quot;https://www.influxdata.com/&quot;&gt;time-series
database&lt;/a&gt;, a single &lt;a href=&quot;https://redis.io/&quot;&gt;distributed cache&lt;/a&gt;, etc. Of course, this
should remain more of a guideline rather than a hard rule in order to prevent
wasted effort fighting the tools rather than using what works best in each use
case.&lt;/p&gt;

&lt;p&gt;An important consequence of the physical separation between microservices is
that it makes it more difficult to create minimally cohesive systems. No longer
can code be simply dumped wherever the developer feels like it. Instead,
reusable libraries can be promoted where appropriate, and business logic remains
exactly where it belongs: within its appropriate domain. This can also provide a
prime opportunity for an organization to contribute back to the greater
community via &lt;a href=&quot;https://fsf.org/&quot;&gt;free&lt;/a&gt; and &lt;a href=&quot;https://opensource.org/&quot;&gt;open source software&lt;/a&gt; with said common
library components (incidentally, this is how many &lt;a href=&quot;https://www.apache.org/&quot;&gt;Apache&lt;/a&gt; projects are
started). Developers must make a conscious decision to make remote calls into
other microservices, so this helps enforce loose coupling. Even when developers
attempt to tightly couple microservices, this problem becomes apparent much
sooner rather than at the most inopportune time when a service hits a spike in
usage one day.&lt;/p&gt;

&lt;p&gt;This leads into another advantage of microservices: the ability to independently
build, deploy, and scale these services. As alluded to before, build scripts can
be immensely simplified just due to the decrease in the amount of code needed to
be packaged together into a single distributable artifact. Deployments can be
simplified out of necessity of supporting deployment of numerous microservices,
a goal that is often ignored when deploying monoliths. As for scalability, each
microservice can be independently copied and deployed multiple times across a
cluster of servers. In order for microservices to communicate in this scenario,
a form of client or server &lt;a href=&quot;https://en.wikipedia.org/wiki/Load_balancing_(computing)&quot;&gt;load balancing&lt;/a&gt; is required so that all
instances of each microservice can be evenly used. This relates to the
&lt;a href=&quot;https://en.wikipedia.org/wiki/Elasticity_(cloud_computing)&quot;&gt;elasticity&lt;/a&gt; aspect of &lt;a href=&quot;http://www.reactivemanifesto.org/&quot;&gt;reactive architecture&lt;/a&gt; where
services can scale up and down to meet the physical requirements of that service
at any given time. In general, &lt;a href=&quot;https://en.wikipedia.org/wiki/Scalability#Horizontal_and_vertical_scaling&quot;&gt;scaling horizontally&lt;/a&gt; like this is
simpler than vertical scaling as it doesn’t require specialized or expensive
hardware.&lt;/p&gt;

&lt;p&gt;There are disadvantages to using a microservice architecture, however.
Deployments are a double-edged sword in that they may be simpler on their own,
but in the real world, this doesn’t negate the need for communication with other
teams. Due to limitations in creating RESTful services, maintaining backwards
compatibility can become difficult. Since REST APIs aren’t exactly statically
typed, programmatic refactoring between services is not very possible at this
time. If a backwards incompatible change is required in one service, then its
dependent services may also need to make a deployment at the same time with
updates, and this can slowly lead back into the problems of coordinating
monolithic deployments. Renewed research into RPC-style protocols is working on
solving these types of problems, but there is no silver bullet. Following
&lt;a href=&quot;http://semver.org/&quot;&gt;semantic versioning&lt;/a&gt; strictly can help smooth migrations between
incompatible versions of APIs, but this comes with its own set of limitations
such as requiring to support multiple versions of an API while migrations take
place as well as challenges in migrating or synchronizing different versions of
data stores behind the APIs.&lt;/p&gt;

&lt;p&gt;A common pitfall when breaking a monolith into microservices is splitting them
up too finely. This can be compared to a similar problem of dropping the use of
relational databases purely in exchange for using a NoSQL style database. While
certain domains may find this pattern lends well to use, other domains are
relational in nature and can’t be physically decoupled without introducing
numerous performance problems. If this happens, though, not all is lost! Using a
distributed trace logging library such as &lt;a href=&quot;http://zipkin.io/&quot;&gt;Zipkin&lt;/a&gt; can help identify
tightly coupled microservices and performance issues in context of the physical
flow of data in relation to particular requests or events. After identifying
these parts of a system, they might be rejoined, or the overall architecture of
how data relate to each other can be revisited.&lt;/p&gt;

&lt;p&gt;Another difficult issue in microservice architectures is that the divide between
frontend and backend needs can become ever greater. While the backend attempts
to remain logically pure, frontend needs begin to clash with UI requirements
that join data from numerous microservices into a single page. Supporting such
UIs is generally much simpler in monolithic applications, but the microservice
world is quickly researching and experimenting with alternative approaches to
exposing APIs to applications.&lt;/p&gt;

&lt;p&gt;An approach that stays pure to RESTful APIs is to layer APIs similar to how code
may be organized in a traditional SOA application. The base layer of APIs is
made up of the individual microservices in a system. Another layer of APIs can
be created to compose those microservices into more specific aggregate domains.
A third layer of APIs can then be added on to expose a more stable, UI-centric
API that in essence translates all the underlying domains into synthetic ones
that are relevant to the user experience. This approach is generally more
complicated, of course, but composite API layers, a common design pattern from
enterprise application integration, can be easily implemented using integration
libraries. This also allows for more intelligent caching and prefetching of data
based on common usage patterns.&lt;/p&gt;

&lt;p&gt;Another approach is to use an alternative method to interacting with APIs such
as via &lt;a href=&quot;http://graphql.org/&quot;&gt;GraphQL&lt;/a&gt;. Instead of exposing RESTful APIs directly to the
frontend, a GraphQL endpoint is provided which can be used to specify the data
structures required for a page along with filters on individual fields of the
data, and individual fields can be updated via the same mechanism. While this is
not RESTful in the slightest, it does tend to work better in practice when
defining an ever-changing API that both frontend and backend developers can
agree on. This also makes it much simpler to refactor individual microservices
without disrupting the frontend as the GraphQL server itself performs the data
translations into the requested structure. This also provides an interesting
opportunity for caching and prefetching of data.&lt;/p&gt;

&lt;p&gt;A fully asynchronous approach (which does not currently have a buzzword name
that I’m aware of) may be to fully rely on using message queues and topics from
frontend to backend. This idea was original inspired by a joke I made with old
co-workers about implementing an “everything” API endpoint that would simply
return all the user data we know about so that the frontend developers would
stop requesting more and more combined REST APIs. To take this idea to its
logical conclusion, the only realistic way I could think to implement it without
an extremely long response latency would be to interpret the request as a
message, and then, using websockets, individual pieces of the user’s data could
be sent back as messages which would feed into a Redux-style state management
library which would fill in relevant UI elements as it loaded. Existing
messaging protocols such as &lt;a href=&quot;https://stomp.github.io/&quot;&gt;STOMP&lt;/a&gt; may be useful in implementing this
idea. While this approach of preloading all of a user’s data is not exactly
realistic, the underlying idea of passing messages between the frontend and
backend is interesting and should be considered as another alternative to
traditional synchronous request/response APIs.&lt;/p&gt;

&lt;p&gt;The patterns behind microservice architectures generally parallel the wider
internet. Instead of attempting to abstract away distributed systems, developers
should be embracing them. A monolithic architecture attempts to group everything
together under the illusion that a simpler local programming model will work
out, but in reality, these patterns do not scale well on the web, in big data,
in the internet of things, in artifical intelligence, or really any non-trivial
application of software engineering or computer science. As technology continues
to improve, past assumptions that allowed monolithic software to thrive no
longer work well for most applications. Embrace the horizontal nature of
interconnected technology as this is still only the beginning as distributed
systems of the future continue to spread out.&lt;/p&gt;

</description>
        <pubDate>Thu, 20 Apr 2017 09:00:00 -0500</pubDate>
        <link>https://musigma.blog/2017/04/20/microservices.html</link>
        <guid isPermaLink="true">https://musigma.blog/2017/04/20/microservices.html</guid>
        
        
        <category>architecture</category>
        
      </item>
    
      <item>
        <title>Security in a reactive microservices architecture</title>
        <description>&lt;p&gt;Applying the &lt;a href=&quot;http://www.reactivemanifesto.org/&quot;&gt;reactive manifesto&lt;/a&gt; to microservice architecture is a
difficult problem to solve. One of the more difficult facets of this type of
architecture is designing secure microservices. One common way to secure
microservices is by using &lt;a href=&quot;https://jwt.io/&quot;&gt;JSON web tokens&lt;/a&gt; to securely share
authentication information between services and clients. This standard is
great as the authentication information can be encoded directly in the token
instead of needing to be queried from a central location on every service
call. For example, upon logging in to an SSO page, the authentication service
can create a JWT using a private RSA key to sign the token, and this JWT is
returned to the client. The client can then use this JWT on subsequent calls,
and the integrity of this token can be checked against the public RSA key of
the authentication service. As a neat side effect, microservices do not need
to be proxied by an authentication gateway as the authentication information
is now self-contained and verifiable in a distributed fashion.&lt;/p&gt;

&lt;p&gt;However, security is not only authentication (authn). Security also covers
authorization (authz), the concept of what roles a user has along with what
roles are required to perform a certain action. These user roles can be
encoded into the JWT payload in addition to the authentication info. This way,
each microservice can decode the provided JWT, verify it against the public
key of the auth service, and perform local authorization checks using that
information. Again, no centralized authorization proxy is required!&lt;/p&gt;

&lt;p&gt;Notice, however, that this architectural design seems to rely heavily on
stuffing everything to do with authentication and authorization into a single
microservice. This means that all secured microservices need to share
information with the auth service. Well, in a reactive system, we still want
our microservices to work even when other microservices go down (resiliency).
There is also the concern that a typical auth service tends to become a
catch-all service for any cross-cutting concerns, so in designing a
microservices architecture, it’s important to prevent such a problem from
evolving. To preemptively avoid this mess, it may be useful to separate the
concerns of authentication and authorization entirely.&lt;/p&gt;

&lt;p&gt;One important architectural principle to follow when creating reactive
microservices is that each service should own its own data, and other services
should obtain that data through its public API rather than through a shared
data source. Such a design tends to create the opposite of relational data
normalization as much data tends to be duplicated across the entirety of your
services. In order to make microservices responsive and resilient, this
replication of data is completely alright. In that regard, every microservice
contains resources and actions on those resources, and each of those
resource/action pairs require a particular user role to be present in order
to authorize a user to perform said action on said resource. With that in mind,
it only seems natural that each microservice owns the authorization data for
each authenticated user.&lt;/p&gt;

&lt;p&gt;Following this idea to its logical conclusion, let’s design a reactive
authorization architecture. Recall that a reactive system is responsive,
resilient, elastic, and message driven. This architecture will have to fulfill
all of these assets.&lt;/p&gt;

&lt;h2 id=&quot;message-driven-security&quot;&gt;Message Driven Security&lt;/h2&gt;
&lt;p&gt;Beginning with messages, what would such a system look like? First, we’ll have
an authentication microservice which owns a user login database. This service
will also be responsible for generating OAuth tokens whether they be simple
bearer tokens or JWTs. Upon successful authentication (e.g., via a web form),
this login service will broadcast a login event message to all interested
microservices. For a more concrete implementation, we could use
&lt;a href=&quot;https://kafka.apache.org/&quot;&gt;Apache Kafka&lt;/a&gt; to broadcast this login event. This event would be
encoded as a JWT, and the payload would contain an access token, a login
username, an expiration timestamp, and the requested OAuth scopes that were
authorized by the user. These scopes will later correspond to local
authorization roles in each service.&lt;/p&gt;

&lt;p&gt;This login message might not be entirely necessary, but it does allow each
microservice to preemptively cache relevant user data that is expected to be
queried soon after login. Using a login message would allow for larger
payloads than allowed in an HTTP header which is particularly useful if a
large payload is required. This also reduces message overhead by allowing
JWTs to be accessed by access tokens. In general, the decision on whether to
use JWTs directly or indirectly is similar to the decision in certain programming
languages on whether to pass data by value or by reference to functions. Do keep
in mind that network traffic is more expensive than in-memory traffic, so the
more fine-grained roles you have, the better an idea it is to use JWTs
indirectly.&lt;/p&gt;

&lt;h2 id=&quot;elastic-security&quot;&gt;Elastic Security&lt;/h2&gt;
&lt;p&gt;In this design, every microservice (including the login service) can be
independently scaled in resource usage in response to changes in input rate
to each service. The fact that we will not be relying on this service to
proxy service requests makes this a lot simpler to scale. Since this service
only contains a small amount of data for each user (along with some additional
role/scope configuration), replication of data is also simpler to handle.&lt;/p&gt;

&lt;h2 id=&quot;responsive-security&quot;&gt;Responsive Security&lt;/h2&gt;
&lt;p&gt;As each service will be performing local authorization checks using data
sent to it in each request, the responses generated by each service should
not be affected by complicated authorization checks involving multiple
services or proxies. In order to maintain low overhead in authorization checks,
each service should contain a local cache of authorization information that can
be recomputed from a hard data store on startup (e.g., by utilizing CQRS
patterns). For example, we could use an event log to store the login message
broadcast earlier, and using a read side processor to maintain an in memory
table of unexpired authentication states. &lt;a href=&quot;http://www.lagomframework.com/&quot;&gt;Lagom&lt;/a&gt; provides a great way
to design a Java microservice around this pattern.&lt;/p&gt;

&lt;h2 id=&quot;resilient-security&quot;&gt;Resilient Security&lt;/h2&gt;
&lt;p&gt;This is perhaps the most interesting aspect of this design. The goal here is
to stay responsive even in the face of failure. For example, if the login
service crashes or is overloaded, we still want the rest of our services to
continue working for authenticated users during the time it takes to fix,
restart, or replicate the login service. To do this, the idea is to apply a
sort of inversion of control over a centralized directory server. In a typical
directory service such as LDAP, authorization checks are queried against this
central server. While easier to configure and easier to code against, this
provides a central point of failure. Instead, we’ll design our services to
maintain their own local stores of authorization data. As each service knows
what roles are available for its APIs, these roles can be broadcast in a sort
of “role discovery” mechanism similar to service discovery. This feature can
be used for an administrative service for configuring which users are allowed
which roles. Said service is unneeded for continued operation of every other
service, so we maintain service isolation.&lt;/p&gt;

&lt;p&gt;Each service is now responsible for maintaining two aggregate roots related
to security: user roles and login events. The login events can be uniquely
identified by access tokens for a low overhead message header, or JWTs can be
used directly. While this may sound like a bit of extra coding needed in
every microservice, it can be abstracted into a security provider library
used by every microservice. For example, a custom Spring
&lt;a href=&quot;http://docs.spring.io/spring-security/site/docs/current/apidocs/org/springframework/security/authentication/AuthenticationProvider.html&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AuthenticationProvider&lt;/code&gt;&lt;/a&gt; and &lt;a href=&quot;http://docs.spring.io/spring-security/site/docs/current/apidocs/org/springframework/security/core/Authentication.html&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Authentication&lt;/code&gt;&lt;/a&gt; class
could be written to handle translations of these access tokens. The
&lt;a href=&quot;https://projects.spring.io/spring-security-oauth/docs/oauth2.html&quot;&gt;Spring Security OAuth2&lt;/a&gt; library can be used to implement a lot of
this functionality, though custom code would still be necessary to
configure it to work in this distributed environment.&lt;/p&gt;

&lt;p&gt;When a service calls another service, the access token (or JWT) is provided
in a message header. For example, an HTTP REST call would use an HTTP header,
while a generic message broker would use either a message header or a custom
payload format that allows for encoding message headers. For example, the
&lt;a href=&quot;https://github.com/netflix/msl&quot;&gt;Message Security Layer&lt;/a&gt; framework can be used for securely encoding and
decoding messages in such an architecture.&lt;/p&gt;

&lt;h2 id=&quot;is-this-overkill&quot;&gt;Is This Overkill?&lt;/h2&gt;
&lt;p&gt;This security architecture started as a shower thought on how to implement
security in a reactive microservices architecture. As of the end of 2016, I
have been unable to find any resources online detailing any sort of solution
to this problem. However, it may be unlikely that most applications do not
need this level of resiliency if enough resources are thrown at a secure API
gateway proxy that all microservices are required to communicate with. It is
also possible to allow microservices to trust each other completely and perform
all necessary access control checks at the gateway level. However, any single
microservice can theoretically be compromised, and if all it takes is
anonymous access to one microservice, then that compromised microservice could
be used to perform actions on any other microservice. On the other hand,
proxying all inter-service communication creates a single point of failure,
though some people may find that acceptable.&lt;/p&gt;

&lt;p&gt;Implementing proper security is a difficult problem to solve, especially since
the development process tends to follow the steps of: make it work, make it
right, make it fast, and make it secure. Security is oftentimes an afterthought,
and proper implementation of security also requires a carefully designed
balance between security and usability. If a security architecture is too
cumbersome to use as a user or developer, then users or developers will take
shortcuts around the system or else lose an unacceptable amount of
productivity. In that regard, it is important not to go overboard creating too
many different authorization roles, and it is also important to make use of
your security layer easy to use for developers. By localizing authorization
to each service, this makes it easier to test in isolation as well while
also maintaining some sort of security over time instead of waiting until
integration testing to find out that something is insecure.&lt;/p&gt;

</description>
        <pubDate>Mon, 26 Dec 2016 09:00:00 -0600</pubDate>
        <link>https://musigma.blog/2016/12/26/reactive-security.html</link>
        <guid isPermaLink="true">https://musigma.blog/2016/12/26/reactive-security.html</guid>
        
        
        <category>security</category>
        
      </item>
    
  </channel>
</rss>
