File
Blob: tun/server/delegation_rpc.go
| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "crypto/rand" |
| 7 | "errors" |
| 8 | "strings" |
| 9 | "time" |
| 10 | |
| 11 | "go.miragespace.co/specter/spec/acme" |
| 12 | "go.miragespace.co/specter/spec/protocol" |
| 13 | "go.miragespace.co/specter/spec/tun" |
| 14 | "go.miragespace.co/specter/util/promise" |
| 15 | |
| 16 | "github.com/twitchtv/twirp" |
| 17 | ) |
| 18 | |
| 19 | var ( |
| 20 | errDelegationNotFound = errors.New("unknown or revoked grant") |
| 21 | errDelegationInvalid = errors.New("invalid grant record") |
| 22 | errDelegationNotOwned = errors.New("hostname is not registered to the grant owner") |
| 23 | ) |
| 24 | |
| 25 | func (s *Server) readDelegation(ctx context.Context, id string) (*protocol.DelegationRecord, error) { |
| 26 | if !tun.IsDelegationID(id) { |
| 27 | return nil, errDelegationInvalid |
| 28 | } |
| 29 | data, err := s.Chord.Get(ctx, []byte(tun.DelegationKey(id))) |
| 30 | if err != nil { |
| 31 | return nil, err |
| 32 | } |
| 33 | if len(data) == 0 { |
| 34 | return nil, errDelegationNotFound |
| 35 | } |
| 36 | rec := &protocol.DelegationRecord{} |
| 37 | if rec.UnmarshalVT(data) != nil || rec.GetVersion() != tun.DelegationRecordVersion || rec.GetId() != id || len(rec.GetOwner().GetToken()) == 0 || rec.GetHostname() == "" || rec.GetExpiresAt() < 0 { |
| 38 | return nil, errDelegationInvalid |
| 39 | } |
| 40 | return rec, nil |
| 41 | } |
| 42 | |
| 43 | func (s *Server) verifyDelegatedAuthority(ctx context.Context, rec *protocol.DelegationRecord) error { |
| 44 | owned, err := s.ownsHostname(ctx, rec.GetOwner(), rec.GetHostname()) |
| 45 | if err != nil { |
| 46 | return err |
| 47 | } |
| 48 | if !owned { |
| 49 | return errDelegationNotOwned |
| 50 | } |
| 51 | return nil |
| 52 | } |
| 53 | |
| 54 | func (s *Server) ownsHostname(ctx context.Context, owner *protocol.ClientToken, hostname string) (bool, error) { |
| 55 | owned, err := s.Chord.PrefixContains(ctx, []byte(tun.ClientHostnamesPrefix(owner)), []byte(hostname)) |
| 56 | if err != nil || !owned { |
| 57 | return false, err |
| 58 | } |
| 59 | if strings.Contains(hostname, ".") { |
| 60 | custom, err := tun.FindCustomHostname(ctx, s.Chord, hostname) |
| 61 | if errors.Is(err, tun.ErrHostnameNotFound) { |
| 62 | return false, nil |
| 63 | } |
| 64 | if err != nil { |
| 65 | return false, err |
| 66 | } |
| 67 | return bytes.Equal(custom.GetClientToken().GetToken(), owner.GetToken()), nil |
| 68 | } |
| 69 | return true, nil |
| 70 | } |
| 71 | |
| 72 | func delegationAuthorityError(err error) error { |
| 73 | if errors.Is(err, errDelegationNotOwned) { |
| 74 | return twirp.PermissionDenied.Error(err.Error()) |
| 75 | } |
| 76 | return twirp.Unavailable.Error(err.Error()) |
| 77 | } |
| 78 | |
| 79 | func normalizeDelegationHostname(hostname string) (string, error) { |
| 80 | if strings.Contains(hostname, ".") { |
| 81 | return acme.Normalize(hostname) |
| 82 | } |
| 83 | // Short labels must match a server-generated owner registration exactly. |
| 84 | return hostname, nil |
| 85 | } |
| 86 | |
| 87 | func grantFromRecord(rec *protocol.DelegationRecord) *protocol.DelegationGrant { |
| 88 | return &protocol.DelegationGrant{ |
| 89 | Id: rec.GetId(), |
| 90 | Hostname: rec.GetHostname(), |
| 91 | ExpiresAt: rec.GetExpiresAt(), |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | func (s *Server) MintDelegation(ctx context.Context, req *protocol.MintDelegationRequest) (*protocol.MintDelegationResponse, error) { |
| 96 | owner, _, err := extractAuthenticated(ctx) |
| 97 | if err != nil { |
| 98 | return nil, err |
| 99 | } |
| 100 | hostname, err := normalizeDelegationHostname(req.GetHostname()) |
| 101 | if err != nil { |
| 102 | return nil, twirp.InvalidArgument.Error(err.Error()) |
| 103 | } |
| 104 | if req.GetExpiresAt() != 0 && req.GetExpiresAt() <= time.Now().Unix() { |
| 105 | return nil, twirp.InvalidArgument.Error("expires_at must be in the future") |
| 106 | } |
| 107 | rec := &protocol.DelegationRecord{ |
| 108 | Version: tun.DelegationRecordVersion, |
| 109 | Owner: owner, |
| 110 | Hostname: hostname, |
| 111 | ExpiresAt: req.GetExpiresAt(), |
| 112 | } |
| 113 | if err := s.verifyDelegatedAuthority(ctx, rec); err != nil { |
| 114 | return nil, delegationAuthorityError(err) |
| 115 | } |
| 116 | index := []byte(tun.ClientDelegationsPrefix(owner)) |
| 117 | ids, err := s.Chord.PrefixList(ctx, index) |
| 118 | if err != nil { |
| 119 | return nil, twirp.Unavailable.Error(err.Error()) |
| 120 | } |
| 121 | if len(ids) >= tun.MaxDelegationsPerOwner { |
| 122 | return nil, twirp.ResourceExhausted.Error("delegation limit reached; revoke unused or incomplete grants") |
| 123 | } |
| 124 | var secret [32]byte |
| 125 | if _, err := rand.Read(secret[:]); err != nil { |
| 126 | return nil, twirp.InternalErrorWith(err) |
| 127 | } |
| 128 | rec.Id = tun.DelegationID(secret) |
| 129 | data, err := rec.MarshalVT() |
| 130 | if err != nil { |
| 131 | return nil, twirp.InternalErrorWith(err) |
| 132 | } |
| 133 | if err := s.Chord.PrefixAppend(ctx, index, []byte(rec.Id)); err != nil { |
| 134 | return nil, twirp.Unavailable.Error(err.Error()) |
| 135 | } |
| 136 | if err := s.Chord.Put(ctx, []byte(tun.DelegationKey(rec.Id)), data); err != nil { |
| 137 | return nil, twirp.Unavailable.Errorf("grant %s may be incomplete: %v", rec.Id, err) |
| 138 | } |
| 139 | return &protocol.MintDelegationResponse{ |
| 140 | Grant: grantFromRecord(rec), |
| 141 | Token: tun.EncodeDelegationToken(secret), |
| 142 | }, nil |
| 143 | } |
| 144 | |
| 145 | func (s *Server) ListDelegations(ctx context.Context, _ *protocol.ListDelegationsRequest) (*protocol.ListDelegationsResponse, error) { |
| 146 | owner, _, err := extractAuthenticated(ctx) |
| 147 | if err != nil { |
| 148 | return nil, err |
| 149 | } |
| 150 | ids, err := s.Chord.PrefixList(ctx, []byte(tun.ClientDelegationsPrefix(owner))) |
| 151 | if err != nil { |
| 152 | return nil, twirp.Unavailable.Error(err.Error()) |
| 153 | } |
| 154 | if len(ids) > tun.MaxDelegationsPerOwner { |
| 155 | ids = ids[:tun.MaxDelegationsPerOwner] |
| 156 | } |
| 157 | resp := &protocol.ListDelegationsResponse{} |
| 158 | for offset := 0; offset < len(ids); offset += 8 { |
| 159 | batch := ids[offset:min(offset+8, len(ids))] |
| 160 | jobs := make([]func(context.Context) (*protocol.DelegationGrant, error), len(batch)) |
| 161 | for i, raw := range batch { |
| 162 | id := string(raw) |
| 163 | jobs[i] = func(ctx context.Context) (*protocol.DelegationGrant, error) { |
| 164 | rec, err := s.readDelegation(ctx, id) |
| 165 | if errors.Is(err, errDelegationNotFound) || errors.Is(err, errDelegationInvalid) || (err == nil && !bytes.Equal(rec.GetOwner().GetToken(), owner.GetToken())) { |
| 166 | return &protocol.DelegationGrant{ |
| 167 | Id: id, |
| 168 | Incomplete: true, |
| 169 | }, nil |
| 170 | } |
| 171 | if err != nil { |
| 172 | return nil, err |
| 173 | } |
| 174 | return grantFromRecord(rec), nil |
| 175 | } |
| 176 | } |
| 177 | grants, errs := promise.All(ctx, jobs...) |
| 178 | for _, err := range errs { |
| 179 | if err != nil { |
| 180 | return nil, twirp.Unavailable.Error(err.Error()) |
| 181 | } |
| 182 | } |
| 183 | resp.Grants = append(resp.Grants, grants...) |
| 184 | } |
| 185 | return resp, nil |
| 186 | } |
| 187 | |
| 188 | func (s *Server) RevokeDelegation(ctx context.Context, req *protocol.RevokeDelegationRequest) (*protocol.RevokeDelegationResponse, error) { |
| 189 | owner, _, err := extractAuthenticated(ctx) |
| 190 | if err != nil { |
| 191 | return nil, err |
| 192 | } |
| 193 | id := req.GetId() |
| 194 | if !tun.IsDelegationID(id) { |
| 195 | return nil, twirp.InvalidArgument.Error("invalid grant ID") |
| 196 | } |
| 197 | rec, err := s.readDelegation(ctx, id) |
| 198 | if err != nil && !errors.Is(err, errDelegationNotFound) && !errors.Is(err, errDelegationInvalid) { |
| 199 | return nil, twirp.Unavailable.Error(err.Error()) |
| 200 | } |
| 201 | index := []byte(tun.ClientDelegationsPrefix(owner)) |
| 202 | if rec == nil || !bytes.Equal(rec.GetOwner().GetToken(), owner.GetToken()) { |
| 203 | indexed, err := s.Chord.PrefixContains(ctx, index, []byte(id)) |
| 204 | if err != nil { |
| 205 | return nil, twirp.Unavailable.Error(err.Error()) |
| 206 | } |
| 207 | if !indexed { |
| 208 | return nil, twirp.NotFound.Error("grant is not owned by this client") |
| 209 | } |
| 210 | } |
| 211 | if err := s.Chord.Delete(ctx, []byte(tun.DelegationKey(id))); err != nil { |
| 212 | return nil, twirp.Unavailable.Error(err.Error()) |
| 213 | } |
| 214 | resp := &protocol.RevokeDelegationResponse{Revoked: true} |
| 215 | if err := s.Chord.PrefixRemove(ctx, index, []byte(id)); err != nil { |
| 216 | resp.IndexError = err.Error() |
| 217 | } |
| 218 | return resp, nil |
| 219 | } |