IBM Support

Root ユーザーとしてのコマンド実行について

How To


Summary

このチュートリアルでは、アプリケーションの開始時に root ( sudo ) ユーザーとしてコマンドを実行する方法を説明します。

Steps

このチュートリアルでは、QRadar アプリケーションのコンテナーにある UNIX ユーザーのリストを表示するシンプルな QRadar アプリケーションをセットアップします。起動時に adduser コマンドを使用して、UNIX ユーザー・リストに表示される testuser という新規ユーザーを追加します。adduser コマンドには sudo 特権が必要で、その場合 as_root 機能を使用する必要があります。

アプリケーションの起動時にコマンドの実行や作業を行うのは推奨されないため、ビルド時にコマンドの実行や作業を行ってください。

前提条件

このチュートリアルでは、以下に従属する必要があります:

  • QRadar App SDK v2

アプリケーションの作成

アプリケーション用の新規フォルダーを作成します:

mkdir AsRootApp && cd AsRootApp

アプリケーション・コードを初期化するには、QRadar App SDK を使用します:

qapp create

Manifest の記入

 manifest.json ファイルを開き、アプリケーションに最も関連のあるいくつかの値を以下のように編集します:

{
  "name": "As Root",
  "description": "App showing using the as_root feature",
  "version": "1.0",
  "image": "qradar-app-base:2.0.0",
  "areas": [
    {
      "id": "QAsRoot",
      "text": "As Root",
      "description": "As Root area showing list of all UNIX users",
      "url": "index",
      "required_capabilities": []
    }
  ],
  "uuid": "<your unique app UUID>"
}

この Manifest は、アプリケーション・ユーザー・インターフェースに役立つ QAsRoot というエリアを定義します。

Jinja Template の記入

 app/templates/hello.html を削除し、新しいファイルの app/templates/users.html を作成します:

<!DOCTYPE html>
<html>
    <head>
    <title>As Root</title>
    </head>

    <body>
    <p id="description">
        This app has added a new user called 'testuser' using 'as_root', the UNIX users in the app container are:
    </p>
    <ul id="user-list">
        {% for user in users %}
        <li>{{user}}</li>
        {% endfor %}
    </ul>
    </body>
</html>

この Jinja Template は、アプリケーションのユーザーインターフェース全体を提供し、主にアプリケーションの Docker コンテナーないのすべての UNIX をリストに表示します。

App Endpoint の作成

 app/views.py を編集して、アプリケーション HTTP エンドポイントを追加します:

import pwd
from flask import Blueprint, render_template

# pylint: disable=invalid-name
viewsbp = Blueprint('viewsbp', __name__, url_prefix='/')

# Simple endpoint that renders and serves 'users.html', displaying all UNIX
# users on the system
@viewsbp.route('/')
@viewsbp.route('/index')
def users():
    # Get all UNIX users and store their names in a list
    user_list = []
    for user in pwd.getpwall():
        user_list.append(user[0])
    # Render users.html using the retrieved user list
    return render_template('users.html', users=user_list)

これは、UNIX ユーザーのリストを取得するためにアプリケーション・エンドポイントを設定し、その後 Jinja template にユーザー・リストを挿入します。

Create User Startup Script の記入

新規スクリプト・コンテナーを container/run/add_user.sh に追加します:

# Create a new user called 'testuser', with a home directory (/home/testuser)
as_root adduser testuser -m

そして、新規ファイルの container/run/ordering.txt を追加します:

/opt/app-root/container/run/add_user.sh

これら 2 つのファイルはアプリケーションの起動動作を定義し、ordering.txt が起動時に実行するファイルを示しており、今回の場合は、add_user.sh, および add_user.shtestuser という新規ユーザーを追加します。

 add_user.sh スクリプトは as_root の機能を使用し、アプリケーションの起動時に root ( sudo ) ユーザーとしてコマンドを実行できます。

アプリケーションをローカルでテストする

 QRadar App SDK を使用してローカルでアプリケーションを実行することにより、テストを行うための準備を行います:

qapp run

 QRadar App SDK は、http://localhost:<app port> のアプリケーションにアクセスできるようにするため、アプリケーションが実行されているポートをレポートします。

as_root

 as_root 機能により、アプリケーション開発者はアプリケーションの起動時に root ユーザーとしてコマンドを実行することができます。

制限事項

 as_root オプションはアプリケーション起動時にのみ使用でき、通常のランタイム操作時に使用すると失敗します。

考察事項

 as_root オプションは必要に応じてのみ使用されるべきで、厳密な検証 ( X-Force Exchange への提出) の対象です。- as_root オプションを使用するには、正当性および必要な理由がなければなりません。

Related Information

Document Location

Worldwide

[{"Line of Business":{"code":"LOB24","label":"Security Software"},"Business Unit":{"code":"BU059","label":"IBM Software w\/o TPS"},"Product":{"code":"SSBQAC","label":"IBM Security QRadar SIEM"},"ARM Category":[{"code":"a8m0z000000cwt3AAA","label":"QRadar Apps"}],"ARM Case Number":"","Platform":[{"code":"PF025","label":"Platform Independent"}],"Version":"All Version(s)"}]

Document Information

Modified date:
10 September 2021

UID

ibm16458087