<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Sts on Geoff Baskwill</title>
    <link>https://geoffbaskwill.ca/tags/sts/</link>
    <description>Geoff Baskwill (Sts)</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-ca</language>
    <lastBuildDate>Mon, 21 Aug 2023 00:00:00 +0000</lastBuildDate>
    
    <atom:link href="https://geoffbaskwill.ca/tags/sts/index.xml" rel="self" type="application/rss+xml" />
    
    
    <item>
      <title>Fun times with AWS STS AssumeRole</title>
      <link>https://geoffbaskwill.ca/posts/sts-assume-role-fun-times/</link>
      <pubDate>Mon, 21 Aug 2023 00:00:00 +0000</pubDate>
      
      <guid>https://geoffbaskwill.ca/posts/sts-assume-role-fun-times/</guid>
      <description>&lt;p&gt;I had an AWS STS &lt;code&gt;AssumeRole&lt;/code&gt; problem today when I was working with the AWS SDK; digging
in it turned out the problem was that &lt;code&gt;AssumeRole&lt;/code&gt; was not actually being invoked. Poking
around I was unsurprised to find that &lt;a href=&#34;https://mastodon.cloud/@ben11kehoe&#34;&gt;@ben11kehoe&lt;/a&gt;
had already clearly stated the problem and GitHub user &lt;a href=&#34;https://github.com/petitout&#34;&gt;petitout&lt;/a&gt; had example Go code
with the right way to do it (&lt;a href=&#34;https://github.com/aws/aws-sdk-go-v2/issues/1382#issuecomment-1010464182&#34;&gt;link&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;Sometimes you need to assume another role than your default execution role in order to
accomplish a task. You can explicitly call STS &lt;code&gt;AssumeRole&lt;/code&gt; yourself and configure the
SDK client to use the returned credentials, or you can configure your SDK client to call
&lt;code&gt;AssumeRole&lt;/code&gt; itself when it needs the credentials. I like the second way because it means
that credentials aren&amp;rsquo;t something that my code has access to.&lt;/p&gt;
&lt;p&gt;Sample code for not assuming a role correctly; you might have stumbled on this the same
way I did:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-go&#34; data-lang=&#34;go&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;// almost certainly not what you want
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;cfg&lt;/span&gt;, &lt;span style=&#34;color:#a6e22e&#34;&gt;_&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;:=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;config&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;LoadDefaultConfig&lt;/span&gt;(
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;	&lt;span style=&#34;color:#a6e22e&#34;&gt;ctx&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;	&lt;span style=&#34;color:#a6e22e&#34;&gt;config&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;WithAssumeRoleCredentialOptions&lt;/span&gt;(&lt;span style=&#34;color:#66d9ef&#34;&gt;func&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;aro&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;stscreds&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;AssumeRoleOptions&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;		&lt;span style=&#34;color:#a6e22e&#34;&gt;aro&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;Client&lt;/span&gt; = &lt;span style=&#34;color:#a6e22e&#34;&gt;stsClient&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;		&lt;span style=&#34;color:#a6e22e&#34;&gt;aro&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;RoleARN&lt;/span&gt; = &lt;span style=&#34;color:#a6e22e&#34;&gt;arn&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;		&lt;span style=&#34;color:#a6e22e&#34;&gt;aro&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;RoleSessionName&lt;/span&gt; = &lt;span style=&#34;color:#a6e22e&#34;&gt;sessionName&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;	}),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;A person might think that the code above is right, because it has all kinds of the right
keywords that trigger pattern recognition. The comments on &lt;code&gt;WithAssumeRoleCredentialOptions&lt;/code&gt; read:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-go&#34; data-lang=&#34;go&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;// WithAssumeRoleCredentialOptions is a helper function to construct functional
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;// options that sets a function to use stscreds.AssumeRoleOptions on config&amp;#39;s
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;// LoadOptions. If assume role credentials options is set to nil, the assume
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;// role credentials value will be ignored. If multiple WithAssumeRoleCredentialOptions
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;// calls are made, the last call overrides the previous call values.
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;I &lt;em&gt;think&lt;/em&gt; that what this actually does is &amp;hellip; you know, I truly have no idea, and don&amp;rsquo;t
intend to dig into what exactly this does. It definitely does not do what I thought it did.&lt;/p&gt;
&lt;p&gt;Sample code for probably doing what you want:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-go&#34; data-lang=&#34;go&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;// almost certainly what you do want instead
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;cfg&lt;/span&gt;, &lt;span style=&#34;color:#a6e22e&#34;&gt;err&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;:=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;config&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;LoadDefaultConfig&lt;/span&gt;(
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;	&lt;span style=&#34;color:#a6e22e&#34;&gt;ctx&lt;/span&gt;, 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;	&lt;span style=&#34;color:#a6e22e&#34;&gt;config&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;WithCredentialsProvider&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;stscreds&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;NewAssumeRoleProvider&lt;/span&gt;(
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;		&lt;span style=&#34;color:#a6e22e&#34;&gt;stsClient&lt;/span&gt;, &lt;span style=&#34;color:#a6e22e&#34;&gt;arn&lt;/span&gt;, &lt;span style=&#34;color:#66d9ef&#34;&gt;func&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;aro&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;stscreds&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;AssumeRoleOptions&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;			&lt;span style=&#34;color:#a6e22e&#34;&gt;aro&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;RoleSessionName&lt;/span&gt; = &lt;span style=&#34;color:#a6e22e&#34;&gt;sessionName&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;		}
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;	))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;When the SDK needs credentials, this setup uses STS &lt;code&gt;AssumeRole&lt;/code&gt; to obtain credentials
with the STS client and role ARN that you provide. The &lt;code&gt;stsClient&lt;/code&gt; in this example can be
configured to take credentials from the environment.&lt;/p&gt;
&lt;p&gt;Hope posting this helps make the solution findable for other folks.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;re-posting and expanding on &lt;a href=&#34;https://mastodon.cloud/@geoff_baskwill/110929299668099158&#34;&gt;my original Mastodon post&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
</description>
    </item>
    
  </channel>
</rss>
