Skip to content

Commit cd68b2e

Browse files
committed
Add ClientVPN connection handler sample to README
1 parent 4a91aa9 commit cd68b2e

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

events/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ This package provides input types for Lambda functions that process AWS events.
1212

1313
[AppSync](README_AppSync.md)
1414

15+
[ClientVPN Connection Handler](README_ClientVPN.md)
16+
1517
[CloudFormation Events](../cfn/README.md)
1618

1719
[CloudWatch Events](README_CloudWatch_Events.md)

events/README_ClientVPN.md

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Sample Function
2+
3+
The following is a sample Lambda function that receives a Client VPN connection handler request as an input and then validates the IP address input and checks whether the connection source IP is on the allowed list defined as a map inside the function. If the source IP matches an allowed IP address it allows the access, otherwise an error message is presented to the user. Debug logs are generated to CloudWatch Logs. (Note that by default anything written to Console will be logged as CloudWatch Logs events.)
4+
5+
```go
6+
import (
7+
"fmt"
8+
"log"
9+
"net"
10+
11+
"encoding/json"
12+
13+
"github.com/aws/aws-lambda-go/events"
14+
"github.com/aws/aws-lambda-go/lambda"
15+
)
16+
17+
var (
18+
AllowedIPs = map[string]bool{
19+
"10.11.12.13": true,
20+
}
21+
)
22+
23+
func handler(request events.ClientVPNConnectionHandlerRequest) (events.ClientVPNConnectionHandlerResponse, error) {
24+
requestJson, _ := json.MarshalIndent(request, "", " ")
25+
log.Printf("REQUEST: %s", requestJson)
26+
27+
sourceIP := request.PublicIP
28+
if net.ParseIP(sourceIP) == nil {
29+
return events.ClientVPNConnectionHandlerResponse{}, fmt.Errorf("Invalid parameter PublicIP passed in request: '%s'", sourceIP)
30+
}
31+
32+
log.Printf("SOURCE IP: %s", sourceIP)
33+
34+
postureCompliance := []string{}
35+
36+
allowed, ok := AllowedIPs[sourceIP]
37+
if !ok {
38+
allowed = false
39+
postureCompliance = []string{"BlockedSourceIP"}
40+
}
41+
42+
if allowed {
43+
log.Printf("Allowing access from: %s", sourceIP)
44+
} else {
45+
log.Printf("Blocking access from: %s", sourceIP)
46+
}
47+
48+
return events.ClientVPNConnectionHandlerResponse{
49+
Allow: allowed,
50+
ErrorMsgOnFailedPostureCompliance: "You're accessing from a blocked IP range.",
51+
PostureComplianceStatuses: postureCompliance,
52+
SchemaVersion: "v1",
53+
}, nil
54+
}
55+
56+
func main() {
57+
lambda.Start(handler)
58+
}
59+
```

0 commit comments

Comments
 (0)