iT邦幫忙

2023 iThome 鐵人賽

DAY 4
0

以下都用 GitLab 說明, Please image the concept if you are using github or a version control tool.

Why use the variables to store your IDs or information?

Variable 其實這概念來自於程式,而現在做 DevOps 的部分除了 Check network or monitor performance 之類的情境外,對於 Maintainer 而言,就會需要大量的使用 Variable to definition resources or token

舉例來說,對於 AWS 的部分就會把自己家的 IP 加入 Security Group ,而 IP 就可以使用 Variable 來取代,例如: MY_HOME_IP=192.168.0.1 ,然後再 AWS Security group setting 就會放入對應的變數名稱


variable "my_home_ip" {
  description = "My home ip"
  type        = list(string)
  default = [
    "192.168.0.1/32",
  ]
}

resource "aws_security_group_rule" "my_ip_allow_in" {
  type              = "ingress"
  description       = "This is taipei office IP address"
  from_port         = 0
  to_port           = 0
  protocol          = "-1"
  cidr_blocks       = var.my_home_ip
  security_group_id = aws_security_group.aws_sg.id
}

這樣的做法有幾總好處:

  1. 如果 IP 有變化直接改變數就可以,改一個其他地方的設定就跟著改了
  2. 同時也可以把同樣的 code 可以複製到其去部屬
  3. 就是避免 IP 這種敏感的資料寫在 code 上面,當push gitlab 的同時就會被大家知道敏感資料

(下面就是要講這件事情)

Why are we talking about sensitive Information on CI?

除了Variable的概念之外,大家還有印象使用 git 吧, .git file 通常都是放在 project 的 根目錄 裡,所以在 git push 的時候通常會把所有的 File 都一起上傳到 Gitlab上面, 有沒有想過 Gitlab 的資料是大家都可以存取的,甚至你的資料是 Gitlab 員工可以讀取的。(Note1)

所有這邊有兩件事情要來做

  1. 使用變數來build image

我使用變數來取代我在 file content 留下任何的key 或者敏感資訊,因此舉 docker build image 的情境而言,為了全程避開我把 token 等資訊帶入因此 dockerfile 的 build 方法比較麻煩一點。

首先 Dockerfile 的部分就宣要 ARG 跟變數名稱


ARG MY_HOME_IP
ARG AWS_ACCESS_TOKEN_ID
ENV AWS_ACCESS_TOKEN_ID=AWS_ACCESS_TOKEN_ID
ARG AWS_SECRET_ACCESS_KEY
ENV AWS_SECRET_ACCESS_KEY=AWS_SECRET_ACCESS_KEY

接著使用 command line 在執行build 的同時,就使用 build-arg 把變數帶進去 docker builder,如此一來這個 docker content 根本不會有任何的 敏感資訊

docker build dockerfile 
--build-arg MY_HOME_IP=192.168.0.1 
--build-arg AWS_ACCESS_TOKEN_ID=ABCDEFGH...xyz
--build-arg AWS_SECRET_ACCESS_KEY=abcdefgh...xyz
.

至於docker build 為什麼要怎麼打那麼多文字很麻煩的話,可以考慮使用 makefile 的工具,把這長串變成一個大變數來處理

  1. 更改 Variable file path

當然知道可以使用 .gitignore 但是,還是有可能被上傳,最好把 sentitive variable 丟到 project 上一層,這樣就不會被 git add 到,所以確保資料能存取的絕對只有自己本機,而 remote 的user 就要自己手動建立 Variable 了

Home
- Variable.file
- Project
- - main.py
- - dockerfile
- - makefile
- - .git
- - .gitignore

如此二來就可以避免敏感資料被git push 到網路上的狀況了。至於我為什麼要特別講出來呢?

因為我先承認,我幹過這種蠢事,千萬別以為世界只有你知道,當你丟到網路上就是大家都會知道

NOTE:

  1. 前陣子的 ChatGPT 跟 copilot 可以自動幫你產生密碼,但是密碼是透過語言模型產生的,這個意思就是 file's content include password from github, 所以不要把敏感性的資料一起丟掉 Gitlab 上面,這也是很重要的原因。

上一篇
{Day 3: Talking about Gitlab CI}
下一篇
{Day 5: EKS - Kubernetes on AWS }
系列文
Don't be a Machine Learning Engineer30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言