Skip to content

Commit

Permalink
Merge pull request #51 from UiPath/alpha
Browse files Browse the repository at this point in the history
v20.10 Metadata
  • Loading branch information
SahilChaudhary25 authored Nov 19, 2020
2 parents 0348050 + 688103e commit 233fb37
Show file tree
Hide file tree
Showing 11 changed files with 402 additions and 111 deletions.
148 changes: 110 additions & 38 deletions database/createDatabases.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,37 @@ Param (
[Parameter(Mandatory = $false, ValueFromPipelineByPropertyName)]
[string] $dbpass,
[Parameter(Mandatory = $false, ValueFromPipelineByPropertyName)]
[string] $suffix
[string] $sauser,
[Parameter(Mandatory = $false, ValueFromPipelineByPropertyName)]
[string] $sapass,
[Parameter(Mandatory = $false, ValueFromPipelineByPropertyName)]
[string] $suffix,
[Parameter(Mandatory = $false, ValueFromPipelineByPropertyName)]
[string] $singleDatabase
)


if($windowsAuthentication.Length -eq 0){
write-host "since windowsAuthentication flag is not set, defaulting to SQL server authentication"
$windowsAuthentication = "N"
}

$username = $sauser
$password = $sapass

if($singleDatabase.Length -eq 0){
write-host "creating multiple databases"
$singleDatabase = "N"
}

if($windowsAuthentication -eq "Y"){
echo "Authenticating via Windows Authentication(i.e Using Integrated Auth)"
} elseif($windowsAuthentication -eq "N"){
$sqlCredentials = Get-Credential
#Password Validation
$username = $sqlCredentials.username
$password = $sqlCredentials.GetNetworkCredential().password
if (($username.Length -eq 0) -or ($password.Length -eq 0)){
$sqlCredentials = Get-Credential
#Password Validation
$username = $sqlCredentials.username
$password = $sqlCredentials.GetNetworkCredential().password
}

if(($username.Length -eq 0) -or ($password.Length -eq 0)){
write-host "SQL Server Auth Enabled and credentials are not supplied so exiting the program" -ForegroundColor Red
Expand Down Expand Up @@ -97,7 +112,7 @@ function executeQuery($database, $query){
Invoke-Sqlcmd -ServerInstance $sqlinstance -Database $database -Query $query -ErrorAction Stop -Verbose
write-host $query "Succeded" -ForegroundColor Yellow
} else{
Invoke-Sqlcmd -ServerInstance $sqlinstance -Database $database -Credential $sqlCredentials -Query $query -ErrorAction Stop -Verbose
Invoke-Sqlcmd -ServerInstance $sqlinstance -Database $database -username $username -password $password -Query $query -ErrorAction Stop -Verbose
write-host $query "Succeded" -ForegroundColor Yellow
}
} catch {
Expand All @@ -107,21 +122,45 @@ function executeQuery($database, $query){
}

#Install modules required
$installed = Test-Path -Path 'C:\Program Files\WindowsPowerShell\Modules\SqlServer'
if(!$installed){
if(!(Get-Module -ListAvailable -Name SqlServer)){
echo "Installing required module..."
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Install-Module -Name SqlServer -AllowClobber -Confirm:$False -Force
}

Set-ItemProperty -Path 'HKLM:\\SOFTWARE\\Wow6432Node\\Microsoft\\.NetFramework\\v4.0.30319' -Name 'SchUseStrongCrypto' -Value '1' -Type DWord -Force
Set-ItemProperty -Path 'HKLM:\\SOFTWARE\\Microsoft\\.NetFramework\\v4.0.30319' -Name 'SchUseStrongCrypto' -Value '1' -Type DWord -Force
# this seems to hold true from WINXP onward
if ($env:OS -eq "Windows_NT") {
Set-ItemProperty -Path 'HKLM:\\SOFTWARE\\Wow6432Node\\Microsoft\\.NetFramework\\v4.0.30319' -Name 'SchUseStrongCrypto' -Value '1' -Type DWord -Force
Set-ItemProperty -Path 'HKLM:\\SOFTWARE\\Microsoft\\.NetFramework\\v4.0.30319' -Name 'SchUseStrongCrypto' -Value '1' -Type DWord -Force
}

#formulate logins for single Database
$helper_login_user=$dbuser+"_helper"
$pkgmanager_login_user=$dbuser+"_pkgmanager"
$deployer_login_user=$dbuser+"_deployer"
$trainer_login_user=$dbuser+"_trainer"
$appmanager_login_user=$dbuser+"_appmanager"


if($singleDatabase -eq "N"){
$sqlcommand = "
GO
CREATE LOGIN $dbuser WITH PASSWORD=N'{{pwd}}'
GO
"
}
else {
$sqlcommand = "
GO
CREATE LOGIN $dbuser WITH PASSWORD=N'{{pwd}}'
GO
"
GO
CREATE LOGIN $helper_login_user WITH PASSWORD=N'{{pwd}}'
CREATE LOGIN $pkgmanager_login_user WITH PASSWORD=N'{{pwd}}'
CREATE LOGIN $deployer_login_user WITH PASSWORD=N'{{pwd}}'
CREATE LOGIN $trainer_login_user WITH PASSWORD=N'{{pwd}}'
CREATE LOGIN $appmanager_login_user WITH PASSWORD=N'{{pwd}}'
GO
"
}


$addUserToMasterQuery = "
GO
Expand All @@ -132,13 +171,26 @@ GO
$createDatabaseQuery = "CREATE DATABASE {{DB}}"


$grantcommand = "
GO
CREATE USER $dbuser FOR LOGIN $dbuser
GO
ALTER ROLE [db_owner] ADD MEMBER $dbuser
GO
"
if($singleDatabase -eq "N"){
$grantcommand = "
GO
CREATE USER $dbuser FOR LOGIN $dbuser
GO
ALTER ROLE [db_owner] ADD MEMBER $dbuser
GO
"
} else {
$grantcommand = "
GO
CREATE USER {{USER_NAME}} FOR LOGIN {{USER_NAME}} WITH DEFAULT_SCHEMA = {{SCHEMA_NAME}}
GO
CREATE SCHEMA {{SCHEMA_NAME}} AUTHORIZATION {{USER_NAME}}
GO
EXEC sp_addrolemember 'db_ddladmin', '{{USER_NAME}}';
GO
"
}


#If user supplied the pass use it, otherwise auto generate the pass
if($dbpass.Length -gt 0){
Expand All @@ -149,38 +201,58 @@ if($dbpass.Length -gt 0){

$sqlcommand = $sqlcommand.Replace("{{pwd}}",$unsecurepassword)

write-host $sqlcommand


#Create Login
$exitStatus = executeQuery master $sqlcommand
echo $exitStatus

if ( $exitStatus -eq "Failed" ) {
$msg = $Error[0].Exception.Message
write-host "Encountered error while creating login. Error Message is $msg." -ForegroundColor Red
write-host "If the login already exists and if the login is generated via this script the same can be found under the file name $dbuser located in the current directory" -ForegroundColor Red
write-host "If you are using WindowsAuthentication mode, make sure that you are using ServerName instead of IP" -ForegroundColor Red
exit 1
} else {
echo "the PASSWORD for $dbuser user is : $unsecurepassword "
New-Item -ItemType File -Force -Path ".\$dbuser" -Value $unsecurepassword
}

#Add login to Master
$exitStatus = executeQuery master $addUserToMasterQuery

if ( $exitStatus -eq "Failed" ) {
write-host "The target database does not support adding user to master anyhow this wont affect user from logging into DB with the generated credentials" -ForegroundColor Yellow
if($singleDatabase -eq "N"){
$exitStatus = executeQuery master $addUserToMasterQuery

if ( $exitStatus -eq "Failed" ) {
write-host "The target database does not support adding user to master anyhow this wont affect user from logging into DB with the generated credentials" -ForegroundColor Yellow
}
}

#Create DB's
executeQuery master $createDatabaseQuery.Replace("{{DB}}", "ai_helper$suffix")
executeQuery master $createDatabaseQuery.Replace("{{DB}}", "ai_deployer$suffix")
executeQuery master $createDatabaseQuery.Replace("{{DB}}", "ai_pkgmanager$suffix")
executeQuery master $createDatabaseQuery.Replace("{{DB}}", "ai_trainer$suffix")
executeQuery master $createDatabaseQuery.Replace("{{DB}}", "ai_appmanager$suffix")

if($singleDatabase -eq "N"){
#Create DB's
executeQuery master $createDatabaseQuery.Replace("{{DB}}", "ai_helper$suffix")
executeQuery master $createDatabaseQuery.Replace("{{DB}}", "ai_deployer$suffix")
executeQuery master $createDatabaseQuery.Replace("{{DB}}", "ai_pkgmanager$suffix")
executeQuery master $createDatabaseQuery.Replace("{{DB}}", "ai_trainer$suffix")
executeQuery master $createDatabaseQuery.Replace("{{DB}}", "ai_appmanager$suffix")
} else{
executeQuery master $createDatabaseQuery.Replace("{{DB}}", "aifabric$suffix")
}


if($singleDatabase -eq "N"){
#Execute Grants
executeQuery "ai_helper$suffix" $grantcommand
executeQuery "ai_deployer$suffix" $grantcommand
executeQuery "ai_pkgmanager$suffix" $grantcommand
executeQuery "ai_trainer$suffix" $grantcommand
executeQuery "ai_appmanager$suffix" $grantcommand
} else{
executeQuery "aifabric$suffix" $grantcommand.Replace("{{USER_NAME}}", $helper_login_user).Replace("{{SCHEMA_NAME}}", "ai_helper")
executeQuery "aifabric$suffix" $grantcommand.Replace("{{USER_NAME}}", $pkgmanager_login_user).Replace("{{SCHEMA_NAME}}", "ai_pkgmanager")
executeQuery "aifabric$suffix" $grantcommand.Replace("{{USER_NAME}}", $deployer_login_user).Replace("{{SCHEMA_NAME}}", "ai_deployer")
executeQuery "aifabric$suffix" $grantcommand.Replace("{{USER_NAME}}", $trainer_login_user).Replace("{{SCHEMA_NAME}}", "ai_trainer")
executeQuery "aifabric$suffix" $grantcommand.Replace("{{USER_NAME}}", $appmanager_login_user).Replace("{{SCHEMA_NAME}}", "ai_appmanager")
}

#Execute Grants
executeQuery "ai_helper$suffix" $grantcommand
executeQuery "ai_deployer$suffix" $grantcommand
executeQuery "ai_pkgmanager$suffix" $grantcommand
executeQuery "ai_trainer$suffix" $grantcommand
executeQuery "ai_appmanager$suffix" $grantcommand
Loading

0 comments on commit 233fb37

Please sign in to comment.