Skip to content

Commit 73c2767

Browse files
authored
Add a ClientVPN connection handler request/response definition (#343)
* Add initial event structure and test for Client VPN * Add ClientVPN connection handler sample to README * Refactor to perform early return * Output IP addresses with %q instead of %s and change ErrorMsgOnFailedPostureCompliance message Co-authored-by: Gerhard Poul <[email protected]>
1 parent 5bc50ab commit 73c2767

File tree

5 files changed

+125
-0
lines changed

5 files changed

+125
-0
lines changed

events/README.md

Lines changed: 2 additions & 0 deletions
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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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: %q", sourceIP)
30+
}
31+
32+
log.Printf("SOURCE IP: %q", sourceIP)
33+
34+
if allowed, ok := AllowedIPs[sourceIP]; ok && allowed {
35+
log.Printf("Allowing access from: %q", sourceIP)
36+
return events.ClientVPNConnectionHandlerResponse{
37+
Allow: true,
38+
ErrorMsgOnFailedPostureCompliance: "",
39+
PostureComplianceStatuses: []string{},
40+
SchemaVersion: "v1",
41+
}, nil
42+
}
43+
44+
log.Printf("Blocking access from: %q", sourceIP)
45+
return events.ClientVPNConnectionHandlerResponse{
46+
Allow: false,
47+
ErrorMsgOnFailedPostureCompliance: "You're trying to connect from an IP address that is not allowed.",
48+
PostureComplianceStatuses: []string{"BlockedSourceIP"},
49+
SchemaVersion: "v1",
50+
}, nil
51+
}
52+
53+
func main() {
54+
lambda.Start(handler)
55+
}
56+
```

events/clientvpn.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package events
2+
3+
type ClientVPNConnectionHandlerRequest struct {
4+
ConnectionID string `json:"connection-id"`
5+
EndpointID string `json:"endpoint-id"`
6+
CommonName string `json:"common-name"`
7+
Username string `json:"username"`
8+
OSPlatform string `json:"platform"`
9+
OSPlatformVersion string `json:"platform-version"`
10+
PublicIP string `json:"public-ip"`
11+
ClientOpenVPNVersion string `json:"client-openvpn-version"`
12+
SchemaVersion string `json:"schema-version"`
13+
}
14+
15+
type ClientVPNConnectionHandlerResponse struct {
16+
Allow bool `json:"allow"`
17+
ErrorMsgOnFailedPostureCompliance string `json:"error-msg-on-failed-posture-compliance"`
18+
PostureComplianceStatuses []string `json:"posture-compliance-statuses"`
19+
SchemaVersion string `json:"schema-version"`
20+
}

events/clientvpn_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package events
2+
3+
import (
4+
"encoding/json"
5+
"io/ioutil"
6+
"testing"
7+
8+
"github.com/aws/aws-lambda-go/events/test"
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestClientVPNConnectionHandlerRequestMarshaling(t *testing.T) {
13+
// read json from file
14+
inputJSON, err := ioutil.ReadFile("./testdata/clientvpn-connectionhandler-request.json")
15+
if err != nil {
16+
t.Errorf("could not open test file. details: %v", err)
17+
}
18+
19+
// de-serialize into ClientVPNConnectionHandlerRequest
20+
var inputEvent ClientVPNConnectionHandlerRequest
21+
if err := json.Unmarshal(inputJSON, &inputEvent); err != nil {
22+
t.Errorf("could not unmarshal event. details: %v", err)
23+
}
24+
25+
// serialize to json
26+
outputJSON, err := json.Marshal(inputEvent)
27+
if err != nil {
28+
t.Errorf("could not marshal event. details: %v", err)
29+
}
30+
31+
assert.JSONEq(t, string(inputJSON), string(outputJSON))
32+
}
33+
34+
func TestClientVPNConnectionHandlerRequestMarshalingMalformedJson(t *testing.T) {
35+
test.TestMalformedJson(t, ClientVPNConnectionHandlerRequest{})
36+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"connection-id": "cvpn-connection-04e7e1b2f0daf9460",
3+
"endpoint-id": "cvpn-endpoint-0f13eab7f860433cc",
4+
"common-name": "",
5+
"username": "username",
6+
"platform": "",
7+
"platform-version": "",
8+
"public-ip": "10.11.12.13",
9+
"client-openvpn-version": "",
10+
"schema-version": "v1"
11+
}

0 commit comments

Comments
 (0)