Reputation: 744
I have an api gateway that is going to be accessed by a front end javascript application. With that in mind we can't feasibly limit access to an api key (I don't think?), so I'd really like to limit it by IP address...but I can't find a way to do that. Is that even possible as it's not actually coming from a server?
So the site is hosted on S3 with Cloudfront in Front. The API gateway accepts couple of keys and that hits a lambda script and gets saved to dynamodb. If someone wanted to they could just spam the hell out of it with values. I only want the web app to be able to talk to it. I'm still learning a lot of AWS! I hoped I could use Shield or WAF but it seems a no go. Any suggestions?
Upvotes: 2
Views: 2386
Reputation: 10567
Of course, it is not a good idea to hard-code any credential or API key in your app, and most of the time, you'll find articles that explain how to use Web federated identity providers to authorize your API calls through API Gateway (see Setting Credentials in a Web Browser), but since you're using CloudFront, there is another workaround for that.
You can store your API key (or even IAM credentials) in a separate file on S3 but limit its access to a specific IP address so that your script can get the key and make the API call. All other IP addresses cannot retrieve the API key. Thus, cannot call the API method.
api-key.json
. Make sure that Restrict Viewer Access is set to Yes for the behavior. Make sure that this bahavior has precedence over the Default behavior by placing it top in the list, i.e., precedence is 0.NOTE: If you're certain your signed URL is stored securely and nobody has access to it, you can remove the IP address requirement from your custom policy since it would be redundant to check for the IP address in this case. Of course, this will allow you to use a canned policy instead.
Upvotes: 4