99import java .util .List ;
1010import java .util .Map ;
1111import java .util .Objects ;
12+ import java .util .Optional ;
1213import java .util .UUID ;
1314
15+ import static io .opentdf .platform .sdk .Autoconfigure .Granter .generatePlanFromDefaultKases ;
16+
1417public class Planner {
1518 private final Config .TDFConfig tdfConfig ;
1619 private final SDK .Services sdkServices ;
17- private final Autoconfigure .Granter granter ;
1820
1921 private static final Logger logger = LoggerFactory .getLogger (Planner .class );
2022
2123 public Planner (Config .TDFConfig config , SDK .Services services ) {
2224 tdfConfig = Objects .requireNonNull (config );
2325 sdkServices = Objects .requireNonNull (services ) ;
26+ }
2427
25- List <String > dk = defaultKases (tdfConfig );
26- if (tdfConfig .autoconfigure ) {
27- if (tdfConfig .splitPlan != null && !tdfConfig .splitPlan .isEmpty ()) {
28- throw new IllegalArgumentException ("cannot use autoconfigure with a split plan provided in the TDFConfig" );
29- }
30- Autoconfigure .Granter granter = new Autoconfigure .Granter (new ArrayList <>());
31- if (tdfConfig .attributeValues != null && !tdfConfig .attributeValues .isEmpty ()) {
32- granter = Autoconfigure .newGranterFromAttributes (services .kas ().getKeyCache (), tdfConfig .attributeValues .toArray (new Value [0 ]));
33- } else if (tdfConfig .attributes != null && !tdfConfig .attributes .isEmpty ()) {
34- granter = Autoconfigure .newGranterFromService (services .attributes (), services .kas ().getKeyCache (),
35- tdfConfig .attributes .toArray (new Autoconfigure .AttributeValueFQN [0 ]));
36- }
28+ private static String getUUID () {
29+ return UUID .randomUUID ().toString ();
30+ }
3731
38- this .granter = granter ;
32+ Map <String , List <Config .KASInfo >> getSplits (Config .TDFConfig tdfConfig ) {
33+ List <Autoconfigure .KeySplitStep > splitPlan ;
34+ List <String > defaultKases = defaultKases (tdfConfig );
35+ if (tdfConfig .autoconfigure ) {
36+ splitPlan = getAutoconfigurePlan (tdfConfig );
37+ } else if (tdfConfig .splitPlan == null || tdfConfig .splitPlan .isEmpty ()) {
38+ splitPlan = defaultKases .isEmpty ()
39+ ? createPlanFromBaseKey ()
40+ : generatePlanFromDefaultKases (defaultKases , Planner ::getUUID );
3941 } else {
40- this . granter = null ;
42+ splitPlan = tdfConfig . splitPlan ;
4143 }
4244
4345 if (tdfConfig .kasInfoList .isEmpty () && tdfConfig .splitPlan .isEmpty ()) {
4446 throw new SDK .KasInfoMissing ("kas information is missing, no key access template specified or inferred" );
4547 }
48+
49+ // split plan: restructure by conjunctions
50+ return fillInKeys (tdfConfig , splitPlan );
4651 }
4752
48- Map <String , List <Config .KASInfo >> getSplits (Config .TDFConfig tdfConfig ) {
53+ private List <Autoconfigure .KeySplitStep > getAutoconfigurePlan (Config .TDFConfig tdfConfig ) {
54+ if (tdfConfig .splitPlan != null && !tdfConfig .splitPlan .isEmpty ()) {
55+ throw new IllegalArgumentException ("cannot use autoconfigure with a split plan provided in the TDFConfig" );
56+ }
57+ Autoconfigure .Granter granter = new Autoconfigure .Granter (new ArrayList <>());
58+ if (tdfConfig .attributeValues != null && !tdfConfig .attributeValues .isEmpty ()) {
59+ granter = Autoconfigure .newGranterFromAttributes (sdkServices .kas ().getKeyCache (), tdfConfig .attributeValues .toArray (new Value [0 ]));
60+ } else if (tdfConfig .attributes != null && !tdfConfig .attributes .isEmpty ()) {
61+ granter = Autoconfigure .newGranterFromService (sdkServices .attributes (), sdkServices .kas ().getKeyCache (),
62+ tdfConfig .attributes .toArray (new Autoconfigure .AttributeValueFQN [0 ]));
63+ }
64+ return granter .getSplits (defaultKases (tdfConfig ), Planner ::getUUID , Optional ::empty );
65+ }
66+
67+ private List <Autoconfigure .KeySplitStep > createPlanFromBaseKey () {
68+ return null ;
69+ }
70+
71+ private Map <String , List <Config .KASInfo >> fillInKeys (Config .TDFConfig tdfConfig , List <Autoconfigure .KeySplitStep > splitPlan ) {
72+ Map <String , List <Config .KASInfo >> conjunction = new HashMap <>();
4973 var latestKASInfo = new HashMap <String , Config .KASInfo >();
5074 // Seed anything passed in manually
5175 for (Config .KASInfo kasInfo : tdfConfig .kasInfoList ) {
@@ -54,10 +78,7 @@ Map<String, List<Config.KASInfo>> getSplits(Config.TDFConfig tdfConfig) {
5478 }
5579 }
5680
57- // split plan: restructure by conjunctions
58- Map <String , List <Config .KASInfo >> conjunction = new HashMap <>();
59-
60- for (Autoconfigure .KeySplitStep splitInfo : tdfConfig .splitPlan ) {
81+ for (Autoconfigure .KeySplitStep splitInfo : splitPlan ) {
6182 // Public key was passed in with kasInfoList
6283 // TODO First look up in attribute information / add to split plan?
6384 Config .KASInfo ki = latestKASInfo .get (splitInfo .kas );
0 commit comments