亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

詳解使用?docker?compose?部署?golang?的?Athens?私有代理問題

 更新時間:2022年04月28日 08:34:08   作者:ZhanLi  
這篇文章主要介紹了使用?docker-compose?部署?golang?的?Athens?私有代理,幫助大家快速學習athens 如何構(gòu)建私有代理,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

go中私有代理搭建

前言

最近公司的代理出現(xiàn)問題了,剛好借這個機會來學習下,athens 如何構(gòu)建私有代理

為什么選擇 athens

私有化代理的選取標準無非就是下面的幾點

1、托管私有模塊;

2、排除對公有模塊的訪問;

3、存儲公有模塊;

athens 的特點:

Athens 首先可以配置訪問私有倉庫;

Athens 的會存儲每次拉取的包,如果該模塊之前沒有通過 athens,athens 會向目標地址請求數(shù)據(jù),在返回給客戶端的時候,會存儲該模塊到存儲中,這樣實現(xiàn)了 go mod download永遠只會發(fā)生一次;

Athens 處理存儲的策略為僅追加,一個模塊被保存,它就永遠不會改變,即使開發(fā)人員對 tag 進行了強推,那么也不會被刪除;

Athens 也可以配置下載策略,過濾一些有安全隱患的包。

Athens 支持 disk, mongo, gcs, s3, minio, 外部存儲/自定義,不過一般建議使用 disk。

使用 docker-compose 部署

官方網(wǎng)站已經(jīng),提供了通過 docker 和 二進制部署的方案,這里秉著好記性不如爛筆頭的原則,這里自己也做了記錄

配置私有倉庫的認證信息

通過 .netrc 文件來配置,里面可以放自己的私有倉庫的地址,以及用戶,密碼認證信息

# cat .netrc
machine gitlab.test.com login test-name password test-pass

有幾個私有倉庫,配置幾個就可以了

配置下載模式

通過 The download mode (下載模式配置策略)是現(xiàn)在 ATHENS 中比較推崇的,之前通過 Filtering modules(過濾模式)的方法,目前已經(jīng)被棄用了。

來看下如何配置

# DownloadMode defines how Athens behaves when a module@version
# is not found in storage. There are 4 options:
# 1. "sync" (default): download the module synchronously and
# return the results to the client.
# 2. "async": return 404, but asynchronously store the module
# in the storage backend.
# 3. "redirect": return a 301 redirect status to the client
# with the base URL as the DownloadRedirectURL from below.
# 4. "async_redirect": same as option number 3 but it will
# asynchronously store the module to the backend.
# 5. "none": return 404 if a module is not found and do nothing.
# 6. "file:<path>": will point to an HCL file that specifies
# any of the 5 options above based on different import paths.
# 7. "custom:<base64-encoded-hcl>" is the same as option 6
# but the file is fully encoded in the option. This is
# useful for using an environment variable in serverless
# deployments.
# Env override: ATHENS_DOWNLOAD_MODE
DownloadMode = "sync"

通過環(huán)境變量 ATHENS_DOWNLOAD_MODE 可指定,也可以修改指定的 config.dev.toml來配置,默認是 sync

ATHENS_DOWNLOAD_MODE 可指定的內(nèi)容:

1、通過 file:<path>指定一個 hcl 文件,里面可以對不同的倉庫,設置下載模式;

2、通過 custom:<base64-encoded-hcl> 指定一個 base64 編碼的 HCL 文件;

3、指定具體的全局策略,sync, async, none, redirect, or async_redirect,這是一個全局的設置,上面的兩種是可以定制策略組的。

來看下具體的下載模式

  • sync: 通過 同步從 VCS 下載模塊 go mod download,將其持久化到存儲中,并立即將其返回給用戶。請注意,這是默認行為;

  • async:向客戶端返回 404,并異步下載 module@version 并將其持久化到存儲中;

  • none:返回 404 并且什么也不做;

  • redirect:重定向到上游代理(例如proxy.golang.org),之后什么也不做;

  • async_redirect:重定向到上游代理(例如proxy.golang.org)并異步下載 module@version 并將其持久化到存儲中;

下面看下配置策略的 hcl 文件

# cat download.hcl  
downloadURL = "https://goproxy.cn"
mode = "async_redirect"
download "gitlab.test.com/*" {
    mode = "sync"
}

部署

這里使用 docker-composer 部署

version: '2'
services:
  athens:
    image: gomods/athens:v0.11.0
    restart: always
    container_name: athens_proxy
    ports:
      - "3000:3000"
    volumes:
      - ./.netrc:/root/.netrc
      - ./athens-storage:/var/lib/athens
      - ./download.hcl:/root/download.hcl
    environment:
      - ATHENS_NETRC_PATH=/root/.netrc
      - ATHENS_STORAGE_TYPE=disk
      - ATHENS_DISK_STORAGE_ROOT=/var/lib/athens
      - ATHENS_GOGET_WORKERS=100
      - ATHENS_DOWNLOAD_MODE=file:/root/download.hcl
      - ATHENS_GONOSUM_PATTERNS=gitlab.test.com

ATHENS_GONOSUM_PATTERNS:配置為私庫地址,配置的倉庫地址,不會進行安全向校驗。

go 處于安全性考慮,為了保證開發(fā)者的依賴庫不被人惡意劫持篡改,所以引入了 GOSUMDB 環(huán)境變量來設置校驗服務器

當你在本地對依賴進行變動(更新/添加)操作時,Go 會自動去這個服務器進行數(shù)據(jù)校驗,保證你下的這個代碼庫和世界上其他人下的代碼庫是一樣的。如果有問題,會有個大大的安全提示。當然背后的這些操作都已經(jīng)集成在 Go 里面了,開發(fā)者不需要進行額外的操作。

對于我們的私有倉庫,去公共安全校驗庫校驗,肯定是不能通過校驗的,我們可以通過 ATHENS_GONOSUM_PATTERNS 這個環(huán)境變量來設置不做校驗的代碼倉庫, 它可以設置多個匹配路徑,用逗號相隔。

啟動 docker-compose up -d

客戶端設置代理 export GOPROXY=http://xxxx:3000

這樣就能使用我們的代理服務了

因為選擇的 ATHENS_STORAGE_TYPE 為 disk,athens 服務會在拉取資源包的同時,也會下載資源包到配置的 ATHENS_DISK_STORAGE_ROOT 中。

使用秘鑰的方式認證私有倉庫

上面通過 .netrc 的方式來認證私有倉庫,因為賬號密碼是銘文的總歸不太好,可以使用秘鑰的方式來認證

1、配置秘鑰

首先查看電腦有沒有秘鑰

# cd .ssh
# ls
id_rsa		id_rsa.pub

沒有的話通過下面的命令的生成

# ssh-keygen -t rsa -C "youremail@example.com"

郵箱換成自己的,一路回車即可

然后將 id_rsa.pub 公鑰的內(nèi)容添加到自己的私有倉庫中,如何添加自己 google 吧,比較簡單

2、配置 HTTP 與 SSH 重寫規(guī)則

# cat gitconfig 
[url "ssh://git@gitlab.test.com"]
        insteadOf = https://gitlab.test.com

3、配置 SSH 來繞過主機 SSH 鍵驗證

# cat config 
Host gitlab.test.com
Hostname gitlab.test.com
StrictHostKeyChecking no
IdentityFile /root/.ssh/id_rsa

將上面配置的認證信息,映射到容器中即可

version: '2'
services:
  athens:
    image: gomods/athens:v0.11.0
    restart: always
    container_name: athens_proxy
    ports:
      - "3000:3000"
    volumes:
      - ./athens-storage:/var/lib/athens
      - ./download.hcl:/root/download.hcl
      - ./gitconfig:/root/.gitconfig
      - ./ssh-keys:/root/.ssh
    environment:
      - ATHENS_STORAGE_TYPE=disk
      - ATHENS_DISK_STORAGE_ROOT=/var/lib/athens
      - ATHENS_GOGET_WORKERS=100
      - ATHENS_DOWNLOAD_MODE=file:/root/download.hcl
      - ATHENS_GONOSUM_PATTERNS=gitlab.test.com

這樣即可實現(xiàn)秘鑰的認證了

需要注意私鑰的權(quán)限,剛開始沒注意,執(zhí)行報了下面的錯誤

        @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
        @         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
        @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
        Permissions 0644 for '/root/.ssh/id_rsa' are too open.
        It is required that your private key files are NOT accessible by others.
        This private key will be ignored.
        Load key "/root/.ssh/id_rsa": bad permissions
        git@gitlab.test.com: Permission denied (publickey).
        fatal: Could not read from remote repository.

看報錯就可推斷出,是權(quán)限太大了,需要私鑰文件不能被其他人所訪問。

修改權(quán)限就可以了

ssh-keys # chmod 600 id_rsa

具體的 demo 地址,可參見athens私有代理部署

參考

【介紹 ATHENS】

https://gomods.io/zh/intro/

【download】

https://github.com/gomods/athens/blob/main/docs/content/configuration/download.md

【athens構(gòu)建golang私有代理】

https://github.com/boilingfrog/Go-POINT/blob/master/golang/go_environment/athens構(gòu)建golang私有代理.md

【使用 docker-compose 部署 golang 的 Athens 私有代理】

https://github.com/boilingfrog/Go-POINT/blob/master/golang/go_environment/athens構(gòu)建golang私有代理.md

到此這篇關于使用 docker-compose 部署 golang 的 Athens 私有代理的文章就介紹到這了,更多相關docker-compose 部署Athens 私有代理內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論