ohadschn/letsencrypt-webapp-renewer

PS script to add config

chrfin opened this issue ยท 9 comments

Hi,

based on Jason Haley's scripts I did create a PS script to easily add new web app's to the config:

.\AddWebAppToLetsEncrypt.ps1 -webAppTarget "AppName" -webAppHosts "appname.com"

Here the script, if someone else wants to use it:

param(
	[string]$webAppTarget,
	[string]$webAppHosts,
	[string]$renewXNumberOfDaysBeforeExpiration = "-1", # set this to e.g. 65 after first request to avoid hitting the cert-limit if the job is triggerd manually more than 5 times
	[string]$tenantId = "<default tenant Id>",
	[string]$subscriptionId = "<default subscription Id>",
	[string]$webAppLetsEncrypt = "<default letsencrypt web app>",
	[string]$resourceGroupLetsEncrypt = "<default resource group of letsencrypt web app>",
	[string]$clientId = "<default client Id>",
	[string]$clientSecret = "<default client secret>",
	[string]$resourceGroupTarget = "<default target resource group>",
	[string]$email = "<default email>")

Login-AzureRmAccount

Set-AzureRmContext -SubscriptionId $subscriptionId

# Load Existing Web App settings for source and target
$webAppSource = Get-AzureRmWebAppSlot -ResourceGroupName $resourceGroupLetsEncrypt -Name $webAppLetsEncrypt -Slot "production"

# Get reference to the source app settings
$appSettingsSource = $webAppSource.SiteConfig.AppSettings

# Create Hash variable for App Settings
$appSettingsTarget = @{}

# Copy over all Existing App Settings to the Hash
ForEach ($appSettingSource in $appSettingsSource) {
    $appSettingsTarget[$appSettingSource.Name] = $appSettingSource.Value
}

# Add new settings
$appSettingsTarget["letsencrypt:" + $webAppTarget + "-clientId"] = $clientId
$appSettingsTarget["letsencrypt:" + $webAppTarget + "-email"] = $email
$appSettingsTarget["letsencrypt:" + $webAppTarget + "-hosts"] = $webAppHosts
$appSettingsTarget["letsencrypt:" + $webAppTarget + "-renewXNumberOfDaysBeforeExpiration"] = $renewXNumberOfDaysBeforeExpiration
$appSettingsTarget["letsencrypt:" + $webAppTarget + "-resourceGroup"] = $resourceGroupTarget
$appSettingsTarget["letsencrypt:" + $webAppTarget + "-subscriptionId"] = $subscriptionId
$appSettingsTarget["letsencrypt:" + $webAppTarget + "-tenantId"] = $tenantId 
if ($appSettingsTarget.ContainsKey("letsencrypt:webApps")) {
	if (!$appSettingsTarget["letsencrypt:webApps"].ToLower().Contains($webAppTarget.ToLower())) {
		$appSettingsTarget["letsencrypt:webApps"] = $appSettingsTarget["letsencrypt:webApps"] + ";" + $webAppTarget
	}
}
else {
	$appSettingsTarget["letsencrypt:webApps"] = $webAppTarget
}

# Save Settings to Target
Set-AzureRmWebAppSlot -ResourceGroupName $resourceGroupTarget -Name $webAppLetsEncrypt -Slot "production" -AppSettings $appSettingsTarget

# Get reference to the source Connection Strings
$connectionStringsSource = $webAppSource.SiteConfig.ConnectionStrings

# Create Hash variable for Connection Strings
$connectionStringsTarget = @{}

# Copy over all Existing Connection Strings to the Hash
ForEach($connStringSource in $connectionStringsSource) {
    $connectionStringsTarget[$connStringSource.Name] = @{ Type = $connStringSource.Type.ToString(); Value = $connStringSource.ConnectionString }
}

# Add new Connection String
$connectionStringsTarget["letsencrypt:" + $webAppTarget + "-clientSecret"] = @{ Type = "Custom"; Value = $clientSecret }

# Save Connection Strings to Target
Set-AzureRmWebAppSlot -ResourceGroupName $resourceGroupTarget -Name $webAppLetsEncrypt -Slot "production" -ConnectionStrings $connectionStringsTarget

Hey, this is really cool!

Would you mind making a PR out of it (including a section in the README), so everyone could enjoy it?
One thing I would add is validation that webAppTarget doesn't already exist.

BTW, check out my markdown skillz - syntax highlighting powers activate!

Hi,

I will improve the script a little as suggested and then create a PR, but I may need a few days until I have time for it...

regards
Christoph

No pressure, just close this issue when you make the PR please.
Thanks!

Took me "a little" longer but I just created the PR...

Thanks, better late than never!
Added a few small comments on the PR.

Thanks for your feedback, I updated the PR with some changes...

I took the liberty of making some improvements to the script, let me know what you think.
I changed the way parameters work as I want to keep it generic, users can always create their own one-liner local script that calls it with the parameters they desire...

Looks good to me. Using a second script to save the "common values" to be able to call it as I did is a good idea to avoid editing the main script, but still be able to reuse the common values!

Great, thanks again!
PR merged ๐Ÿ‘Œ