You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 19 Next »

The sample Python script below will allow you to migrate credentials from MQTT Distributor to Chariot at scale using the MQTT Distributor API and the Chariot REST API. 

 The script is designed to run from the Script Console in the Ignition instance where MQTT Distributor is installed and will:

  • read the user configuration from the MQTT Distributor
  • transform the users to the required Chariot REST API format
  • add the users to the Chariot MQTT Credentials store
  • read and output the Chariot MQTT Credentials


  • You will need to set the chariotUrl variable to the URL for your Chariot installation.
  • You will need to create a key:value pair dictionary to assign a password to each MQTT Distributor user as user passwords returned via the MQTT Distributor API call will be encrypted.
import requests
import base64

#HTTP call method
def httpCall(method,url,headers,data):
	if method == "POST":
		resp = requests.post(url, headers=headers, data=data)
	if method == "GET":
		resp = requests.get(url, headers=headers, data=data)
	if method == "PUT":
		resp = requests.put(url, headers=headers, data=data)
	
	#check for failures on httpCall	
	if resp.status_code != 200:
		print resp.json()
		print data
	
	return resp.json()

#Set URL for you Chariot installation
#chariotUrl = "http://localhost:8080"

#Create Chariot username:password dictionary for your MQTT Distributor users
#userpassword = {"admin":"password","test2":"password2","test3":"password3"}

#############################################################################
#Accept Chariot EULA
url = chariotUrl + "/eula"
headers["accept"] = "application/json;api-version=1.0"
headers["content-type"] = "application/json"
data = "{\"isAccepted\": true}"
http_response = httpCall("POST",url,headers,data)

#Chariot login using admin user to get bearer token valid for 90 minutes
url = chariotUrl + "/login"
headers["accept"] = "application/json;api-version=1.0"
headers["Authorization"] = "Basic " + base64.b64encode("admin:password")
data = {}
http_response = httpCall("POST",url,headers,data)
accessToken = http_response["access_token"]

#Read Distributor users
users = system.cirruslink.distributor.readConfig("Users")

#Create new format Access Control Lists (ACLs)
for i in range(len(users)):
	subscribeList = ""
	publishList = ""	
	userName = users[i]["Username"]
	password = list(userpassword.values())[list(userpassword.keys()).index(userName)]
	acls = users[i]["ACLs"]
	acl_list = acls.split(",")
	for i in range(len(acl_list)):
		if acl_list[i][:2] == "R ":
			subscription = acl_list[i][2:len(acl_list[i])]
			subscription = "\"" + subscription + "\""
			subscribeList = subscribeList + subscription + ","
		if acl_list[i][:2] == "W ":
			publish = acl_list[i][2:len(acl_list[i])]
			publish = "\"" + publish + "\""
			publishList = publishList + publish + ","
		if acl_list[i][:2] == "RW":
			subscription = acl_list[i][3:len(acl_list[i])]
			subscription = "\"" + subscription + "\""
			subscribeList = subscribeList + subscription + ","
			publish = acl_list[i][3:len(acl_list[i])]
			publish = "\"" + publish + "\""
			publishList = publishList + publish + ","
				
	#create Chariot user
	url = chariotUrl + "/mqttusers"
	headers["accept"] = "application/json;api-version=1.0"
	headers["content-type"] = "application/json"
	headers["Authorization"] = "Bearer " + accessToken
	data = "[{\"username\":\"" + userName + "\"," + "\"password\":\"" + password + "\"," + "\"acl\":{\"publishTopics\":[" + publishList[:-1] +"],\"subscribeTopics\":[" + subscribeList[:-1] + "]}}]"
	http_response = httpCall("POST",url,headers,data)


#Read Chariot MQtt Users
url = chariotUrl + "/mqttusers"
headers["accept"] = "application/json;api-version=1.0"
headers["Authorization"] = "Bearer " + accessToken
data = {}
chariotMqttUsers = httpCall("GET",url,headers,data)
print chariotMqttUsers
#############################################################################
  • No labels