0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Automation PilotのコマンドをAPIから起動(認証方法について)

Posted at

この記事の目的

Automation Pilotのコマンドを外部からAPIで起動する方法について確認します。
Automation PilotのAPIはSAP Business Accelerator Hubに載っていますが、認証のためのクレデンシャルの取得方法が特殊だったため、記事にします。

Automation Pilotの認証方法

通常BTPのサービスをAPIで呼び出す場合、①サービスキーを作成し、②サービスキーに記載されたclientid, clientsecretなどを使用してOAuth 2.0のトークンを取得、という流れになります。

一方、Automation Pilotではサービスの中で"Service Account"を作成する必要があります。
image.png

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を起動してみます。

Automation Pilot API.drawio.png

ステップ

  1. SAP Cloud PKI インフラストラクチャ証明書の生成
  2. Destinationを作成
  3. Automation Pilotでサービスアカウントを作成
  4. CAPからAutomation PilotのAPIを呼び出し

1. SAP Cloud PKI インフラストラクチャ証明書の生成

参考:Use Destination Certificates | Help Portal

1.1. 証明書の作成

BTPコックピットのDestinationsの画面から、Certificatesをクリックします。
image.png

Generate Certificateをクリックして証明書を生成します。(もう一つのボタン:Upload Certificateは手元にある証明書をアップロードする場合に使う)
image.png

以下のように入力して、Generate Certificateをクリックします。
(Certificate File Name, Certificate Common Nameには任意の値を設定。Certificate Validity Valueには証明書の有効期限を設定する)
image.png

2. Destinationを作成

Automation PilotのAPIを呼ぶためのDestinationを登録します。

2.1. Automation PilotのURLを確認

Automation Pilotの"API"画面にあるBase URLを確認します。
image.png

2.2. Destinationを登録

以下の設定でDestinationを登録します。
image.png

項目 設定値
Name 任意の名前
Type HTTP
URL 2.1.で確認したBase URL
Proxy Type Internet
Authentication ClientCertificateAuthentication
Key Store Location 1.で作成した証明書の名前をドロップダウンから選択

2.3. Destinationからキーチェーンを取得

Destinationから証明書をダウンロードし、キーチェーンを取得します。取得したキーチェーンはステップ3でAutomation Pilotのサービスアカウントに設定します。

Destinationをダウンロードします。
image.png

zipファイルがダウンロードされるので、解凍して中に含まれる.pemファイルを取得します。
image.png

ファイルはbase64でエンコードされているので、デコードします。

linux
base64 -d <input.pem> > output.pem

デコードされたファイルから以下の部分で囲まれたプライベートキーを削除します。

-----BEGIN PRIVATE KEY-----
-----END PRIVATE KEY-----

3. Automation Pilotでサービスアカウントを作成

Automation Pilotにサービスアカウントを作成し、ステップ2で取得したDestinationのキーチェーンをアップロードします。

Service Accounts画面からCreateをクリックします。
image.png

以下のように設定します。
image.png

項目 設定値
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形式でダウンロード
image.png

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を使っています。
image.png

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>の形式で指定します。
image.png

以下のようにレスポンスが返ってくれば成功です。
image.png

Automation Pilot側で実行結果を確認すると、Ownerがサービスアカウントで実行されていました。
image.png

まとめ

  • Automation PilotのコマンドをAPI経由で起動するには、サービスアカウントを作成し、その認証情報を使う
  • 利用可能な認証方法には、ベーシック認証とクライアント証明書を使った認証がある
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?