Skip to content

Instantly share code, notes, and snippets.

@patrickbrandt
Last active March 13, 2018 04:38
Show Gist options
  • Save patrickbrandt/4d8297cd2314d9737e714dd702b5c7fc to your computer and use it in GitHub Desktop.
Save patrickbrandt/4d8297cd2314d9737e714dd702b5c7fc to your computer and use it in GitHub Desktop.

Revisions

  1. patrickbrandt revised this gist Jan 13, 2017. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions handler.py
    Original file line number Diff line number Diff line change
    @@ -7,6 +7,7 @@ def ping(event, context):
    print('two different referring IP address parsing techniques ip1: %s and ip2: %s') % (ip1, ip2)

    referringIP = event['requestContext']['identity']['sourceIp']
    #TODO: use a comma-delimited string to store multiple IP address values in the environment variable
    if (os.environ['IP_WHITELIST'] != referringIP):
    return {
    'statusCode': 401,
  2. patrickbrandt revised this gist Jan 13, 2017. No changes.
  3. patrickbrandt created this gist Jan 13, 2017.
    19 changes: 19 additions & 0 deletions handler.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    import json
    import os

    def ping(event, context):
    ip1 = event['headers']['X-Forwarded-For'].split(',')[0]
    ip2 = event['requestContext']['identity']['sourceIp']
    print('two different referring IP address parsing techniques ip1: %s and ip2: %s') % (ip1, ip2)

    referringIP = event['requestContext']['identity']['sourceIp']
    if (os.environ['IP_WHITELIST'] != referringIP):
    return {
    'statusCode': 401,
    'body': 'This IP address is not allowed %s' % referringIP
    }

    return {
    'statusCode': 200,
    'body': 'This IP address is allowed %s' % referringIP
    }
    18 changes: 18 additions & 0 deletions serverless.yml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    service: ip-restrict

    provider:
    name: aws
    runtime: python2.7
    stage: dev
    region: us-west-2
    profile: ko-playground-admin
    environment:
    IP_WHITELIST: "44.99.53.83"

    functions:
    ping:
    handler: handler.ping
    events:
    - http:
    path: ping
    method: GET