docker compose 部署 golang 的 Athens 私有代理问题


Posted in Servers onApril 28, 2022

go中私有代理搭建

前言

最近公司的代理出现问题了,刚好借这个机会来学习下,athens 如何构建私有代理

为什么选择 athens

私有化代理的选取标准无非就是下面的几点

1、托管私有模块;

2、排除对公有模块的访问;

3、存储公有模块;

athens 的特点:

Athens 首先可以配置访问私有仓库;

Athens 的会存储每次拉取的包,如果该模块之前没有通过 athens,athens 会向目标地址请求数据,在返回给客户端的时候,会存储该模块到存储中,这样实现了 go mod download永远只会发生一次;

Athens 处理存储的策略为仅追加,一个模块被保存,它就永远不会改变,即使开发人员对 tag 进行了强推,那么也不会被删除;

Athens 也可以配置下载策略,过滤一些有安全隐患的包。

Athens 支持 disk, mongo, gcs, s3, minio, 外部存储/自定义,不过一般建议使用 disk。

使用 docker-compose 部署

官方网站已经,提供了通过 docker 和 二进制部署的方案,这里秉着好记性不如烂笔头的原则,这里自己也做了记录

配置私有仓库的认证信息

通过 .netrc 文件来配置,里面可以放自己的私有仓库的地址,以及用户,密码认证信息

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

有几个私有仓库,配置几个就可以了

配置下载模式

通过 The download mode (下载模式配置策略)是现在 ATHENS 中比较推崇的,之前通过 Filtering modules(过滤模式)的方法,目前已经被弃用了。

来看下如何配置

# 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"

通过环境变量 ATHENS_DOWNLOAD_MODE 可指定,也可以修改指定的 config.dev.toml来配置,默认是 sync

ATHENS_DOWNLOAD_MODE 可指定的内容:

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 处于安全性考虑,为了保证开发者的依赖库不被人恶意劫持篡改,所以引入了 GOSUMDB 环境变量来设置校验服务器

当你在本地对依赖进行变动(更新/添加)操作时,Go 会自动去这个服务器进行数据校验,保证你下的这个代码库和世界上其他人下的代码库是一样的。如果有问题,会有个大大的安全提示。当然背后的这些操作都已经集成在 Go 里面了,开发者不需要进行额外的操作。

对于我们的私有仓库,去公共安全校验库校验,肯定是不能通过校验的,我们可以通过 ATHENS_GONOSUM_PATTERNS 这个环境变量来设置不做校验的代码仓库, 它可以设置多个匹配路径,用逗号相隔。

启动 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 公钥的内容添加到自己的私有仓库中,如何添加自己 google 吧,比较简单

2、配置 HTTP 与 SSH 重写规则

# 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

这样即可实现秘钥的认证了

需要注意私钥的权限,刚开始没注意,执行报了下面的错误

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
        @         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.

看报错就可推断出,是权限太大了,需要私钥文件不能被其他人所访问。

修改权限就可以了

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构建golang私有代理】

https://github.com/boilingfrog/Go-POINT/blob/master/golang/go_environment/athens构建golang私有代理.md

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

https://github.com/boilingfrog/Go-POINT/blob/master/golang/go_environment/athens构建golang私有代理.md

到此这篇关于使用 docker-compose 部署 golang 的 Athens 私有代理的文章就介绍到这了!


Tags in this post...

Servers 相关文章推荐
基于Nginx实现限制某IP短时间访问次数
Mar 31 Servers
Filebeat 采集 Nginx 日志的方法
Mar 31 Servers
使用 Apache 反向代理的设置技巧
Jan 18 Servers
nginx刷新页面出现404解决方案(亲测有效)
Mar 18 Servers
OpenStack虚拟机快照和增量备份实现方法
Apr 04 Servers
Nginx隐藏式跳转(浏览器URL跳转后保持不变)
Apr 07 Servers
阿里云k8s服务升级时502错误 springboot项目应用
Apr 09 Servers
Nginx+Tomcat负载均衡多实例详解
Apr 11 Servers
阿里云ECS云服务器快照的概念以及如何使用
Apr 21 Servers
Vscode中SSH插件如何远程连接Linux
May 02 Servers
解决Git推送错误non-fast-forward的方法
Jun 25 Servers
nginx七层负载均衡配置详解
Jul 15 Servers
Nginx 安装SSL证书完成HTTPS部署
Ubuntu Server 安装Tomcat并配置systemctl
Apr 28 #Servers
Windows Server 2019 安装DHCP服务及相关配置
Windows Server 2019 域控制器安装图文教程
阿里云 Windows server 2019 配置FTP
Windows Server 2012 修改远程默认端口3389的方法
Windows Server 2008 修改远程登录端口以及配置防火墙
You might like
图片存储与浏览一例(Linux+Apache+PHP+MySQL)
2006/10/09 PHP
PHP 万年历实现代码
2012/10/18 PHP
深入phpMyAdmin的安装与配置的详细步骤
2013/05/07 PHP
php和jquery实现地图区域数据统计展示数据示例
2014/02/12 PHP
以实例全面讲解PHP中多进程编程的相关函数的使用
2015/08/18 PHP
详解php与ethereum客户端交互
2018/04/28 PHP
PHP按符号截取字符串的指定部分的实现方法
2018/09/10 PHP
关于document.cookie的使用javascript
2010/10/29 Javascript
JS 去除Array中的null值示例代码
2013/11/20 Javascript
简单的ajax连接库分享(不用jquery的ajax)
2014/01/19 Javascript
js调用打印机打印网页字体总是缩小一号的解决方法
2014/01/24 Javascript
JavaScript实现Java中StringBuffer的方法
2015/02/09 Javascript
JavaScript将Web页面内容导出到Word及Excel的方法
2015/02/13 Javascript
javascript实现checkbox复选框实例代码
2016/01/10 Javascript
javascript单页面手势滑屏切换原理详解
2016/03/21 Javascript
JavaScript第一篇之实现按钮全选、功能
2016/08/21 Javascript
jquery 动态增加删除行的简单实例(推荐)
2016/10/12 Javascript
Node.js Sequelize如何实现数据库的读写分离
2016/10/23 Javascript
基于openlayers4实现点的扩散效果
2020/08/17 Javascript
详解Vue-axios 设置请求头问题
2018/12/06 Javascript
vscode配置vue下的es6规范自动格式化详解
2019/03/20 Javascript
简单学习5种处理Vue.js异常的方法
2019/06/17 Javascript
[01:06:30]DOTA2-DPC中国联赛定级赛 Phoenix vs DLG BO3第二场 1月9日
2021/03/11 DOTA
对python requests发送json格式数据的实例详解
2018/12/19 Python
Python使用ctypes调用C/C++的方法
2019/01/29 Python
python读取多层嵌套文件夹中的文件实例
2020/02/27 Python
浅谈移动端网页图片预加载方案
2018/11/05 HTML / CSS
Chicco婴儿用品美国官网:汽车座椅、婴儿推车、高脚椅等
2018/11/05 全球购物
中科软测试工程师面试题
2012/06/16 面试题
材料员岗位职责
2014/03/13 职场文书
小学老师寄语大全
2014/04/04 职场文书
股东合作协议书范本
2014/04/14 职场文书
2014应届本科生自我评价
2014/09/13 职场文书
九一八事变纪念日演讲稿
2014/09/14 职场文书
大学生安全教育心得体会
2016/01/15 职场文书
聊聊基于pytorch实现Resnet对本地数据集的训练问题
2022/03/25 Python