Skip to content

Commit 3a66485

Browse files
fix(core): Fixes for ec-wrapped from js client (#1923)
### Proposed Changes * Javascript still uses the old, non-bulk request object format for rewrap * As such, the logic for upgrading the request body needs to support the ec wrapped kao type ### Checklist - [ ] I have added or updated unit tests - [ ] I have added or updated integration tests (if appropriate) - [ ] I have added or updated documentation ### Testing Instructions
1 parent e6a53a3 commit 3a66485

File tree

2 files changed

+37
-25
lines changed

2 files changed

+37
-25
lines changed

service/kas/access/keyaccess.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package access
22

33
type KeyAccess struct {
4-
EncryptedMetadata string `json:"encryptedMetadata,omitempty"`
5-
PolicyBinding interface{} `json:"policyBinding,omitempty"`
6-
Protocol string `json:"protocol"`
7-
Type string `json:"type"`
8-
URL string `json:"url"`
9-
KID string `json:"kid,omitempty"`
10-
SID string `json:"sid,omitempty"`
11-
WrappedKey []byte `json:"wrappedKey,omitempty"`
12-
Header []byte `json:"header,omitempty"`
13-
Algorithm string `json:"algorithm,omitempty"`
4+
EncryptedMetadata string `json:"encryptedMetadata,omitempty"`
5+
PolicyBinding interface{} `json:"policyBinding,omitempty"`
6+
Protocol string `json:"protocol"`
7+
Type string `json:"type"`
8+
URL string `json:"url"`
9+
KID string `json:"kid,omitempty"`
10+
SID string `json:"sid,omitempty"`
11+
WrappedKey []byte `json:"wrappedKey,omitempty"`
12+
Header []byte `json:"header,omitempty"`
13+
Algorithm string `json:"algorithm,omitempty"`
14+
EphemeralPublicKey string `json:"ephemeralPublicKey,omitempty"`
1415
}

service/kas/access/rewrap.go

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -158,17 +158,21 @@ func extractAndConvertV1SRTBody(body []byte) (kaspb.UnsignedRewrapRequest, error
158158
reqs := []*kaspb.UnsignedRewrapRequest_WithPolicyRequest{
159159
{
160160
KeyAccessObjects: []*kaspb.UnsignedRewrapRequest_WithKeyAccessObject{
161-
{KeyAccessObjectId: "kao-0", KeyAccessObject: &kaspb.KeyAccess{
162-
EncryptedMetadata: kao.EncryptedMetadata,
163-
PolicyBinding: &kaspb.PolicyBinding{Hash: binding, Algorithm: kao.Algorithm},
164-
Protocol: kao.Protocol,
165-
KeyType: kao.Type,
166-
KasUrl: kao.URL,
167-
Kid: kao.KID,
168-
SplitId: kao.SID,
169-
WrappedKey: kao.WrappedKey,
170-
Header: kao.Header,
171-
}},
161+
{
162+
KeyAccessObjectId: "kao-0",
163+
KeyAccessObject: &kaspb.KeyAccess{
164+
EncryptedMetadata: kao.EncryptedMetadata,
165+
PolicyBinding: &kaspb.PolicyBinding{Hash: binding, Algorithm: kao.Algorithm},
166+
Protocol: kao.Protocol,
167+
KeyType: kao.Type,
168+
KasUrl: kao.URL,
169+
Kid: kao.KID,
170+
SplitId: kao.SID,
171+
WrappedKey: kao.WrappedKey,
172+
Header: kao.Header,
173+
EphemeralPublicKey: []byte(kao.EphemeralPublicKey),
174+
},
175+
},
172176
},
173177
Algorithm: requestBody.Algorithm,
174178
Policy: &kaspb.UnsignedRewrapRequest_WithPolicy{
@@ -402,21 +406,24 @@ func (p *Provider) Rewrap(ctx context.Context, req *connect.Request[kaspb.Rewrap
402406

403407
if isV1 {
404408
if len(results) != 1 {
405-
return nil, fmt.Errorf("invalid request")
409+
p.Logger.WarnContext(ctx, "400 due to wrong result set size", "results", results)
410+
return nil, err400("invalid request")
406411
}
407412
kaoResults := *getMapValue(results)
408413
if len(kaoResults) != 1 {
409-
return nil, fmt.Errorf("invalid request")
414+
p.Logger.WarnContext(ctx, "400 due to wrong result set size", "kaoResults", kaoResults, "results", results)
415+
return nil, err400("invalid request")
410416
}
411417
kao := *getMapValue(kaoResults)
412418

413419
if kao.Error != nil {
420+
p.Logger.DebugContext(ctx, "forwarding legacy err", "err", err)
414421
return nil, kao.Error
415422
}
416423
resp.EntityWrappedKey = kao.Encapped //nolint:staticcheck // deprecated but keeping behavior for backwards compatibility
417424
}
418425

419-
return connect.NewResponse(resp), err
426+
return connect.NewResponse(resp), nil
420427
}
421428

422429
func (p *Provider) verifyRewrapRequests(ctx context.Context, req *kaspb.UnsignedRewrapRequest_WithPolicyRequest) (*Policy, map[string]kaoResult, error) {
@@ -555,8 +562,11 @@ func (p *Provider) tdf3Rewrap(ctx context.Context, requests []*kaspb.UnsignedRew
555562
policyReqs := make(map[*Policy]*kaspb.UnsignedRewrapRequest_WithPolicyRequest)
556563
for _, req := range requests {
557564
policy, kaoResults, err := p.verifyRewrapRequests(ctx, req)
558-
results[req.GetPolicy().GetId()] = kaoResults
565+
policyID := req.GetPolicy().GetId()
566+
results[policyID] = kaoResults
559567
if err != nil {
568+
p.Logger.WarnContext(ctx, "rewrap: verifyRewrapRequests failed", "err", err, "policyID", policyID)
569+
// TODO Fail all requests for this policy
560570
continue
561571
}
562572
policies = append(policies, policy)
@@ -569,6 +579,7 @@ func (p *Provider) tdf3Rewrap(ctx context.Context, requests []*kaspb.UnsignedRew
569579
}
570580
pdpAccessResults, accessErr := p.canAccess(ctx, tok, policies)
571581
if accessErr != nil {
582+
p.Logger.DebugContext(ctx, "tdf3rewrap: cannot access policy", "err", accessErr, "policies", policies)
572583
failAllKaos(requests, results, err403("could not perform access"))
573584
return "", results
574585
}

0 commit comments

Comments
 (0)