亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從戈朗腳本中檢索帶有空格的 Github 機密

從戈朗腳本中檢索帶有空格的 Github 機密

Go
墨色風雨 2022-09-12 16:09:49
我想通過戈朗從腳本中檢索一個Github秘密內容,該內容是從Github操作執行的。在這種特殊情況下,存儲在 Github 機密中的機密值具有空間。我的意思是秘密值是:.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 操作運行器時,我沒有從 Github 機密中獲取值。我知道自從我試圖以這種方式打印它以來,這種情況正在發生,但沒有任何結果:fmt.Println("My JWT", os.Getenv("USER_JWT"))我擔心它正在發生,因為令牌和令牌之間的空間,我的意思是。"JWT "JWT <token-string>這里說:機密名稱只能包含字母數字字符([a-z]、[A-Z]、[0-9])或下劃線 (_)。不允許使用空格。作為一個重要的事實,我的令牌秘密值在其值中也包含字符。該值如下所示:.JWT xxxxxxx8888xxxxdsdsfsfsf9.eyJxxxxxxx8888xxxxdsdsfsfsf9.Tfgxadsdsfsfsasasad_s7sdsdsfgsgcs所以我相信,這就是我無法獲得秘密價值的原因。我不確定如何從我的Golang腳本中獲取它,我甚至試圖修改Github秘密值,只是為了避免值中的空格,我以這種方式從go調用它:<token-string>"Authorization": []string{"JWT ", os.Getenv("SPECKLE_USER_JWT")}但它沒有奏效。我在這里讀到,當從github操作中調用具有特殊字符的機密時,我們必須用單引號轉義它們,但此過程來自文件github操作。' '.yaml我正在嘗試以前的解決方案替代方案,它們適用于我的本地計算機,因為我的bash cli能夠獲取值中帶有空格的環境變量。我不確定我 - 讓我們說“逃脫” - 一個在字符串中留有空間的秘密,就像我從golang那里得到的那樣。
查看完整描述

1 回答

?
茅侃侃

TA貢獻1842條經驗 獲得超22個贊

我設法從執行戈蘭泰拉泰坦代碼的GitHub操作中讀取了存儲在GitHub秘密中的JWT秘密。


如前所述,由于Github機密不允許空格和點字符,并且令牌有一些點加一個空格,因此我首先做的是對其進行編碼" ".


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

這將生成一個沒有空格的整個字符串,然后我將此值存儲在Github機密上。我從戈朗這樣讀:.



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試圖告訴我的那樣,我從上面調用的gorang中使用的新環境變量必須包含在我的github操作中,以便從命令運行這些測試。所以我的github操作文件最終是這樣:SPECKLE_USER_JWT_ENCODEgo test.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

一個很好的參考,以了解一些如何處理HTTP包


查看完整回答
反對 回復 2022-09-12
  • 1 回答
  • 0 關注
  • 112 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號