Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add scripts for aws module to help testing locally #11589

Merged
merged 2 commits into from
Apr 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions x-pack/metricbeat/module/aws/mtest/scripts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
The script directory contains various scripts which exist to help testing aws module in metricbeat.
Below is a brief description of each file / folder.


| File / Folder | Description |
|----------------------------|------------------------------------------------------------------------------------------------------------------|
| get_temp_creds.go | Use MFA token to get temporary AWS credentials. |
| sqs_send_receive_delete.go | Get a list of SQS queues for a given region, send messages, receive messages and delete message from each queue. |
49 changes: 49 additions & 0 deletions x-pack/metricbeat/module/aws/mtest/scripts/get_temp_creds.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

package scripts

import (
"fmt"
"os"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/external"
"github.com/aws/aws-sdk-go-v2/service/sts"
)

func getCredentialsUsingMFA() {
fmt.Println("Please setup MFA_TOKEN, SERIAL_NUMBER, AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY first.")
mfaToken := "123456"
serialNumber := "arn:aws:iam::654321:mfa/test@test.com"

// access key id and secret access key of your IAM user.
accessKeyID := "AKIAIFX3D"
secretAccessKey := "jyVKiT"

os.Setenv("AWS_ACCESS_KEY_ID", accessKeyID)
os.Setenv("AWS_SECRET_ACCESS_KEY", secretAccessKey)
cfg, err := external.LoadDefaultAWSConfig()
if err != nil {
fmt.Println("failed to load config: ", err.Error())
}

stsSvc := sts.New(cfg)
durationLongest := int64(129600)
getSessionTokenInput := sts.GetSessionTokenInput{
DurationSeconds: &durationLongest,
SerialNumber: aws.String(serialNumber),
TokenCode: aws.String(mfaToken),
}

req := stsSvc.GetSessionTokenRequest(&getSessionTokenInput)
tempToken, err := req.Send()
if err != nil {
fmt.Println("GetSessionToken failed: ", err)
}

fmt.Println("temp aws_access_key_id =", *tempToken.Credentials.AccessKeyId)
fmt.Println("temp aws_secret_access_key =", *tempToken.Credentials.SecretAccessKey)
fmt.Println("temp aws_session_token =", *tempToken.Credentials.SessionToken)
}
139 changes: 139 additions & 0 deletions x-pack/metricbeat/module/aws/mtest/scripts/sqs_send_receive_delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

package scripts

import (
"fmt"
"strconv"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/defaults"
"github.com/aws/aws-sdk-go-v2/service/sqs"
"github.com/aws/aws-sdk-go-v2/service/sqs/sqsiface"
)

func getQueueUrls(svc sqsiface.SQSAPI) ([]string, error) {
// ListQueues
listQueuesInput := &sqs.ListQueuesInput{}
req := svc.ListQueuesRequest(listQueuesInput)
output, err := req.Send()
if err != nil {
return nil, err
}
return output.QueueUrls, nil
}

func sendMessages(qURL string, svc sqsiface.SQSAPI, idx int) error {
sendMessageInput := &sqs.SendMessageInput{
DelaySeconds: aws.Int64(10),
MessageAttributes: map[string]sqs.MessageAttributeValue{
"Title": {
DataType: aws.String("String"),
StringValue: aws.String("The Whistler" + strconv.Itoa(idx)),
},
"Author": {
DataType: aws.String("String"),
StringValue: aws.String("John Grisham" + strconv.Itoa(idx)),
},
"WeeksOn": {
DataType: aws.String("Number"),
StringValue: aws.String("6" + strconv.Itoa(idx)),
},
},
MessageBody: aws.String("Information about current NY Times fiction bestseller for week of 01/01/2019"),
QueueUrl: &qURL,
}

req := svc.SendMessageRequest(sendMessageInput)
output, err := req.Send()
if err != nil {
return err
}

fmt.Println("Succeed writing message ", *output.MessageId)
return nil
}

func receiveMessages(qURL string, svc sqsiface.SQSAPI) ([]sqs.Message, error) {
receiveMessageInput := &sqs.ReceiveMessageInput{
QueueUrl: &qURL,
MaxNumberOfMessages: aws.Int64(10),
//VisibilityTimeout: aws.Int64(20), // 20 seconds
//WaitTimeSeconds: aws.Int64(0),
}
req := svc.ReceiveMessageRequest(receiveMessageInput)
output, err := req.Send()
if err != nil {
return nil, err
}

fmt.Println("Received # messages: " + strconv.Itoa(len(output.Messages)))
return output.Messages, nil
}

func deleteMessage(qURL string, svc sqsiface.SQSAPI, message sqs.Message) error {
deleteMessageInput := &sqs.DeleteMessageInput{
QueueUrl: &qURL,
ReceiptHandle: message.ReceiptHandle,
}
reqD := svc.DeleteMessageRequest(deleteMessageInput)
output, err := reqD.Send()
if err != nil {
return err
}

fmt.Println("DeleteMessage: ", output.SDKResponseMetadata().Request.RequestID)
return nil
}

func sqsSendReceiveDelete() {
fmt.Println("Please setup AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY and SESSION_TOKEN first. If a temp credentials are needed, please run getTempCreds.go first.")
regionsList := []string{"us-west-1", "us-east-1"}
accessKeyID := "ASIAZENKQPPN4AXZ3KN2"
secretAccessKey := "GcotQB6fb8dPCoCp37BZ4qZWQhwacybhqtbk+xH6"
sessionToken := "H6s4gTiZO1kG5150Y9nGkvqkgiyEuT08+CnY0DnmsrPnIpZsqsx9AkltCQF/7iOxF97blS9oCu08hBbmibPU+V/RKLWWr+QF"

awsConfig := defaults.Config()
awsCreds := aws.Credentials{
AccessKeyID: accessKeyID,
SecretAccessKey: secretAccessKey,
SessionToken: sessionToken,
}

awsConfig.Credentials = aws.StaticCredentialsProvider{
Value: awsCreds,
}

for _, regionName := range regionsList {
awsConfig.Region = regionName
svc := sqs.New(awsConfig)
queueURLs, err := getQueueUrls(svc)
if err != nil {
fmt.Println("Failed getQueueUrls: ", err)
}

for i, qURL := range queueURLs {
//SEND
errS := sendMessages(qURL, svc, i)
if errS != nil {
fmt.Println("Error sendMessageSQS", errS)
}

// RECEIVE
messages, errR := receiveMessages(qURL, svc)
if errR != nil {
fmt.Println("Error receiveMessages", errR)
}

// DELETE
if len(messages) > 0 {
errD := deleteMessage(qURL, svc, messages[0])
if errD != nil {
fmt.Println("Error deleteMessage", errD)
}
}
}
}
}