<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Parker Drake</title>
    <description>The latest articles on Forem by Parker Drake (@parkertdrake).</description>
    <link>https://forem.com/parkertdrake</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F462228%2Fce5b4508-6a13-4351-a8c3-ce1f09f01010.png</url>
      <title>Forem: Parker Drake</title>
      <link>https://forem.com/parkertdrake</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/parkertdrake"/>
    <language>en</language>
    <item>
      <title>Spring Boot Validation in Kotlin with @field</title>
      <dc:creator>Parker Drake</dc:creator>
      <pubDate>Fri, 29 Jan 2021 05:05:32 +0000</pubDate>
      <link>https://forem.com/focused_dot_io/spring-boot-validation-in-kotlin-with-field-11om</link>
      <guid>https://forem.com/focused_dot_io/spring-boot-validation-in-kotlin-with-field-11om</guid>
      <description>&lt;p&gt;Recently I found myself needing to validate fields in a Spring Boot controller written in Kotlin. Here's the code I ended up with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Controller&lt;/span&gt;
&lt;span class="nd"&gt;@RequestMapping&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"api/sample"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SampleController&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@PostMapping&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;post&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@Valid&lt;/span&gt; &lt;span class="nd"&gt;@RequestBody&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;SampleType&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;ResponseEntity&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;build&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;data class&lt;/span&gt; &lt;span class="nc"&gt;SampleType&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="n"&gt;field&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nc"&gt;NotBlank&lt;/span&gt; &lt;span class="n"&gt;info&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The key is the &lt;code&gt;@field&lt;/code&gt; in the data class. Without that, Kotlin will apply validation to the constructor parameters by default. This makes sense when you see it, but is less obvious when you're wading through your 10th Java based tutorial on Spring validation. &lt;/p&gt;

&lt;p&gt;Hope this helps!&lt;/p&gt;

</description>
      <category>todayilearned</category>
      <category>kotlin</category>
      <category>spring</category>
    </item>
    <item>
      <title>Debugging Spring Security</title>
      <dc:creator>Parker Drake</dc:creator>
      <pubDate>Thu, 21 Jan 2021 17:53:57 +0000</pubDate>
      <link>https://forem.com/focused_dot_io/debugging-spring-security-703</link>
      <guid>https://forem.com/focused_dot_io/debugging-spring-security-703</guid>
      <description>&lt;p&gt;Spring Security is hard to debug and hard to test. Make your life easier with significantly better log output by using &lt;code&gt;debug = true&lt;/code&gt; in the &lt;code&gt;EnableWebSecurity&lt;/code&gt; annotation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@EnableWebSecurity&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;debug&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CustomConfig&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;WebSecurityConfigurerAdapter&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// your config here&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Don't use this in production!&lt;/p&gt;

</description>
      <category>security</category>
      <category>spring</category>
      <category>bloggolf</category>
      <category>todayilearned</category>
    </item>
    <item>
      <title>CORS Hides Real Bugs</title>
      <dc:creator>Parker Drake</dc:creator>
      <pubDate>Thu, 14 Jan 2021 23:01:30 +0000</pubDate>
      <link>https://forem.com/focused_dot_io/cors-hides-real-bugs-41om</link>
      <guid>https://forem.com/focused_dot_io/cors-hides-real-bugs-41om</guid>
      <description>&lt;h1&gt;
  
  
  Before You Scroll
&lt;/h1&gt;

&lt;p&gt;If you're here because you have a CORS error and you think it's hiding your &lt;em&gt;real&lt;/em&gt; problem, run this command: &lt;/p&gt;

&lt;p&gt;&lt;code&gt;/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --disable-web-security --user-data-dir=~/temp/chrome&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This will start up a Chrome instance with web security turned off, which gets CORS errors out of the way leaving you free to debug your response (assuming the CORS error itself isn't your problem).&lt;/p&gt;

&lt;p&gt;For the bug in this blog, we spent about 4 hours debugging before we ran the above command, and then had the issue fixed an hour later. &lt;/p&gt;

&lt;h1&gt;
  
  
  The Background
&lt;/h1&gt;

&lt;p&gt;Recently my pair and I implemented a feature to process some data with a long running GraphQL mutation (about 2 minutes to return a response on a production size data set). There are better ways to do this without such a long-running request, but this particular action happens rarely enough we just went with this approach and plan to revisit it later. &lt;/p&gt;

&lt;h1&gt;
  
  
  The Bug
&lt;/h1&gt;

&lt;p&gt;On our development machines, everything would run just fine, but in our higher environments, we'd get this lovely error in the browser console: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Access to fetch at '[OUR API]' from origin '[OUR FRONTEND]' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Some other interesting things we noticed: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;There were no errors in the server logs. &lt;/li&gt;
&lt;li&gt;The data processing was actually happening just fine, but the &lt;em&gt;response&lt;/em&gt; was buggy. &lt;/li&gt;
&lt;li&gt;We'd get this message exactly 1 minute after we fired the request, every time.&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  The Fix
&lt;/h1&gt;

&lt;p&gt;Turns out, because of the long running request, we were bumping up against 2 different timeout limits in our stack, one at our ingress controller, and one at our DNS. It makes sense that we couldn't replicate this locally, since in our dev environments there is neither ingress nor DNS. Tweaking those limits was all we needed. Once we launched into a CORS free world using this command: &lt;/p&gt;

&lt;p&gt;&lt;code&gt;/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --disable-web-security --user-data-dir=~/temp/chrome&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Everything started falling into place!&lt;/p&gt;

</description>
      <category>security</category>
      <category>kubernetes</category>
      <category>todayilearned</category>
      <category>bloggolf</category>
    </item>
  </channel>
</rss>
