@echo off
setlocal enableDelayedExpansion
set "SCRIPT_DIR=%~dp0"

echo **************************************************************************
echo ** This tool will be deprecated in the next major release of OpenSearch **
echo ** https://github.com/opensearch-project/security/issues/1755           **
echo **************************************************************************

echo.
echo OpenSearch Security Demo Installer
echo  ** Warning: Do not use on production or public reachable systems **

echo.

set "assumeyes=0"
set "initsecurity=0"
set "cluster_mode=0"
set "skip_updates=-1"

goto :GETOPTS

:show_help
echo install_demo_configuration.bat [-y] [-i] [-c]
echo   -h show help
echo   -y confirm all installation dialogues automatically
echo   -i initialize Security plugin with default configuration (default is to ask if -y is not given)
echo   -c enable cluster mode by binding to all network interfaces (default is to ask if -y is not given)
echo   -s skip updates if config is already applied to opensearch.yml
EXIT /B 0

:GETOPTS
if /I "%1" == "-h" call :show_help & exit /b 0
if /I "%1" == "-y" set "assumeyes=1"
if /I "%1" == "-i" set "initsecurity=1"
if /I "%1" == "-c" set "cluster_mode=1"
if /I "%1" == "-s" set "skip_updates=0"
shift
if not "%1" == "" goto :GETOPTS

if "%1" == "--" shift

if %assumeyes% == 0 (
    set /p "response=Install demo certificates? [y/N] "
    if /I "!response!" neq "Y" exit /b 0
)

if %initsecurity% == 0 (
    if %assumeyes% == 0 (
        set /p "response=Initialize Security Modules? [y/N] "
        if /I "!response!" == "Y" (set "initsecurity=1") ELSE (set "initsecurity=0")
    )
)

if %cluster_mode% == 0 (
    if %assumeyes% == 0 (
        echo Cluster mode requires maybe additional setup of:
        echo   - Virtual memory [vm.max_map_count]
        echo.
        set /p "response=Enable cluster mode? [y/N] "
        if /I "!response!" == "Y" (set "cluster_mode=1") ELSE (set "cluster_mode=0")
    )
)

set BASE_DIR=%SCRIPT_DIR%\..\..\..\
if not exist %BASE_DIR% (
    echo "basedir does not exist"
    exit /b 1
)

set "CUR=%cd%"
cd %BASE_DIR%
set "BASE_DIR=%cd%\"
cd %CUR%
echo Basedir: %BASE_DIR%

set "OPENSEARCH_CONF_FILE=%BASE_DIR%config\opensearch.yml"
set "OPENSEARCH_CONF_DIR=%BASE_DIR%config\"
set "OPENSEARCH_BIN_DIR=%BASE_DIR%bin\"
set "OPENSEARCH_PLUGINS_DIR=%BASE_DIR%plugins\"
set "OPENSEARCH_MODULES_DIR=%BASE_DIR%modules\"
set "OPENSEARCH_LIB_PATH=%BASE_DIR%lib\"
set "OPENSEARCH_INSTALL_TYPE=.zip"

if not exist %OPENSEARCH_CONF_FILE% (
    echo Unable to determine OpenSearch config file. Quit.
    exit /b 1
)

if not exist %OPENSEARCH_BIN_DIR% (
	echo Unable to determine OpenSearch bin directory. Quit.
	exit /b 1
)

if not exist %OPENSEARCH_PLUGINS_DIR% (
	echo Unable to determine OpenSearch plugins directory. Quit.
	exit /b 1
)

if not exist %OPENSEARCH_MODULES_DIR% (
	echo Unable to determine OpenSearch modules directory. Quit.
	exit /b 1
)

if not exist %OPENSEARCH_LIB_PATH% (
	echo Unable to determine OpenSearch lib directory. Quit.
	exit /b 1
)

if not exist %OPENSEARCH_PLUGINS_DIR%\opensearch-security\ (
    echo OpenSearch Security plugin not installed. Quit.
    exit /b 1
)

set "OPENSEARCH_VERSION="
for %%F in ("%OPENSEARCH_LIB_PATH%opensearch-*.jar") do set "OPENSEARCH_VERSION=%%~nxF" & goto :opensearch_version
:opensearch_version
set "OPENSEARCH_JAR_VERSION="
for /f "tokens=2 delims=[-]" %%a in ("%OPENSEARCH_VERSION%") do set "OPENSEARCH_JAR_VERSION=%%a"

set "SECURITY_VERSION="
for %%F in ("%OPENSEARCH_PLUGINS_DIR%\opensearch-security\opensearch-security-*.jar") do set "SECURITY_VERSION=%%~nxF"
set "SECURITY_JAR_VERSION="
for /f "tokens=3 delims=[-]" %%a in ("%SECURITY_VERSION%") do set "SECURITY_JAR_VERSION=%%a"

for /f "tokens=4-7 delims=[.] " %%i in ('ver') do (if %%i==Version (set "OS=%%j.%%k") else (set v="%%i.%%j"))
echo OpenSearch install type: %OPENSEARCH_INSTALL_TYPE% on %OS%
echo OpenSearch config dir: %OPENSEARCH_CONF_DIR%
echo OpenSearch config file: %OPENSEARCH_CONF_FILE%
echo OpenSearch bin dir: %OPENSEARCH_BIN_DIR%
echo OpenSearch plugins dir: %OPENSEARCH_PLUGINS_DIR%
echo OpenSearch lib dir: %OPENSEARCH_LIB_PATH%
echo Detected OpenSearch Version: %OPENSEARCH_JAR_VERSION%
echo Detected OpenSearch Security Version: %SECURITY_JAR_VERSION%

>nul findstr /c:"plugins.security" "%OPENSEARCH_CONF_FILE%" && (
  echo %OPENSEARCH_CONF_FILE% seems to be already configured for Security. Quit.
  exit /b %skip_updates%
)

set LF=^


:: two empty line required after LF
set ADMIN_CERT=-----BEGIN CERTIFICATE-----!LF!^
MIIEdzCCA1+gAwIBAgIGAWLrc1O4MA0GCSqGSIb3DQEBCwUAMIGPMRMwEQYKCZIm!LF!^
iZPyLGQBGRYDY29tMRcwFQYKCZImiZPyLGQBGRYHZXhhbXBsZTEZMBcGA1UECgwQ!LF!^
RXhhbXBsZSBDb20gSW5jLjEhMB8GA1UECwwYRXhhbXBsZSBDb20gSW5jLiBSb290!LF!^
IENBMSEwHwYDVQQDDBhFeGFtcGxlIENvbSBJbmMuIFJvb3QgQ0EwHhcNMTgwNDIy!LF!^
MDM0MzQ3WhcNMjgwNDE5MDM0MzQ3WjBNMQswCQYDVQQGEwJkZTENMAsGA1UEBwwE!LF!^
dGVzdDEPMA0GA1UECgwGY2xpZW50MQ8wDQYDVQQLDAZjbGllbnQxDTALBgNVBAMM!LF!^
BGtpcmswggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDCwgBOoO88uMM8!LF!^
dREJsk58Yt4Jn0zwQ2wUThbvy3ICDiEWhiAhUbg6dTggpS5vWWJto9bvaaqgMVoh!LF!^
ElfYHdTDncX3UQNBEP8tqzHON6BFEFSGgJRGLd6f5dri6rK32nCotYS61CFXBFxf!LF!^
WumXjSukjyrcTsdkR3C5QDo2oN7F883MOQqRENPzAtZi9s3jNX48u+/e3yvJzXsB!LF!^
GS9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wEB/zAf!LF!^
BgNVHSMEGDAWgBSSNQzgDx4rRfZNOfN7X6LmEpdAczAdBgNVHQ4EFgQUkjUM4A8e!LF!^
K0X2TTnze1+i5hKXQHMwDgYDVR0PAQH/BAQDAgGGMA0GCSqGSIb3DQEBCwUAA4IB!LF!^
AQBoQHvwsR34hGO2m8qVR9nQ5Klo5HYPyd6ySKNcT36OZ4AQfaCGsk+SecTi35QF!LF!^
RHL3g2qffED4tKR0RBNGQSgiLavmHGCh3YpDupKq2xhhEeS9oBmQzxanFwWFod4T!LF!^
nnsG2cCejyR9WXoRzHisw0KJWeuNlwjUdJY0xnn16srm1zL/M/f0PvCyh9HU1mF1!LF!^
ivnOSqbDD2Z7JSGyckgKad1Omsg/rr5XYtCeyJeXUPcmpeX6erWJJNTUh6yWC/hY!LF!^
G/dFC4xrJhfXwz6Z0ytUygJO32bJG4Np2iGAwvvgI9EfxzEv/KP+FGrJOvQJAq4/!LF!^
BU36ZAa80W/8TBnqZTkNnqZV!LF!^
-----END CERTIFICATE-----!LF!


echo !ADMIN_CERT! > "%OPENSEARCH_CONF_DIR%kirk.pem"
echo !NODE_CERT! > "%OPENSEARCH_CONF_DIR%esnode.pem"
echo !ROOT_CA! > "%OPENSEARCH_CONF_DIR%root-ca.pem"
echo !NODE_KEY! > "%OPENSEARCH_CONF_DIR%esnode-key.pem"
echo !ADMIN_CERT_KEY! > "%OPENSEARCH_CONF_DIR%kirk-key.pem"

echo. >>  "%OPENSEARCH_CONF_FILE%"
echo ######## Start OpenSearch Security Demo Configuration ######## >> "%OPENSEARCH_CONF_FILE%"
echo # WARNING: revise all the lines below before you go into production >> "%OPENSEARCH_CONF_FILE%"
echo plugins.security.ssl.transport.pemcert_filepath: esnode.pem >> "%OPENSEARCH_CONF_FILE%"
echo plugins.security.ssl.transport.pemkey_filepath: esnode-key.pem >> "%OPENSEARCH_CONF_FILE%"
echo plugins.security.ssl.transport.pemtrustedcas_filepath: root-ca.pem >> "%OPENSEARCH_CONF_FILE%"
echo plugins.security.ssl.transport.enforce_hostname_verification: false >> "%OPENSEARCH_CONF_FILE%"
echo plugins.security.ssl.http.enabled: true >> "%OPENSEARCH_CONF_FILE%"
echo plugins.security.ssl.http.pemcert_filepath: esnode.pem >> "%OPENSEARCH_CONF_FILE%"
echo plugins.security.ssl.http.pemkey_filepath: esnode-key.pem >> "%OPENSEARCH_CONF_FILE%"
echo plugins.security.ssl.http.pemtrustedcas_filepath: root-ca.pem >> "%OPENSEARCH_CONF_FILE%"
echo plugins.security.allow_unsafe_democertificates: true >> "%OPENSEARCH_CONF_FILE%"
if %initsecurity% == 1 (
    echo plugins.security.allow_default_init_securityindex: true >> "%OPENSEARCH_CONF_FILE%"
)
echo plugins.security.authcz.admin_dn: >> "%OPENSEARCH_CONF_FILE%"
echo   - CN=kirk,OU=client,O=client,L=test, C=de >> "%OPENSEARCH_CONF_FILE%"
echo. >> "%OPENSEARCH_CONF_FILE%"
echo plugins.security.audit.type: internal_opensearch >> "%OPENSEARCH_CONF_FILE%"
echo plugins.security.enable_snapshot_restore_privilege: true >> "%OPENSEARCH_CONF_FILE%"
echo plugins.security.check_snapshot_restore_write_privileges: true >> "%OPENSEARCH_CONF_FILE%"
echo plugins.security.restapi.roles_enabled: ["all_access", "security_rest_api_access"] >> "%OPENSEARCH_CONF_FILE%"
echo plugins.security.system_indices.enabled: true >> "%OPENSEARCH_CONF_FILE%"
echo plugins.security.system_indices.indices: [".plugins-ml-config", ".plugins-ml-connector", ".plugins-ml-model-group", ".plugins-ml-model", ".plugins-ml-task", ".opendistro-alerting-config", ".opendistro-alerting-alert*", ".opendistro-anomaly-results*", ".opendistro-anomaly-detector*", ".opendistro-anomaly-checkpoints", ".opendistro-anomaly-detection-state", ".opendistro-reports-*", ".opensearch-notifications-*", ".opensearch-notebooks", ".opensearch-observability", ".ql-datasources", ".opendistro-asynchronous-search-response*", ".replication-metadata-store", ".opensearch-knn-models"] >> "%OPENSEARCH_CONF_FILE%"

:: network.host
>nul findstr /b /c:"network.host" "%OPENSEARCH_CONF_FILE%" && (
    echo network.host already present
) || (
	if %cluster_mode% == 1 (
        echo network.host: 0.0.0.0 >> "%OPENSEARCH_CONF_FILE%"
        echo node.name: smoketestnode >> "%OPENSEARCH_CONF_FILE%"
        echo cluster.initial_cluster_manager_nodes: smoketestnode >> "%OPENSEARCH_CONF_FILE%"
    )
)

>nul findstr /b /c:"node.max_local_storage_nodes" "%OPENSEARCH_CONF_FILE%" && (
    echo node.max_local_storage_nodes already present
) || (
    echo node.max_local_storage_nodes: 3 >> "%OPENSEARCH_CONF_FILE%"
)

echo ######## End OpenSearch Security Demo Configuration ######## >> "%OPENSEARCH_CONF_FILE%"

echo ### Success
echo ### Execute this script now on all your nodes and then start all nodes
:: Generate securityadmin_demo.bat
echo. > securityadmin_demo.bat
echo %OPENSEARCH_PLUGINS_DIR%opensearch-security\tools\securityadmin.bat -cd %OPENSEARCH_CONF_DIR%opensearch-security -icl -key %OPENSEARCH_CONF_DIR%kirk-key.pem -cert %OPENSEARCH_CONF_DIR%kirk.pem -cacert %OPENSEARCH_CONF_DIR%root-ca.pem -nhnv >> securityadmin_demo.bat

if %initsecurity% == 0 (
	echo ### After the whole cluster is up execute: 
	type securityadmin_demo.bat
	echo ### or run ./securityadmin_demo.bat
    echo ### After that you can also use the Security Plugin ConfigurationGUI
) else (
    echo ### OpenSearch Security will be automatically initialized.
    echo ### If you like to change the runtime configuration 
    echo ### change the files in ../../../config/opensearch-security and execute: 
	type securityadmin_demo.bat
	echo ### or run ./securityadmin_demo.bat
	echo ### To use the Security Plugin ConfigurationGUI
)

echo ### To access your secured cluster open https://<hostname>:<HTTP port> and log in with admin/admin.
echo ### [Ignore the SSL certificate warning because we installed self-signed demo certificates]
