aws elasticsearch使用es客户端返回403禁止的错误

eivnm1vs  于 2021-06-13  发布在  ElasticSearch
关注(0)|答案(1)|浏览(1180)

我正在尝试通过一个用go编写的索引作业来创建索引。我拥有对aws上es群集的所有访问权限,并使用我的访问密钥和密钥。
我可以使用kibana轻松地创建索引,但是当我尝试使用go客户机时,它不起作用并返回403禁止的错误。
aws elasticsearch策略:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "AWS": "*"
      },
      "Action": "es:*",
      "Resource": "arn:aws:es:<region>:111111111111:domain/prod-elasticsearch/*"
    }
  ]
}

索引.go

package main

import (
    "flag"
    "fmt"
    "log"

    "github.com/aws/aws-sdk-go/aws/credentials"
    "github.com/olivere/elastic/v7"
    "github.com/spf13/viper"

    aws "github.com/olivere/elastic/v7/aws/v4"
)

func main() {
    var (
        accessKey = viper.GetString("aws.access_key")
        secretKey = viper.GetString("aws.secret_key")
        url       = viper.GetString("elasticsearch.host")
        sniff     = flag.Bool("sniff", false, "Enable or disable sniffing")
        region    = flag.String("region", "ap-southeast-1", "AWS Region name")
    )

    if url == "" {
        log.Fatal("please specify a URL with -url")
    }
    if accessKey == "" {
        log.Fatal("missing -access-key or AWS_ACCESS_KEY environment variable")
    }
    if secretKey == "" {
        log.Fatal("missing -secret-key or AWS_SECRET_KEY environment variable")
    }
    if *region == "" {
        log.Fatal("please specify an AWS region with -region")
    }

    creds := credentials.NewStaticCredentials(accessKey, secretKey, "")
    _, err := creds.Get()

    if err != nil {
        log.Fatal("Wrong credentials: ", err)
    }

    signingClient := aws.NewV4SigningClient(creds, *region)

    // Create an Elasticsearch client
    client, err := elastic.NewClient(
        elastic.SetURL(url),
        elastic.SetSniff(*sniff),
        elastic.SetHealthcheck(*sniff),
        elastic.SetHttpClient(signingClient),
    )

    if err != nil {
        log.Fatal(err)
    }

    // This part gives 403 forbidden error
    indices, err := client.IndexNames()

    if err != nil {
        log.Fatal(err)
    }

    // Just a status message
    fmt.Println("Connection succeeded")
}

配置.toml

[elasticsearch]
host = "https://vpc-prod-elasticsearch-111111.ap-southeast-1.es.amazonaws.com"

[aws]
access_key = <ACCESS_KEY>
secret_key = <SECRET_KEY>
klh5stk1

klh5stk11#

密码看起来不错。由于aws密钥中的一些问题,出现了403错误。
调试此类问题:
检查aws elasticsearch群集运行的vpc子网是否已删除。如果vpc子网已被删除,则它将始终抛出403错误。
如果子网配置很好,那么很可能是aws机密的问题。您可以为aws elasticsearch创建新的aws应用程序用户,然后重试。这应该很管用。
我按照以上两点解决了上述问题。

相关问题