教學課程 / Cloud One Workload Security:第 13 課-API 與雲端帳戶整合的進階操作 / Azure 帳戶 REST API 程式碼(舊版帳戶)
單元 15 / 22

Azure 帳戶 REST API 程式碼(舊版帳戶)

警告

下列影片與程式碼會將使用者名稱、密碼及帳戶名稱提交至 /rest/authentication/login/,屬於舊版帳戶的舊式 REST 流程,不適用於 Cloud One 帳戶。Cloud One 帳戶應建立 Cloud One API key,使用帳戶資料所在區域的 Workload Security API 端點,並在 HTTP 標頭中提供 Authorization: ApiKey <API key>。請勿將此範例視為現行建議作法。

舊版帳戶的 Workload Security API 程式碼

危險

範例中的 verify=False 會停用 TLS 憑證驗證,僅為歷史教材內容;正式環境不得沿用此設定。

from __future__ import print_function
import requests
import json

# Set the variables
url = "https://cloudone.trendmicro.com:443"
data = {"dsCredentials":{"userName":"your username","password":"your password","tenantName":"your tenant name"}}
headers = {'Content-type': 'application/json'}

#Log in, get the session ID, and set the cookies for the post request
ses = requests.session()
loginurl = url + "/rest/authentication/login/"
res = ses.post(loginurl,data=json.dumps(data), headers=headers, verify=False)
cookies = {'sID':res.text}

#Add the Azure Account
data2 = {"createCloudAccountRequest":{"cloudAccountElement":{"subscriptionId":"your subscription Id","cloudType":"AZURE_ARM","name":"Automation Azure Account","azureAdTenantId":"Your azure AD tenant ID","azureAdApplicationId":"Your azure AD Application ID","azureAdApplicationPassword":"Your Secret Key"},"sessionId":res.text}}
res2 = ses.post(url + "/rest/cloudaccounts/", data=json.dumps(data2), headers=headers, verify=False)
print(res2.text)

#Log out
logouturl = url + "/rest/authentication/logout/?sID={" + res.text + "}"
print(logouturl)
res3 = ses.delete(logouturl,verify=False)
print(res3.text)
← 上一單元:使用 Cloud One API 新增… 下一單元:使用 Azure CLI 建立 Azu… →

Cloud One Workload Security:第 13 課-API 與雲端帳戶整合的進階操作 · 22 單元