Skip to content

Commit 9ff3806

Browse files
authored
feat(sdk): Enable base key support. (#2425)
### Proposed Changes 1.) Add base key support in SDK 2.) If the base key is present we attempt to use it, if not we use the default kases passed in. >[!IMPORTANT] >Decision points located [here](https://github.com/opentdf/platform/pull/2445/files) ### 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 98c3c16 commit 9ff3806

File tree

13 files changed

+1277
-233
lines changed

13 files changed

+1277
-233
lines changed

sdk/basekey.go

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package sdk
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"errors"
7+
"fmt"
8+
9+
"github.com/opentdf/platform/lib/ocrypto"
10+
"github.com/opentdf/platform/protocol/go/policy"
11+
"github.com/opentdf/platform/protocol/go/wellknownconfiguration"
12+
"google.golang.org/protobuf/encoding/protojson"
13+
)
14+
15+
// Should match:
16+
// https://github.com/opentdf/platform/blob/main/service/wellknownconfiguration/wellknown_configuration.go#L25
17+
const (
18+
baseKeyWellKnown = "base_key"
19+
baseKeyAlg = "algorithm"
20+
baseKeyPublicKey = "public_key"
21+
wellKnownConfigKey = "configuration"
22+
)
23+
24+
// TODO: Move this function to ocrypto?
25+
func getKasKeyAlg(alg string) policy.Algorithm {
26+
switch alg {
27+
case string(ocrypto.RSA2048Key):
28+
return policy.Algorithm_ALGORITHM_RSA_2048
29+
case rsa4096:
30+
return policy.Algorithm_ALGORITHM_RSA_4096
31+
case string(ocrypto.EC256Key):
32+
return policy.Algorithm_ALGORITHM_EC_P256
33+
case string(ocrypto.EC384Key):
34+
return policy.Algorithm_ALGORITHM_EC_P384
35+
case string(ocrypto.EC521Key):
36+
return policy.Algorithm_ALGORITHM_EC_P521
37+
default:
38+
return policy.Algorithm_ALGORITHM_UNSPECIFIED
39+
}
40+
}
41+
42+
// TODO: Move this function to ocrypto?
43+
func formatAlg(alg policy.Algorithm) (string, error) {
44+
switch alg {
45+
case policy.Algorithm_ALGORITHM_RSA_2048:
46+
return string(ocrypto.RSA2048Key), nil
47+
case policy.Algorithm_ALGORITHM_RSA_4096:
48+
return rsa4096, nil
49+
case policy.Algorithm_ALGORITHM_EC_P256:
50+
return string(ocrypto.EC256Key), nil
51+
case policy.Algorithm_ALGORITHM_EC_P384:
52+
return string(ocrypto.EC384Key), nil
53+
case policy.Algorithm_ALGORITHM_EC_P521:
54+
return string(ocrypto.EC521Key), nil
55+
case policy.Algorithm_ALGORITHM_UNSPECIFIED:
56+
fallthrough
57+
default:
58+
return "", fmt.Errorf("unsupported algorithm: %s", alg)
59+
}
60+
}
61+
62+
func getBaseKey(ctx context.Context, s SDK) (*policy.SimpleKasKey, error) {
63+
req := &wellknownconfiguration.GetWellKnownConfigurationRequest{}
64+
response, err := s.wellknownConfiguration.GetWellKnownConfiguration(ctx, req)
65+
if err != nil {
66+
return nil, errors.Join(errors.New("unable to retrieve config information, and none was provided"), err)
67+
}
68+
configuration := response.GetConfiguration()
69+
if configuration == nil {
70+
return nil, ErrWellKnowConfigEmpty
71+
}
72+
73+
configMap := configuration.AsMap()
74+
if len(configMap) == 0 {
75+
return nil, ErrWellKnowConfigEmpty
76+
}
77+
78+
baseKeyStructure, ok := configMap[baseKeyWellKnown]
79+
if !ok {
80+
return nil, errBaseKeyNotFound
81+
}
82+
83+
baseKeyMap, ok := baseKeyStructure.(map[string]interface{})
84+
if !ok {
85+
return nil, errBaseKeyInvalidFormat
86+
}
87+
88+
simpleKasKey, err := parseSimpleKasKey(baseKeyMap)
89+
if err != nil {
90+
return nil, err
91+
}
92+
93+
return simpleKasKey, nil
94+
}
95+
96+
func parseSimpleKasKey(baseKeyMap map[string]interface{}) (*policy.SimpleKasKey, error) {
97+
simpleKasKey := &policy.SimpleKasKey{}
98+
99+
if len(baseKeyMap) == 0 {
100+
return nil, errBaseKeyEmpty
101+
}
102+
103+
publicKey, ok := baseKeyMap[baseKeyPublicKey].(map[string]interface{})
104+
if !ok {
105+
return nil, errBaseKeyInvalidFormat
106+
}
107+
108+
alg, ok := publicKey[baseKeyAlg].(string)
109+
if !ok {
110+
return nil, errBaseKeyInvalidFormat
111+
}
112+
publicKey[baseKeyAlg] = getKasKeyAlg(alg)
113+
baseKeyMap[baseKeyPublicKey] = publicKey
114+
configJSON, err := json.Marshal(baseKeyMap)
115+
if err != nil {
116+
return nil, errors.Join(errMarshalBaseKeyFailed, err)
117+
}
118+
119+
err = protojson.Unmarshal(configJSON, simpleKasKey)
120+
if err != nil {
121+
return nil, errors.Join(errUnmarshalBaseKeyFailed, err)
122+
}
123+
return simpleKasKey, nil
124+
}

0 commit comments

Comments
 (0)