cmdbox 0.5.1.2__py3-none-any.whl → 0.5.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of cmdbox might be problematic. Click here for more details.
- cmdbox/app/app.py +4 -2
- cmdbox/app/auth/signin.py +634 -631
- cmdbox/app/client.py +10 -10
- cmdbox/app/common.py +50 -6
- cmdbox/app/commons/convert.py +9 -0
- cmdbox/app/commons/module.py +113 -113
- cmdbox/app/commons/redis_client.py +40 -29
- cmdbox/app/edge.py +4 -4
- cmdbox/app/features/cli/audit_base.py +138 -0
- cmdbox/app/features/cli/cmdbox_audit_createdb.py +224 -0
- cmdbox/app/features/cli/cmdbox_audit_delete.py +308 -0
- cmdbox/app/features/cli/cmdbox_audit_search.py +416 -0
- cmdbox/app/features/cli/cmdbox_audit_write.py +247 -0
- cmdbox/app/features/cli/cmdbox_client_file_copy.py +207 -207
- cmdbox/app/features/cli/cmdbox_client_file_download.py +207 -207
- cmdbox/app/features/cli/cmdbox_client_file_list.py +193 -193
- cmdbox/app/features/cli/cmdbox_client_file_mkdir.py +191 -191
- cmdbox/app/features/cli/cmdbox_client_file_move.py +199 -199
- cmdbox/app/features/cli/cmdbox_client_file_remove.py +190 -190
- cmdbox/app/features/cli/cmdbox_client_file_rmdir.py +190 -190
- cmdbox/app/features/cli/cmdbox_client_file_upload.py +212 -212
- cmdbox/app/features/cli/cmdbox_client_server_info.py +166 -166
- cmdbox/app/features/cli/cmdbox_server_list.py +88 -88
- cmdbox/app/features/cli/cmdbox_server_stop.py +138 -138
- cmdbox/app/features/web/cmdbox_web_audit.py +81 -0
- cmdbox/app/features/web/cmdbox_web_audit_metrics.py +72 -0
- cmdbox/app/features/web/cmdbox_web_del_cmd.py +2 -0
- cmdbox/app/features/web/cmdbox_web_del_pipe.py +1 -0
- cmdbox/app/features/web/cmdbox_web_do_signin.py +12 -2
- cmdbox/app/features/web/cmdbox_web_do_signout.py +1 -0
- cmdbox/app/features/web/cmdbox_web_exec_cmd.py +31 -2
- cmdbox/app/features/web/cmdbox_web_exec_pipe.py +1 -0
- cmdbox/app/features/web/cmdbox_web_filer download.py +43 -42
- cmdbox/app/features/web/cmdbox_web_filer.py +1 -0
- cmdbox/app/features/web/cmdbox_web_filer_upload.py +65 -64
- cmdbox/app/features/web/cmdbox_web_gui.py +166 -165
- cmdbox/app/features/web/cmdbox_web_load_pin.py +43 -43
- cmdbox/app/features/web/cmdbox_web_raw_pipe.py +87 -87
- cmdbox/app/features/web/cmdbox_web_save_cmd.py +1 -0
- cmdbox/app/features/web/cmdbox_web_save_pin.py +42 -42
- cmdbox/app/features/web/cmdbox_web_save_pipe.py +1 -0
- cmdbox/app/features/web/cmdbox_web_user_data.py +58 -0
- cmdbox/app/features/web/cmdbox_web_users.py +12 -0
- cmdbox/app/options.py +788 -601
- cmdbox/app/web.py +7 -1
- cmdbox/extensions/features.yml +23 -0
- cmdbox/extensions/sample_project/sample/app/features/cli/sample_client_time.py +82 -82
- cmdbox/extensions/sample_project/sample/app/features/cli/sample_server_time.py +145 -145
- cmdbox/extensions/user_list.yml +5 -0
- cmdbox/licenses/{LICENSE.Sphinx.8.1.3(BSD License).txt → LICENSE.Sphinx.8.2.3(UNKNOWN).txt} +1 -1
- cmdbox/licenses/LICENSE.argcomplete.3.6.2(Apache Software License).txt +177 -0
- cmdbox/licenses/{LICENSE.babel.2.16.0(BSD License).txt → LICENSE.babel.2.17.0(BSD License).txt } +1 -1
- cmdbox/licenses/{LICENSE.pkginfo.1.10.0(MIT License).txt → LICENSE.charset-normalizer.3.4.1(MIT License).txt } +1 -1
- cmdbox/licenses/LICENSE.gevent.25.4.1(MIT).txt +25 -0
- cmdbox/licenses/LICENSE.greenlet.3.2.0(MIT AND Python-2.0).txt +30 -0
- cmdbox/licenses/LICENSE.gunicorn.23.0.0(MIT License).txt +23 -0
- cmdbox/licenses/LICENSE.importlib_metadata.8.6.1(Apache Software License).txt +202 -0
- cmdbox/licenses/LICENSE.nh3.0.2.21(MIT).txt +21 -0
- cmdbox/licenses/{LICENSE.pillow.11.0.0(CMU License (MIT-CMU)).txt → LICENSE.pillow.11.1.0(CMU License (MIT-CMU)).txt } +27 -40
- cmdbox/licenses/LICENSE.pillow.11.2.1(UNKNOWN).txt +1200 -0
- cmdbox/licenses/LICENSE.plyer.2.1.0(MIT License).txt +19 -0
- cmdbox/licenses/LICENSE.prompt_toolkit.3.0.50(BSD License).txt +27 -0
- cmdbox/licenses/LICENSE.prompt_toolkit.3.0.51(BSD License).txt +27 -0
- cmdbox/licenses/LICENSE.psycopg-binary.3.2.6(GNU Lesser General Public License v3 (LGPLv3)).txt +165 -0
- cmdbox/licenses/LICENSE.psycopg-pool.3.2.6(GNU Lesser General Public License v3 (LGPLv3)).txt +165 -0
- cmdbox/licenses/LICENSE.psycopg.3.2.6(GNU Lesser General Public License v3 (LGPLv3)).txt +165 -0
- cmdbox/licenses/LICENSE.pycryptodome.3.22.0(BSD License; Public Domain).txt +61 -0
- cmdbox/licenses/LICENSE.pydantic.2.11.3(MIT License).txt +21 -0
- cmdbox/licenses/LICENSE.pydantic_core.2.33.1(MIT License).txt +21 -0
- cmdbox/licenses/LICENSE.pystray.0.19.5(GNU Lesser General Public License v3 (LGPLv3)).txt +674 -0
- cmdbox/licenses/LICENSE.questionary.2.1.0(MIT License).txt +19 -0
- cmdbox/licenses/LICENSE.roman-numerals-py.3.1.0(CC0 1.0 Universal (CC0 1.0) Public Domain Dedication; Zero-Clause BSD (0BSD)).txt +146 -0
- cmdbox/licenses/{LICENSE.six.1.16.0(MIT License).txt → LICENSE.six.1.17.0(MIT License).txt } +1 -1
- cmdbox/licenses/LICENSE.starlette.0.46.2(BSD License).txt +27 -0
- cmdbox/licenses/{LICENSE.charset-normalizer.3.4.0(MIT License).txt → LICENSE.typing-inspection.0.4.0(MIT License).txt } +2 -2
- cmdbox/licenses/LICENSE.typing_extensions.4.13.2(UNKNOWN).txt +279 -0
- cmdbox/licenses/LICENSE.tzdata.2025.2(Apache Software License).txt +15 -0
- cmdbox/licenses/LICENSE.urllib3.2.4.0(UNKNOWN).txt +21 -0
- cmdbox/licenses/LICENSE.uvicorn.0.34.1(BSD License).txt +27 -0
- cmdbox/licenses/LICENSE.watchfiles.1.0.5(MIT License).txt +21 -0
- cmdbox/licenses/files.txt +49 -38
- cmdbox/logconf_audit.yml +30 -0
- cmdbox/logconf_cmdbox.yml +30 -0
- cmdbox/version.py +2 -2
- cmdbox/web/assets/apexcharts/apexcharts.css +679 -0
- cmdbox/web/assets/apexcharts/apexcharts.min.js +38 -0
- cmdbox/web/assets/cmdbox/audit.js +340 -0
- cmdbox/web/assets/cmdbox/color_mode.css +520 -0
- cmdbox/web/assets/cmdbox/common.js +416 -24
- cmdbox/web/assets/cmdbox/filer_modal.js +1 -1
- cmdbox/web/assets/cmdbox/list_cmd.js +10 -275
- cmdbox/web/assets/cmdbox/list_pipe.js +3 -3
- cmdbox/web/assets/cmdbox/main.js +2 -2
- cmdbox/web/assets/cmdbox/result.js +2 -2
- cmdbox/web/assets/cmdbox/signin.js +2 -2
- cmdbox/web/assets/cmdbox/users.js +19 -20
- cmdbox/web/assets/cmdbox/view_raw.js +1 -1
- cmdbox/web/assets/cmdbox/view_result.js +11 -13
- cmdbox/web/assets/filer/filer.js +2 -2
- cmdbox/web/assets/filer/main.js +2 -2
- cmdbox/web/assets_license_list.txt +4 -1
- cmdbox/web/audit.html +268 -0
- cmdbox/web/filer.html +37 -12
- cmdbox/web/gui.html +36 -53
- cmdbox/web/result.html +24 -3
- cmdbox/web/signin.html +35 -14
- cmdbox/web/users.html +21 -3
- {cmdbox-0.5.1.2.dist-info → cmdbox-0.5.3.dist-info}/METADATA +28 -5
- {cmdbox-0.5.1.2.dist-info → cmdbox-0.5.3.dist-info}/RECORD +142 -103
- {cmdbox-0.5.1.2.dist-info → cmdbox-0.5.3.dist-info}/entry_points.txt +0 -1
- cmdbox/licenses/LICENSE.nh3.0.2.18(MIT).txt +0 -1
- /cmdbox/licenses/{LICENSE.Jinja2.3.1.4(BSD License).txt → LICENSE.Jinja2.3.1.6(BSD License).txt} +0 -0
- /cmdbox/licenses/{LICENSE.Pygments.2.18.0(BSD License).txt → LICENSE.Pygments.2.19.1(BSD License).txt} +0 -0
- /cmdbox/licenses/{LICENSE.anyio.4.6.2.post1(MIT License).txt → LICENSE.anyio.4.9.0(MIT License).txt} +0 -0
- /cmdbox/licenses/{LICENSE.argcomplete.3.5.1(Apache Software License).txt → LICENSE.argcomplete.3.6.1(Apache Software License).txt} +0 -0
- /cmdbox/licenses/{LICENSE.certifi.2024.8.30(Mozilla Public License 2.0 (MPL 2.0)).txt → LICENSE.certifi.2025.1.31(Mozilla Public License 2.0 (MPL 2.0)).txt} +0 -0
- /cmdbox/licenses/{LICENSE.click.8.1.7(BSD License).txt → LICENSE.click.8.1.8(BSD License).txt} +0 -0
- /cmdbox/licenses/{LICENSE.cryptography.43.0.3(Apache Software License; BSD License).txt → LICENSE.cryptography.44.0.2(Apache Software License; BSD License).txt} +0 -0
- /cmdbox/licenses/{LICENSE.fastapi.0.115.5(MIT License).txt → LICENSE.fastapi.0.115.12(MIT License).txt} +0 -0
- /cmdbox/licenses/{LICENSE.importlib_metadata.8.5.0(Apache Software License).txt → LICENSE.id.1.5.0(Apache Software License).txt} +0 -0
- /cmdbox/licenses/{LICENSE.keyring.25.5.0(MIT License).txt → LICENSE.keyring.25.6.0(MIT License).txt} +0 -0
- /cmdbox/licenses/{LICENSE.more-itertools.10.5.0(MIT License).txt → LICENSE.more-itertools.10.6.0(MIT License).txt} +0 -0
- /cmdbox/licenses/{LICENSE.numpy.2.1.3(BSD License).txt → LICENSE.numpy.2.2.4(BSD License).txt} +0 -0
- /cmdbox/licenses/{LICENSE.prettytable.3.12.0(BSD License).txt → LICENSE.prettytable.3.16.0(UNKNOWN).txt} +0 -0
- /cmdbox/licenses/{LICENSE.pydantic.2.10.2(MIT License).txt → LICENSE.pydantic.2.11.1(MIT License).txt} +0 -0
- /cmdbox/licenses/{LICENSE.pydantic_core.2.27.1(MIT License).txt → LICENSE.pydantic_core.2.33.0(MIT License).txt} +0 -0
- /cmdbox/licenses/{LICENSE.python-dotenv.1.0.1(BSD License).txt → LICENSE.python-dotenv.1.1.0(BSD License).txt} +0 -0
- /cmdbox/licenses/{LICENSE.python-multipart.0.0.17(Apache Software License).txt → LICENSE.python-multipart.0.0.20(Apache Software License).txt} +0 -0
- /cmdbox/licenses/{LICENSE.redis.5.2.0(MIT License).txt → LICENSE.redis.5.2.1(MIT License).txt} +0 -0
- /cmdbox/licenses/{LICENSE.rich.13.9.4(MIT License).txt → LICENSE.rich.14.0.0(MIT License).txt} +0 -0
- /cmdbox/licenses/{LICENSE.sphinx-intl.2.3.0(BSD License).txt → LICENSE.sphinx-intl.2.3.1(BSD License).txt} +0 -0
- /cmdbox/licenses/{LICENSE.starlette.0.41.3(BSD License).txt → LICENSE.starlette.0.46.1(BSD License).txt} +0 -0
- /cmdbox/licenses/{LICENSE.tomli.2.1.0(MIT License).txt → LICENSE.tomli.2.2.1(MIT License).txt} +0 -0
- /cmdbox/licenses/{LICENSE.twine.5.1.1(Apache Software License).txt → LICENSE.twine.6.1.0(Apache Software License).txt} +0 -0
- /cmdbox/licenses/{LICENSE.typing_extensions.4.12.2(Python Software Foundation License).txt → LICENSE.typing_extensions.4.13.0(UNKNOWN).txt} +0 -0
- /cmdbox/licenses/{LICENSE.urllib3.2.2.3(MIT License).txt → LICENSE.urllib3.2.3.0(MIT License).txt} +0 -0
- /cmdbox/licenses/{LICENSE.uvicorn.0.32.1(BSD License).txt → LICENSE.uvicorn.0.34.0(BSD License).txt} +0 -0
- /cmdbox/licenses/{LICENSE.watchfiles.1.0.0(MIT License).txt → LICENSE.watchfiles.1.0.4(MIT License).txt} +0 -0
- /cmdbox/licenses/{LICENSE.websockets.14.1(BSD License).txt → LICENSE.websockets.15.0.1(BSD License).txt} +0 -0
- /cmdbox/licenses/{LICENSE.zope.interface.7.1.1(Zope Public License).txt → LICENSE.zope.interface.7.2(Zope Public License).txt} +0 -0
- {cmdbox-0.5.1.2.dist-info → cmdbox-0.5.3.dist-info}/LICENSE +0 -0
- {cmdbox-0.5.1.2.dist-info → cmdbox-0.5.3.dist-info}/WHEEL +0 -0
- {cmdbox-0.5.1.2.dist-info → cmdbox-0.5.3.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
from cmdbox.app import feature
|
|
2
|
+
from cmdbox.app.options import Options
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
from typing import Any
|
|
5
|
+
import logging
|
|
6
|
+
import psycopg
|
|
7
|
+
import sqlite3
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class AuditBase(feature.ResultEdgeFeature):
|
|
11
|
+
TBL_COLS = ['audit_type', 'clmsg_id', 'clmsg_date', 'clmsg_src', 'clmsg_title', 'clmsg_user', 'clmsg_body', 'clmsg_tag', 'svmsg_id', 'svmsg_date']
|
|
12
|
+
DT_FMT = ['%Y/%m/%d %H:%M', '%Y/%m/%d %H', '%Y/%m/%d', '%Y/%m', '%Y', '%m', '%u']
|
|
13
|
+
def get_option(self):
|
|
14
|
+
"""
|
|
15
|
+
この機能のオプションを返します
|
|
16
|
+
|
|
17
|
+
Returns:
|
|
18
|
+
Dict[str, Any]: オプション
|
|
19
|
+
"""
|
|
20
|
+
return dict(
|
|
21
|
+
use_redis=self.USE_REDIS_MEIGHT, nouse_webmode=False,
|
|
22
|
+
discription_ja="",
|
|
23
|
+
discription_en="",
|
|
24
|
+
choice=[
|
|
25
|
+
dict(opt="host", type=Options.T_STR, default=self.default_host, required=True, multi=False, hide=True, choice=None, web="mask",
|
|
26
|
+
discription_ja="Redisサーバーのサービスホストを指定します。",
|
|
27
|
+
discription_en="Specify the service host of the Redis server."),
|
|
28
|
+
dict(opt="port", type=Options.T_INT, default=self.default_port, required=True, multi=False, hide=True, choice=None, web="mask",
|
|
29
|
+
discription_ja="Redisサーバーのサービスポートを指定します。",
|
|
30
|
+
discription_en="Specify the service port of the Redis server."),
|
|
31
|
+
dict(opt="password", type=Options.T_STR, default=self.default_pass, required=True, multi=False, hide=True, choice=None, web="mask",
|
|
32
|
+
discription_ja=f"Redisサーバーのアクセスパスワード(任意)を指定します。省略時は `{self.default_pass}` を使用します。",
|
|
33
|
+
discription_en=f"Specify the access password of the Redis server (optional). If omitted, `{self.default_pass}` is used."),
|
|
34
|
+
dict(opt="svname", type=Options.T_STR, default="server", required=True, multi=False, hide=True, choice=None, web="readonly",
|
|
35
|
+
discription_ja="サーバーのサービス名を指定します。省略時は `server` を使用します。",
|
|
36
|
+
discription_en="Specify the service name of the inference server. If omitted, `server` is used."),
|
|
37
|
+
|
|
38
|
+
dict(opt="retry_count", type=Options.T_INT, default=3, required=False, multi=False, hide=True, choice=None,
|
|
39
|
+
discription_ja="Redisサーバーへの再接続回数を指定します。0以下を指定すると永遠に再接続を行います。",
|
|
40
|
+
discription_en="Specifies the number of reconnections to the Redis server.If less than 0 is specified, reconnection is forever."),
|
|
41
|
+
dict(opt="retry_interval", type=Options.T_INT, default=5, required=False, multi=False, hide=True, choice=None,
|
|
42
|
+
discription_ja="Redisサーバーに再接続までの秒数を指定します。",
|
|
43
|
+
discription_en="Specifies the number of seconds before reconnecting to the Redis server."),
|
|
44
|
+
dict(opt="timeout", type=Options.T_INT, default="15", required=False, multi=False, hide=True, choice=None,
|
|
45
|
+
discription_ja="サーバーの応答が返ってくるまでの最大待ち時間を指定。",
|
|
46
|
+
discription_en="Specify the maximum waiting time until the server responds."),
|
|
47
|
+
|
|
48
|
+
dict(opt="pg_enabled", type=Options.T_BOOL, default=False, required=False, multi=False, hide=True, choice=[True, False], web="mask",
|
|
49
|
+
discription_ja="postgresqlデータベース・サーバを使用する場合はTrueを指定します。",
|
|
50
|
+
discription_en="Specify True if using the postgresql database server."),
|
|
51
|
+
dict(opt="pg_host", type=Options.T_STR, default='localhost', required=False, multi=False, hide=True, choice=None, web="mask",
|
|
52
|
+
discription_ja="postgresqlホストを指定する。",
|
|
53
|
+
discription_en="Specify the postgresql host."),
|
|
54
|
+
dict(opt="pg_port", type=Options.T_INT, default=5432, required=False, multi=False, hide=True, choice=None, web="mask",
|
|
55
|
+
discription_ja="postgresqlのポートを指定する。",
|
|
56
|
+
discription_en="Specify the postgresql port."),
|
|
57
|
+
dict(opt="pg_user", type=Options.T_STR, default='postgres', required=False, multi=False, hide=True, choice=None, web="mask",
|
|
58
|
+
discription_ja="postgresqlのユーザー名を指定する。",
|
|
59
|
+
discription_en="Specify the postgresql user name."),
|
|
60
|
+
dict(opt="pg_password", type=Options.T_STR, default='postgres', required=False, multi=False, hide=True, choice=None, web="mask",
|
|
61
|
+
discription_ja="postgresqlのパスワードを指定する。",
|
|
62
|
+
discription_en="Specify the postgresql password."),
|
|
63
|
+
dict(opt="pg_dbname", type=Options.T_STR, default='audit', required=False, multi=False, hide=True, choice=None,
|
|
64
|
+
discription_ja="postgresqlデータベース名を指定します。",
|
|
65
|
+
discription_en="Specify the postgresql database name."),
|
|
66
|
+
]
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
def initdb(self, data_dir:Path, logger:logging.Logger, pg_enabled:bool, pg_host:str, pg_port:int, pg_user:str, pg_password:str, pg_dbname:str) -> Any:
|
|
70
|
+
"""
|
|
71
|
+
データベースを初期化します
|
|
72
|
+
|
|
73
|
+
Args:
|
|
74
|
+
data_dir (Path): データディレクトリ
|
|
75
|
+
logger (logging.Logger): ロガー
|
|
76
|
+
pg_enabled (bool): PostgreSQLを使用するかどうか
|
|
77
|
+
pg_host (str): PostgreSQLホスト
|
|
78
|
+
pg_port (int): PostgreSQLポート
|
|
79
|
+
pg_user (str): PostgreSQLユーザー名
|
|
80
|
+
pg_password (str): PostgreSQLパスワード
|
|
81
|
+
pg_dbname (str): PostgreSQLデータベース名
|
|
82
|
+
|
|
83
|
+
Returns:
|
|
84
|
+
Any: データベース接続オブジェクト
|
|
85
|
+
"""
|
|
86
|
+
if pg_enabled:
|
|
87
|
+
constr = f"host={pg_host} port={pg_port} user={pg_user} password={pg_password} dbname={pg_dbname} connect_timeout=60"
|
|
88
|
+
conn = psycopg.connect(constr, autocommit=False)
|
|
89
|
+
cursor = conn.cursor()
|
|
90
|
+
try:
|
|
91
|
+
cursor.execute("SELECT count(*) FROM information_schema.tables WHERE table_name='audit'")
|
|
92
|
+
if cursor.fetchone()[0] == 0:
|
|
93
|
+
# テーブルが存在しない場合は作成
|
|
94
|
+
cursor.execute('''
|
|
95
|
+
CREATE TABLE IF NOT EXISTS audit (
|
|
96
|
+
id SERIAL PRIMARY KEY,
|
|
97
|
+
audit_type TEXT,
|
|
98
|
+
clmsg_id TEXT,
|
|
99
|
+
clmsg_date TIMESTAMP WITH TIME ZONE,
|
|
100
|
+
clmsg_src TEXT,
|
|
101
|
+
clmsg_title TEXT,
|
|
102
|
+
clmsg_user TEXT,
|
|
103
|
+
clmsg_body JSON,
|
|
104
|
+
clmsg_tag JSON,
|
|
105
|
+
svmsg_id TEXT,
|
|
106
|
+
svmsg_date TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
|
|
107
|
+
)
|
|
108
|
+
''')
|
|
109
|
+
finally:
|
|
110
|
+
cursor.close()
|
|
111
|
+
conn.rollback()
|
|
112
|
+
else:
|
|
113
|
+
db_path = data_dir / '.audit' / 'audit.db'
|
|
114
|
+
db_path.parent.mkdir(parents=True, exist_ok=True)
|
|
115
|
+
conn = sqlite3.connect(db_path)
|
|
116
|
+
cursor = conn.cursor()
|
|
117
|
+
try:
|
|
118
|
+
cursor.execute('SELECT COUNT(*) FROM sqlite_master WHERE TYPE="table" AND NAME="audit"')
|
|
119
|
+
if cursor.fetchone()[0] == 0:
|
|
120
|
+
# テーブルが存在しない場合は作成
|
|
121
|
+
cursor.execute('''
|
|
122
|
+
CREATE TABLE IF NOT EXISTS audit (
|
|
123
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
124
|
+
audit_type TEXT,
|
|
125
|
+
clmsg_id TEXT,
|
|
126
|
+
clmsg_date TEXT,
|
|
127
|
+
clmsg_src TEXT,
|
|
128
|
+
clmsg_title TEXT,
|
|
129
|
+
clmsg_user TEXT,
|
|
130
|
+
clmsg_body JSON,
|
|
131
|
+
clmsg_tag JSON,
|
|
132
|
+
svmsg_id TEXT,
|
|
133
|
+
svmsg_date TEXT DEFAULT CURRENT_TIMESTAMP
|
|
134
|
+
)
|
|
135
|
+
''')
|
|
136
|
+
finally:
|
|
137
|
+
cursor.close()
|
|
138
|
+
return conn
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
from cmdbox.app import common, client, feature
|
|
2
|
+
from cmdbox.app.commons import convert, redis_client
|
|
3
|
+
from cmdbox.app.features.cli import audit_base
|
|
4
|
+
from cmdbox.app.options import Options
|
|
5
|
+
from datetime import datetime
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from typing import Dict, Any, Tuple, List, Union
|
|
8
|
+
import argparse
|
|
9
|
+
import logging
|
|
10
|
+
import json
|
|
11
|
+
import psycopg
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class AuditCreatedb(feature.UnsupportEdgeFeature):
|
|
15
|
+
def get_mode(self) -> Union[str, List[str]]:
|
|
16
|
+
"""
|
|
17
|
+
この機能のモードを返します
|
|
18
|
+
|
|
19
|
+
Returns:
|
|
20
|
+
Union[str, List[str]]: モード
|
|
21
|
+
"""
|
|
22
|
+
return 'audit'
|
|
23
|
+
|
|
24
|
+
def get_cmd(self):
|
|
25
|
+
"""
|
|
26
|
+
この機能のコマンドを返します
|
|
27
|
+
|
|
28
|
+
Returns:
|
|
29
|
+
str: コマンド
|
|
30
|
+
"""
|
|
31
|
+
return 'createdb'
|
|
32
|
+
|
|
33
|
+
def get_option(self):
|
|
34
|
+
"""
|
|
35
|
+
この機能のオプションを返します
|
|
36
|
+
|
|
37
|
+
Returns:
|
|
38
|
+
Dict[str, Any]: オプション
|
|
39
|
+
"""
|
|
40
|
+
return dict(
|
|
41
|
+
use_redis=self.USE_REDIS_MEIGHT, nouse_webmode=True,
|
|
42
|
+
discription_ja="監査を記録するデータベースを作成します。",
|
|
43
|
+
discription_en="Create a database to record audits.",
|
|
44
|
+
choice=[
|
|
45
|
+
dict(opt="pg_host", type=Options.T_STR, default='localhost', required=True, multi=False, hide=False, choice=None,
|
|
46
|
+
discription_ja="postgresqlホストを指定する。",
|
|
47
|
+
discription_en="Specify the postgresql host."),
|
|
48
|
+
dict(opt="pg_port", type=Options.T_INT, default=5432, required=True, multi=False, hide=False, choice=None,
|
|
49
|
+
discription_ja="postgresqlのポートを指定する。",
|
|
50
|
+
discription_en="Specify the postgresql port."),
|
|
51
|
+
dict(opt="pg_user", type=Options.T_STR, default='postgres', required=True, multi=False, hide=False, choice=None,
|
|
52
|
+
discription_ja="postgresqlのユーザー名を指定する。",
|
|
53
|
+
discription_en="Specify the postgresql user name."),
|
|
54
|
+
dict(opt="pg_password", type=Options.T_STR, default='postgres', required=True, multi=False, hide=False, choice=None,
|
|
55
|
+
discription_ja="postgresqlのパスワードを指定する。",
|
|
56
|
+
discription_en="Specify the postgresql password."),
|
|
57
|
+
dict(opt="pg_dbname", type=Options.T_STR, default='audit', required=True, multi=False, hide=False, choice=None,
|
|
58
|
+
discription_ja="postgresqlデータベース名を指定します。",
|
|
59
|
+
discription_en="Specify the postgresql database name."),
|
|
60
|
+
dict(opt="new_pg_dbname", type=Options.T_STR, default='audit', required=True, multi=False, hide=False, choice=None,
|
|
61
|
+
discription_ja="新しいpostgresqlデータベース名を指定します。",
|
|
62
|
+
discription_en="Specify a new postgresql database name."),
|
|
63
|
+
|
|
64
|
+
dict(opt="host", type=Options.T_STR, default=self.default_host, required=True, multi=False, hide=True, choice=None, web="mask",
|
|
65
|
+
discription_ja="Redisサーバーのサービスホストを指定します。",
|
|
66
|
+
discription_en="Specify the service host of the Redis server."),
|
|
67
|
+
dict(opt="port", type=Options.T_INT, default=self.default_port, required=True, multi=False, hide=True, choice=None, web="mask",
|
|
68
|
+
discription_ja="Redisサーバーのサービスポートを指定します。",
|
|
69
|
+
discription_en="Specify the service port of the Redis server."),
|
|
70
|
+
dict(opt="password", type=Options.T_STR, default=self.default_pass, required=True, multi=False, hide=True, choice=None, web="mask",
|
|
71
|
+
discription_ja=f"Redisサーバーのアクセスパスワード(任意)を指定します。省略時は `{self.default_pass}` を使用します。",
|
|
72
|
+
discription_en=f"Specify the access password of the Redis server (optional). If omitted, `{self.default_pass}` is used."),
|
|
73
|
+
dict(opt="svname", type=Options.T_STR, default="server", required=True, multi=False, hide=True, choice=None, web="readonly",
|
|
74
|
+
discription_ja="サーバーのサービス名を指定します。省略時は `server` を使用します。",
|
|
75
|
+
discription_en="Specify the service name of the inference server. If omitted, `server` is used."),
|
|
76
|
+
dict(opt="retry_count", type=Options.T_INT, default=3, required=False, multi=False, hide=True, choice=None,
|
|
77
|
+
discription_ja="Redisサーバーへの再接続回数を指定します。0以下を指定すると永遠に再接続を行います。",
|
|
78
|
+
discription_en="Specifies the number of reconnections to the Redis server.If less than 0 is specified, reconnection is forever."),
|
|
79
|
+
dict(opt="retry_interval", type=Options.T_INT, default=5, required=False, multi=False, hide=True, choice=None,
|
|
80
|
+
discription_ja="Redisサーバーに再接続までの秒数を指定します。",
|
|
81
|
+
discription_en="Specifies the number of seconds before reconnecting to the Redis server."),
|
|
82
|
+
dict(opt="timeout", type=Options.T_INT, default="15", required=False, multi=False, hide=True, choice=None,
|
|
83
|
+
discription_ja="サーバーの応答が返ってくるまでの最大待ち時間を指定。",
|
|
84
|
+
discription_en="Specify the maximum waiting time until the server responds."),
|
|
85
|
+
]
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def get_svcmd(self):
|
|
90
|
+
"""
|
|
91
|
+
この機能のサーバー側のコマンドを返します
|
|
92
|
+
|
|
93
|
+
Returns:
|
|
94
|
+
str: サーバー側のコマンド
|
|
95
|
+
"""
|
|
96
|
+
return 'audit_create'
|
|
97
|
+
|
|
98
|
+
def apprun(self, logger:logging.Logger, args:argparse.Namespace, tm:float, pf:List[Dict[str, float]]=[]) -> Tuple[int, Dict[str, Any], Any]:
|
|
99
|
+
"""
|
|
100
|
+
この機能の実行を行います
|
|
101
|
+
|
|
102
|
+
Args:
|
|
103
|
+
logger (logging.Logger): ロガー
|
|
104
|
+
args (argparse.Namespace): 引数
|
|
105
|
+
tm (float): 実行開始時間
|
|
106
|
+
pf (List[Dict[str, float]]): 呼出元のパフォーマンス情報
|
|
107
|
+
|
|
108
|
+
Returns:
|
|
109
|
+
Tuple[int, Dict[str, Any], Any]: 終了コード, 結果, オブジェクト
|
|
110
|
+
"""
|
|
111
|
+
if args.svname is None:
|
|
112
|
+
msg = dict(warn=f"Please specify the --svname option.")
|
|
113
|
+
common.print_format(msg, args.format, tm, args.output_json, args.output_json_append, pf=pf)
|
|
114
|
+
return 1, msg, None
|
|
115
|
+
if args.pg_host is None:
|
|
116
|
+
msg = dict(warn=f"Please specify the --pg_host option.")
|
|
117
|
+
common.print_format(msg, args.format, tm, args.output_json, args.output_json_append, pf=pf)
|
|
118
|
+
return 1, msg, None
|
|
119
|
+
if args.pg_port is None:
|
|
120
|
+
msg = dict(warn=f"Please specify the --pg_port option.")
|
|
121
|
+
common.print_format(msg, args.format, tm, args.output_json, args.output_json_append, pf=pf)
|
|
122
|
+
return 1, msg, None
|
|
123
|
+
if args.pg_user is None:
|
|
124
|
+
msg = dict(warn=f"Please specify the --pg_user option.")
|
|
125
|
+
common.print_format(msg, args.format, tm, args.output_json, args.output_json_append, pf=pf)
|
|
126
|
+
return 1, msg, None
|
|
127
|
+
if args.pg_password is None:
|
|
128
|
+
msg = dict(warn=f"Please specify the --pg_password option.")
|
|
129
|
+
common.print_format(msg, args.format, tm, args.output_json, args.output_json_append, pf=pf)
|
|
130
|
+
return 1, msg, None
|
|
131
|
+
if args.pg_dbname is None:
|
|
132
|
+
msg = dict(warn=f"Please specify the --pg_dbname option.")
|
|
133
|
+
common.print_format(msg, args.format, tm, args.output_json, args.output_json_append, pf=pf)
|
|
134
|
+
return 1, msg, None
|
|
135
|
+
if args.new_pg_dbname is None:
|
|
136
|
+
msg = dict(warn=f"Please specify the --new_pg_dbname option.")
|
|
137
|
+
common.print_format(msg, args.format, tm, args.output_json, args.output_json_append, pf=pf)
|
|
138
|
+
return 1, msg, None
|
|
139
|
+
|
|
140
|
+
pg_host_b64 = convert.str2b64str(args.pg_host)
|
|
141
|
+
pg_port = args.pg_port if isinstance(args.pg_port, int) else None
|
|
142
|
+
pg_user_b64 = convert.str2b64str(args.pg_user)
|
|
143
|
+
pg_password_b64 = convert.str2b64str(args.pg_password)
|
|
144
|
+
pg_dbname_b64 = convert.str2b64str(args.pg_dbname)
|
|
145
|
+
new_pg_dbname_b64 = convert.str2b64str(args.new_pg_dbname)
|
|
146
|
+
|
|
147
|
+
cl = client.Client(logger, redis_host=args.host, redis_port=args.port, redis_password=args.password, svname=args.svname)
|
|
148
|
+
ret = cl.redis_cli.send_cmd(self.get_svcmd(),
|
|
149
|
+
[pg_host_b64, pg_port, pg_user_b64, pg_password_b64, pg_dbname_b64, new_pg_dbname_b64],
|
|
150
|
+
retry_count=args.retry_count, retry_interval=args.retry_interval, timeout=args.timeout)
|
|
151
|
+
common.print_format(ret, False, tm, None, False, pf=pf)
|
|
152
|
+
if 'success' not in ret:
|
|
153
|
+
return 1, ret, cl
|
|
154
|
+
return 0, ret, cl
|
|
155
|
+
|
|
156
|
+
def is_cluster_redirect(self):
|
|
157
|
+
"""
|
|
158
|
+
クラスター宛のメッセージの場合、メッセージを転送するかどうかを返します
|
|
159
|
+
|
|
160
|
+
Returns:
|
|
161
|
+
bool: メッセージを転送する場合はTrue
|
|
162
|
+
"""
|
|
163
|
+
return False
|
|
164
|
+
|
|
165
|
+
def svrun(self, data_dir:Path, logger:logging.Logger, redis_cli:redis_client.RedisClient, msg:List[str],
|
|
166
|
+
sessions:Dict[str, Dict[str, Any]]) -> int:
|
|
167
|
+
"""
|
|
168
|
+
この機能のサーバー側の実行を行います
|
|
169
|
+
|
|
170
|
+
Args:
|
|
171
|
+
data_dir (Path): データディレクトリ
|
|
172
|
+
logger (logging.Logger): ロガー
|
|
173
|
+
redis_cli (redis_client.RedisClient): Redisクライアント
|
|
174
|
+
msg (List[str]): 受信メッセージ
|
|
175
|
+
sessions (Dict[str, Dict[str, Any]]): セッション情報
|
|
176
|
+
|
|
177
|
+
Returns:
|
|
178
|
+
int: 終了コード
|
|
179
|
+
"""
|
|
180
|
+
pg_host = convert.b64str2str(msg[2])
|
|
181
|
+
pg_port = int(msg[3]) if msg[3]!='None' else None
|
|
182
|
+
pg_user = convert.b64str2str(msg[4])
|
|
183
|
+
pg_password = convert.b64str2str(msg[5])
|
|
184
|
+
pg_dbname = convert.b64str2str(msg[6])
|
|
185
|
+
new_pg_dbname = convert.b64str2str(msg[7])
|
|
186
|
+
st = self.createdb(msg[1], pg_host, pg_port, pg_user, pg_password, pg_dbname, new_pg_dbname,
|
|
187
|
+
data_dir, logger, redis_cli)
|
|
188
|
+
return st
|
|
189
|
+
|
|
190
|
+
def createdb(self, reskey:str, pg_host:str, pg_port:int, pg_user:str, pg_password:str, pg_dbname:str, new_pg_dbname:str,
|
|
191
|
+
data_dir:Path, logger:logging.Logger, redis_cli:redis_client.RedisClient) -> int:
|
|
192
|
+
"""
|
|
193
|
+
監査ログデータベースを作成する
|
|
194
|
+
|
|
195
|
+
Args:
|
|
196
|
+
reskey (str): レスポンスキー
|
|
197
|
+
pg_host (str): PostgreSQLホスト
|
|
198
|
+
pg_port (int): PostgreSQLポート
|
|
199
|
+
pg_user (str): PostgreSQLユーザー
|
|
200
|
+
pg_password (str): PostgreSQLパスワード
|
|
201
|
+
pg_dbname (str): PostgreSQLデータベース名
|
|
202
|
+
new_pg_dbname (str): 新しいPostgreSQLデータベース名
|
|
203
|
+
data_dir (Path): データディレクトリ
|
|
204
|
+
logger (logging.Logger): ロガー
|
|
205
|
+
redis_cli (redis_client.RedisClient): Redisクライアント
|
|
206
|
+
|
|
207
|
+
Returns:
|
|
208
|
+
int: レスポンスコード
|
|
209
|
+
"""
|
|
210
|
+
try:
|
|
211
|
+
constr = f"host={pg_host} port={pg_port} user={pg_user} password={pg_password} dbname={pg_dbname} connect_timeout=5"
|
|
212
|
+
with psycopg.connect(constr, autocommit=True) as conn:
|
|
213
|
+
cursor = conn.cursor()
|
|
214
|
+
try:
|
|
215
|
+
cursor.execute(f'create database {new_pg_dbname}')
|
|
216
|
+
rescode, msg = (self.RESP_SCCESS, dict(success=True))
|
|
217
|
+
redis_cli.rpush(reskey, msg)
|
|
218
|
+
return rescode
|
|
219
|
+
finally:
|
|
220
|
+
cursor.close()
|
|
221
|
+
except Exception as e:
|
|
222
|
+
logger.warning(f"Failed to createdb: {e}", exc_info=True)
|
|
223
|
+
redis_cli.rpush(reskey, dict(warn=f"Failed to createdb: {e}"))
|
|
224
|
+
return self.RESP_WARN
|