Yogesh Dhimate

Notes to Myself

Jan 16, 2021 - 4 minute read - Comments - Programming

How to Use Different Authentication Options for Salesforce Connector in Mule 4

Introduction

MuleSoft offers multiple connectors for Salesforce integration. This post is about Salesforce Connector for Mule 4. This connector helps in integrating with Sales Cloud, Service Cloud, Salesforce Platform, and Force.com. We will specifically look into different options for authentication with Salesforce.

Software Prerequisites

You need a Salesforce developer account. You can get one for free by signing up here. I have used the following versions for MuleSoft software.

  1. Anypoint Studio 7.7
  2. Salesforce Connector 10.8.0

Configuration

Security Token in Salesforce

Salesforce security token is an alphanumeric code associated with your password. You do not need a security token if you are trying to authenticate from an IP address that is inside your Salesforce org’s trusted IP range, or your Salesforce profile’s login IP range. When you sign up for a new developer account, your Salesforce org does not have trusted IP ranges. You can either set up trusted or login IP ranges in your org, or use the security token for authentication. Generating security token is simple.

  1. Go to settings

    dwarf

  2. Reset security token by using the ‘Reset My Security Token’ menu and clicking on the ‘Reset Security Token’ button.

    dwarf

  3. You should receive your new security token in your email account that you used to sign up for the Salesforce developer account.

Certificate setup in Salesforce

For certificate-based security, you need to set up a certificate in Salesforce.

  1. Go to setup

    dwarf

  2. Search for certificate and key management and select the option

    dwarf

  3. Click on ‘Create Self-Signed Certificate’ button to create a new certificate and enter the information and save it.

    dwarf

  4. Once certificate is created, download the certificate (.crt) file. We need it for setting up the connected app.

    dwarf

  5. Export the certificate to keystore. We need it to configure the connection information in Mule app.

    dwarf

  6. Enter the password when exporting to keystore

    dwarf

  7. Make sure you save both the .crt file (downloaded in step 4 above) and the .jks file safely on your disk.

Connected App setup in Salesforce

  1. Go to setup

    dwarf

  2. Search for app manager and select the ‘App Manager’ option

    dwarf

  3. Use the ‘New Connected App’ button to create a new connected app.

    dwarf

  4. Fill in name and email. Select ‘Enable OAuth Settings’ Enter two Callback URLs

Select ‘Use digital signatures’ and upload the .crt file received when setting up the certificate above. Select all the OAuth Scopes if possible

dwarf
Note the client_id and client_Secret

  1. In your connected app’s page, click on Manage button to review and update the app policies. Click on Manage.

    dwarf

  2. And then on ‘Edit Policies’

    dwarf

  3. In the Permitted Users option you can see two options

    • Admin approved users are pre-authorized.

      We will not go into the details of this option, but if you want to use it you can use the following steps

      • Go to Settings > Manage Users > Profiles
      • Edit the profile associated with the user
      • For ‘Connected App Access’ under the profile, select the checkbox against the connected app. Save.
    • All users may self-authorize.

      We will use this option. Ensure that you select this option and save the policy.

      dwarf

  4. Open your browser and go to https://oauthdebugger.com/. Enter following details

    • Authorization url = ‘https://login.salesforce.com/services/oauth2/authorize’
    • Redirect URI = ‘https://oauthdebugger.com/debug’
    • Client ID = obtained from the connected app
    • Scope = ‘api refresh_token offline_access’
    • Response type = ‘code’
    • Response mode = ‘query’
    • Click on “send request”
dwarf
  1. It should redirect you to enter your credentials on Salesforce login screen and you should receive an authorization code.

    dwarf

  2. This will complete the connected app setup.

Basic Authentication

Basic authentication is the easiest way to authenticate with Salesforce. For basic authentication you need username and password, and optionally a security token.

dwarf

OAuth JWT

For OAuth JWT connection copy and paste the key store file (.jks) that you created in the certificate setup section into the src/main/resources folder.

  • Consumer Key – Consumer key of the Connected App that was created in Salesforce
  • Key Store – Keystore file (.jks) that you downloaded from the certificate setup
  • Store Password – Keystore password that was created when you generated the key store file
  • Principal – Salesforce username of the user that was approved against the authorization URL
dwarf

OAuth SAML

For OAuth SAML connection copy and paste the key store file (.jks) that you created in the certificate setup section into the src/main/resources folder.

  • Consumer Key – Consumer key of the Connected App that was created in Salesforce
  • Key Store – Keystore file (.jks) that you downloaded from the certificate setup
  • Store Password – Keystore password that was created when you generated the key store file
  • Principal – Salesforce username of the user that was approved against the authorization URL
dwarf

OAuth Username Password

For OAuth Username Password configure the following properties

  • Consumer Key – Consumer key of the Connected App that was created in Salesforce
  • Consumer Secret – Consumer secret of the Connected App that was created in Salesforce
  • Username – Salesforce login id
  • Password – Salesforce password
  • Security Token - Security token associated with the username
dwarf

Jan 13, 2021 - 2 minute read - Comments - Books

Reflections on Range by David Epstein

I recently finished reading Range - Why Generalists Triumph in a Specialized World by David Epstein. It wasn’t exactly a new topic. It was recommended in Bill Gates' blog so I was curious about it and picked it up. The book talks about (hyper)specialization vs broad thinking in career as well as life. I had read one perspective about this in other books like ‘Talent is Overrated’ by Geoffrey Colvin, and Cal Newport’s ‘So Good They Can’t Ignore You’.

This book offers a different perspective. It’s quite a myth-buster about the 10000 hours theory or deliberate practice. It explains how the hyperspecialization of narrow skills can be harmful to your long term growth. David kept me engaged throughout the book, even though sometimes the content kept on dragging the same message. It also felt relevant to my experience in life and career. I have been a sort of generalist, trying out different things like software engineering, solution architecture, or pre-sales over the last 20 years in my career. I intentionally avoided sticking to a single tech stack or framework or career track. And sometimes felt bad (or incompetent) being a jack of all trades type of a person. Turns out it isn’t bad to be that guy. In a way, I felt vindicated of everything I have done so far.

The key messages that stuck with me were about the experimentation and match quality - the term used to describe the fit between work someone does and their abilities. Everyone progresses at a different rate, so feeling behind doesn’t help much. Most likely you don’t know where exactly you are going. In this situation, the proactive pursuit of match quality, experimentation with different things, learning and adjusting as you go is the best approach. Comparing yourself to (cherry-picked) younger people doesn’t help either. Instead, compare yourself to your yesterday’s self and see how you are progressing. Epstein provides a good framework and reason to encourage range (rather than depth) over different areas of interest and stay curious.

As noted above, some of the repetitive content could’ve been edited to make it more concise. But other than that it was a fun and enjoyable read.

Jan 9, 2021 - 2 minute read - Comments - Programming

Externalizing Queries in Mule Application

I like to keep my code clean, understandable, and readable. It helps the next person who will take over my work. I try my best to avoid any trouble reading, maintaining, and updating the code. There are recommended and well known best practices about organizing Mule code. It involves better structuring of the project, maintaining configuration files, hiding or securing sensitive information like passwords and API keys, transaction management, or reconnection and caching strategies.

The lesser-known trick is about externalizing SOQL and SQL queries. Before I knew this trick I organized my SQL queries in properties files, and used spring placeholders like ${account.query} or dataweave Mule::p function to inject or read the queries in the code. It is not an elegant approach. It made the properties file difficult to manage and read. Then I discovered ${file::} notation to inject the queries. It offers a much better way to externalize the queries.

Using this notation is easy. In your Mule app, organize your queries in the src/main/resources folder in their own files as shown below.

dwarf

Once you organize your queries properly, injecting those queries is easy.

dwarf

This organization helps in keeping the XML configuration file readable and easily maintainable.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
	xmlns:http="http://www.mulesoft.org/schema/mule/http"
	xmlns:db="http://www.mulesoft.org/schema/mule/db"
	xmlns="http://www.mulesoft.org/schema/mule/core"
	xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core 
		http://www.mulesoft.org/schema/mule/core/current/mule.xsd
		http://www.mulesoft.org/schema/mule/db 
		http://www.mulesoft.org/schema/mule/db/current/mule-db.xsd
		http://www.mulesoft.org/schema/mule/http 
		http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
		http://www.mulesoft.org/schema/mule/ee/core 
		http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
	<flow name="demo-apiFlow"
		doc:id="e3653667-121e-47cf-b2a6-33345b66e2e4">
		<http:listener doc:name="/Patient" 
			doc:id="39e23081-26d6-4469-953d-0efd3219b34f" 
			config-ref="HTTP_Listener_config" 
			path="/Patient"/>
		<db:select 
			doc:name="Select all patients" 
			doc:id="b7c2a60b-d6bb-431b-bb27-10419910aae6" 
			config-ref="Database_Config">
			<db:sql ><![CDATA[${file::sql/select-patients-all.sql}]]></db:sql>
		</db:select>
		<ee:transform 
			doc:name="Prepare response" 
			doc:id="e2774a96-2f54-4a41-acc2-e8f271b29f52" >
			<ee:message >
				<ee:set-payload resource="dw/patients-all-response.dwl" />
			</ee:message>
		</ee:transform>
	</flow>
</mule>