2021年11月26日 星期五

MongoDB Atlas

IP 地址白名單


為了安全,mongodb atlas 資料庫,除了連線時需要提供使用者名稱與密碼之外,可以設定IP地址(IP Address)白名單。只有列在白名單的電腦可以存取它。

白名單中,可以是單一IP,也可以是CIDR IP 區間(CIDR-notated range of addresses)。

CIDR IP 區間

甚麼是CIDR IP 區間?

舉例來說:
  • 10.0.0.0/24 代表的最小IP是10.0.0.0,最大IP是10.0.0.255
  • 10.0.0.0/16 代表的最小IP是10.0.0.0,最大IP是10.0.255.255
  • 10.0.0.0/8 代表的最小IP是10.0.0.0,最大IP是10.255.255.255
  • 0.0.0.0/0 代表說有的IP
  • 10.0.0.0/32 代表單一IP,即10.0.0.0
摘錄 https://www.ipaddressguide.com/cidr 網站的一段話
CIDR is the short for Classless Inter-Domain Routing, an IP addressing scheme that replaces the older system based on classes A, B, and C. A single IP address can be used to designate many unique IP addresses with CIDR. A CIDR IP address looks like a normal IP address except that it ends with a slash followed by a number, called the IP network prefix. CIDR addresses reduce the size of routing tables and make more IP addresses available within organizations.

該網站也提供CIDR IP 區間對應的最小IP、最大IP與Netmask。

將 Azure Web App 的 IP 地址列入白名單

Azure Web App 要存取 MongoDB Atlas,必須將 App 的 outbound IP 地址列入資料庫的白名單中。

問題來了,App 的 outbound IP 地址不是一個而是一大串(當 App Scale Up/Down 時還會變動)。

我們可以用 Azure Powershell 指令 (Get-AzWebApp -Name gglapp-azure).outboundIpAddresses 來取得 outbound IP 群後,在資料庫端的 Network Access > IP Access IP List > Add Ip Address 裡一筆一筆的的加入白名單內。

請參考 Azure App Service Documentation >  Concepts > Networking > Inbound and outbound IPs 的  https://docs.microsoft.com/en-us/azure/app-service/overview-inbound-outbound-ips 。

2021年10月28日 星期四

了解Azure



Azure 帳號

Build your .NET apps with your Azure free account 建立了Azure 免費帳號。建帳號時,必須提供信用卡資料,先扣款台幣30元確認信用卡的正確性再退款台幣30元。建完免費帳號後,https://portal.azure.com/#home 就是自己在Azure的門戶網站。


Azure for .NET developers

Azure for .NET developers 是學習的起點。


第一個 Web App 發佈成 Azure Application Service

Quickstart: Deploy an ASP.NET web app 示範在Visual Studio 裡建立 Aspnet Web Core App,發布到Azure App Service。
  1. 以 AspNetCore Web App 模板起始沒有認證與Docker功能的Web App。
  2. 發佈到Azure自己的帳號裡。
  3. 用瀏覽器瀏覽到發佈到Azure的Web App。
  4. 更改程式、再次發佈、再次瀏覽。
  5. 到自己的門戶網站管理自己帳戶的資源使用。

第一個 Web App 發佈成 Azure Container Instance 

Quickstart: Docker in Visual Studio 示範將Web App 做成 Docker Container 發佈到 Azure

  1. 用 AspNet Core Web App 模板,選擇 Enable Docker + Docker OS Linux,製成 WebAppDockerAzure 專案。
  2. 若要在 Docker 裡進行偵錯,必須參考 Container Tools launch settings,修改launchSettings.json 裡面關於Docker 的設定。
  3. 專案結點上點擊右鍵,發佈 Docker 映像到 Azure Container Registry
  4. Quickstart: Deploy a container instance in Azure using the Azure portal

第一個由前端與後段組成的 Web App,發佈成 Azure Application Service

Container Tools in Visual Studio 裡面的 Tutorial: Create a multi-container app with Docker Compose 示範如何用 Docker Compose 製作由兩個映像組成的Webp App,講解如何在在地端測試組合效果。

發布映像組的Azure Web App
參考 Create a multi-container (preview) app using a Docker Compose configuration 的 "az webapp create ... --multicontainer-config-type compose ... compose-wordpress.yml" 與
Migrate custom software to Azure App Service using a custom container 的 az role assignment create ..., az resource update ...,製造powershell 指令檔 gglapp-azure-final.ps1。


在下列條件下
  • 已安裝Azure-CLI
  • Powershell 已安裝 Azure Powershell Module(Az)
  • 在地端的Desktop Docker已有 gglcr.azurecr.io/<api>, gglcr.azurecr.io/<front> 兩映像
  • Azure 帳戶內沒有 gglgroup(Azure Resource Group)
  • <compose-azure.ps1> 與 <compose-azure.yml>  在工作目錄中
從 Powershell 內執行 .\<compose-azure.ps1>,可產生 <app-name>(Azure WebApp),之後只要瀏覽到 https://<app-name>.azurewebsites.net 就可使用。

<compose-azure.ps1> 的內容:
# https://docs.microsoft.com/en-us/azure/app-service/tutorial-custom-container?pivots=container-linux#configure-app-service-to-deploy-the-image-from-the-registry
# https://docs.microsoft.com/en-us/azure/app-service/tutorial-multi-container-app#create-a-docker-compose-app

# Login to Azure
Connect-AzAccount

# Create gglgroup(ResourceGroup) at japaneast(Location)
New-AzResourceGroup -Name gglgroup -Location japaneast

# Create gglcr(ContainerRegistry) with basic(Tier) in gglgroup
New-AzContainerRegistry -ResourceGroupName gglgroup -Name gglcr -Sku "Basic" -EnableAdminUser

# Create gglappserviceplan(AppServicePlan) with basic(Tier), linux(OS) at japaneast(Location)
New-AzAppServicePlan -ResourceGroupName gglgroup -Name gglappserviceplan -Location japaneast -Tier "Basic" -Linux

# Use docker to push local docker images, 
#docker tag <front>gglcr.azurecr.io/<front>
#docker tag <api> gglcr.azurecr.io/<api>
## step 1 :set variable $password with password for gglcr
$password = (Get-AzContainerRegistryCredential -ResourceGroupName gglgroup -Name gglcr).password
## step 2 : login to gglcr
az acr login --name gglcr --username gglcr --password $password
## step 3 : push <api> and <front> to gglcr
docker push gglcr.azurecr.io/<front>
docker push gglcr.azurecr.io/<api>

# Create gglapp-azure(Web App) with gglappserviceplan(AppServicePlane) in gglgroup(ResourceGroup) using <compose-azure.yml>(ComposeFile)
az webapp create --resource-group gglgroup --plan gglappserviceplan --name <app-name> --multicontainer-config-type compose --multicontainer-config-file <compose-azure.yml>

# Assign managed service identity [system] to gglapp-azure(Web App).
az webapp identity assign --resource-group gglgroup --name <app-name>

## Retrieve <app-name> as $app(Variable)
$app = Get-AzWebApp -name <app-name>
## Retrieve subscription-id as $subscriptionid(Variable) $subscriptionid = (Get-AzSubscription).id # Grant [system](the managed identity) permission to access the gglcr(Container Registry) az role assignment create --assignee $app.identity.principalid --scope /subscriptions/$subscriptionid/resourceGroups/gglgroup/providers/Microsoft.ContainerRegistry/registries/gglcr --role "AcrPull" # Configure <app-name>(Web App) to use [system](managed identity) to pull from gglcr(ContainerRegistry). az resource update --ids /subscriptions/$subscriptionid/resourceGroups/gglgroup/providers/Microsoft.Web/sites/<app-name>/config/web --set properties.acrUseManagedIdentityCreds=True #Remove-AzResourceGroup -Name gglgroup -Force
<compose-azure.yml> 的內容:
version: '3.7'
services:
  <api>:
    image: gglcr.azurecr.io/<api>
    restart: always
  <front>:
    depends_on:
       - <api>
    image: gglcr.azurecr.io/<front>
    ports:
      - 80:80
    restart: always

End 

MongoDB Atlas

IP 地址白名單 為了安全,mongodb atlas 資料庫,除了連線時需要提供使用者名稱與密碼之外,可以設定IP地址(IP Address)白名單。只有列在白名單的電腦可以存取它。 白名單中,可以是單一IP,也可以是 CIDR  IP 區間( CIDR -notated ra...