fredag den 1. november 2019

Upload folder to AWS S3 using Terraform

With the introduction of terraform 0.12, the configuration can now be alot more dynamic.
The following example is written using terraform version 0.12.8 and AWS provider version 0.26.0.

The configuration creates a S3 bucket and makes it easier to upload the content of a folder and subfolders to the bucket, then sets the content type for each file, in this example indented for a static website.
Configuring the bucket as a website have not been included in this example.

resource "aws_s3_bucket" "website" {
  bucket = var.fqdn
  acl = "private" 
}

resource "aws_s3_bucket_object" "website_files" {
  for_each = fileset(path.module, "${var.file_path}**")
  content_type = lookup(var.content_typeelement(split(".", each.value), length(split(".", each.value))-1), "text/html")
  bucket = aws_s3_bucket.website.id
  key = replace(each.value, var.file_path"")
  source = each.value
  etag = filemd5(each.value)
}

variable "content_type" {
  type = map(string)
description = "The file MIME types"
  default = {
    "html" = "text/html"
    "htm" = "text/html"
    "svg" = "image/svg+xml"
    "jpg" = "image/jpeg"
    "jpeg" = "image/jpeg"
    "gif" = "image/gif"
    "png" = "application/pdf"
    "css" = "text/css"
    "js" = "application/javascript"
    "txt" = "text/plain"
  }
}

variable "file_path" {
  type = string
  description = "The path to the folder of the files you want to upload to S3"
  default = "../website/"
}

variable "fqdn" {
  type = string
  description = "The fully qualified domain name for the website"
  default = "www.example.com"
}

tirsdag den 21. februar 2017

Free SSL certificate on Unifi Controller on Windows

When you setup a Unifi Controller, the default certificate that are provided with the installation, are not trusted, and you will therefore get a SSL warning in your browser when you access the site.
This guide will show you how to use Let's Encrypt and Powershell, to get a free certificate for your Unifi Controller.

Note that i have both Java and my Unifi controller placed in "C:\Program Files\" if yours are place in different folders, you need to chance the location in the commands.

Some of the commands will require admin permissions.

#You need IIS installed for automatic Let's Encrypt Verification
Install-WindowsFeature -Name "Web-Server" -IncludeAllSubFeature -IncludeManagementTools
Import-Module WebAdministration

#Install ACMESharp from Powershell Gallery

Install-PackageProvider -Name NuGet -Force
Install-Module -Name ACMESharp -Force -AllowClobber
Import-Module ACMESharp
Initialize-ACMEVault

The easiest way to automatically prove the ownership of your domain, is to let the ACMESharp module modify the default website on Microsoft's Internet Information Service, it's also possible to prove the ownership using DNS, but i use IIS in the example below.

#Prove Overship of Domain to Let's Encrypt
$CertDomain = 'domain.example.com'
New-ACMERegistration -Contacts mailto:email@example.com -AcceptTos
New-ACMEIdentifier -Dns $CertDomain -Alias dns1
Complete-ACMEChallenge -IdentifierRef dns1 -ChallengeType http-01 -Handler iis -HandlerParameters @{ WebSiteRef = 'Default Web Site' }
Submit-ACMEChallenge -IdentifierRef dns1 -ChallengeType http-01

#Wait for status to be valid

Update-ACMEIdentifier -IdentifierRef dns1 -ChallengeType http-01

You can run the Update-ACMEIdentifier as meny times as you need, you need it to return valid instead of pending, if it starts returning invalid, something has gone wrong, and you need to start over with a new IdentifierRef and ChallangeType name.

#Request the new certificate
$CertAlias = "Cert" + (get-date -f MMddyyyyHHmmss)
New-ACMECertificate -IdentifierRef dns1 -Generate -Alias $CertAlias
Submit-ACMECertificate -CertificateRef $CertAlias
Update-ACMECertificate -CertificateRef $CertAlias

According to different forum posts you need to use the password "aircontrolenterprise" for the new keystore to work with the Unifi Controller, I have not tested if this is true or not, as of writing this guide.

#Export certificate for UniFi Keystore
Get-ACMECertificate -CertificateRef $CertAlias -ExportPkcs12 "C:\Program Files\Ubiquiti UniFi\data\$CertAlias.pfx" -CertificatePassword "aircontrolenterprise"

#Stopping service
Get-Service "*UniFi*" | Stop-Service

#Backup the original keystore
Rename-Item "C:\Program Files\Ubiquiti UniFi\data\keystore" keystore.backup

#Create the New keystore
& 'C:\Program Files\Java\jre1.8.0_121\bin\keytool.exe' -importkeystore -srcstoretype pkcs12 -srcalias "1" -srckeystore "C:\Program Files\Ubiquiti UniFi\data\$CertAlias.pfx" -keystore "C:\Program Files\Ubiquiti UniFi\data\keystore" -destalias unifi -srcstorepass aircontrolenterprise -deststorepass aircontrolenterprise

#Starting service
Get-Service "*UniFi*" | Start-Service

#Stop IIS website as you don't need it anymore
Set-ItemProperty "IIS:\Sites\Default Web Site" serverAutoStart False
Get-Website "Default Web Site" | Stop-Website

1FzmPvovvS3ZsxHvT4WdZxnzPCnUcS3YRS

torsdag den 9. februar 2017

Start menu problemer på Windows 10?

Efter en Windows opdatering, har jeg oplevet at min start knap og min search knap i menu baren er holdt op med at reagere på venstre klik.

Dette er der åbenbart flere der har været ude for, hvorfor jeg her deler løsningen der virkede for mig.

  • Åben en kommando prompt med administrative rettigheder.
    • Denne kan åbnes ved at højreklikke på start og vælge "Command Prompt (Admin)".
  • Her køre du følgende kommando uden citationstegn: "sfc /scannow".
    • Dette starter en system scanning, som kan tage lidt tid.
  • Hvis du har samme fejl som mig, skulle du gerne se fejlbeskeden "Windows Resource Protection found corrupt files but was unable to fix some".
  • Nu køre du kommandoen "dism /online /cleanup-image /restorehealth".
    • Dette vil geninstallere, det Microsoft kalder Windows 10 Apps, det er de apps man kan finde i Windows Store, dette vil tage få minutter.
  • Nu skulle din start menu og search gerne virke igen.