Construction of Go 1.13 Private Agent Service

Keywords: Go Google git GitLab Apache

Original address: Construction of Go 1.13 Private Agent Service.

Setting GOPROXY can only specify one proxy service address prior to the release of Go version 1.13.After entering Go 1.13, GOPROXY supports multi-proxy settings, which can be separated.The following:

export GOPROXY=https://proxy.golang.org,direct

According to the official documentation, when the first proxy returns an HTTP status code of 404 or 410 when it processes an HTTP request made by a ge get, the next proxy is found.

This promotion is indeed the most desired addition to the Go Module since it became available.With this multi-agent proxy setting, you can separate private package agents from common package agents in your daily build enterprise projects so that you no longer need to maintain a large Go Module repository, but only a limited size private package repository.Private package warehouses mainly store enterprise internal packages plus shared packages outside the walls.

Here is a brief description of how to build an enterprise private proxy service.

Private Package Warehouse

Start by creating a code warehouse project on your enterprise GitLab: private-modules.

After you turn on the Go Module feature, Gowill cache all Go Module packages downloaded locally at $GOPATH/pkg/mod/cache/download/during each build process.

Submit enterprise private packages and shared packages outside the wall to the code repository project: private-modules.

$: tree -L 1 $GOPATH/pkg/mod/cache/download/
$GOPATH/pkg/mod/cache/download/
├── **cloud.google.com**  //Wall Outsourcing
├── **your.company.com**  //Private Package
├── git.apache.org
├── github.com
├── go.etcd.io
├── go.opencensus.io
├── go.uber.org
├── golang.org
├── gonum.org
├── google.golang.org
├── gopkg.in
├── gotest.tools
├── honnef.co
├── k8s.io
├── layeh.com
└── rsc.io

As illustrated, you can add directories for out-of-wall packages and enterprise internal packages to your private-modules project for management.

Private Agent Service

Write a private proxy service program.The Private Proxy Service is a simple file system-based HTTP service that adds User/Password for security authentication.More secure controls can restrict access through non-intranet IP.

The proxy server implementation is very simple, as follows:


func ProxyHandler(wr http.ResponseWriter, req *http.Request) {
    //Authentication
    user, password, ok := req.BasicAuth()
    if !ok {
        http.Error(wr, "basic auth required", http.StatusForbidden)
        return
    }
    
    if user != "[YOUR-USER]" || password != "[YOUR-PASSWORD]" {
        http.Error(wr, "basic auth failed", http.StatusForbidden)
        return
    }
    
    //Wall Outsourcing
    if strings.HasPrefix(req.URL.RequestURI(), "cloud.google.com") {
        http.FileServer("[PrivateModulePath]").ServeHTTP(wr, req)
        return
    }
    
    //Private Package
    if strings.HasPrefix(req.URL.RequestURI(), "your.company.com") {
        http.FileServer("[PrivateModulePath]").ServeHTTP(wr, req)
        return
    }
    
    //404
    http.NotFound(wr, req)
}

Private agents combine with enterprise CI tools to ensure that packages in the [PrivateModulePath] directory are updated in real time.

Program building

Once you've built these enterprise private proxy services, you can build your Go program in multiple stages in the CI phase.Show a simple sample Dockerfile for your readers to test.

FROM golang:1.13-alpine3.10 AS builder
RUN  apk --update --no-cache add git mercurial subversion bzr ca-certificates 
ENV  GOPROXY=https://[YOUR-USER]:[YOUR-PASSWORD]@proxy.yourcompany.com,direct
WORKDIR /app
COPY . .
RUN go build -o main

FROM alpine:3.10
WORKDIR /app
COPY --from=builder /app/main /usr/local/bin
ENTRYPOINT [ "main" ] 

The main scenario for private agent service in an enterprise is to use it in the intranet. For employees who need to work at home, it can be built by file agent.

Posted by webpoet on Mon, 09 Sep 2019 18:23:23 -0700