donkey111111 2019-02-26 19:12
浏览 114
已采纳

进行AWS测试以进行无效登录

Using Golang with go-aws-sdk and having some issues catching invalid session credentials.

Using the shared credential files in ~/.aws/{config, credentials}

Sess, err := session.NewSessionWithOptions(session.Options{
    Profile: instance.Config.AWS.AWSProfile,
})

Works fine when the credentials are valid, but if I use an invalid aws_access_key_id in the credentials file, I need a way to detect this with my AWSLogin() func.

What's happening is any subsequent calls to an AWS service raises a panic when using Sess above.

How can I detect a failed login with the above NewSessionWithOptions() method?

UPDATE: yes, the error type is nil, so the following is of no use:

 if err != nil {
        return nil, fmt.Errorf("Error logging into AWS: %v", err.Error())
    }
  • 写回答

2条回答 默认 最新

  • dongxing2302 2019-02-26 22:52
    关注

    Here's what I ended up doing. Test that credentials are loaded, test a known service such as an S3 bucket this application needs access to.

    // login to AWS
    AWSProfile := "default"
    fmt.Printf("Using AWS Profile: %v
    ", instance.Config.AWS.AWSProfile)
    Sess, err := session.NewSessionWithOptions(session.Options{
        Profile: AWSProfile,
    })
    if err != nil {
        return fmt.Errorf("Error logging into AWS: %v", err.Error())
    }
    
    // attempt to load config (e.g. env variables, shared config, instance profile) 
    // log which AWS API Key is being used
    svc := s3.New(Sess)
    credentials, err := svc.Config.Credentials.Get()
    if err != nil {
        return errors.New("Error logging into AWS. Check Credentials.")
    }
    fmt.Printf("Using Access Key ID: (%v)
    ", credentials.AccessKeyID)
    bucketName := "s3bucketname"
    
    // test the login can access a typical aws service (s3) and known bucket 
    params := &s3.ListObjectsInput {
        Bucket: aws.String(bucketName),
    }
    resp, _ := svc.ListObjects(params)
    
    if len(resp.Contents) < 1 {
        return nil, fmt.Errorf("Error logging into AWS. Can not access bucket (%v). Check Credentials.", bucketName)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题