This repository has been archived by the owner on Nov 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
bootstrap_aws.go
72 lines (65 loc) · 2.2 KB
/
bootstrap_aws.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package app
import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/labstack/echo/v4"
awsprovider "github.com/criticalstack/crit/cmd/bootstrap-server/internal/providers/aws"
"github.com/criticalstack/crit/pkg/cluster/bootstrap"
"github.com/criticalstack/crit/pkg/cluster/bootstrap/authorizers/ec2metadata"
)
func (r *bootstrapRouter) handleAmazonIdentityDocumentAndSignature(data []byte) echo.HandlerFunc {
return func(c echo.Context) error {
var sdoc ec2metadata.SignedDocument
if err := json.Unmarshal(data, &sdoc); err != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"error": err.Error()})
}
if err := ec2metadata.Verify(sdoc.Document, sdoc.Signature); err != nil {
return c.JSON(http.StatusUnauthorized, map[string]string{"error": err.Error()})
}
var doc ec2metadata.Document
if err := json.Unmarshal(sdoc.Document, &doc); err != nil {
return c.JSON(http.StatusUnauthorized, map[string]string{"error": err.Error()})
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
cfg := &aws.Config{Region: aws.String(doc.Region)}
ip, profile, err := awsprovider.GetInstanceInfo(ctx, cfg, doc.InstanceId)
if err != nil {
return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
}
if c.RealIP() != ip {
return c.JSON(http.StatusUnauthorized, map[string]string{
"error": fmt.Sprintf("expected ip %q, received %q", ip, c.RealIP()),
})
}
for k, v := range r.cfg.Filters {
switch k {
case "account-id":
if doc.AccountId != v {
return c.JSON(http.StatusUnauthorized, map[string]string{
"error": fmt.Sprintf("account not authorized: %#v", doc.AccountId),
})
}
case "iam-profile":
if profile != v {
return c.JSON(http.StatusUnauthorized, map[string]string{
"error": fmt.Sprintf("IAM instance profile not authorized: %#v", profile),
})
}
}
}
token, err := createNewToken(r.cfg.Kubeconfig)
if err != nil {
return c.JSON(http.StatusInternalServerError, map[string]string{
"error": err.Error(),
})
}
return c.JSON(http.StatusOK, &bootstrap.Response{
BootstrapToken: token,
})
}
}