Jenkins auth over AWS Cognito
Recently I had a case where I had to enforce the following options for Jenkins:
- Password policy (length, secial characters)
- MFA
None of the plugins supports this but AWS Cognito does. Unfortunately there is no native Jenkins Cognito plugin so I stated to dig into OpenId Connect Authentication jenkins plugin and it worked.
Next I’m going to describe the steps it took me to configure the cognito-jenkins connection.
Cognito configuration
- In AWS console find the “Cognito” section, select “Manage userpools” and select “Create a userpool”.
- In the initial screen specify the name of the pool and select “Step through settings”
- On the second screen make sure to select the following options:
- Which standard attributes are required:
- phone_number
- Which standard attributes are required:
- On the next screen:
- Specify the desired password options (length, require numbers, etc.) according to the security policy of your organization.
- In our case we don’t want to allow users sign up for jenkins access so select “Only allow administrators to create users”
- On the next screen make sure to select the following options:
- Do you want to enable Multi-Factor Authentication?:
- required
- Which second factors do you want to enable:
- SMS text message
- Time-based One-time Password
- How will a user be able to recover their account?:
- Email only
- Which attributes do you want to verify?:
- Phone number
- You must provide a role to allow Amazon Cognito to send SMS messages:
- this is either existing role or the name of the IAM role to be created for you.
- Do you want to enable Multi-Factor Authentication?:
- On the next screen you can customize notifications at you desire. I will not describe it. I’ll focus on hte items we need to select for this setup:
- Do you want to send emails through your Amazon SES Configuration:
- No - Use Cognito (Default)
- Do you want to send emails through your Amazon SES Configuration:
- On the next screen you can add tags to the userpool according to your organisation needs.
- Do you want to remember your user’s devices:
- User Opt In
- On the next screen you can add the application client that will be working with this pool. Click “Add an app client”:
- Specify App client name (it will be jenkins in our case)
- Select “Enable username password based authentication (ALLOW_USER_PASSWORD_AUTH)”
- Uncheck “Enable lambda trigger based custom authentication (ALLOW_CUSTOM_AUTH)” Click “Create app client” button on the bottom of the screen.
- Triggers page can be skipped.
- On the review page click “Create pool” button on the bottom.
- You’ll be redirected to the recently created userpool page.
- Find the “Domain name” section
- On this page you’ll need to specify the unique domain name prefix. It can be verified by clicking the “Check availability button”. I’d recommend to use “jenkins-${company_name}-${some_random_string}”. Click “Save changes” when done.
- Go to “App clients” and click “Show details.” Note the values of the following:
- App client id
- App client secret
- Go to “App client settings”:
- Enabled Identity Providers
- “Select all”
- Callback URL(s):
- https://${jenkins-url}/securityRealm/finishLogin
- Sign out URL(s)
- https://${jenkins-url}/OicLogout, https://${jenkins-url}/securityRealm/finishLogin
- Allowed OAuth Flows:
- Authorization code grant
- Allowed OAuth Scopes:
- openid
- profile
- Enabled Identity Providers
Jenkins configuration
- Go to “Manage Jenkins->Plugins” and install the OpenId Connect Authentication plugin
- Go to “Manage Jenkins->Configure Global Security”
- In the “Security Realm” section select “Login with Openid Connect”
- Specify the “Client id” “Client secret” by using the values read in step #14 from the previous list
- Select “Manual configuration”. You’ll need the value of the cognito domain that was created at step #13 in the cognito section. It should look like the following:
https://jenkins-${company_name}-${some_random_string}.${aws_region}.amazoncognito.com
Specify the following values:
- Token server url:
- https://jenkins-${company_name}-${some_random_string}.${aws_region}.amazoncognito.com/oauth2/token
- Authorization server url:
- https://jenkins-${company_name}-${some_random_string}.${aws_region}.amazoncognito.com/oauth2/authorize
- UserInfo server url:
- https://jenkins-${company_name}-${some_random_string}.${aws_region}.amazoncognito.com/oauth2/userInfo
- Scopes:
- openid email profile
- Select “Logout from OpenID Provider”
- End session URL for OpenID Provider:
- https://jenkins-${company_name}-${some_random_string}.${aws_region}.amazoncognito.com/logout?client_id=${client_id}&logout_uri=https://${jenkins-url}
- Click “Advanced button”
- Specify “User name field name” = username
- “Email field “name = email
- Scroll down a bit and find the “Configure ‘escape hatch’ for when the OpenID Provider is unavailable” checkbox.
- Specify the username and passowrd to be used to access jenkins bypassing the cognito auth. To use the functionally goto ‘/login’ using the username and secret as username and password resp.
- Token server url:
Now you can add the user into the pool with the valid email and the phone number. Jenkins login page will regirect you to the cognito auth page.
Source links: