星期日, 9月 15, 2024

GCP 建立 project with terraform 小記

GCP 建立 project with terraform 小記



OS: openSUSE Leap 15.6

Terraform: 1.9.5


隨著雲端專案越來越多, 自動化工作就越來越重要, 雖然不同的雲端平臺有各自的 CLI tool 或是 SDK, 但是有些行為是一連貫的, 從早期的 ansible, 到現在的 terraform, 都需要有工具來進行標準化的執行.


今天就來實作使用 Terraform 建立 GCP 專案 ( project )


來講一下, 爲何要先寫這一篇

  • 在 GCP 建立 project 是非常方便與迅速的, 相對於 AWS ID 建立要有 payment 與 驗證

  • 使用 Project 可以輕鬆劃分不同專案的資源與費用, 對公司內部費用歸屬很重要

  • 實際在建立 project 時, 會調整一些預設值, 例如 VPC


準備事項

已有 GCP 環境, 確認自己的 Billing acount ID

  • 可以在專案 -- > 點選 Billing -- > MANAGE BILLING ACCOUNTS



已安裝 Terraform


然後 Terraform 的驗證方式, 我是採取 ADCs ( Application Default Credentials )


接下來說明設定的檔案


官方文件 


variables.tf 檔案內容


variable "gcp_project_id" {

 default = "sakana-20240915-2"

}


variable "gcp_project_name" {

 default = "sakana-20240915-2"

}


variable "gcp_billing_account_id" {

 default = "YOUR-BILLING-ACCOUNT"

}


# 組織 ID 視狀況設定

#variable "gcp_organization_id" {

#  default = ""

#}


  • 這個檔案放我們要設定的變數內容

  • gcp_billing_account_id 的 default 內, 請替換你的 Billing accout ID

  • id 與 name 按照你的需求設定名稱與 ID


provider.tf 檔案內容


# 配置 Terraform 使用 Google 提供者。

terraform {

 required_providers {

   # 定義 Google 提供者及其來源。

   google = {

     source = "hashicorp/google"

   }

 }

}


provider "google" {

#  credentials = file("${credentials}")

#  region      = "asia-east1"

}


  • 這邊很簡單的設定 GCP 提供者與來源

  • 因為使用 ADCs, 所以沒有設定 credentials


project.tf 檔案內容


# 建立一個 Google Cloud 專案資源。

resource "google_project" "create" {

 # 不要自動建立網路,預設值為 true。

 auto_create_network = false

 # 專案的標籤,預設為空字典。

 labels              = {}

 # 專案顯示名稱,使用變數 project_name 的值。

 name                = var.gcp_project_name

 # 專案 ID,使用變數 project_name 的值。

 project_id          = var.gcp_project_id

 # 組織 ID,註解掉表示不設定。

##org_id              = var.gcp_organization_id

 # 專案的計費帳戶,使用變數 billing_account 的值。

 billing_account     = var.gcp_billing_account_id

 # 設定資源操作的逾時時間,目前為空字典,表示使用預設值。

 timeouts {}

}


  • 這邊有設定 auto_create_network 爲 false , 原因爲

    • 實務上使用 GCP 應該會在固定的 region, 不會全球都用

    • Default VPC 網段應該跟地端網管確認是否有重複, 否則之後串 VPN 會有問題, 另外不同 GCP 專案 Peering 也會遇到一樣的問題

    • 官方文件 https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/google_project 有提到建議使用 constraint 處理

    • terraform 的處理方式是會建立 GCP project 與 default VPC, 然後再進行刪除 default VPC


建立方式


修改 variables.tf 內的 billing account id / project name / project id


初始化 terraform

> terraform  init


Initializing the backend...

Initializing provider plugins...

- Finding latest version of hashicorp/google...

- Installing hashicorp/google v6.2.0...

- Installed hashicorp/google v6.2.0 (signed by HashiCorp)

Terraform has created a lock file .terraform.lock.hcl to record the provider

selections it made above. Include this file in your version control repository

so that Terraform can guarantee to make the same selections by default when

you run "terraform init" in the future.


Terraform has been successfully initialized!


You may now begin working with Terraform. Try running "terraform plan" to see

any changes that are required for your infrastructure. All Terraform commands

should now work.


If you ever set or change modules or backend configuration for Terraform,

rerun this command to reinitialize your working directory. If you forget, other

commands will detect it and remind you to do so if necessary.


  • 會下載相關資源


確認執行內容

> terraform plan


Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the

following symbols:

  + create


Terraform will perform the following actions:


  # google_project.create will be created

  + resource "google_project" "create" {

      + auto_create_network = false

      + billing_account     = "YOUR-BILLING-ID"

      + deletion_policy     = "PREVENT"

      + effective_labels    = {

          + "goog-terraform-provisioned" = "true"

        }

      + id                  = (known after apply)

      + name                = "sakana-20240915-2"

      + number              = (known after apply)

      + project_id          = "sakana-20240915-2"

      + terraform_labels    = {

          + "goog-terraform-provisioned" = "true"

        }


      + timeouts {}

    }


Plan: 1 to add, 0 to change, 0 to destroy.


  • 這邊我們沒有特別去設定 deletion_policy, 預設是 PREVENT, 防止誤刪 ( terraform destroy 沒辦法直接刪除, 需要手動刪除 )


使用 terrafom 執行建立專案

> terraform  apply


Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the

following symbols:

  + create


Terraform will perform the following actions:


  # google_project.create will be created

  + resource "google_project" "create" {

      + auto_create_network = false

      + billing_account     = "YOUR-BILLING-ID"

      + deletion_policy     = "PREVENT"

      + effective_labels    = {

          + "goog-terraform-provisioned" = "true"

        }

      + id                  = (known after apply)

      + name                = "sakana-20240915-2"

      + number              = (known after apply)

      + project_id          = "sakana-20240915-2"

      + terraform_labels    = {

          + "goog-terraform-provisioned" = "true"

        }


      + timeouts {}

    }


Plan: 1 to add, 0 to change, 0 to destroy.


Do you want to perform these actions?

  Terraform will perform the actions described above.

  Only 'yes' will be accepted to approve.


  Enter a value: yes


建立完成之後, 到 GCP console 確認專案是否建立

順便到 VPC Network 確認是否有 VPC



也同步將相關檔案放在 Github 上面


又向 terraform with GCP 前進一小步


~ enjoy it


References

星期五, 9月 13, 2024

Pulse Secure Linux client with openSUSE Leap 15.6 安裝小記

Pulse Secure Linux client with openSUSE Leap 15.6 安裝小記



OS: openSUSE Leap 15.6

Pulse Secure: 22


公司的 SSLVPN 用的是 Pulse Secure, 但是 openSUSE Leap 的參考文章相對少

今天就寫相關的安裝方式


取得套件 RPM from 公司同事


先嘗試安裝

# rpm  -ivh  ps-pulse-linux-22.7r3-b30227-installer.rpm 


error: Failed dependencies:

        gtkmm30 >= 3.22.2 is needed by pulsesecure-2:22.7-R3.x86_64

        libbsd is needed by pulsesecure-2:22.7-R3.x86_64

        libcurl >= 7.29.0 is needed by pulsesecure-2:22.7-R3.x86_64

        nss-tools is needed by pulsesecure-2:22.7-R3.x86_64

        webkit2gtk3 >= 2.24.4 is needed by pulsesecure-2:22.7-R3.x86_64


  • 直接安裝會遇到相依性的問題



安裝 mozilla-nss-tools

# zypper  install  mozilla-nss-tools


Loading repository data...

Reading installed packages...

Resolving package dependencies...


The following 4 NEW packages are going to be installed:

  libfreebl3 libsoftokn3 mozilla-nss mozilla-nss-tools


4 new packages to install.

Overall download size: 2.1 MiB. Already cached: 0 B. After the operation, additional 5.4 MiB

will be used.


Backend:  classic_rpmtrans

Continue? [y/n/v/...? shows all options] (y):y


新增兩個 repo


# zypper   addrepo   https://download.opensuse.org/repositories/server:mail/openSUSE_Tumbleweed/server:mail.repo 

  • 這個 repo 雖然有 15.6 但是他 15.5 沒有編譯過, 所以我還是維持 Tumbleweed


# zypper  addrepo  https://download.opensuse.org/repositories/devel:gcc:next/openSUSE_Tumbleweed/devel:gcc:next.repo 


  • 這個 repo 就沒有 15.6 了


更新 repo, 並信任相關 Key

# zypper  refresh


安裝 glibc 套件

# zypper  install  glibc


Loading repository data...

Reading installed packages...

There is an update candidate for 'glibc' from vendor 'obs://build.opensuse.org/devel:gcc:next', while the current vendor is 'SUSE LLC <https://www.suse.com/>'. Use 'zypper install glibc-2.40.9000.164.gc9154cad6-4264.1.x86_64' to install this candidate.

Resolving package dependencies...


The following package is going to be upgraded:

  glibc


1 package to upgrade.

Overall download size: 1.9 MiB. Already cached: 0 B. No additional space will be used or freed after

the operation.


Backend:  classic_rpmtrans

Continue? [y/n/v/...? shows all options] (y): y


  • 這次不像上一個版本有衝突


安裝 pulse secure 套件

# zypper  install  ps-pulse-linux-22.7r3-b30227-installer.rpm


Loading repository data...

Reading installed packages...

Resolving package dependencies...


Problem: 1: nothing provides 'libbsd' needed by the to be installed pulsesecure-2:22.7-R3.x86_64

 Solution 1: do not install pulsesecure-2:22.7-R3.x86_64

 Solution 2: break pulsesecure-2:22.7-R3.x86_64 by ignoring some of its dependencies


Choose from above solutions by number or cancel [1/2/c/d/?] (c):  2

Resolving dependencies...

Resolving package dependencies...


The following NEW package is going to be installed:

  pulsesecure


1 new package to install.

Overall download size: 11.8 MiB. Already cached: 0 B. After the operation, additional 33.9 MiB will

be used.


Backend:  classic_rpmtrans

Continue? [y/n/v/...? shows all options] (y): y

Retrieving: pulsesecure-2:22.7-R3.x86_64 (Plain RPM files cache)                 (1/1),  11.8 MiB    

ps-pulse-linux-22.7r3-b30227-installer.rpm:

    Package header is not signed!


pulsesecure-2:22.7-R3.x86_64 (Plain RPM files cache): Signature verification failed [6-File is unsigned]

Abort, retry, ignore? [a/r/i] (a): i


  • 這邊使用方案 2 , 忽略相依性的方式來進行安裝, 並忽略簽章問題


PulseSecure UI 在 Leap 15.6 還要補一個套件


# zypper  install  libwebkit2gtk-4_0-37


  • 因為預設的套件比較新


安裝完畢就可以找到圖形介面的程式



點選 新增的 + 按鈕

輸入 名稱與 Server URL

點選 Connect 就可以進行連線




同場加映, 文字介面連線方式


建立憑證存放資料夾

# mkdir  -p  /etc/pki/ca-trust/extracted/openssl


建立檔案

# touch  /etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt


使用 Brave 瀏覽器開啟之後要存取的網址, 例如 https://YOUR_SERVER_URL  

點選 鎖頭 -- > 點選 憑證有效的另開視窗



點選 詳細資訊



將 3 個憑證全部匯出



將憑證放入


# cat  /home/sakanamax/Builtin\ Object\ Token_TWCA\ Global\ Root\ CA  >  /etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt


# cat  /home/sakanamax/TWCA\ Secure\ SSL\ Certification\ Authority  >>  /etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt


# cat  /home/sakanamax/_.YOURS.com.tw  >>  /etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt


  • 公司憑證名稱記得換成自己的


進行連線 - 要切換成一般使用者

> /opt/pulsesecure/bin/pulselauncher  -U  https://YOUR_SERVER_URL -u   YOUR_USER


  • 實務上測試用一般使用者可以連線, root 無法連線

  • 輸入User Realm:  (詢問網管, 也許是 RWXY Users)

  • 輸入密碼


  • 連線成功可能不一定有訊息, 可以另外開一個視窗用 > ip r s 看路由表確認


這樣之後就可以用 Pulse Secure 進行 SSLVPN 連線了


~ enjoy it


Reference