从 golang 脚本中检索带有空格的 Github 机密

问题描述

我想从 golang 的脚本中检索 Github 机密内容。它是从 Github 动作执行的。 作为一种特殊情况,Github secrets 中存储的 secret 值有一个空格。我的意思是秘密值是:JWT <token-string>

从任何语言的任何脚本中检索 Github 机密的方法是将它们作为环境变量读取,只要它们在 Github 操作运行器中执行即可。所以我所做的就是以这种方式读取它:(请参阅下面字符串切片中的 Authorization: 元素)

func MyTestFunction(t *testing.T,serverURL string) {

    type AppLogin struct {
        token string
    }
    method := "GET"

    
    headers := map[string][]string{
        "Content-Type": []string{"application/json,text/plain,*/*"},"Authorization": []string{os.Getenv("USER_JWT")},}

问题是我在运行 Github action runner 时没有从 Github secrets 中获得价值。我知道这正在发生,因为我试图以这种方式打印它,但没有出现:

fmt.Println("My JWT",os.Getenv("USER_JWT"))

我担心它正在发生,因为 "JWT " 和令牌之间的空间,我的意思是 JWT <token-string>

Here 说:

秘密名称只能包含字母数字字符([a-z]、[A-Z]、[0-9])或下划线 (_)。 不允许有空格。

一个重要的事实是,我的令牌秘密值在其值中也包含 . 字符。价值是这样的:

JWT xxxxxxx8888xxxxdsdsfsfsf9.eyJxxxxxxx8888xxxxdsdsfsfsf9.Tfgxadsdsfsfsasasad_s7sdsdsfgsgcs

所以我相信,这就是我无法获得秘密值的原因。

我不确定如何从我的 golang 脚本中获取它,我什至尝试修改 github 秘密值,将其作为值 <token-string> 以避开值中的空格,并且我我以这种方式从 go 调用它:

"Authorization": []string{"JWT ",os.Getenv("SPECKLE_USER_JWT")}

但它不起作用。 我读到 here,当从 github 操作中调用带有特殊字符的机密时,我们必须用单引号 ' ' 对它们进行转义,但此过程来自 .yaml 文件 github 操作。

我尝试使用的以前的解决方案替代方案,它们适用于我的本地机器,因为我的 bash cli 能够获取值中带有空格的环境变量。我不知道我怎么能——比如说“转义”——一个字符串中有空格的秘密,就像我从 golang 那里得到的一样。

解决方法

我设法从执行 golang terratest 代码的 GitHub 操作中读取了存储在 GitHub 机密中的 JWT 机密。

如前所述,由于 Github secrets 不允许空格 " " 和点 . 字符,并且令牌有一些点加一个空格,因此我首先对其进行了编码

echo -n '<token-value>' | base64

这会生成一个没有 . 或空格的完整字符串,然后我将此值存储在 Github secrets 中。我是这样从 golang 中读取的:


func main() {
   var t *testing.T
   serverURL := os.Getenv("SERVER_URL")
   MyTestFunction(t,serverURL)

}
func MyTestFunction(t *testing.T,serverURL string) {

    type SpeckleLogin struct {
        token string
    }
    method := "GET"

    // The encoded token is read from github secrets
    b64EncodeJwt := os.Getenv("USER_JWT_ENCODE")
    // fmt.Println("The encode JWT is:",b64EncodeJwt)

    // The encoded read token is decoded
    b64DecodeJwt,_ := b64.StdEncoding.DecodeString(b64EncodeJwt)
    // fmt.Println("JWT Decoded",string(b64DecodeJwt))
    // fmt.Println()
    
    headers := map[string][]string{
        "Content-Type": []string{"application/json,text/plain,*/*"},// The content of the token already decoded is included in the headers slice of strings. 
        "Authorization": []string{(string(b64DecodeJwt))},}


    jsonLogin := []byte(fmt.Sprintf(`{
        "email":"%s","password": "%s"
    }`,os.Getenv("USER_EMAIL"),os.Getenv("USER_PASSWORD")))
    
    // The HTTP request is created
    reqLogin,errReq := http.NewRequest(method,serverURL+"/api/accounts",bytes.NewBuffer(jsonLogin))

    // The headers are added to the HTTP request
    reqLogin.Header = headers

    if errReq != nil {
        messageReq := fmt.Sprintf("Error GET login request: %s",errReq.Error())
        t.Fatal(messageReq)
    }

    clientLogin := &http.Client{
        Transport: &http.Transport{
            TLSClientConfig: &tls.Config{
                InsecureSkipVerify: true,},}
    // Sending the request
    respLogin,errResp := clientLogin.Do(reqLogin)

    if errResp != nil {
        messageResp := fmt.Sprintf("Error GET login response: %s",errResp.Error())
        t.Fatal(messageResp)
    }

    defer respLogin.Body.Close()

    body,_ := ioutil.ReadAll(respLogin.Body)

    // fmt.Println("BODY IS:")
    // fmt.Println(string(body))

    var speckleLogin map[string]interface{}

    if err := json.Unmarshal([]byte(body),&speckleLogin); err != nil {
        t.Fatal("Could not unmarshal json")
    }

    // We take the API token from the response
    data := speckleLogin["resource"].(map[string]interface{})["apitoken"]   

    if speckleToken,ok := data.(string); ok {

        // Here we assert the token is not empty
        assert.NotEmpty(t,speckleToken)
}

但除此之外,@WishwaPerera 试图告诉我,我在 golang 中使用的名为 SPECKLE_USER_JWT_ENCODE 的新环境变量必须包含在我的 github 操作中,以便从go test 命令。所以我的github action .yaml文件最后是这样的:

name: Preview_Workflow

on:
  pull_request:
    branches:
    - master

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    steps:
    - name: 'Checkout GitHub Action'
      uses: actions/checkout@master

    - name: Install terraform 
      uses: hashicorp/setup-terraform@v1
      with:
        terraform_version: 0.13.5
        terraform_wrapper: false

    - name: 'Terraform Version'
      shell: bash
      run: |
        terraform version

    - name: 'Login via Azure CLI'
      uses: azure/login@v1
      with:
        creds: ${{ secrets.AZURE_CREDENTIALS }}

    - name: 'Setup Go'
      id: go
      uses: actions/setup-go@v2
      with:
        go-version: '^1.16.5'

    - name: 'Run Terratest'
      id: terratest
      run: |
        cd tests
        go get -u github.com/Azure/azure-storage-blob-go/azblob
        go get -u github.com/gruntwork-io/terratest/modules/terraform
        go get -u github.com/stretchr/testify/assert
        // executing the test
        go test
      env:
        SERVER_URL: "https://my-service-application-url"
        USER_EMAIL: ${{ secrets.USER_EMAIL }}
        USER_PASSWORD: ${{ secrets.USER_PASSWORD }}
        USER_JWT_ENCODE: ${{ secrets.USER_JWT_ENCODE }}

        # I am using these other ones to connect to azure.
        ARM_CLIENT_ID: ${{ secrets.ARM_CLIENT_ID }}
        ARM_CLIENT_SECRET: ${{ secrets.ARM_CLIENT_SECRET }}
        ARM_SUBSCRIPTION_ID: ${{ secrets.ARM_SUBSCRIPTION_ID }}
        ARM_TENANT_ID: ${{ secrets.ARM_TENANT_ID }}

    - name: Azure logout
      run: |
        az logout

一个很好的参考,可以理解一点how to handle the HTTP package