How can I protect my AWS access id and secret key in my python application

后端 未结 4 1732
花落未央
花落未央 2021-02-05 23:11

I\'m making an application in Python and using Amazon Web Services in some modules.

I\'m now hard coding my AWS access id and secret key in *.py file. Or might move them

相关标签:
4条回答
  • 2021-02-05 23:59

    I've been trying to answer the same question... the generate_url(x) looks quite promising.

    This link had a suggestion about creating a cloudfront origin access identity, which I'm guessing taps into the IAM authentication... meaning you could create a key for each application without giving away your main account details. With IAM, you can set permissions based on keys as to what they can do, so they can have limited access.

    Note: I don't know if this really works, I haven't tried it yet, but it might be another avenue to explore.

    2 - Create a Cloudfront "Origin Access Identity"

    This identity can be reused for many different distributions and keypairs. It is only used to allow cloudfront to access your private S3 objects without allowing everyone. As of now, this step can only be performed using the API. Boto code is here:

    # Create a new Origin Access Identity
    oai = cf.create_origin_access_identity(comment='New identity for secure videos')
    
    print("Origin Access Identity ID: %s" % oai.id)
    print("Origin Access Identity S3CanonicalUserId: %s" % oai.s3_user_id)
    
    0 讨论(0)
  • 2021-02-06 00:02

    Don't put it in applications you plan to distribute. It'll be visible and they can launch instances that are directly billable to you or worst..they can take down instances if you use it in production.

    I would look at your programs design and seriously question why I need to include that information in the app. If you post more details on the design I'm sure we can help you figure out a way in which you don't need to bundle this information.

    0 讨论(0)
  • 2021-02-06 00:13

    You're right, you can't upload using pre-signed URLs.

    There is a different, more complex capability that you can use called GetFederationToken. This will return you some temporary credentials, to which you can apply any policy (permissions) that you like.

    So for example, you could write a web service POST /upload that creates a new folder in S3, then creates temporary credentials with permissions to PutObject to only this folder, and returns the folder path and credentials to the caller. Presumably, some authorization check would be performed by this method as well.

    You can't embed cloud credentials, or any other credentials, in your application code. Which isn't to say that nobody ever accidentally does this, even security professionals.

    To safely distribute credentials to your infrastructure, you need tool support. If you use an AWS facility like CloudFormation, you can (somewhat more) safely give it your credentials. CloudFormation can also create new credentials on the fly. If you use a PaaS like Heroku, you can load your credentials into it, and Heroku will presumably treat them carefully. Another option for AWS is IAM Role. You can create an IAM Role with permission to do what you need, then "pass" the role to your EC2 instance. It will be able to perform the actions permitted by the role.

    A final option is a dedicated secrets management service, such as Conjur. (Disclaimer: I'm a founder of the company). You load your credentials and other secrets into a dedicated virtual appliance, and you define access permissions that govern the modification and distribution of the credentials. These permissions can be granted to people or to "robots" like your EC2 box. Credentials can be retrieved via REST or client APIs, and every interaction with credentials is recorded to a permanent record.

    0 讨论(0)
  • 2021-02-06 00:17

    There's no way to protect your keys if you're going to distribute your code. They're going to be accessible to anyone who has access to your server or source code.

    There are two things you can do to protect yourself against malicious use of your keys.

    1. Use the amazon IAM service to create a set of keys that only has permission to perform the tasks that you require for your script. http://aws.amazon.com/iam/

    2. If you have a mobile app or some other app that will require user accounts you can create a service to create temporary tokens for each user. The user must have a valid token and your keys to perform any actions. If you want to stop a user from using your keys you can stop generating new tokens for them. http://awsdocs.s3.amazonaws.com/STS/latest/sts-api.pdf


    Specifically to S3 if you're creating an application to allow people to upload content. The only way to protect your account and the information of the other users is to make them register an account with you.

    1. The first step of the application would be to authenticate with your server.
    2. Once your server authenticates you make a request to amazons token server and return a token
    3. Your application then makes a request using the keys built into the exe and the token.
    4. Based on the permissions applied to this user he can upload only to the bucket that is assigned to him.

    If this seems pretty difficult then you're probably not ready to design an application that will help users upload data to S3. You're going to have significant security problems if you only distribute 1 key even if you can hide that key from the user they would be able to edit any data added by any user.

    The only way around this is to have each user create their own AWS account and your application will help them upload files to their S3 account. If this is the case then you don't need to worry about protecting the keys because the user will be responsible for adding their own keys after installing your application.

    0 讨论(0)
提交回复
热议问题