この記事の目的
Automation Pilotのコマンドを外部からAPIで起動する方法について確認します。
Automation PilotのAPIはSAP Business Accelerator Hubに載っていますが、認証のためのクレデンシャルの取得方法が特殊だったため、記事にします。
Automation Pilotの認証方法
通常BTPのサービスをAPIで呼び出す場合、①サービスキーを作成し、②サービスキーに記載されたclientid, clientsecretなどを使用してOAuth 2.0のトークンを取得、という流れになります。
一方、Automation Pilotではサービスの中で"Service Account"を作成する必要があります。
Service Accountを作成する際に選択できる認証方式は2つあります。
参考:Service Account | Help Portal
Basic Authentication
ユーザID、パスワードでの認証。パスワードは自動で生成され、180日で失効する。同時に2つのパスワードを保持しておくことができる。
Cloud PKI Client Certificate Authentication
X.509クライアント証明書を使った認証。証明書はBTPのDestination経由で発行される。
Cloud PKI Client Certificate Authenticationを使ってAPIを呼び出し
以下の構成で、クライアント証明書を使った認証方法でAPIを起動してみます。
ステップ
- SAP Cloud PKI インフラストラクチャ証明書の生成
- Destinationを作成
- Automation Pilotでサービスアカウントを作成
- CAPからAutomation PilotのAPIを呼び出し
1. SAP Cloud PKI インフラストラクチャ証明書の生成
参考:Use Destination Certificates | Help Portal
1.1. 証明書の作成
BTPコックピットのDestinationsの画面から、Certificatesをクリックします。
Generate Certificateをクリックして証明書を生成します。(もう一つのボタン:Upload Certificateは手元にある証明書をアップロードする場合に使う)
以下のように入力して、Generate Certificateをクリックします。
(Certificate File Name, Certificate Common Nameには任意の値を設定。Certificate Validity Valueには証明書の有効期限を設定する)
2. Destinationを作成
Automation PilotのAPIを呼ぶためのDestinationを登録します。
2.1. Automation PilotのURLを確認
Automation Pilotの"API"画面にあるBase URLを確認します。
2.2. Destinationを登録
項目 | 設定値 |
---|---|
Name | 任意の名前 |
Type | HTTP |
URL | 2.1.で確認したBase URL |
Proxy Type | Internet |
Authentication | ClientCertificateAuthentication |
Key Store Location | 1.で作成した証明書の名前をドロップダウンから選択 |
2.3. Destinationからキーチェーンを取得
Destinationから証明書をダウンロードし、キーチェーンを取得します。取得したキーチェーンはステップ3でAutomation Pilotのサービスアカウントに設定します。
zipファイルがダウンロードされるので、解凍して中に含まれる.pem
ファイルを取得します。
ファイルはbase64でエンコードされているので、デコードします。
base64 -d <input.pem> > output.pem
デコードされたファイルから以下の部分で囲まれたプライベートキーを削除します。
-----BEGIN PRIVATE KEY-----
-----END PRIVATE KEY-----
3. Automation Pilotでサービスアカウントを作成
Automation Pilotにサービスアカウントを作成し、ステップ2で取得したDestinationのキーチェーンをアップロードします。
Service Accounts画面からCreateをクリックします。
項目 | 設定値 |
---|---|
Username (suffix) | 任意の名前 |
Permission | APIで実行したいことに応じて設定(※) |
Authentication Type | SAP Cloud PKI Client Certificate |
Certificate Chain | ステップ2.3.で取得したキーチェーン |
※参考:Permissions and Roles | Help Portal
4. CAPからAutomation PilotのAPIを呼び出し
CAP (Node.js)でAutomation PilotのAPIを呼び出し、コマンドを実行します。
コードは以下のGitリポジトリにあります。
4.1. Automation PilotのAPI仕様をダウンロードします。
SAP Business Accelerator HubからAPI仕様をJSON形式でダウンロード
4.2. CAPプロジェクトにAPI仕様をインポート
プロジェクトのルートディレクトリにダウンロードしたAPI仕様を置いて、以下のコマンドを実行します。
cds import <input_file> --as cds
//例
cds import ExecutionsAPI.json --as cds
4.3. package.jsonの定義
以下の設定を追加します。"destination"には、ステップ2.で登録したDestinationを設定します。
"cds": {
"requires": {
"auth": "mocked",
"Executions": {
"kind": "rest",
"credentials": {
"destination": "automationPilot",
"path": "/api/v1"
},
"[production]": {
"credentials": {
"destination": "automationPilot",
"path": "/api/v1"
}
}
}
}
}
4.4. サービスの定義
srv/automation-pilot.cdsで以下のサービスを定義します。function triggerCommand
は受け取ったコマンドをAutomation Pilotに渡して実行します。
using { Executions_types } from './external/ExecutionsAPI';
service AutomationPilotService {
function triggerCommand(commandId: String) returns Executions_types.execution;
}
返り値の型には、インポートの結果作成されたsrv/external/ExecutionsAPI.cdsで定義されているExecutions_types.execution
を使っています。
4.5. イベントハンドラの実装
srv/automation-pilot.jsでイベントハンドラを実装します。
const cds = require('@sap/cds')
module.exports=class AutomationPilotService extends cds.ApplicationService{
init(){
this.on('triggerCommand', async(req) => {
const commandId = req.data.commandId
const executionApi = await cds.connect.to('Executions')
const response = await executionApi.send('POST', '/executions', {
commandId: commandId
})
return response
})
return super.init()
}
}
4.6. テスト実行
VS CodeのRest Clientから以下のリクエストを送ってみます。
POST {{server}}/odata/v4/automation-pilot/triggerCommand
Content-Type: application/json
{
"commandId": "welcome-T001565R7:AdvancedInputTransformer:1"
}
コマンドIDは、<catalog>:<command>:<version>
の形式で指定します。
Automation Pilot側で実行結果を確認すると、Ownerがサービスアカウントで実行されていました。
まとめ
- Automation PilotのコマンドをAPI経由で起動するには、サービスアカウントを作成し、その認証情報を使う
- 利用可能な認証方法には、ベーシック認証とクライアント証明書を使った認証がある