如何在戈朗中将秘密作为配置文件拉取

因此,我对Go非常陌生,我很好奇如何动态导入密码和URL而不会在我的脚本中公开它们。在python中,我能够使用JSON有效负载来实现这一点,并且基本上会导入JSON,加载有效载荷,并指定何时需要传递需要保密的规范。


这是我的Go脚本:


package main


import (

    "io/ioutil"

    "net/http"

    "fmt"


    "gopkg.in/yaml.v2"

)

// curl -u <username>:<password> <some_url>


func main() {

    type Config struct {

    URL      string `yaml:"url"`

    Username string `yaml:"username"`

    Token    string `yaml:"token"`

    }


    func readConfig() (*Config, error) {

    config := &Config{}

    cfgFile, err := ioutil.ReadFile("config.yaml")

    if err != nil {

        return nil, err

    }

    err = yaml.Unmarshal(cfgFile, config)

    return config, err

    }


    req, err := http.NewRequest("GET", config.URL, nil)

    if err != nil {

        // handle err

    }


    req.SetBasicAuth(config.Username, config.Token)


    resp, err := http.DefaultClient.Do(req)

    if err != nil {

        // handle err

    }


    defer resp.Body.Close()

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

    fmt.Println(string(body))

}

可以想象,我在加载密码时运气不佳,当我使用直接令牌更改以下内容时,它会在脚本中公开我的密码,并在Jira中生成JSON有效负载。


config.URL

config.Username

config.Token

因此,如果我在YAML中有一个配置文件, 如下所示:


config:

    URL: "<some_URL>"

    Username: "<some_username>"

    Token: "<some_token>"

如何将 YAML 文件加载到脚本中?如何加载 JSON 等效项?


{

  "config": {

    "URL": "<some_URL>"

    "Username": "<some_username>",

    "Token": "<some_token>"

  }

}


森栏
浏览 72回答 2
2回答

慕码人8056858

好吧,这里有几个问题。除非使用变量声明语法,否则不能在另一个函数中声明函数func main() {&nbsp; &nbsp; // This&nbsp; &nbsp; var myFunc = func() {&nbsp; &nbsp; &nbsp; &nbsp; // ...&nbsp; &nbsp; }&nbsp; &nbsp; // Not this&nbsp; &nbsp; func myFunc() {&nbsp; &nbsp; &nbsp; &nbsp; // ...&nbsp; &nbsp; }}该结构告诉 YAML 解封船者期望。您的结构应具有与 yaml 文件的大小写和结构匹配的 yaml 标记。Config// It is expecting thisurl: "<some_URL>"username: "<some_username>"token: "<some_token>"// Your yaml looks like thisconfig:&nbsp; Url: "<some_URL>"&nbsp; Username: "<some_username>"&nbsp; Token: "<some_token>"以下对我有用:package mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "io/ioutil"&nbsp; &nbsp; "net/http"&nbsp; &nbsp; "gopkg.in/yaml.v2")type YAMLFile struct {&nbsp; &nbsp; Config Config `yaml:"config"`}type Config struct {&nbsp; &nbsp; URL&nbsp; &nbsp; &nbsp; string `yaml:"url"`&nbsp; &nbsp; Username string `yaml:"username"`&nbsp; &nbsp; Token&nbsp; &nbsp; string `yaml:"token"`}func main() {&nbsp; &nbsp; config, err := readConfig()&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Printf("%+v", config)&nbsp; &nbsp; req, err := http.NewRequest("GET", config.URL, nil)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; }&nbsp; &nbsp; req.SetBasicAuth(config.Username, config.Token)&nbsp; &nbsp; resp, err := http.DefaultClient.Do(req)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; }&nbsp; &nbsp; defer resp.Body.Close()&nbsp; &nbsp; body, err := ioutil.ReadAll(resp.Body)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Println(string(body))}func readConfig() (*Config, error) {&nbsp; &nbsp; config := &YAMLFile{}&nbsp; &nbsp; cfgFile, err := ioutil.ReadFile("./config.yaml")&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; return nil, err&nbsp; &nbsp; }&nbsp; &nbsp; err = yaml.Unmarshal(cfgFile, config)&nbsp; &nbsp; return &config.Config, err}

侃侃尔雅

巴雷特这是我前段时间为解决这个问题而制作的配置库:https://github.com/fulldump/goconfig。用法很简单:使用您需要的所有配置定义一个结构:type Config struct {&nbsp; &nbsp; URL&nbsp; &nbsp; &nbsp; string&nbsp; &nbsp; Username string&nbsp; &nbsp; Token&nbsp; &nbsp; string}实例化具有该类型的变量,并使用默认值填充它:c := &Config{&nbsp; &nbsp; URL:&nbsp; &nbsp; &nbsp;"http://default/url"&nbsp; &nbsp; Username: "default username"}使用来自环境、命令行参数和/或 json 文件的数据自动填充配置变量:使用 follog 行:goconfig.Read(c)例如,在您的情况下,您可以按如下方式传递 JSON 文件,以从文件中读取所有配置密钥,但在启动时使用参数覆盖令牌。./my-binary -token "my_secret_token" -config my-config-file.json
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go