0
votes

I have a working python/boto script which posts a message to my AWS SQS queue. The message body however is hardcoded into the script.

I creates a file called ~/file which contains two values

$ cat ~/file
Username 'encrypted_password_string'

I would like my boto script (see below) to send a message to my AWS SQS queue that contains these two values.

Can anyone please advise how to modify my script below so the message body sent to SQS contains the contents of file ~/file. Please also take note of the special characters that exists within a encrypted password string

Example: ~/file username d5MopV/EsfSKk8BExCyLHFwNfBrOTzQ1

#!/usr/bin/env python

conf = {
  "sqs-access-key": "xxxx",
  "sqs-secret-key": "xxxx",
  "sqs-queue-name": "UserPassChange",
  "sqs-region": "xxxx",
  "sqs-path": "sqssend"
}

import boto.sqs
conn = boto.sqs.connect_to_region(
        conf.get('sqs-region'),
        aws_access_key_id       = conf.get('sqs-access-key'),
        aws_secret_access_key   = conf.get('sqs-secret-key')
)

q = conn.create_queue(conf.get('sqs-queue-name'))

from boto.sqs.message import RawMessage
m = RawMessage()
m.set_body('hardcoded message')
retval = q.write(m)
print 'added message, got retval: %s' % retval

one way to get it working:

in the script I added

import commands

then added,

USERNAME = commands.getoutput("echo $(who am i | awk '{print $1}')")    
PASS = commands.getoutput("cat /tmp/.s")

and then added these values to my message body :

MSG = RawMessage()    
MSG.set_body(json.dumps({'pass': PASS, 'user': USERNAME}))
1
First, STOP using boto, start using boto3 . Once in boto3, all you need is follow the example here : boto3.readthedocs.io/en/latest/guide/sqs.html Bare in mind that, boto3 have a high resource method boto3.resource() and also allow you to use low level boto3.client() method. Remember to document them down if you intent to write a wrapper.mootmoot
thanks mootmoot however that doesn't answer the question of how I can enter dynamic values into my script mentioned above.KarlH
Honestly, you should clarify your question and your intention. It is strange that you use who am i to get the user name but NOT MENTIONED IN YOUR QUESTION. Then it suddenly jump to /tmp/.s. Please clarify it or risk being mod down.mootmoot
You mentioned Please also take note of the special characters that exists within a encrypted password string is too ambiguous. What do you mean special character ? Is it binary, unicode? Or base64 encoded?mootmoot

1 Answers

0
votes

The following example shows how to use Boto3 to send a file to a receiver.

test_sqs.py

import boto3
from moto import mock_sqs

@mock_sqs
def test_sqs():
    sqs = boto3.resource('sqs', 'us-east-1')
    queue = sqs.create_queue(QueueName='votes')

    queue.send_message(MessageBody=open('beer.txt').read())

    messages = queue.receive_messages()
    assert len(messages) == 1
    assert messages[0].body == 'tasty\n'