Skip to content

Commit ad52b6b

Browse files
refactor: rename GetPublicKey to GetAgentManifest, drop EvalKey from manifest
- Proto: GetPublicKey → GetAgentManifest, key_bundle_json → manifest_json - crypto: ReadPublicKeyBundle → ReadEncKey (string return, no EvalKey read) - buildBundle: remove EvalKey.json field; avoid unnecessary multi-MB disk read - Update interceptor method map, type switch, and all tests
1 parent 9888411 commit ad52b6b

9 files changed

Lines changed: 100 additions & 113 deletions

File tree

vault/internal/crypto/keys.go

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -86,26 +86,14 @@ func (f *EnvectorKeys) Decrypt(blob []byte) (scores [][]float64, shardIdx []int3
8686
return f.keys.Decrypt(blob)
8787
}
8888

89-
// PublicKeyBundle reads EncKey.json and EvalKey.json file contents from
90-
// disk. The strings are returned verbatim for inclusion in the GetPublicKey
91-
// gRPC response — clients re-parse them with their own SDK.
92-
type PublicKeyBundle struct {
93-
EncKey string
94-
EvalKey string
95-
}
96-
97-
func ReadPublicKeyBundle(p KeysParams) (*PublicKeyBundle, error) {
98-
encPath := filepath.Join(p.keyDir(), "EncKey.json")
99-
evalPath := filepath.Join(p.keyDir(), "EvalKey.json")
100-
enc, err := os.ReadFile(encPath)
101-
if err != nil {
102-
return nil, fmt.Errorf("crypto: read EncKey.json: %w", err)
103-
}
104-
eval, err := os.ReadFile(evalPath)
89+
// ReadEncKey reads EncKey.json from disk and returns its contents verbatim
90+
// for inclusion in the GetAgentManifest gRPC response.
91+
func ReadEncKey(p KeysParams) (string, error) {
92+
enc, err := os.ReadFile(filepath.Join(p.keyDir(), "EncKey.json"))
10593
if err != nil {
106-
return nil, fmt.Errorf("crypto: read EvalKey.json: %w", err)
94+
return "", fmt.Errorf("crypto: read EncKey.json: %w", err)
10795
}
108-
return &PublicKeyBundle{EncKey: string(enc), EvalKey: string(eval)}, nil
96+
return string(enc), nil
10997
}
11098

11199
func (f *EnvectorKeys) Close() error {

vault/internal/crypto/keys_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ func TestOpenSecretKeyMissingReturnsError(t *testing.T) {
2727
}
2828
}
2929

30-
func TestReadPublicKeyBundleMissingReturnsError(t *testing.T) {
30+
func TestReadEncKeyMissingReturnsError(t *testing.T) {
3131
p := KeysParams{Root: t.TempDir(), KeyID: "vault-key", Dim: 1024}
32-
if _, err := ReadPublicKeyBundle(p); err == nil {
33-
t.Error("ReadPublicKeyBundle on missing keys returned nil error")
32+
if _, err := ReadEncKey(p); err == nil {
33+
t.Error("ReadEncKey on missing keys returned nil error")
3434
}
3535
}
3636

vault/internal/server/grpc.go

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
pb "github.com/CryptoLabInc/rune-admin/vault/pkg/vaultpb"
1919
)
2020

21-
// MaxMessageSize bounds gRPC frames. EvalKey alone can be tens of MB.
21+
// MaxMessageSize bounds gRPC frames.
2222
const MaxMessageSize = 256 * 1024 * 1024
2323

2424
// Vault is the runtime container shared by all RPC handlers and the
@@ -79,9 +79,9 @@ type VaultGRPC struct {
7979

8080
func NewVaultGRPC(v *Vault) *VaultGRPC { return &VaultGRPC{v: v} }
8181

82-
// ── GetPublicKey ──────────────────────────────────────────────────
82+
// ── GetAgentManifest ─────────────────────────────────────────────
8383

84-
func (s *VaultGRPC) GetPublicKey(ctx context.Context, req *pb.GetPublicKeyRequest) (*pb.GetPublicKeyResponse, error) {
84+
func (s *VaultGRPC) GetAgentManifest(ctx context.Context, req *pb.GetAgentManifestRequest) (*pb.GetAgentManifestResponse, error) {
8585
start := time.Now()
8686
user := s.v.tokens.GetUsername(req.GetToken())
8787
if user == "" {
@@ -91,52 +91,51 @@ func (s *VaultGRPC) GetPublicKey(ctx context.Context, req *pb.GetPublicKeyReques
9191
statusStr := "success"
9292
var errDetail *string
9393
defer func() {
94-
s.emit(ctx, "get_public_key", user, nil, resultCount, statusStr, errDetail, time.Since(start))
94+
s.emit(ctx, "get_agent_manifest", user, nil, resultCount, statusStr, errDetail, time.Since(start))
9595
}()
9696

9797
username, role, err := s.v.tokens.Validate(req.GetToken())
9898
if err != nil {
9999
st, msg := mapTokenError(err)
100100
statusStr, errDetail = errStatus(err)
101-
return &pb.GetPublicKeyResponse{Error: msg}, status.Error(st, msg)
101+
return &pb.GetAgentManifestResponse{Error: msg}, status.Error(st, msg)
102102
}
103103
user = username
104104
if err := role.CheckScope("get_public_key"); err != nil {
105105
statusStr = "denied"
106106
ed := err.Error()
107107
errDetail = &ed
108-
return &pb.GetPublicKeyResponse{Error: err.Error()}, status.Error(codes.PermissionDenied, err.Error())
108+
return &pb.GetAgentManifestResponse{Error: err.Error()}, status.Error(codes.PermissionDenied, err.Error())
109109
}
110110

111111
bundle, err := s.v.buildBundle(req.GetToken())
112112
if err != nil {
113113
statusStr = "error"
114114
ed := err.Error()
115115
errDetail = &ed
116-
return &pb.GetPublicKeyResponse{Error: err.Error()}, status.Error(codes.Internal, err.Error())
116+
return &pb.GetAgentManifestResponse{Error: err.Error()}, status.Error(codes.Internal, err.Error())
117117
}
118118
js, err := json.Marshal(bundle)
119119
if err != nil {
120120
statusStr = "error"
121121
ed := err.Error()
122122
errDetail = &ed
123-
return &pb.GetPublicKeyResponse{Error: err.Error()}, status.Error(codes.Internal, err.Error())
123+
return &pb.GetAgentManifestResponse{Error: err.Error()}, status.Error(codes.Internal, err.Error())
124124
}
125125
resultCount = 1
126-
return &pb.GetPublicKeyResponse{KeyBundleJson: string(js)}, nil
126+
return &pb.GetAgentManifestResponse{ManifestJson: string(js)}, nil
127127
}
128128

129-
// buildBundle assembles the per-token JSON bundle returned by GetPublicKey.
129+
// buildBundle assembles the per-token JSON manifest returned by GetAgentManifest.
130130
// Order of keys is irrelevant — clients parse by name.
131131
func (s *Vault) buildBundle(token string) (map[string]any, error) {
132-
pub, err := crypto.ReadPublicKeyBundle(s.bundleParams)
132+
encKey, err := crypto.ReadEncKey(s.bundleParams)
133133
if err != nil {
134134
return nil, err
135135
}
136136
bundle := map[string]any{
137-
"EncKey.json": pub.EncKey,
138-
"EvalKey.json": pub.EvalKey,
139-
"key_id": s.bundleParams.KeyID,
137+
"EncKey.json": encKey,
138+
"key_id": s.bundleParams.KeyID,
140139
}
141140
if s.cfg.Keys.IndexName != "" {
142141
bundle["index_name"] = s.cfg.Keys.IndexName

vault/internal/server/grpc_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,10 @@ func newTestVault(t *testing.T) *Vault {
103103
return NewVault(cfg, store, nil, audit)
104104
}
105105

106-
func TestGetPublicKeyInvalidToken(t *testing.T) {
106+
func TestGetAgentManifestInvalidToken(t *testing.T) {
107107
v := newTestVault(t)
108108
srv := NewVaultGRPC(v)
109-
resp, err := srv.GetPublicKey(context.Background(), &pb.GetPublicKeyRequest{
109+
resp, err := srv.GetAgentManifest(context.Background(), &pb.GetAgentManifestRequest{
110110
Token: "evt_ffffffffffffffffffffffffffffffff",
111111
})
112112
if status.Code(err) != codes.Unauthenticated {

vault/internal/server/interceptors.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ import (
1919
// vaultMethods enumerates the gRPC method paths owned by VaultService.
2020
// Other services routed through the same gRPC server bypass runtime checks.
2121
var vaultMethods = map[string]bool{
22-
"/rune.vault.v1.VaultService/GetPublicKey": true,
23-
"/rune.vault.v1.VaultService/DecryptScores": true,
24-
"/rune.vault.v1.VaultService/DecryptMetadata": true,
22+
"/rune.vault.v1.VaultService/GetAgentManifest": true,
23+
"/rune.vault.v1.VaultService/DecryptScores": true,
24+
"/rune.vault.v1.VaultService/DecryptMetadata": true,
2525
}
2626

2727
// NewValidationInterceptor returns a unary server interceptor that runs
@@ -55,7 +55,7 @@ func NewValidationInterceptor() (grpc.UnaryServerInterceptor, error) {
5555
func runtimeCheckToken(req any) error {
5656
var token string
5757
switch r := req.(type) {
58-
case *pb.GetPublicKeyRequest:
58+
case *pb.GetAgentManifestRequest:
5959
token = r.GetToken()
6060
case *pb.DecryptScoresRequest:
6161
token = r.GetToken()

vault/internal/server/interceptors_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ func vaultMethodInfo(name string) *grpc.UnaryServerInfo {
5252

5353
func TestInterceptorPassesValidRequest(t *testing.T) {
5454
ic := mustInterceptor(t)
55-
req := &pb.GetPublicKeyRequest{Token: "evt_0123456789abcdef0123456789abcdef"}
56-
out, err := ic(context.Background(), req, vaultMethodInfo("GetPublicKey"), noopHandler)
55+
req := &pb.GetAgentManifestRequest{Token: "evt_0123456789abcdef0123456789abcdef"}
56+
out, err := ic(context.Background(), req, vaultMethodInfo("GetAgentManifest"), noopHandler)
5757
if err != nil {
5858
t.Fatalf("err = %v, want nil", err)
5959
}
@@ -65,8 +65,8 @@ func TestInterceptorPassesValidRequest(t *testing.T) {
6565
func TestInterceptorRejectsBadProtovalidate(t *testing.T) {
6666
ic := mustInterceptor(t)
6767
// Token shorter than 36 fails the proto-level constraint.
68-
req := &pb.GetPublicKeyRequest{Token: "too_short"}
69-
_, err := ic(context.Background(), req, vaultMethodInfo("GetPublicKey"), noopHandler)
68+
req := &pb.GetAgentManifestRequest{Token: "too_short"}
69+
_, err := ic(context.Background(), req, vaultMethodInfo("GetAgentManifest"), noopHandler)
7070
if err == nil {
7171
t.Fatal("err = nil, want validation error")
7272
}
@@ -79,11 +79,11 @@ func TestInterceptorRejectsControlCharToken(t *testing.T) {
7979
ic := mustInterceptor(t)
8080
// 36-char token containing a control byte (\x00) inside.
8181
// protovalidate only checks length, so the runtime layer catches this.
82-
req := &pb.GetPublicKeyRequest{Token: "evt_0123456789abcdef0123456789abc\x00ef"}
82+
req := &pb.GetAgentManifestRequest{Token: "evt_0123456789abcdef0123456789abc\x00ef"}
8383
if len(req.Token) != 36 {
8484
t.Fatalf("test setup: token length = %d, want 36", len(req.Token))
8585
}
86-
_, err := ic(context.Background(), req, vaultMethodInfo("GetPublicKey"), noopHandler)
86+
_, err := ic(context.Background(), req, vaultMethodInfo("GetAgentManifest"), noopHandler)
8787
if err == nil {
8888
t.Fatal("err = nil, want runtime error")
8989
}
@@ -97,7 +97,7 @@ func TestInterceptorAllowsNonVaultMethod(t *testing.T) {
9797
// Whitespace-around token would normally fail runtime check, but
9898
// non-Vault methods skip runtime checks (and the proto for this
9999
// dummy message doesn't apply).
100-
req := &pb.GetPublicKeyRequest{Token: "evt_0123456789abcdef0123456789abcdef"}
100+
req := &pb.GetAgentManifestRequest{Token: "evt_0123456789abcdef0123456789abcdef"}
101101
info := &grpc.UnaryServerInfo{FullMethod: "/grpc.health.v1.Health/Check"}
102102
if _, err := ic(context.Background(), req, info, noopHandler); err != nil {
103103
t.Errorf("non-vault method blocked: %v", err)

vault/pkg/vaultpb/vault_service.pb.go

Lines changed: 39 additions & 39 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)