Golang中elasticsearch.Config结构体字面量出现未知字段Username

Golang中elasticsearch.Config结构体字面量出现未知字段Username 有人能帮我解决这些错误吗:

server/modules/securityonion/soelastic.go:46:5: 结构字面量 elasticsearch.Config 中存在未知字段 Username server/modules/securityonion/soelastic.go:47:5: 结构字面量 elasticsearch.Config 中存在未知字段 Password server/modules/securityonion/soelastic.go:58:27: elastic.esConfig.Password 未定义(elasticsearch.Config 类型没有 Password 字段或方法) server/modules/securityonion/soelastic.go:66:34: elastic.esConfig.Username 未定义(elasticsearch.Config 类型没有 Username 字段或方法)

以下是用于调试的代码块:

  type SoElastic struct {
  esConfig              elasticsearch.Config
  esClient              *elasticsearch.Client
  timeShiftMs   int
}

func NewSoElastic() *SoElastic {
  return &SoElastic{}
}

func (elastic *SoElastic) Init(host string, user string, pass string, verifyCert bool, timeShiftMs int) error {
  hosts := make([]string, 1)
  elastic.timeShiftMs = timeShiftMs
  hosts[0] = host
  elastic.esConfig = elasticsearch.Config {
    Addresses: hosts,
    Username: user,
    Password: pass, 
    Transport: &http.Transport{
      MaxIdleConnsPerHost:   10,
      ResponseHeaderTimeout: time.Second,
      DialContext:           (&net.Dialer{Timeout: time.Second}).DialContext,
      TLSClientConfig: &tls.Config{
        InsecureSkipVerify: !verifyCert,
      },
    },
  }
  maskedPassword := "*****"
  if len(elastic.esConfig.Password) == 0 {
    maskedPassword = ""
  }

  esClient, err := elasticsearch.NewClient(elastic.esConfig)
  fields := log.Fields {
    "InsecureSkipVerify": !verifyCert,
    "Host": hosts[0],
    "Username": elastic.esConfig.Username,
    "Password": maskedPassword,
  }

更多关于Golang中elasticsearch.Config结构体字面量出现未知字段Username的实战教程也可以访问 https://www.itying.com/category-94-b0.html

1 回复

更多关于Golang中elasticsearch.Config结构体字面量出现未知字段Username的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


这个错误是因为你使用的 elasticsearch.Config 结构体中没有 UsernamePassword 字段。在 Elasticsearch Go 客户端中,认证信息是通过 HeaderAPIKey 字段设置的。

以下是正确的配置方式:

func (elastic *SoElastic) Init(host string, user string, pass string, verifyCert bool, timeShiftMs int) error {
    hosts := make([]string, 1)
    elastic.timeShiftMs = timeShiftMs
    hosts[0] = host
    
    // 设置基本认证头部
    headers := make(http.Header)
    headers.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(user+":"+pass)))
    
    elastic.esConfig = elasticsearch.Config{
        Addresses: hosts,
        Header: headers,
        Transport: &http.Transport{
            MaxIdleConnsPerHost:   10,
            ResponseHeaderTimeout: time.Second,
            DialContext:           (&net.Dialer{Timeout: time.Second}).DialContext,
            TLSClientConfig: &tls.Config{
                InsecureSkipVerify: !verifyCert,
            },
        },
    }
    
    maskedPassword := "*****"
    if len(pass) == 0 {
        maskedPassword = ""
    }

    esClient, err := elasticsearch.NewClient(elastic.esConfig)
    if err != nil {
        return err
    }
    
    fields := log.Fields{
        "InsecureSkipVerify": !verifyCert,
        "Host": hosts[0],
        "Username": user,
        "Password": maskedPassword,
    }
    
    elastic.esClient = esClient
    return nil
}

如果你使用的是 API Key 认证,可以这样配置:

elastic.esConfig = elasticsearch.Config{
    Addresses: hosts,
    APIKey:    "your-api-key",
    Transport: &http.Transport{
        MaxIdleConnsPerHost:   10,
        ResponseHeaderTimeout: time.Second,
        DialContext:           (&net.Dialer{Timeout: time.Second}).DialContext,
        TLSClientConfig: &tls.Config{
            InsecureSkipVerify: !verifyCert,
        },
    },
}

对于云服务,可以使用 CloudID

elastic.esConfig = elasticsearch.Config{
    CloudID: "your-cloud-id",
    APIKey:  "your-api-key",
    Transport: &http.Transport{
        TLSClientConfig: &tls.Config{
            InsecureSkipVerify: !verifyCert,
        },
    },
}

你需要删除代码中对 elastic.esConfig.Usernameelastic.esConfig.Password 的直接引用,因为这些字段在 elasticsearch.Config 结构体中不存在。认证信息应该通过配置时的参数传递。

回到顶部