-
Notifications
You must be signed in to change notification settings - Fork 10
/
service_account.go
79 lines (67 loc) · 1.64 KB
/
service_account.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
73
74
75
76
77
78
79
package server
import (
"context"
"fmt"
"github.com/hashicorp/horizon/pkg/dbx"
hznpb "github.com/hashicorp/horizon/pkg/pb"
"google.golang.org/grpc/peer"
"github.com/hashicorp/waypoint-hzn/pkg/models"
"github.com/hashicorp/waypoint-hzn/pkg/pb"
)
var (
GuestLimits = &hznpb.Account_Limits{
HttpRequests: 5, // per second
Bandwidth: 1024 / 60.0, // in KB/second
}
)
func (s *service) RegisterGuestAccount(
ctx context.Context,
req *pb.RegisterGuestAccountRequest,
) (*pb.RegisterGuestAccountResponse, error) {
p, _ := peer.FromContext(ctx)
accountId := hznpb.NewULID()
s.Logger.Info("creating guest account",
"client-ip", p.Addr.String(),
"account-id", accountId.String(),
"accept-tos", req.AcceptTos,
)
if !req.AcceptTos {
return nil, fmt.Errorf("TOS not accepted, rejecting request to create guest account")
}
_, err := s.HznControl.AddAccount(ctx, &hznpb.AddAccountRequest{
Account: &hznpb.Account{
AccountId: accountId,
Namespace: s.Namespace,
},
Limits: GuestLimits,
})
if err != nil {
return nil, err
}
// Register the token with the control server
ctr, err := s.HznControl.CreateToken(ctx, &hznpb.CreateTokenRequest{
Account: &hznpb.Account{
AccountId: accountId,
Namespace: s.Namespace,
},
Capabilities: []hznpb.TokenCapability{
{
Capability: hznpb.SERVE,
},
{
Capability: hznpb.CONNECT,
},
},
})
if err != nil {
return nil, err
}
// Register the account in our database
err = dbx.Check(s.DB.Create(&models.Registration{
AccountId: accountId.Bytes(),
}))
if err != nil {
return nil, err
}
return &pb.RegisterGuestAccountResponse{Token: ctr.Token}, nil
}